//获取应用实例 import { $wuxCountDown } from '../../components/index' import { config, postCaptcha, loginCaptcha, loginPwd, wechatApplet, loginWechat } from "../api/user" import { zconfig, loginToken, getBaseInfo } from "../api/ztb" import { pconfig } from "../api/payment" import { mconfig } from "../api/moment" import { sconfig, getOrderExists } from "../api/saas" const storage = require('../../utils/storage') const util = require('../../utils/util') const event = require('../../utils/event') const app = getApp() Page({ data: { pwdType: 'password', mobileEnable: false, codeEnable: true, loginEnable: true, type: 1, second: null, form: { tmpAuthToken: null, accountType: 2, password: null, account: null, captcha: null }, loging: false, wxflag: false, userInfo: null }, onLoad: function (options) { if (options.mobile) { this.setData({ ['form.mobile']: options.mobile }) } this.wxLogin() var check = storage.get('AgreeCheck') app.globalData.kg = storage.get('ztb-kg') if (check) { this.setData({ check: true }) } }, wxLogin: function () { wx.login({ success: res => { wechatApplet({ authCode: res.code }).then(result => { storage.put('tmpAuthToken', result.data.tmpAuthToken, 98) }) } }) }, fetchUserInfo: function () { var authorization = app.globalData.token || storage.get('Authorization') if (util.isEmpty(authorization)) { return } app.globalData.token = authorization config.header = { 'Authorization': 'QNT ' + authorization } loginToken({ loginToken: app.globalData.token }).then(result => { zconfig.header = { 'Authorization': 'QNT ' + result.data.token } pconfig.header = { 'Authorization': 'QNT ' + result.data.token } mconfig.header = { 'Authorization': 'QNT ' + result.data.token } // 登录流程完成; getBaseInfo().then(result => { storage.put('userIsLoginFirst', '0') app.globalData.userInfo = result.data if (!util.isEmpty(result.data.userToken)) { this.handleUserToken(result.data.userToken) } else { this.setData({ loging: false }) wx.hideLoading() wx.navigateBack() event.emit('EventMessage', { what: 888, desc: 'Logined' }) } }).catch(err => { wx.hideLoading() util.showToast(err) app.globalData.token = null storage.remove('Authorization') config.header = null this.setData({ loging: false }) }) }).catch(err => { wx.hideLoading() util.showToast(err) app.globalData.token = null storage.remove('Authorization') config.header = null this.setData({ loging: false }) }) }, handleUserToken: function (userToken) { storage.put('userToken', userToken) sconfig.header = { 'user-token': userToken } getOrderExists().then(result => { wx.hideLoading() if (Number(result.data) != 1 || !app.release) { wx.navigateBack() } else { wx.reLaunch({ url: '/pages/paper/index' }) } }).catch(err => { wx.hideLoading() util.showToast(err) app.globalData.token = null storage.remove('Authorization') config.header = null this.setData({ loging: false }) wx.navigateBack() }) }, changePwd: function () { if (this.data.pwdType === 'password') { this.setData({ pwdType: 'text' }) } else { this.setData({ pwdType: 'password' }) } }, bindInput: function (e) { this.data.form[e.target.id] = e.detail.value if ('account' == e.target.id) { var enable = false if ((/^1\d{10}$/.test(e.detail.value)) && e.detail.value.length == 11) { enable = true this.setData({ mobileEnable: enable }) } } if ((/^1\d{10}$/.test(this.data.form.account)) && this.data.form.account.length == 11) { if (this.data.type == 1 && !util.isEmpty(this.data.form.captcha)) { this.setData({ loginEnable: false }) } else if (this.data.type == 0 && !util.isEmpty(this.data.form.password)) { this.setData({ loginEnable: false }) } else { this.setData({ loginEnable: true }) } } else { this.setData({ loginEnable: true }) } }, loginForm: function () { if (!(/^1\d{10}$/.test(this.data.form.account)) || this.data.form.account.length < 11) { util.showToast('请输入11位手机号码') return } if (this.data.type == 1 && util.isEmpty(this.data.form.captcha)) { util.showToast('请输入验证码') return } if (this.data.type == 0 && util.isEmpty(this.data.form.password)) { util.showToast('请输入密码') return } wx.showLoading({ title: '登录中', mask: true }) if (this.data.type == 1) { loginCaptcha(this.data.form).then(result => { storage.put('Authorization', result.data.token) app.globalData.token = result.data.token this.fetchUserInfo() }).catch(error => { wx.hideLoading() util.showToast(error) }) } else { loginPwd(this.data.form).then(result => { storage.put('Authorization', result.data.token) app.globalData.token = result.data.token this.fetchUserInfo() }).catch(error => { wx.hideLoading() util.showToast(error) }) } }, changeType: function () { if (this.data.type == 1) { this.setData({ type: 0, ['form.captcha']: null, loginEnable: true }) } else { this.setData({ type: 1, ['form.password']: null, loginEnable: true }) } }, fetchCode: function (e) { if (!(/^1\d{10}$/.test(this.data.form.account)) || this.data.form.account.length < 11) { util.showToast('请输入11位手机号码') return } if (this.second && this.second.interval) return !1 wx.showLoading({ title: '正在获取', mask: true }) postCaptcha({ verifiableAccount: this.data.form.account, verifiableAccountType: 1, purpose: 1 }).then(result => { wx.hideLoading() this.setData({ codeEnable: false }) util.showToast('验证码已经发送') this.wuxCountDown = new $wuxCountDown({ date: +(new Date) + 60000, onEnd() { this.setData({ second: '重新获取验证码', codeEnable: true }) }, render(date) { const sec = this.leadingZeros(date.sec, 2) + ' 秒 ' date.sec !== 0 && this.setData({ second: sec }) }, }) }).catch(error => { wx.hideLoading() util.showToast(error) }) }, readAgreement: function () { wx.navigateTo({ url: '/pages/html/agreement/index' }) }, onGotUserInfo: function (e) { this.setData({ loging: true }) loginWechat({ encryptedData: e.detail.encryptedData, iv: e.detail.iv }).then(result => { storage.put('Authorization', result.data.token) app.globalData.token = result.data.token this.fetchUserInfo() }).catch(error => { wx.hideLoading() this.setData({ loging: false }) if (error && error.data) { if (error.code == 200101) { this.setData({ wxflag: true }) } this.data.userInfo = e.detail.userInfo this.data.form.tmpAuthToken = error.data.tmpAuthToken util.showToast(error.message) } }) }, onUnload: function () { if (this.wuxCountDown) { this.wuxCountDown.stop() this.wuxCountDown = null } } })