基础库
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.
 
 
 
 
 

148 lines
4.6 KiB

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