纸通宝SAAS仓库
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.
 

411 lines
14 KiB

import Dialog from '../../components/dialog/dialog'
import { config, headerFactoryId, wechatApplet, loginWechat, postCaptcha, loginCaptcha, loginPwd, verifyCode, bindAccount, applyEmplyee } from "../../api/user"
import { sconfig, loginToken, getUserInfo, bindingFactory, bindCustomer } from "../../api/saas"
import { pconfig } from "../../api/payment"
import { zconfig } from "../../api/ztb"
import { mconfig } from "../../api/moment"
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,
path: null,
code: null
// code: '?mark=ztb_saas&QrCodeRecordId=597399780326510592'
},
/************************************** 初始化登录流程 ********************************************/
onLoad: function (options) {
app.globalData.keyboardHeight = storage.get('keyboardHeight') || 0
if(options.q){
//扫二维码进入的,
let q = decodeURIComponent(options.q)
this.data.code = q.substring(q.indexOf('?'))
} else if(options.qrcode){
//分享进入
this.data.code = '?mark=ztb_saas&QrCodeRecordId=' + options.qrcode
}
if (options.url) {
this.data.path = options.url
if (options.key && options.value) {
this.data.path += '&key=' + options.key + '&value=' + options.value
}
}
app.globalData.token = app.globalData.token || storage.get('Authorization')
if(this.data.code){
this.fetchQrCode(this.data.code)
} else if(!util.isEmpty(app.globalData.token)){
this.setData({ loging: true})
this.fetchUserInfo(app.globalData.token)
}
wx.login({
success: res => {
wechatApplet({ authCode: res.code }).then(result => {
storage.put('tmpAuthToken', result.data.tmpAuthToken, 98)
})
}
})
},
/************************************** 获取用户信息,进行登录 ********************************************/
checkFactoryId: function(storageFactoryId){
for (let mIndex = app.userInfo.enterpriseInfos.length - 1; mIndex >= 0; mIndex--) {
const element = app.userInfo.enterpriseInfos[mIndex]
for (let nIndex = element.factoryInfos.length - 1; nIndex >= 0; nIndex--) {
const item = element.factoryInfos[nIndex]
if(storageFactoryId){
if(item.factoryId == storageFactoryId){
return item
}
} else {
if(item.factoryId){
return item
}
}
}
}
return null
},
toIndex: function(){
var factoryId = storage.get('X-FACTORY-ID' + app.userInfo.userId)
var factory = null
if(factoryId){
// 有可能被移除了,缓存的factoryId还在,那要进行处理
factory = this.checkFactoryId(factoryId)
if(factory) {
factoryId = factory.factoryId
}
}
if(!factory){
factory = this.checkFactoryId()
if(factory) {
factoryId = factory.factoryId
}
}
if(!factory){
this.setData({ loging: false})
Dialog.alert({ title: '温馨提示', message: '还没有您所属的打包站信息,请联系相关客服人员' }).then(() => {
})
return
}
if(factoryId){
storage.put('X-FACTORY-ID' + app.userInfo.userId, factoryId)
}
app.userInfo.factory = factory
app.userInfo.factoryId = factoryId
headerFactoryId(app.userInfo.factoryId)
if(this.data.path) {
wx.redirectTo({ url: '/pages/index/index?url=' + this.data.path })
} else {
wx.redirectTo({ url: '/pages/index/index' })
}
},
contactMeta: function(metaData){
for (let mIndex = 0; mIndex < app.userInfo.enterpriseInfos.length; mIndex++) {
const element = app.userInfo.enterpriseInfos[mIndex]
if(element.isEnterprisePrimary && element.enterpriseId == metaData.enterpriseId){
return true
}
for (let nIndex = 0; nIndex < element.factoryInfos.length; nIndex++) {
const item = element.factoryInfos[nIndex]
if(item.factoryId == metaData.factoryId){
return true
}
}
}
return false
},
fetchUserInfo: function(authorization){
config.header = { 'Authorization': 'QNT ' + authorization }
// token 切换流程
loginToken({loginToken: authorization}).then(result => {
app.globalData.authorization = result.data
sconfig.header = { 'Authorization': 'QNT ' + result.data }
pconfig.header = { 'Authorization': 'QNT ' + result.data }
zconfig.header = { 'Authorization': 'QNT ' + result.data }
mconfig.header = { 'Authorization': 'QNT ' + result.data }
getUserInfo().then(result => {
app.userInfo = result.data
app.globalData.token = authorization
storage.put('Authorization', app.globalData.token)
if(this.data.metaData){
// 如果metaData的factoryId或者enterpriseId已经包含用户信息中了,那就直接登录;
if(this.contactMeta(this.data.metaData)){
this.toIndex()
return
}
// 这里要进行账号的绑定,对工厂进行绑定;或者进行申请操作;
if(this.data.metaData.qrPage == '/page/index/register'){
this.data.metaData.userName = this.data.metaData.factoryCustomerName || '默认姓名'
}
if(!util.isEmpty(this.data.metaData.factoryCustomerMobile)){
this.setData({ loging: false, metaData: this.data.metaData, ['form.account']: this.data.metaData.factoryCustomerMobile })
} else {
this.setData({ loging: false, metaData: this.data.metaData })
}
} else if(app.userInfo.enterpriseInfos && app.userInfo.enterpriseInfos.length){
for (let mIndex = 0; mIndex < app.userInfo.enterpriseInfos.length; mIndex++) {
const element = app.userInfo.enterpriseInfos[mIndex]
if(element.isEnterprisePrimary){
// 如果是企业主
this.toIndex()
return
}
for (let nIndex = 0; nIndex < element.factoryInfos.length; nIndex++) {
// 是否有职位
if(element.factoryInfos[nIndex].type){
this.toIndex()
return
}
}
}
this.setData({ loging: false})
Dialog.alert({ title: '温馨提示', message: '还没有您所属的打包站信息,请联系相关客服人员' }).then(() => {
})
} else {
this.setData({ loging: false})
Dialog.alert({ title: '温馨提示', message: '还没有您所属的打包站信息,请联系相关客服人员' }).then(() => {
})
}
}).catch(err => {
if(err.code == 200101){
util.showToast('获取用户信息失败,请注册登录')
this.setData({ loging: false, regist: true})
} else {
util.showToast(err)
this.setData({ loging: false})
}
config.header = null
})
}).catch(err => {
if(err !== null && err instanceof Object) {
util.showToast('服务器错误')
} else {
util.showToast(err)
}
config.header = null
this.setData({ loging: false})
})
},
/************************************** 注册流程 ********************************************/
readAgreement: function () {
if(this.data.loging){
return
}
wx.navigateTo({ url: '/pages/htmls/agreement/index' })
},
onGetUserProfile: function(){
if(this.data.loging){
return
}
wx.getUserProfile({
desc: '业务需要',
success: res => {
this.onGotUserInfo({detail: res })
}
})
},
onGotUserInfo: function ({detail}) {
if(this.data.loging){
return
}
this.setData({ loging: true })
loginWechat({ encryptedData: detail.encryptedData, iv: 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
storage.put('keyboardHeight', app.globalData.keyboardHeight)
},
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
}
this.setData({ loging: true })
if (this.data.type == 1) {
loginCaptcha(this.data.form).then(result => {
this.fetchUserInfo(result.data.token)
}).catch(error => {
util.showToast(error)
this.setData({ loging: false })
})
} else {
loginPwd(this.data.form).then(result => {
this.fetchUserInfo(result.data.token)
}).catch(error => {
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.loging){
return
}
if (this.data.pwdType === 'password') {
this.setData({ pwdType: 'text' })
} else {
this.setData({ pwdType: 'password' })
}
},
/************************************** 二维码流程 ********************************************/
fetchQrCode: function(code){
this.setData({ loging: true})
verifyCode(code).then(result => {
this.data.code = null
if(result.data.redirectUrl == '/login'){
this.fetchUserInfo(result.data.metaData.token)
} else {
this.data.metaData = result.data.metaData
this.data.metaData.qrPage = result.data.redirectUrl
if(!util.isEmpty(app.globalData.token)){
this.fetchUserInfo(app.globalData.token)
} else {
this.setData({ loging: false})
}
}
}).catch(err => {
util.showToast('二维码解析失败,请重新扫码')
this.setData({ loging: false})
})
},
submitForm: function(){
if(!this.data.metaData){
util.showToast('您的申请已经提交,不要重复提交')
return
}
this.setData({loging: true})
if(this.data.metaData.qrPage == '/page/index/ztbsaas/yaoqing'){
// 绑定企业主账号
bindAccount(this.data.metaData).then(result => {
this.data.metaData = null
this.fetchUserInfo(app.globalData.token)
}).catch(error => {
this.setData({loging: false})
util.showToast(error)
})
} else if(this.data.metaData.qrPage == '/page/index/yaoqing'){
// 绑定工厂主账号
bindingFactory(this.data.metaData).then(result => {
this.data.metaData = null
this.fetchUserInfo(app.globalData.token)
}).catch(error => {
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
this.setData({loging: false})
Dialog.alert({ title: '温馨提示', message: '您的申请已经提交,待管理员审核通过之后在进行登录。' }).then(() => {
this.fetchUserInfo(app.globalData.token)
})
}).catch(error => {
this.setData({loging: false})
util.showToast(error)
})
} else if(this.data.metaData.qrPage == '/page/index/register'){
this.data.metaData.factorycustomerId = this.data.metaData.factoryCustomerId
bindCustomer(this.data.metaData).then(result => {
this.data.metaData = null
this.setData({loging: false})
wx.redirectTo({ url: '/xtends/client/index/index' })
}).catch(error => {
this.setData({loging: false})
util.showToast(error)
})
} else {
Dialog.alert({ title: '温馨提示', message: '扫码错误,请重新扫码。' }).then(() => {
})
}
}
})