基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

164 lines
4.9 KiB

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:mobsms/mobsms.dart';
  3. import 'package:sharesdk_plugin/sharesdk_plugin.dart';
  4. import 'package:zhiying_comm/pages/login_page/model/login_style_model.dart';
  5. import 'package:zhiying_comm/util/empty_util.dart';
  6. import 'package:zhiying_comm/util/enum_util.dart';
  7. import 'package:zhiying_comm/util/global_config.dart';
  8. import 'package:zhiying_comm/util/log/let_log.dart';
  9. import 'package:zhiying_comm/util/mob_util/secverify/quick_login_util.dart';
  10. import 'package:zhiying_comm/util/net_util.dart';
  11. import 'package:zhiying_comm/util/shared_prefe_util.dart';
  12. ///
  13. /// MOB SDK 工具类
  14. ///
  15. class MobUtil {
  16. ///
  17. /// MOB 各个sdk初始化
  18. ///
  19. ///
  20. static void init() {
  21. // 秒验的初始化
  22. QuickLoginUtil.getInstance().init();
  23. }
  24. ///
  25. /// 打开秒验页面
  26. ///
  27. /// context: 当前应用的上下文
  28. /// model: 快速登录的样式
  29. ///
  30. static void openQuickLoginPage(BuildContext context, Quick model) {
  31. QuickLoginUtil.getInstance().openQuickLogin(context, model);
  32. }
  33. ///
  34. /// 获取短信
  35. /// 流程:先根据类型请求后台获取是否余额充足,根据返回的模版类型进行MOB SDK的验证码下发。
  36. ///
  37. /// phone: 手机号
  38. /// zoneCode: 地区号码
  39. /// templateID: 模板ID
  40. ///
  41. /// 例子:获取绑定手机号验证码:bool result = await MobUtil.getTextCode('手机号码', zoneCode = '86', smsCodeType = SMSCodeType.BIND_ALIPAY);
  42. ///
  43. static Future<bool> getTextCode(final String phone, {String zoneCode = '86', SMSCodeType smsCodeType = SMSCodeType.NORMAL}) async {
  44. bool result = false;
  45. String vCodeType = await _getSMSStrategy(phone, type: smsCodeType);
  46. if (EmptyUtil.isEmpty(vCodeType)) return result;
  47. Logger.debug('phone = $phone, zoneCode = $zoneCode, vCodeType = $vCodeType');
  48. await Smssdk.getTextCode(phone, zoneCode, vCodeType, (ret, err) {
  49. if (err != null) {
  50. result = false;
  51. Logger.debug('get vcode fail err = ${err?.toString()}');
  52. } else {
  53. String rst = ret.toString();
  54. if (rst == null || rst == "") {
  55. rst = '获取验证码成功! ret = ${ret?.toString()}';
  56. }
  57. Logger.debug(rst);
  58. result = true;
  59. }
  60. });
  61. return Future.value(result);
  62. }
  63. /// 询问验证码是否可以下发
  64. static Future<String> _getSMSStrategy(final String phone, {SMSCodeType type = SMSCodeType.NORMAL}) async {
  65. try {
  66. String vCodeType = enumToString(type)?.toLowerCase();
  67. if (EmptyUtil.isEmpty(vCodeType)) {
  68. vCodeType = enumToString(SMSCodeType.NORMAL).toLowerCase();
  69. }
  70. String url = type == SMSCodeType.AUTO
  71. ? '/api/v1/mob/sms/tmp/${vCodeType}?phone=${phone}'
  72. : '/api/v1/mob/sms/tmp/$vCodeType';
  73. var result = await NetUtil.post(url, method: NetMethod.GET);
  74. if (NetUtil.isSuccess(result) &&
  75. !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
  76. return result[GlobalConfig.HTTP_RESPONSE_KEY_DATA];
  77. }
  78. } catch (e, s) {
  79. Logger.error(e, s);
  80. }
  81. return null;
  82. }
  83. static Future storeInvitedCode(String code) {
  84. return SharedPreferencesUtil.setStringValue('mobInvitedCode', code);
  85. }
  86. // 获取一件锁粉邀请码
  87. static Future<String> getInvitedCode() async {
  88. return SharedPreferencesUtil.getStringValue('mobInvitedCode');
  89. }
  90. /// 隐私协议授权
  91. static void submitPrivacyGrantResult(bool permission) {
  92. SharesdkPlugin.uploadPrivacyPermissionStatus(permission ? 1 : 0, (bool success) {
  93. if(success == true) {
  94. Logger.log('隐私协议授权提交结果: 成功');
  95. } else {
  96. Logger.log('隐私协议授权提交结果: 失败');
  97. }
  98. });
  99. }
  100. ///
  101. /// 提交验证码
  102. ///
  103. /// phone: 手机号码
  104. /// code: 验证码
  105. /// zoneCode: 区号
  106. ///
  107. // static Future<bool> commitCode(final String phone, String code, {String zoneCode = '86'}) async {
  108. // bool result = false;
  109. // await Smssdk.commitCode(phone, zoneCode, code, (ret, err) {
  110. // if (err != null) {
  111. // // showAlert(err.toString(),context);
  112. // result = false;
  113. // } else {
  114. // // showAlert('提交验证码成功!',context);
  115. // result = true;
  116. // // _countMobSMS(phone, code, zoneCode: zoneCode);
  117. // }
  118. // });
  119. // return Future.value(result);
  120. // }
  121. /// 统计MOB验证码提交
  122. // static void _countMobSMS(final String phone, final String code, {String zoneCode = '86'}) async {
  123. // try {
  124. // NetUtil.post('/api/v1/funtions/mobsms', params: {'phone': phone, "code": code, 'zone': zoneCode}, showToast: false);
  125. // } catch (e, s) {
  126. // Logger.error(e, s);
  127. // }
  128. // }
  129. }
  130. enum SMSCodeType {
  131. // 普通验证码
  132. NORMAL,
  133. // 登录
  134. LOGIN,
  135. // 注册模版验证码
  136. REGISTER,
  137. // 绑定支付宝验证码
  138. BIND_ALIPAY,
  139. // 绑定手机号验证码
  140. BIND_PHONE,
  141. // 取消绑定支付宝验证码
  142. UNBIND_ALIPAY,
  143. // 取消绑定手机号
  144. UNBIND_PHONE,
  145. // 后台自动判断登录还是注册
  146. AUTO,
  147. }