login.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // pages/login/login.js
  2. const app = getApp();
  3. const speed = 1000;
  4. const time = 60;
  5. const interval = 50;
  6. let phoneInter: number = 0;
  7. let codeInter: number = 0;
  8. let setInter: number = 0
  9. import { baseurl, captcha } from '../../config'
  10. import { getAuthCodeImpl, phoneLoginImpl } from "../../service/impl/hwUser.impl";
  11. Page({
  12. /**
  13. * 页面的初始数据
  14. */
  15. data: {
  16. color: false, // 验证码字体颜色
  17. code_message: '获取验证码', // 验证码文字
  18. time, // 时间
  19. canLogin: false, //是否可以登陆
  20. phone: '', // 手机
  21. formatPhone: '',
  22. code: '', // 验证码
  23. userAgreement: encodeURIComponent(baseurl + '/uploadfile/userAgreement.html'),
  24. privacyPolicy: encodeURIComponent(baseurl + '/uploadfile/privacyPolicy.html'),
  25. isCheck: false, // 是否勾选用户协议
  26. captcha
  27. },
  28. /**
  29. * 勾选用户协议
  30. */
  31. checked() {
  32. this.setData({
  33. isCheck: !this.data.isCheck,
  34. canLogin: Boolean(app.globalData.phone_zz.test(this.data.phone) && this.data.code && !this.data.isCheck),
  35. })
  36. },
  37. // 打开用户协议 和 隐私政策
  38. openWebView(e: any) {
  39. wx.navigateTo({
  40. url: '/pages/web-view/web-view?url=' + e.currentTarget.dataset.url,
  41. })
  42. },
  43. /**
  44. *
  45. * 时间倒计时
  46. */
  47. countDown(options: any) {
  48. const _time = --this.data.time;
  49. this.setData({
  50. code_message: `已发送(${_time}s)`,
  51. color: true,
  52. time: _time
  53. })
  54. setInter = setInterval(_ => {
  55. let _time = this.data.time;
  56. _time--;
  57. if (_time > 0) {
  58. this.setData({
  59. code_message: `已发送(${_time}s)`,
  60. color: true,
  61. time: _time
  62. })
  63. } else {
  64. clearInterval(setInter);
  65. this.setData({
  66. code_message: `重新发送`,
  67. color: false,
  68. time
  69. });
  70. }
  71. }, speed)
  72. getAuthCodeImpl({ phone: this.data.phone, ...options }).then(res => {
  73. if (res.errCode === 0) {
  74. wx.showToast({
  75. icon: 'none',
  76. title: '发送成功',
  77. })
  78. }
  79. }).catch(() => {
  80. clearInterval(setInter);
  81. wx.showModal({
  82. title: '提示',
  83. content: '发送失败,请稍后重试',
  84. showCancel: false,
  85. success: () => {
  86. this.setData({
  87. code_message: `重新发送`,
  88. color: false,
  89. time
  90. })
  91. }
  92. })
  93. })
  94. },
  95. /**
  96. * 获取验证码
  97. * @phone 电话号码
  98. */
  99. getCode() {
  100. if (this.data.time === time) {
  101. if (app.globalData.phone_zz.test(this.data.phone)) {
  102. // wx.navigateToMiniProgram({
  103. // appId: 'wx5a3a7366fd07e119',
  104. // path: '/pages/captcha/index',
  105. // extraData: {
  106. // appId: captcha//您申请的验证码的 appId
  107. // }
  108. // })
  109. this.selectComponent('#captcha').show()
  110. } else {
  111. wx.showToast({
  112. icon: 'none',
  113. title: this.data.phone ? '手机号码格式错误!' : '请填写手机号码!',
  114. })
  115. }
  116. }
  117. },
  118. /**
  119. * 检查是否可以登陆
  120. * @canLogin 是否可以登陆
  121. * @phone 电话号码
  122. */
  123. checkPhone(e: any) {
  124. const value: string = e.detail.value;
  125. const phone = value.replace(/\s/g, '');
  126. clearTimeout(phoneInter);
  127. phoneInter = setTimeout(_ => {
  128. let formatPhone: string
  129. if (phone.length === 11) {
  130. formatPhone = phone.replace(/^(.{3})(.*)(.{4})/, '$1 $2 $3');
  131. this.setData({
  132. formatPhone
  133. })
  134. }
  135. this.setData({
  136. canLogin: Boolean(app.globalData.phone_zz.test(value) && this.data.code && this.data.isCheck),
  137. phone: phone,
  138. })
  139. }, interval)
  140. },
  141. /**
  142. * 检查验证码是否可以登陆
  143. * @canLogin 是否可以登陆
  144. * @code 验证码
  145. */
  146. checkCode(e: any) {
  147. const value = e.detail.value;
  148. clearTimeout(codeInter);
  149. codeInter = setTimeout(_ => {
  150. this.setData({
  151. canLogin: Boolean(app.globalData.phone_zz.test(this.data.phone) && value && this.data.isCheck),
  152. code: value
  153. })
  154. }, interval)
  155. },
  156. /**
  157. * 点击登录
  158. * @phone {number} 电话号码
  159. * @code {string} 验证码
  160. * @register_from {注册来源} 0 小程序 1 APP 2 后台
  161. */
  162. submit() {
  163. // 首先判断是否可以登陆
  164. const status = this.data.canLogin;
  165. if (status) {
  166. phoneLoginImpl({
  167. phone: this.data.phone,
  168. code: this.data.code,
  169. register_from: 0
  170. }).then(res => {
  171. if (res.errCode === 0) {
  172. // 请求成功 存入userID和userToken 并根据来源跳转不同的页面 后台账号跳转首页
  173. wx.setStorage({
  174. key: 'userId',
  175. data: res.data.userInfo.id,
  176. success: () => {
  177. wx.setStorage({
  178. key: 'userToken',
  179. data: res.data.userInfo.userToken,
  180. success: () => {
  181. wx.setStorageSync('userInfo', res.data.userInfo)
  182. wx.setStorageSync('firstLogin', true)
  183. if (!wx.getStorageInfoSync().keys.length) {
  184. wx.setStorageSync('hasClear', true)
  185. }
  186. // 提交是否登录信息
  187. if (res.data.userInfo.isDemonstrate) {
  188. wx.reLaunch({
  189. url: '/pages/index/index'
  190. })
  191. } else {
  192. wx.reLaunch({
  193. url: '/pages/my-order/my-order'
  194. })
  195. }
  196. }
  197. })
  198. }
  199. })
  200. }
  201. })
  202. } else {
  203. let content = ''
  204. if (!this.data.isCheck) {
  205. content = '请勾选用用户协议与隐私政策'
  206. }
  207. if (!this.data.code) {
  208. content = '请填写验证码'
  209. }
  210. if (!this.data.phone || !app.globalData.phone_zz.test(this.data.phone)) {
  211. content = '电话号码格式错误'
  212. }
  213. wx.showModal({
  214. title: '登录失败',
  215. content,
  216. showCancel: false,
  217. confirmColor: '#31364C',
  218. })
  219. }
  220. },
  221. // 验证码验证结果回调
  222. handlerVerify: function (ev: any) {
  223. // 如果使用了 mpvue,ev.detail 需要换成 ev.mp.detail
  224. if (ev.detail.ret === 0) {
  225. // 验证成功
  226. console.log('randstr:', ev.detail.randstr)
  227. console.log('ticket:', ev.detail.ticket)
  228. this.countDown({ ticket: ev.detail.ticket, randstr: ev.detail.randstr })
  229. } else {
  230. // 验证失败
  231. // 请不要在验证失败中调用refresh,验证码内部会进行相应处理
  232. }
  233. },
  234. // 验证码准备就绪
  235. handlerReady: function () {
  236. console.log('验证码准备就绪')
  237. },
  238. // 验证码弹框准备关闭
  239. handlerClose: function (ev: any) {
  240. // 如果使用了 mpvue,ev.detail 需要换成 ev.mp.detail,ret为0是验证完成后自动关闭验证码弹窗,ret为2是用户主动点击了关闭按钮关闭验证码弹窗
  241. if (ev && ev.detail.ret && ev.detail.ret === 2) {
  242. console.log('点击了关闭按钮,验证码弹框准备关闭');
  243. } else {
  244. console.log('验证完成,验证码弹框准备关闭');
  245. }
  246. },
  247. // 验证码出错
  248. handlerError: function (ev: any) {
  249. console.log(ev.detail.errMsg)
  250. wx.showModal({
  251. title: '提示',
  252. content: ev.detail.errMsg,
  253. showCancel: false
  254. })
  255. },
  256. /**
  257. * 生命周期函数--监听页面加载
  258. */
  259. onLoad: function (options) {
  260. this.options = options;
  261. },
  262. /**
  263. * 生命周期函数--监听页面初次渲染完成
  264. */
  265. onReady: function () {
  266. },
  267. /**
  268. * 生命周期函数--监听页面显示
  269. */
  270. onShow: function () {
  271. wx.hideHomeButton();
  272. // const captchaResult = app.globalData.captchaResult;
  273. // console.log(captchaResult);
  274. // if (captchaResult && captchaResult.ret === 0) {
  275. // // 将验证码的结果返回至服务端校验
  276. // const ticket = captchaResult.ticket;
  277. // const randstr = captchaResult.randstr;
  278. // app.globalData.captchaResult = null; // 验证码的票据为一次性票据,取完需要置空
  279. // }
  280. },
  281. /**
  282. * 生命周期函数--监听页面隐藏
  283. */
  284. onHide: function () {
  285. },
  286. /**
  287. * 生命周期函数--监听页面卸载
  288. */
  289. onUnload: function () {
  290. },
  291. /**
  292. * 页面相关事件处理函数--监听用户下拉动作
  293. */
  294. onPullDownRefresh: function () {
  295. }
  296. })