login.js 6.8 KB

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