基础库
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 6.1 KiB

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