| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- // pages/login/login.js
- const app = getApp();
- const speed = 1000;
- const time = 60;
- const interval = 50;
- let phoneInter = false;
- let codeInter = false;
- import { baseurl, captcha } from '../../config'
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- color: '', // 验证码字体颜色
- code_message: '', // 验证码文字
- time, // 时间
- canLogin: false, //是否可以登陆
- phone: '', // 手机
- code: '', // 验证码
- userAgreement: encodeURIComponent(baseurl + '/uploadfile/userAgreement.html'),
- privacyPolicy: encodeURIComponent(baseurl + '/uploadfile/privacyPolicy.html')
- },
- // 打开用户协议
- openWebView(e) {
- console.log(e)
- wx.navigateTo({
- url: '/pages/web-view/web-view?url=' + e.currentTarget.dataset.url,
- })
- },
- /**
- *
- * 时间倒计时
- */
- countDown(options) {
- const _time = --this.data.time;
- this.setData({
- code_message: `重新发送(${_time}s)`,
- color: true,
- time: _time
- })
- this.setInter = setInterval(_ => {
- let _time = this.data.time;
- _time--;
- if (_time > 0) {
- this.setData({
- code_message: `重新发送(${_time}s)`,
- color: true,
- time: _time
- })
- } else {
- clearInterval(this.setInter);
- this.setData({
- code_message: `重新发送`,
- color: false,
- time
- });
- }
- }, speed)
- wx.kx_request({
- url: wx.kx_api.hwuser.getAuthCode,
- data: {
- phone: this.data.phone,
- ...options
- },
- success: res => {
- if (res.errcode === 0) {
- wx.showToast({
- icon: 'none',
- title: '发送成功',
- })
- }
- },
- fail: res => {
- clearInterval(this.setInter);
- wx.showModal({
- title: '提示',
- content: '发送失败,请稍后重试',
- showCancel: false,
- success: () => {
- this.setData({
- code_message: `重新发送`,
- color: false,
- time
- })
- }
- })
- }
- })
- },
- /**
- * 获取验证码
- * @phone 电话号码
- */
- getCode(e) {
- if (this.data.time === time) {
- if (app.globalData.phone_zz.test(this.data.phone)) {
- wx.navigateToMiniProgram({
- appId: 'wx5a3a7366fd07e119',
- path: '/pages/captcha/index',
- extraData: {
- appId: captcha//您申请的验证码的 appId
- }
- })
- } else {
- wx.showToast({
- icon: 'none',
- title: this.data.phone ? '手机号码格式错误!' : '请填写手机号码!',
- })
- }
- }
- },
- /**
- * 检查是否可以登陆
- * @canLogin 是否可以登陆
- * @phone 电话号码
- */
- checkPhone(e) {
- const value = e.detail.value;
- clearTimeout(phoneInter);
- phoneInter = setTimeout(_ => {
- this.setData({
- canLogin: app.globalData.phone_zz.test(value) && this.data.code,
- phone: value
- })
- }, interval)
- },
- /**
- * 检查验证码是否可以登陆
- * @canLogin 是否可以登陆
- * @code 验证码
- */
- checkcode(e) {
- const value = e.detail.value;
- clearTimeout(codeInter);
- codeInter = setTimeout(_ => {
- this.setData({
- canLogin: app.globalData.phone_zz.test(this.data.phone) && value,
- code: value
- })
- }, interval)
- },
- /**
- * 点击登录
- * @phone {number} 电话号码
- * @code {string} 验证码
- * @register_from {注册来源} 0 小程序 1 APP 2 后台
- */
- submit(e) {
- // 首先判断是否可以登陆
- const status = this.data.canLogin;
- if (status) {
- wx.kx_request({
- url: wx.kx_api.hwuser.phoneLogin,
- type: 'post',
- data: {
- phone: this.data.phone,
- code: this.data.code,
- register_from: 0
- },
- success: res => {
- if (res.errcode === 0) {
- // 请求成功 存入userID和usertoken 并根据来源跳转不同的页面 后台账号跳转首页
- wx.setStorage({
- key: 'userid',
- data: res.data.userInfo.id,
- success: data => {
- wx.setStorage({
- key: 'usertoken',
- data: res.data.userInfo.userToken,
- success: data => {
- wx.setStorageSync('userinfo', res.data.userInfo)
- // 提交是否登录信息
- wx.$emit('is_login', true, () => {
- wx.navigateBack({
- delta: 1,
- })
- })
- // if (res.data.userInfo.registerFrom === 2) {
- // if (this.options.path) {
- // let _str = '';
- // for (let key in this.options) {
- // if (key !== 'path') {
- // _str += `${key}=${this.options[key]}&&`
- // }
- // }
- // wx.redirectTo({
- // url: `${this.options.path}?${_str}`,
- // })
- // } else {
- // wx.reLaunch({
- // url: '/pages/index/index',
- // })
- // }
- // } else {
- // wx.redirectTo({
- // url: '/pages/success/success?type=login_success',
- // })
- // }
- }
- })
- }
- })
- }
- }
- })
- } else {
- wx.showModal({
- title: '登录失败',
- content: '电话号码格式错误',
- showCancel: false,
- confirmColor: '#31364C',
- })
- }
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- this.options = options;
- // 监听是否离线
- wx.$watch('offline', this, (value) => {
- if (value) {
- clearInterval(this.setInter)
- }
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- const captchaResult = app.captchaResult;
- app.captchaResult = null; // 验证码的票据为一次性票据,取完需要置空
- if (captchaResult && captchaResult.ret === 0) {
- // 将验证码的结果返回至服务端校验
- const ticket = captchaResult.ticket;
- const randstr = captchaResult.randstr;
- this.countDown({ ticket, randstr })
- }
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- }
- })
|