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.
334 lines
12 KiB
334 lines
12 KiB
//获取应用实例
|
|
import Dialog from '../../components/dialog/dialog'
|
|
import { config, wechatApplet, loginWechat, postCaptcha, loginCaptcha, loginPwd, verifyCode, applyEmplyee } from "../../api/user"
|
|
import { sconfig, loginToken, getUserInfo, bindingAdmin } from "../../api/saas"
|
|
const util = require('../../utils/util')
|
|
const storage = require('../../utils/storage')
|
|
const app = getApp()
|
|
|
|
Page({
|
|
data: {
|
|
type: 1,
|
|
pwdType: 'password',
|
|
mobileEnable: false,
|
|
codeEnable: true,
|
|
loginEnable: true,
|
|
second: '获取验证码',
|
|
form: {
|
|
accountType: 2,
|
|
tmpAuthToken: null,
|
|
account: null,
|
|
captcha: null
|
|
},
|
|
loging: false,
|
|
regist: false,
|
|
metaData: null,
|
|
code: null
|
|
// code: '?mark=ztb_saas&QrCodeRecordId=554641854092677120'
|
|
},
|
|
/************************************** 初始化登录流程 ********************************************/
|
|
onLoad: function (options) {
|
|
const updateManager = wx.getUpdateManager()
|
|
updateManager.onCheckForUpdate()
|
|
updateManager.onUpdateReady(function () {
|
|
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
|
|
updateManager.applyUpdate()
|
|
})
|
|
if(options.q){
|
|
//扫二维码进入的,
|
|
let q = decodeURIComponent(options.q)
|
|
this.data.code = q.substring(q.indexOf('?'))
|
|
}
|
|
this.wxLogin()
|
|
},
|
|
onReady: function () {
|
|
wx.getSystemInfo({
|
|
success: e => {
|
|
app.globalData.isIos = this.checkIos(e)
|
|
app.globalData.dev = e.platform == 'devtools'
|
|
|
|
let custom = wx.getMenuButtonBoundingClientRect()
|
|
app.globalData.Custom = custom
|
|
// 顶部操作栏高度
|
|
app.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight + (app.globalData.isIos ? 4 : 0)
|
|
let windowHeight = e.windowHeight * (750 / e.windowWidth)
|
|
|
|
let safeBottom = e.windowHeight - e.safeArea.bottom
|
|
if(safeBottom > e.windowHeight){
|
|
safeBottom = 34
|
|
}
|
|
app.globalData.safeBottom = safeBottom * (750 / e.windowWidth)
|
|
// 状态栏高度
|
|
app.globalData.StatusBar = e.statusBarHeight * (750 / e.windowWidth)
|
|
let statusBarHeight = app.globalData.CustomBar * (750 / e.windowWidth)
|
|
app.globalData.statusBarHeight = statusBarHeight
|
|
// fragmentHeight:指的是整个页面statusBarHeight以下的高度
|
|
app.globalData.fragmentHeight = windowHeight - statusBarHeight
|
|
app.globalData.safeFragmentHeight = windowHeight - statusBarHeight - app.globalData.safeBottom
|
|
app.globalData.windowWidth = e.windowWidth
|
|
|
|
var authorization = app.globalData.token || storage.get('Authorization')
|
|
if(this.data.code){
|
|
this.fetchQrCode(this.data.code)
|
|
} else if(!util.isEmpty(authorization)){
|
|
wx.showLoading({ title: '登录中', mask: true })
|
|
this.setData({ loging: true})
|
|
this.fetchUserInfo(authorization)
|
|
}
|
|
}
|
|
})
|
|
},
|
|
checkIos: function (e) {
|
|
if ('ios' === e.platform) {
|
|
return true
|
|
}
|
|
if (e.system.startsWith('iOS')) {
|
|
return true
|
|
}
|
|
return false
|
|
},
|
|
/************************************** 获取用户信息,进行登录 ********************************************/
|
|
fetchUserInfo: function(authorization){
|
|
config.header = { 'Authorization': 'QNT ' + authorization }
|
|
// token 切换流程
|
|
loginToken({loginToken: authorization}).then(result => {
|
|
sconfig.header = { 'Authorization': 'QNT ' + result.data }
|
|
getUserInfo().then(result => {
|
|
wx.hideLoading()
|
|
app.userInfo = result.data
|
|
app.globalData.token = authorization
|
|
storage.put('Authorization', app.globalData.token)
|
|
if(app.userInfo.factoryId && this.data.metaData && app.userInfo.factoryId != this.data.metaData.factoryId){
|
|
Dialog.alert({ title: '温馨提示', message: '您已经绑定过工厂了,现在就进入?' }).then(() => {
|
|
wx.redirectTo({ url: '/pages/index/index' })
|
|
})
|
|
} else if(this.data.metaData){
|
|
// 这里要进行账号的绑定,对工厂进行绑定;或者进行申请操作;
|
|
this.setData({ loging: false, metaData: this.data.metaData })
|
|
} else {
|
|
wx.redirectTo({ url: '/pages/index/index' })
|
|
}
|
|
// if(app.userInfo.enterpriseId){
|
|
// storage.put('Authorization', app.globalData.token)
|
|
// this.setData({ loging: false})
|
|
// if(this.data.metaData && app.userInfo.factoryId != this.data.metaData.factoryId){
|
|
// Dialog.alert({ title: '温馨提示', message: '您已经绑定过工厂了,现在就进入?' }).then(() => {
|
|
// wx.redirectTo({ url: '/pages/index/index' })
|
|
// })
|
|
// } else {
|
|
// wx.redirectTo({ url: '/pages/index/index' })
|
|
// }
|
|
// } else if(this.data.metaData){
|
|
// // 这里要进行账号的绑定,对工厂进行绑定;或者进行申请操作;
|
|
// this.setData({ loging: false, metaData: this.data.metaData })
|
|
// } else if(app.userInfo.applicationStatus == 0){
|
|
// this.setData({ loging: false})
|
|
// Dialog.alert({ title: '温馨提示', message: '您的申请还在审核中,如有疑问请联系管理员。' }).then(() => {
|
|
// })
|
|
// } else {
|
|
// this.setData({ loging: false})
|
|
// Dialog.alert({ title: '温馨提示', message: '您还不是纸通宝SAAS用户,请联系工厂管理员,或者联系纸通宝客户人员。' }).then(() => {
|
|
// })
|
|
// }
|
|
}).catch(err => {
|
|
wx.hideLoading()
|
|
if(err.code == 400){
|
|
util.showToast('获取用户信息失败,请注册登录')
|
|
this.setData({ loging: false, regist: true})
|
|
} else {
|
|
util.showToast(err)
|
|
this.setData({ loging: false})
|
|
}
|
|
config.header = null
|
|
})
|
|
}).catch(err => {
|
|
wx.hideLoading()
|
|
util.showToast(err)
|
|
config.header = null
|
|
this.setData({ loging: false})
|
|
})
|
|
},
|
|
wxLogin: function(){
|
|
wx.login({
|
|
success: res => {
|
|
wechatApplet({ authCode: res.code }).then(result => {
|
|
storage.put('tmpAuthToken', result.data.tmpAuthToken, 98)
|
|
})
|
|
}
|
|
})
|
|
},
|
|
/************************************** 注册流程 ********************************************/
|
|
readAgreement: function () {
|
|
wx.navigateTo({ url: '/pages/htmls/agreement/index' })
|
|
},
|
|
onGotUserInfo: function (e) {
|
|
if(this.data.loging){
|
|
return
|
|
}
|
|
this.setData({ loging: true })
|
|
loginWechat({ encryptedData: e.detail.encryptedData, iv: e.detail.iv }).then(result => {
|
|
this.data.form.tmpAuthToken = result.data.tmpAuthToken
|
|
this.fetchUserInfo(result.data.token)
|
|
}).catch(error => {
|
|
if(error.data && error.data.tmpAuthToken){
|
|
this.data.form.tmpAuthToken = error.data.tmpAuthToken
|
|
this.setData({ regist: true, loging: false })
|
|
} else {
|
|
this.setData({ loging: false })
|
|
}
|
|
util.showToast(error.message)
|
|
})
|
|
},
|
|
inputFocus: function(e) {
|
|
app.globalData.keyboardHeight = e.detail.height
|
|
},
|
|
bindInput: function (e) {
|
|
this.data.form[e.target.id] = e.detail.value
|
|
if ('account' == e.target.id) {
|
|
if ((/^1\d{10}$/.test(e.detail.value)) && e.detail.value.length == 11) {
|
|
this.setData({ mobileEnable: true })
|
|
}
|
|
} else if ('userName' == e.target.id) {
|
|
this.setData({ ['metaData.userName']: e.detail.value })
|
|
}
|
|
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 })
|
|
}
|
|
},
|
|
onCountChange: function(e) {
|
|
if(e.detail.minutes != 0){
|
|
return
|
|
}
|
|
this.setData({ second: e.detail.seconds + ' 秒 ' })
|
|
},
|
|
onFinished: function(){
|
|
this.setData({ second: '重新获取验证码', codeEnable: true })
|
|
},
|
|
fetchCaptcha: function(){
|
|
if (!(/^1\d{10}$/.test(this.data.form.account)) || this.data.form.account.length < 11) {
|
|
util.showToast('请输入11位手机号码')
|
|
return
|
|
}
|
|
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.countDown = this.countDown || this.selectComponent('.count-down')
|
|
this.countDown.reset()
|
|
this.countDown.start()
|
|
}).catch(err => {
|
|
wx.hideLoading()
|
|
util.showToast(err)
|
|
})
|
|
},
|
|
registAccount: 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.setData({ loging: true })
|
|
if (this.data.type == 1) {
|
|
loginCaptcha(this.data.form).then(result => {
|
|
this.fetchUserInfo(result.data.token)
|
|
}).catch(error => {
|
|
wx.hideLoading()
|
|
util.showToast(error)
|
|
this.setData({ loging: false })
|
|
})
|
|
} else {
|
|
loginPwd(this.data.form).then(result => {
|
|
this.fetchUserInfo(result.data.token)
|
|
}).catch(error => {
|
|
wx.hideLoading()
|
|
util.showToast(error)
|
|
this.setData({ loging: false })
|
|
})
|
|
}
|
|
},
|
|
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 })
|
|
}
|
|
},
|
|
changePwd: function () {
|
|
if (this.data.pwdType === 'password') {
|
|
this.setData({ pwdType: 'text' })
|
|
} else {
|
|
this.setData({ pwdType: 'password' })
|
|
}
|
|
},
|
|
/************************************** 二维码流程 ********************************************/
|
|
fetchQrCode: function(code){
|
|
wx.showLoading({ title: '处理中', mask: true })
|
|
this.setData({ loging: true})
|
|
verifyCode(code).then(result => {
|
|
this.data.code = null
|
|
if(result.data.redirectUrl == '/login'){
|
|
// 根据userId来获取用户信息;并且直接登录
|
|
this.fetchUserInfo(result.data.metaData.token)
|
|
} else {
|
|
wx.hideLoading()
|
|
this.data.metaData = result.data.metaData
|
|
this.data.metaData.qrPage = result.data.redirectUrl
|
|
this.setData({ loging: false})
|
|
}
|
|
}).catch(err => {
|
|
wx.hideLoading()
|
|
util.showToast('二维码解析失败,请重新扫码')
|
|
this.setData({ loging: false})
|
|
})
|
|
},
|
|
submitForm: function(){
|
|
if(this.data.metaData.qrPage == '/page/index/yaoqing'){
|
|
bindingAdmin(this.data.metaData).then(result => {
|
|
wx.hideLoading()
|
|
storage.put('Authorization', app.globalData.token)
|
|
app.userInfo.factoryId = this.data.metaData.factoryId
|
|
app.userInfo.factoryName = this.data.metaData.factoryName
|
|
this.data.metaData = null
|
|
this.setData({loging: false})
|
|
wx.redirectTo({ url: '/pages/index/index' })
|
|
}).catch(error => {
|
|
wx.hideLoading()
|
|
this.setData({loging: false})
|
|
util.showToast(error)
|
|
})
|
|
} else if(this.data.metaData.qrPage == '/page/index/shenqing'){
|
|
applyEmplyee(this.data.metaData).then(result => {
|
|
this.data.metaData = null
|
|
wx.hideLoading()
|
|
this.setData({loging: false})
|
|
Dialog.alert({ title: '温馨提示', message: '您的申请已经提交,待管理员审核通过之后在进行登录。' }).then(() => {
|
|
wx.redirectTo({ url: '/pages/index/index' })
|
|
})
|
|
}).catch(error => {
|
|
wx.hideLoading()
|
|
this.setData({loging: false})
|
|
util.showToast(error)
|
|
})
|
|
} else {
|
|
Dialog.alert({ title: '温馨提示', message: '扫码错误,请重新扫码。' }).then(() => {
|
|
})
|
|
}
|
|
}
|
|
})
|