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

135 lines
4.2 KiB

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