纸通宝小程序
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.
 

237 lines
7.6 KiB

//获取应用实例
import { $wuxCountDown } from '../../components/index'
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 })
}
var check = wx.getStorageSync('AgreeCheck')
app.globalData.kg = wx.getStorageSync('ztb-kg')
this.checkLogin()
if (check) {
this.setData({check: true})
}
},
wxLogin: function(){
wx.login({
success: res => {
// 获取openId
wx.request({
header: { 'X-APP-ID': app.xAppId },
url: app.httpUrl + '/recycle-service/authorize/wechat-applet/user',
data: { authCode: res.code },
method: 'POST',
success: function (result) {
if (result.data.code == 0) {
storage.put('tempToken', result.data.data.tmpAuthToken, 98)
}
}
})
}
})
},
checkLogin: function () {
var authorization = wx.getStorageSync('Authorization')
// console.log('authorization>>>' + authorization)
if (authorization) {
app.globalData.token = authorization
var userToken = wx.getStorageSync('accessToken')
if (userToken) {
app.globalData.userToken = userToken
}
this.setData({ loging: true})
// /front/customer/myInfo 用户信息
request.get('/recycle-service/user/get/base-info').then(result => {
this.doLoginResponse(result)
}).catch(err => {
//异常回调
this.setData({ loging: false})
this.wxLogin()
})
} else {
this.wxLogin()
}
},
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 })
// this.data.form.password = md5.hexMD5(this.data.form.password).toUpperCase()
var url = '/recycle-service/authorize/by-captcha'
if (this.data.type == 1) {
// /authorize/by-captcha 验证码认证
url = '/recycle-service/authorize/by-captcha'
} else {
// authorize/by-password 密码登录
url = '/recycle-service/authorize/by-password'
}
request.post(url, this.data.form).then(result => {
wx.setStorageSync('accessToken', result.data.userToken)
wx.setStorageSync('Authorization', result.data.token)
app.globalData.token = result.data.token
app.globalData.userToken = result.data.userToken
this.checkLogin()
}).catch(error => {
wx.hideLoading()
util.showToast(error)
})
},
// 进行登录处理;
doLoginResponse: function (result) {
if (result.data.token) {
wx.setStorageSync('accessToken', result.data.userToken)
wx.setStorageSync('Authorization', result.data.token)
app.globalData.token = result.data.token
app.globalData.userToken = result.data.userToken
}
app.globalData.userInfo = result.data
event.emit('EventMessage', { what: 888, desc: 'Logined' })
if(app.globalData.userToken){
// /receive/v150/get/OrderExists 判断该用户是否有订单存在 1存在 2不存在
request.get('/saas-trade/receive/v150/get/OrderExists').then(result => {
wx.hideLoading()
if (Number(result.data) != 1 || !app.release) {
wx.navigateBack()
} else {
wx.reLaunch({ url: '/pages/paper/index' })
}
}).catch(err => {
//异常回调
wx.hideLoading()
wx.navigateBack()
})
} else {
wx.navigateBack()
}
},
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 发送验证码
request.post('/recycle-service/send/captcha', { account: this.data.form.account, accountType: 'MOBILE', purpose: 'RECYCLE_CLIENT_LOGIN' }).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) {
request.loginWechat({ encryptedData: e.detail.encryptedData, iv: e.detail.iv }).then(result => {
wx.setStorageSync('accessToken', result.data.userToken)
wx.setStorageSync('Authorization', result.data.token)
app.globalData.token = result.data.token
app.globalData.userToken = result.data.userToken
this.checkLogin()
}).catch(error => {
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
}
}
})