login.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. import { getPrivacySetting } from "../../utils/util";
  12. Page({
  13. /**
  14. * 页面的初始数据
  15. */
  16. data: {
  17. color: false, // 验证码字体颜色
  18. code_message: '获取验证码', // 验证码文字
  19. time, // 时间
  20. canLogin: false, //是否可以登陆
  21. phone: '', // 手机
  22. formatPhone: '',
  23. code: '', // 验证码
  24. userAgreement: encodeURIComponent(baseurl + '/uploadfile/userAgreement.html'),
  25. privacyPolicy: encodeURIComponent(baseurl + '/uploadfile/privacyPolicy.html'),
  26. isCheck: false, // 是否勾选用户协议
  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. } else {
  110. wx.showToast({
  111. icon: 'none',
  112. title: this.data.phone ? '手机号码格式错误!' : '请填写手机号码!',
  113. })
  114. }
  115. }
  116. },
  117. /**
  118. * 检查是否可以登陆
  119. * @canLogin 是否可以登陆
  120. * @phone 电话号码
  121. */
  122. checkPhone(e: any) {
  123. const value: string = e.detail.value;
  124. const phone = value.replace(/\s/g, '');
  125. clearTimeout(phoneInter);
  126. phoneInter = setTimeout(_ => {
  127. let formatPhone: string
  128. if (phone.length === 11) {
  129. formatPhone = phone.replace(/^(.{3})(.*)(.{4})/, '$1 $2 $3');
  130. this.setData({
  131. formatPhone
  132. })
  133. }
  134. this.setData({
  135. canLogin: Boolean(app.globalData.phone_zz.test(value) && this.data.code && this.data.isCheck),
  136. phone: phone,
  137. })
  138. }, interval)
  139. },
  140. /**
  141. * 检查验证码是否可以登陆
  142. * @canLogin 是否可以登陆
  143. * @code 验证码
  144. */
  145. checkCode(e: any) {
  146. const value = e.detail.value;
  147. clearTimeout(codeInter);
  148. codeInter = setTimeout(_ => {
  149. this.setData({
  150. canLogin: Boolean(app.globalData.phone_zz.test(this.data.phone) && value && this.data.isCheck),
  151. code: value
  152. })
  153. }, interval)
  154. },
  155. /**
  156. * 点击登录
  157. * @phone {number} 电话号码
  158. * @code {string} 验证码
  159. * @register_from {注册来源} 0 小程序 1 APP 2 后台
  160. */
  161. submit() {
  162. // 首先判断是否可以登陆
  163. const status = this.data.canLogin;
  164. getPrivacySetting().then(data => {
  165. if (data.needAuthorization) {
  166. return
  167. }
  168. if (status) {
  169. phoneLoginImpl({
  170. phone: this.data.phone,
  171. code: this.data.code,
  172. register_from: 0
  173. }).then(res => {
  174. if (res.errCode === 0) {
  175. // 请求成功 存入userID和userToken 并根据来源跳转不同的页面 后台账号跳转首页
  176. wx.setStorage({
  177. key: 'userId',
  178. data: res.data.userInfo.id,
  179. success: () => {
  180. wx.setStorage({
  181. key: 'userToken',
  182. data: res.data.userInfo.userToken,
  183. success: () => {
  184. wx.setStorageSync('userInfo', res.data.userInfo)
  185. wx.setStorageSync('firstLogin', true)
  186. if (!wx.getStorageInfoSync().keys.length) {
  187. wx.setStorageSync('hasClear', true)
  188. }
  189. // 提交是否登录信息
  190. if (res.data.userInfo.isDemonstrate) {
  191. wx.reLaunch({
  192. url: '/pages/index/index'
  193. })
  194. } else {
  195. wx.reLaunch({
  196. url: '/pages/my-order/my-order'
  197. })
  198. }
  199. }
  200. })
  201. }
  202. })
  203. }
  204. })
  205. } else {
  206. let content = ''
  207. if (!this.data.isCheck) {
  208. content = '请勾选用用户协议与隐私政策'
  209. }
  210. if (!this.data.code) {
  211. content = '请填写验证码'
  212. }
  213. if (!this.data.phone || !app.globalData.phone_zz.test(this.data.phone)) {
  214. content = '电话号码格式错误'
  215. }
  216. wx.showModal({
  217. title: '登录失败',
  218. content,
  219. showCancel: false,
  220. confirmColor: '#31364C',
  221. })
  222. }
  223. })
  224. },
  225. /**
  226. * 生命周期函数--监听页面加载
  227. */
  228. onLoad: function (options) {
  229. this.options = options;
  230. },
  231. /**
  232. * 生命周期函数--监听页面初次渲染完成
  233. */
  234. onReady: function () {
  235. },
  236. /**
  237. * 生命周期函数--监听页面显示
  238. */
  239. onShow: function () {
  240. wx.hideHomeButton();
  241. const captchaResult = app.globalData.captchaResult;
  242. console.log(captchaResult);
  243. if (captchaResult && captchaResult.ret === 0) {
  244. // 将验证码的结果返回至服务端校验
  245. const ticket = captchaResult.ticket;
  246. const randstr = captchaResult.randstr;
  247. this.countDown({ ticket, randstr })
  248. app.globalData.captchaResult = null; // 验证码的票据为一次性票据,取完需要置空
  249. }
  250. },
  251. /**
  252. * 生命周期函数--监听页面隐藏
  253. */
  254. onHide: function () {
  255. },
  256. /**
  257. * 生命周期函数--监听页面卸载
  258. */
  259. onUnload: function () {
  260. },
  261. /**
  262. * 页面相关事件处理函数--监听用户下拉动作
  263. */
  264. onPullDownRefresh: function () {
  265. }
  266. })