纸通宝小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

206 lines
6.5 KiB

//获取应用实例
import { $wuxCountDown } from '../../components/index'
import { config, postCaptcha, loginCaptcha, wechatApplet, getUserInfo, loginWechat } from "../api/user"
import { zconfig, loginToken } from "../api/ztb"
const storage = require('../../utils/storage')
const request = require('../../utils/request') //导入模块
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: 'MOBILE',
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 = wx.getStorageSync('AgreeCheck')
app.globalData.kg = wx.getStorageSync('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 || wx.getStorageSync('Authorization')
if (util.isEmpty(authorization)) {
return
}
app.globalData.token = authorization
config.header = { 'Authorization': 'QNT ' + authorization }
getUserInfo().then(result => {
app.globalData.userInfo = result.data
this.setData({ loging: false})
loginToken({loginToken: app.globalData.token}).then(result => {
zconfig.header = { 'Authorization': 'QNT ' + result.data }
// cconfig.header = { 'Authorization': 'QNT ' + result.data }
// pconfig.header = { 'Authorization': 'QNT ' + result.data }
// 登录流程完成;
wx.hideLoading()
wx.navigateBack()
event.emit('EventMessage', { what: 888, desc: 'Logined' })
}).catch(err => {
console.log(err)
app.globalData.token = null
config.header = null
wx.hideLoading()
util.showToast(err)
wx.navigateBack()
})
}).catch(err => {
wx.hideLoading()
app.globalData.token = null
config.header = null
this.setData({ loging: false})
})
},
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) {
// /authorize/by-captcha 验证码认证
loginCaptcha(this.data.form).then(result => {
wx.setStorageSync('Authorization', result.data.token)
app.globalData.token = result.data.token
this.fetchUserInfo()
}).catch(error => {
wx.hideLoading()
util.showToast(error)
})
} else {
// authorize/by-password 密码登录
}
},
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 })
// /send/captcha 发送验证码
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)
})
},
onGotPhoneNumber: function (e) {
request.phoneWechat(e.detail).then(result => {
this.setData({ ['form.account']: result.data.phoneNumber, mobileEnable: true })
})
},
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 => {
wx.setStorageSync('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
util.showToast(error.message)
}
})
},
onUnload: function () {
if (this.wuxCountDown) {
this.wuxCountDown.stop()
this.wuxCountDown = null
}
}
})