// pages/login/login.js
|
|
// 导入封装通用模块方法
|
import { toast } from '@/utils/extendApi'
|
// 导入本地存储 api
|
import { setStorage } from '@/utils/storage'
|
// 导入接口 API 函数
|
import { loginMessageUser, loginVerifyMessage, PostLogin } from '@/api/account';
|
// 导入 ComponentWithStore 方法
|
import { ComponentWithStore } from 'mobx-miniprogram-bindings'
|
// 导入 store 对象
|
import { accountStore } from '@/stores/accountStore'
|
|
import { STATIC_FILE_BASE_URL } from '@/config/constants'
|
import { verifyPhone } from '@/utils/toolsValidate';
|
const app = getApp();
|
// 使用 ComponentWithStore 方法替换 Component 方法构造页面
|
ComponentWithStore({
|
// 让页面和 Store 对象建立关联
|
storeBindings: {
|
store: accountStore,
|
fields: ['session', 'username'],
|
actions: ['setSession', 'setUsername']
|
},
|
data: {
|
activeTab: 'account',
|
user: '',
|
password: '',
|
STATIC_FILE_BASE_URL,
|
countNum: null,
|
countTimer: null,
|
phoneNumber: '',
|
verifyCode: '',
|
},
|
lifetimes: {
|
attached() {
|
wx.hideHomeButton();
|
},
|
|
},
|
pageLifetimes: {
|
hide() {
|
this.resetContent();
|
}
|
},
|
methods: {
|
|
resetContent() {
|
switch (this.data.activeTab) {
|
case 'account':
|
this.setData({
|
user: '',
|
password: '',
|
})
|
break;
|
case 'cellPhoneNumber':
|
this.setData({
|
phoneNumber: '',
|
verifyCode: '',
|
})
|
this.stopCount();
|
this.setData({
|
countNum: null
|
})
|
default:
|
break;
|
}
|
|
},
|
tabChange: function (event) {
|
this.setData({
|
activeTab: event.detail.name
|
})
|
this.resetContent();
|
},
|
async getSMSClick() {
|
if (this.data.countNum !== null) {
|
return;
|
}
|
if (!this.data.phoneNumber) {
|
toast({
|
title: '请输入手机号码!'
|
});
|
return;
|
}
|
console.log(this.data.phoneNumber);
|
const isValid = verifyPhone(this.data.phoneNumber)
|
console.log(isValid);
|
|
if (!isValid) {
|
toast({
|
title: '请输入正确的手机号码!'
|
});
|
return;
|
}
|
this.setData({
|
verifyCode: ''
|
})
|
const res = await loginVerifyMessage({
|
phone: this.data.phoneNumber
|
},{
|
noAuth: true
|
})
|
|
if (!res?.json_ok) {
|
return;
|
}
|
this.startCount();
|
},
|
stopCount() {
|
clearInterval(this.data.countTimer)
|
},
|
|
startCount() {
|
this.stopCount();
|
this.setData({
|
countNum: 60
|
})
|
this.data.countTimer = setInterval(() => {
|
if (this.data.countNum === 0) {
|
this.setData({
|
countNum: null
|
})
|
clearInterval(this.data.countTimer);
|
return;
|
}
|
this.setData({
|
countNum: this.data.countNum - 1
|
})
|
}, 1000);
|
},
|
|
handlePhoneNumName(phoneStr){
|
return phoneStr.substr(0,3)+"****"+phoneStr.substr(7);
|
},
|
|
// 授权登录
|
login: wx.$_.debounce(async function () {
|
|
let res: any = null;
|
switch (this.data.activeTab) {
|
case 'account':
|
if (!this.data.user) {
|
toast({
|
title: '请输入账号!'
|
});
|
return;
|
}
|
if (!this.data.password) {
|
toast({
|
title: '请输入密码!'
|
});
|
return;
|
}
|
|
res = await PostLogin({
|
user: this.data.user,
|
pass: this.data.password,
|
}, {
|
noAuth: true
|
});
|
setStorage('username', this.data.user);
|
this.setUsername(this.data.user);
|
app.globalData.user.phoneNum = '';
|
|
break;
|
case 'cellPhoneNumber':
|
|
if (!this.data.phoneNumber) {
|
toast({
|
title: '请输入手机号码!'
|
});
|
return;
|
}
|
if (!verifyPhone(this.data.phoneNumber)) {
|
toast({
|
title: '请输入正确的手机号码!'
|
});
|
return;
|
}
|
if (!this.data.verifyCode) {
|
toast({
|
title: '请输入验证码!'
|
});
|
return;
|
}
|
res = await loginMessageUser({
|
phone: this.data.phoneNumber,
|
code: this.data.verifyCode,
|
}, {
|
noAuth: true
|
});
|
const phoneName = this.handlePhoneNumName(this.data.phoneNumber)
|
setStorage('username',phoneName);
|
this.setUsername(phoneName);
|
app.globalData.user.phoneNum = this.data.phoneNumber;
|
break;
|
default:
|
break;
|
}
|
if (!res.json_ok || !res.hswatersession) {
|
toast({
|
title: res.json_msg
|
})
|
return;
|
}
|
setStorage('session', res.hswatersession);
|
this.setSession(res.hswatersession)
|
|
wx.switchTab({
|
url: '/pages/question/question'
|
});
|
}, 500),
|
}
|
})
|