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 { private static final String TAG = "AliyunPhoneNumberAuth"; private final Activity activity; private final BasicMessageChannel messageChannel; private PhoneNumberAuthHelper mPhoneNumberAuthHelper; private BasicMessageChannel.Reply reply; private FlutterAliyunPhoneNumberAuthPlugins(Activity activity, BinaryMessenger messager) { this.activity = activity; this.messageChannel = new BasicMessageChannel(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 reply) { this.reply = reply; Map arguments = (Map) message; String method = arguments.get("method"); switch (method) { //初始化 case "init": initOneKeyLogin(arguments.get("secret"), arguments.get("privacy"), arguments.get("protocol")); Map 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 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 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 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 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 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); } }