基础库
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

224 řádky
7.2 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/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)) {
  74. result = await _getSMSDefault(phone, type: smsCodeType);
  75. return Future.value(result);
  76. } else {
  77. Logger.debug('phone = $phone, zoneCode = $zoneCode, vCodeType = $vCodeType');
  78. await Smssdk.getTextCode(phone, zoneCode, vCodeType, (ret, err) {
  79. if (err != null) {
  80. result = false;
  81. Logger.debug('get vcode fail err = ${err?.toString()}, ret = ${ret?.toString()}');
  82. if (!EmptyUtil.isEmpty(err)) {
  83. Fluttertoast.showToast(msg: '获取验证码失败:${err['msg']}');
  84. } else {
  85. Fluttertoast.showToast(msg: '获取验证码失败~');
  86. }
  87. } else {
  88. String rst = ret.toString();
  89. if (rst == null || rst == "") {
  90. rst = '获取验证码成功! ret = ${ret?.toString()}, err = ${err?.toString()} ';
  91. }
  92. Logger.debug(rst);
  93. result = true;
  94. }
  95. });
  96. }
  97. return Future.value(result);
  98. }
  99. ///
  100. /// 获取智联盟的短信验证码
  101. ///
  102. static Future<bool> _getSMSDefault(final String phone, {SMSCodeType type = SMSCodeType.NORMAL}) async {
  103. bool rlt = false;
  104. try {
  105. String vCodeType = enumToString(type)?.toLowerCase();
  106. if (EmptyUtil.isEmpty(vCodeType)) {
  107. vCodeType = enumToString(SMSCodeType.NORMAL).toLowerCase();
  108. }
  109. var result = await NetUtil.post('/api/v1/sign/more_sms/captcha', method: NetMethod.POST, params: {
  110. 'mobile': phone,
  111. 'type': vCodeType
  112. });
  113. if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
  114. // Fluttertoast.showToast(msg: result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
  115. Logger.debug(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
  116. rlt = true;
  117. } else {
  118. Fluttertoast.showToast(msg: result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
  119. }
  120. } catch (e, s) {
  121. Logger.error(e, s);
  122. }
  123. return rlt;
  124. }
  125. /// 询问验证码是否可以下发
  126. static Future<String> _getSMSStrategy(final String phone, {SMSCodeType type = SMSCodeType.NORMAL}) async {
  127. try {
  128. String vCodeType = enumToString(type)?.toLowerCase();
  129. if (EmptyUtil.isEmpty(vCodeType)) {
  130. vCodeType = enumToString(SMSCodeType.NORMAL).toLowerCase();
  131. }
  132. String url = type == SMSCodeType.AUTO ? '/api/v1/mob/sms/tmp/${vCodeType}?phone=${phone}' : '/api/v1/mob/sms/tmp/$vCodeType';
  133. var result = await NetUtil.post(url, method: NetMethod.GET);
  134. if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
  135. return result[GlobalConfig.HTTP_RESPONSE_KEY_DATA];
  136. }
  137. } catch (e, s) {
  138. Logger.error(e, s);
  139. }
  140. return null;
  141. }
  142. static Future storeInvitedCode(String code) {
  143. return SharedPreferencesUtil.setStringValue('mobInvitedCode', code);
  144. }
  145. // 获取一件锁粉邀请码
  146. static Future<String> getInvitedCode() async {
  147. return SharedPreferencesUtil.getStringValue('mobInvitedCode');
  148. }
  149. /// 隐私协议授权
  150. static void submitPrivacyGrantResult(bool permission) {
  151. SharesdkPlugin.uploadPrivacyPermissionStatus(permission ? 1 : 0, (bool success) {
  152. if (success == true) {
  153. Logger.log('隐私协议授权提交结果: 成功');
  154. } else {
  155. Logger.log('隐私协议授权提交结果: 失败');
  156. }
  157. });
  158. }
  159. ///
  160. /// 提交验证码
  161. ///
  162. /// phone: 手机号码
  163. /// code: 验证码
  164. /// zoneCode: 区号
  165. ///
  166. // static Future<bool> commitCode(final String phone, String code, {String zoneCode = '86'}) async {
  167. // bool result = false;
  168. // await Smssdk.commitCode(phone, zoneCode, code, (ret, err) {
  169. // if (err != null) {
  170. // // showAlert(err.toString(),context);
  171. // result = false;
  172. // } else {
  173. // // showAlert('提交验证码成功!',context);
  174. // result = true;
  175. // // _countMobSMS(phone, code, zoneCode: zoneCode);
  176. // }
  177. // });
  178. // return Future.value(result);
  179. // }
  180. /// 统计MOB验证码提交
  181. // static void _countMobSMS(final String phone, final String code, {String zoneCode = '86'}) async {
  182. // try {
  183. // NetUtil.post('/api/v1/funtions/mobsms', params: {'phone': phone, "code": code, 'zone': zoneCode}, showToast: false);
  184. // } catch (e, s) {
  185. // Logger.error(e, s);
  186. // }
  187. // }
  188. }
  189. enum SMSCodeType {
  190. // 普通验证码
  191. NORMAL,
  192. // 登录
  193. LOGIN,
  194. // 注册模版验证码
  195. REGISTER,
  196. // 绑定支付宝验证码
  197. BIND_ALIPAY,
  198. // 绑定手机号验证码
  199. BIND_PHONE,
  200. // 取消绑定支付宝验证码
  201. UNBIND_ALIPAY,
  202. // 取消绑定手机号
  203. UNBIND_PHONE,
  204. // 后台自动判断登录还是注册
  205. AUTO,
  206. }