loginmobile.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. const {
  2. api_mobile_prefix,
  3. api_user_verification,
  4. api_mobile_login
  5. } = require('../../utils/api.js');
  6. const {
  7. net
  8. } = require('../../utils/net.js');
  9. const util = require('../../utils/util.js');
  10. Page({
  11. data: {
  12. loadingFlag: false,
  13. indexMobile: 0,
  14. mobilePrefixList: [],
  15. btnDisabled: true,
  16. btnCodeDisable: false,
  17. codeText: '获取验证码',
  18. mobile: '',
  19. code: '',
  20. timer: null
  21. },
  22. onLoad: function (options) {
  23. this.getMobileArrHandle()
  24. },
  25. onUnload: function () {
  26. let {
  27. timer
  28. } = this.data;
  29. if (timer) {
  30. clearTimeout(timer);
  31. timer = null;
  32. }
  33. },
  34. bindTelHandle(e) {
  35. let mobile = e.detail.value;
  36. this.setData({
  37. mobile: mobile
  38. });
  39. let {
  40. code
  41. } = this.data;
  42. if (util.validator.checkMobile(mobile) && util.validator.checkCode(code)) {
  43. this.setData({
  44. btnDisabled: false
  45. });
  46. } else {
  47. this.setData({
  48. btnDisabled: true
  49. });
  50. }
  51. },
  52. codeChange(e) {
  53. let code = e.detail.value;
  54. this.setData({
  55. code: code
  56. });
  57. let {
  58. mobile
  59. } = this.data;
  60. if (util.validator.checkMobile(mobile) && util.validator.checkCode(code)) {
  61. this.setData({
  62. btnDisabled: false
  63. });
  64. } else {
  65. this.setData({
  66. btnDisabled: true
  67. });
  68. }
  69. },
  70. getMobileArrHandle() {
  71. this.setData({
  72. loadingFlag: true
  73. })
  74. let params = {};
  75. params.url = api_mobile_prefix;
  76. net.req(params, true).then((res) => {
  77. this.setData({
  78. loadingFlag: false
  79. })
  80. let {
  81. data
  82. } = res
  83. let prefixArr = [];
  84. if (data && data.list && Array.isArray(data.list) && data.list.length > 0) {
  85. prefixArr = data.list
  86. prefixArr.forEach(ele => {
  87. ele.customize = `${ele.name}(+${ele.mobilePrefix})`
  88. })
  89. this.setData({
  90. mobilePrefixList: prefixArr
  91. }, () => {
  92. for (let i = 0; i < prefixArr.length; i++) {
  93. let tmp = prefixArr[i];
  94. if (["86", "086", "+86", "+086"].indexOf(tmp.mobilePrefix) !== -1) {
  95. this.setData({
  96. indexMobile: i
  97. })
  98. break;
  99. }
  100. }
  101. })
  102. }
  103. }, (e) => {
  104. this.setData({
  105. loadingFlag: false
  106. })
  107. wx.showToast({
  108. title: e.msg,
  109. icon: 'none'
  110. })
  111. });
  112. },
  113. bindPickerChange(e) {
  114. this.setData({
  115. indexMobile: e.detail.value
  116. })
  117. },
  118. checkMobile() {
  119. let {
  120. mobile
  121. } = this.data;
  122. if (!util.validator.checkMobile(mobile)) {
  123. wx.showToast({
  124. title: '请输入正确的手机号',
  125. icon: 'none',
  126. duration: 2000
  127. });
  128. return false;
  129. }
  130. this.getCodeHandle();
  131. },
  132. getCodeHandle() {
  133. let {
  134. mobile,
  135. mobilePrefixList,
  136. indexMobile
  137. } = this.data;
  138. let prefix = mobilePrefixList[indexMobile].id;
  139. let params = {};
  140. params.data = {
  141. hooid: 'hoolipartner', // 应用id, hooli客户端: hoolihome, partner: hoolipartner, 后台: platform
  142. senceId: 1, // 发送短信的场景id 1登录/三方绑定/绑定手机号 2设置密码 3找回密码 5填写申请表发送短信验证码 10绑定支付卡
  143. mobile: mobile,
  144. mobilePrefixId: prefix
  145. }
  146. params.url = api_user_verification;
  147. net.req(params, true).then((res) => {
  148. wx.showToast({
  149. title: '发送短信验证码成功',
  150. icon: 'none'
  151. })
  152. this.countDown();
  153. }, (e) => {
  154. this.setData({
  155. btnCodeDisable: false
  156. });
  157. wx.showToast({
  158. title: (e && e.msg) || '发送短信验证码失败',
  159. icon: 'none'
  160. })
  161. });
  162. this.setData({
  163. btnCodeDisable: true
  164. });
  165. },
  166. countDown: function () {
  167. /*短信验证码倒计时*/
  168. let timeCount = 60;
  169. let loop = () => {
  170. if (timeCount > 0) {
  171. this.setData({
  172. codeText: `重新获取(${timeCount}s)`
  173. });
  174. timeCount--;
  175. this.data.timer = setTimeout(loop, 1000);
  176. } else {
  177. this.setData({
  178. codeText: `重新获取`,
  179. btnCodeDisable: false
  180. });
  181. if (this.data.timer) {
  182. clearTimeout(this.data.timer);
  183. this.data.timer = null;
  184. }
  185. }
  186. };
  187. loop();
  188. },
  189. gotoLoginAction: function () {
  190. let {
  191. mobilePrefixList,
  192. indexMobile,
  193. mobile,
  194. code
  195. } = this.data;
  196. let prefix = mobilePrefixList[indexMobile].id;
  197. let mobilePrefix = mobilePrefixList[indexMobile].mobilePrefix;
  198. if (!util.validator.checkMobile(mobile)) {
  199. wx.showToast({
  200. title: '请输入正确的手机号',
  201. icon: 'none'
  202. })
  203. return false;
  204. }
  205. if (!util.validator.checkCode(code)) {
  206. wx.showToast({
  207. title: '请输入正确的短信验证码',
  208. icon: 'none'
  209. })
  210. return false;
  211. }
  212. let params = {};
  213. params.data = {
  214. hooid: 'hoolipartner', // 应用id, hooli客户端: hoolihome, partner: hoolipartner, 后台: platform
  215. mobile: mobile,
  216. mobilePrefixId: prefix,
  217. verifyCode: code,
  218. mobilePrefix: mobilePrefix,
  219. }
  220. params.url = api_mobile_login;
  221. net.req(params, true).then((res) => {
  222. this.setData({
  223. btnDisabled: false
  224. });
  225. if (res) {
  226. let { token, userInfo } = res.data;
  227. wx.setStorageSync('userInfo', { ...userInfo, token });
  228. wx.reLaunch({
  229. url: '/pages/home/home'
  230. });
  231. }
  232. }, (e) => {
  233. this.setData({
  234. btnDisabled: false
  235. });
  236. wx.showModal({
  237. title: '提示',
  238. content: (e && e.msg) || '抱歉,您暂无登录权限,合作请咨询400-898-6659',
  239. showCancel: false
  240. });
  241. });
  242. this.setData({
  243. btnDisabled: true
  244. });
  245. }
  246. })