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

mob_util.dart 5.8 KiB

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