login.ts 6.9 KB

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