admin
2021-11-25 70a344485bd0c9b68ac91f72ed23ec5bfa998b09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.yeshi.location.plugins;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.widget.ImageView;
 
import com.mobile.auth.gatewayauth.AuthUIConfig;
import com.mobile.auth.gatewayauth.PhoneNumberAuthHelper;
import com.mobile.auth.gatewayauth.ResultCode;
import com.mobile.auth.gatewayauth.TokenResultListener;
import com.mobile.auth.gatewayauth.model.TokenRet;
import com.yeshi.location.R;
import com.yeshi.location.utils.DimenUtils;
 
import java.util.HashMap;
import java.util.Map;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.common.StringCodec;
 
public class FlutterAliyunPhoneNumberAuthPlugins implements BasicMessageChannel.MessageHandler<Object> {
 
    private static final String TAG = "AliyunPhoneNumberAuth";
 
    private final Activity activity;
    private final BasicMessageChannel<Object> messageChannel;
 
    private PhoneNumberAuthHelper mPhoneNumberAuthHelper;
    private BasicMessageChannel.Reply<Object> reply;
 
 
    private FlutterAliyunPhoneNumberAuthPlugins(Activity activity, BinaryMessenger messager) {
        this.activity = activity;
        this.messageChannel = new BasicMessageChannel<Object>(messager, "AliyunPhoneNumberAuth", StandardMessageCodec.INSTANCE);
        messageChannel.setMessageHandler(this);
    }
 
    public static FlutterAliyunPhoneNumberAuthPlugins registerWith(Activity activity, BinaryMessenger messager) {
        return new FlutterAliyunPhoneNumberAuthPlugins(activity, messager);
    }
 
 
    @Override
    public void onMessage(@Nullable Object message, @NonNull BasicMessageChannel.Reply<Object> reply) {
        this.reply = reply;
        Map<String, String> arguments = (Map<String, String>) message;
        String method = arguments.get("method");
        switch (method) {
            //初始化
            case "init":
                initOneKeyLogin(arguments.get("secret"), arguments.get("privacy"), arguments.get("protocol"));
                Map<String, Object> map = new HashMap<>();
                map.put("code", 0);
                reply.reply(map);
                break;
 
            case "checkEnv":
                mPhoneNumberAuthHelper.checkEnvAvailable(2);
                break;
            case "startLogin":
                mPhoneNumberAuthHelper.getLoginToken(activity, 5000);
            case "closeLogin":
                mPhoneNumberAuthHelper.quitLoginPage();
                break;
        }
 
    }
 
    /**
     * 向Dart发送消息,并接受Dart的反馈
     *
     * @param message  要给Dart发送的消息内容
     * @param callback 来自Dart的反馈
     */
    void send(String message, BasicMessageChannel.Reply<Object> callback) {
        messageChannel.send(message, callback);
    }
 
 
    private void initOneKeyLogin(String secretInfo, String privacyUrl, String userProtocolUrl) {
        sdkInit(secretInfo);
        initUI(privacyUrl, userProtocolUrl);
    }
 
    private void initUI(String privacy, String protocol) {
 
        int width = DimenUtils.getScreenWidth(activity);
        int widthDP = DimenUtils.px2dip(activity, width);
        AuthUIConfig uiCOnfig = new AuthUIConfig.Builder()
                .setPageBackgroundPath("shape_login_onkey_bg")
 
                .setNavText("")
                .setNavColor(Color.WHITE)
                .setNavHidden(false)
                .setNavReturnImgPath("ic_login_close")
                .setNavReturnImgWidth(32)
                .setNavReturnImgHeight(32)
                .setNavReturnScaleType(ImageView.ScaleType.FIT_XY)
                .setWebNavColor(Color.BLACK)
//                .setWebNavReturnImgPath("ic_back_two")
                .setWebViewStatusBarColor(Color.WHITE)
                .setStatusBarColor(Color.WHITE)
                .setLogoWidth(0)
                .setLogoHeight(0)
//                .setLogoImgPath("ic_login_logo")
                .setLogoOffsetY(24 - 10)
 
                .setSloganOffsetY(125 - 10)
                .setSloganTextSize(12)
                .setSloganTextColor(activity.getResources().getColor(R.color.onekey_login_theme_color))
 
                .setLogBtnText("⼀键登录")
                .setLogBtnTextSize(17)
                .setLogBtnTextColor(Color.WHITE)
                .setLogBtnBackgroundPath("shape_login_btn")
                .setLogBtnOffsetY(170 - 10)
                .setLogBtnHeight(44)
 
 
                .setDialogWidth(305 * widthDP / 375)
                .setDialogHeight(378)
                .setNavColor(Color.RED)
                .setSwitchAccHidden(true)
                .setNumberColor(activity.getResources().getColor(R.color.onekey_login_theme_color))
                .setNumberSize(36)
                .setNumFieldOffsetY(65)
 
                .setAppPrivacyOne("《用户服务协议》", protocol)
                .setAppPrivacyTwo("《隐私政策》", privacy)
                .setAppPrivacyColor(activity.getResources().getColor(R.color.onekey_login_notify_color), activity.getResources().getColor(R.color.onekey_login_theme_color))
                .setPrivacyBefore("登录即表示同意")
                .setPrivacyEnd("")
                .setCheckedImgPath("ic_login_privacy_checked")
                .setUncheckedImgPath("ic_login_privacy_unchecked")
                .setPrivacyTextSize(11)
                .setPrivacyState(false)
                .setVendorPrivacyPrefix("《")
                .setVendorPrivacySuffix("》")
                .create();
 
        mPhoneNumberAuthHelper.setAuthUIConfig(uiCOnfig);
    }
 
    private void sdkInit(String secretInfo) {
        mPhoneNumberAuthHelper = PhoneNumberAuthHelper.getInstance(activity.getApplicationContext(), new TokenResultListener() {
 
            @Override
            public void onTokenSuccess(String s) {
                Log.i(TAG, "onTokenSuccess:" + s);
                TokenRet tokenRet = null;
                try {
                    tokenRet = TokenRet.fromJson(s);
 
                    switch (tokenRet.getCode()) {
                        case ResultCode.CODE_ERROR_ENV_CHECK_SUCCESS: {
                            Log.i(TAG
                                    , "终端环境校验成功:" + s);
                            Map<String, Object> map = new HashMap<>();
                            map.put("code", 0);
                            map.put("msg", "环境监测正常");
                            reply.reply(map);
                        }
                        break;
 
                        case ResultCode.CODE_START_AUTHPAGE_SUCCESS:
                            Log.i(TAG, "唤起授权页成功:" + s);
                            break;
 
                        case ResultCode.CODE_ERROR_USER_CANCEL: {
                            Log.i(TAG, "用户取消操作:" + s);
                            Map<String, Object> map = new HashMap<>();
                            map.put("code", Integer.parseInt(tokenRet.getCode()));
                            map.put("msg", "取消操作");
                            reply.reply(map);
                        }
                        break;
 
 
                        case ResultCode.CODE_SUCCESS: {
                            Log.i(TAG, "获取token成功:" + s);
                            mPhoneNumberAuthHelper.setAuthListener(null);
                            Map<String, Object> map = new HashMap<>();
                            map.put("code", 0);
                            map.put("token", tokenRet.getToken());
                            map.put("msg", "获取token成功");
                            reply.reply(map);
                        }
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
 
            @Override
            public void onTokenFailed(String s) {
                Log.i(TAG, "onTokenFailed:" + s);
                TokenRet
                        tokenRet = TokenRet.fromJson(s);
                Map<String, Object> map = new HashMap<>();
                map.put("code", Integer.parseInt(tokenRet.getCode()));
                map.put("msg", tokenRet.getMsg());
                reply.reply(map);
            }
        });
        mPhoneNumberAuthHelper.getReporter().setLoggerEnable(true);
        mPhoneNumberAuthHelper.setAuthSDKInfo(secretInfo);
    }
 
 
}