|
@@ -1,35 +1,93 @@
|
|
|
-// import Captcha from '@/components/common/captcha';
|
|
|
|
|
-
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * PC 登录页共用逻辑:账号密码走 /auth/login,短信登录走 /auth/send-login-sms、/auth/sms-login(对齐 ghsApp)。
|
|
|
|
|
+ */
|
|
|
export default {
|
|
export default {
|
|
|
- // components: {
|
|
|
|
|
- // Captcha
|
|
|
|
|
- // },
|
|
|
|
|
-
|
|
|
|
|
data() {
|
|
data() {
|
|
|
return {
|
|
return {
|
|
|
form: {
|
|
form: {
|
|
|
mobile: null,
|
|
mobile: null,
|
|
|
password: null,
|
|
password: null,
|
|
|
- // captchaId: '',
|
|
|
|
|
- verifyCode: ''
|
|
|
|
|
|
|
+ verifyCode: '',
|
|
|
|
|
+ smsCode: ''
|
|
|
},
|
|
},
|
|
|
|
|
+ /** pwd=账号密码 / sms=短信验证码 */
|
|
|
|
|
+ loginMode: 'pwd',
|
|
|
|
|
+ smsCountdown: 0,
|
|
|
|
|
+ smsTimer: null,
|
|
|
saving: false,
|
|
saving: false,
|
|
|
loading: true
|
|
loading: true
|
|
|
};
|
|
};
|
|
|
},
|
|
},
|
|
|
mounted() {
|
|
mounted() {
|
|
|
this.loading = false;
|
|
this.loading = false;
|
|
|
|
|
+ this.initSmsCountdown();
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
this.form.mobile = '15280215347';
|
|
this.form.mobile = '15280215347';
|
|
|
this.form.password = 'a.1234';
|
|
this.form.password = 'a.1234';
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
|
|
+ beforeDestroy() {
|
|
|
|
|
+ if (this.smsTimer) {
|
|
|
|
|
+ clearInterval(this.smsTimer);
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
|
|
|
methods: {
|
|
methods: {
|
|
|
captchaChange() {
|
|
captchaChange() {
|
|
|
this.form.verifyCode = '';
|
|
this.form.verifyCode = '';
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
|
|
+ /** 与小程序 checkMobile 一致:11 位国内手机号 */
|
|
|
|
|
+ isMobile(mobile) {
|
|
|
|
|
+ return /^1\d{10}$/.test(String(mobile || ''));
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ /** 从 localStorage 恢复发码倒计时,避免刷新立刻再发 */
|
|
|
|
|
+ initSmsCountdown() {
|
|
|
|
|
+ const lastSent = localStorage.getItem('last_sms_sent_time_login');
|
|
|
|
|
+ if (!lastSent) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ const passed = Math.floor((Date.now() - Number(lastSent)) / 1000);
|
|
|
|
|
+ if (passed < 60) {
|
|
|
|
|
+ this.startCountdown(60 - passed);
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ /** 启动验证码倒计时 */
|
|
|
|
|
+ startCountdown(seconds) {
|
|
|
|
|
+ this.smsCountdown = seconds;
|
|
|
|
|
+ if (this.smsTimer) {
|
|
|
|
|
+ clearInterval(this.smsTimer);
|
|
|
|
|
+ }
|
|
|
|
|
+ this.smsTimer = setInterval(() => {
|
|
|
|
|
+ this.smsCountdown--;
|
|
|
|
|
+ if (this.smsCountdown <= 0) {
|
|
|
|
|
+ clearInterval(this.smsTimer);
|
|
|
|
|
+ }
|
|
|
|
|
+ }, 1000);
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ /** 登录成功后拉用户/菜单并进首页,密码登录与短信登录共用 */
|
|
|
|
|
+ async afterLoginSuccess() {
|
|
|
|
|
+ if (this.$projectName == 'business') {
|
|
|
|
|
+ await this.$store.dispatch('userInfo');
|
|
|
|
|
+ await this.$store.dispatch('shopInfo');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ await this.$store.dispatch('adminInfo');
|
|
|
|
|
+ }
|
|
|
|
|
+ const [first] = await this.$store.dispatch('permMenu');
|
|
|
|
|
+ this.saving = false;
|
|
|
|
|
+ if (!first) {
|
|
|
|
|
+ this.$message.error('该账号没有权限');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ this.$nextTick(() => {
|
|
|
|
|
+ this.$router.push('/');
|
|
|
|
|
+ });
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ /** 账号密码登录:/auth/login */
|
|
|
async next() {
|
|
async next() {
|
|
|
const { mobile, password } = this.form;
|
|
const { mobile, password } = this.form;
|
|
|
|
|
|
|
@@ -44,37 +102,52 @@ export default {
|
|
|
this.saving = true;
|
|
this.saving = true;
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- // 登录
|
|
|
|
|
await this.$store.dispatch('userLogin', this.form);
|
|
await this.$store.dispatch('userLogin', this.form);
|
|
|
|
|
+ await this.afterLoginSuccess();
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ this.saving = false;
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
|
|
|
- if (this.$projectName == 'business') {
|
|
|
|
|
- // 用户信息
|
|
|
|
|
- await this.$store.dispatch('userInfo');
|
|
|
|
|
- // 商户信息
|
|
|
|
|
- await this.$store.dispatch('shopInfo');
|
|
|
|
|
- } else {
|
|
|
|
|
- // 后台信息
|
|
|
|
|
- await this.$store.dispatch('adminInfo');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 权限菜单
|
|
|
|
|
- const [first] = await this.$store.dispatch('permMenu');
|
|
|
|
|
|
|
+ /** 获取登录短信验证码:/auth/send-login-sms */
|
|
|
|
|
+ getSmsCode() {
|
|
|
|
|
+ const mobile = this.form.mobile;
|
|
|
|
|
+ if (!mobile) {
|
|
|
|
|
+ return this.$message.warning('请输入手机号');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!this.isMobile(mobile)) {
|
|
|
|
|
+ return this.$message.warning('请输入正确手机号');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (this.smsCountdown > 0) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ this.$service.common.sendLoginSms({ mobile }).then(() => {
|
|
|
|
|
+ this.$message.success('验证码已发送');
|
|
|
|
|
+ localStorage.setItem('last_sms_sent_time_login', String(Date.now()));
|
|
|
|
|
+ this.startCountdown(60);
|
|
|
|
|
+ });
|
|
|
|
|
+ },
|
|
|
|
|
|
|
|
- this.saving = false;
|
|
|
|
|
|
|
+ /** 短信验证码登录:/auth/sms-login,成功后与密码登录同一套进首页流程 */
|
|
|
|
|
+ async smsNext() {
|
|
|
|
|
+ const mobile = this.form.mobile;
|
|
|
|
|
+ const code = this.form.smsCode;
|
|
|
|
|
+ if (!mobile) {
|
|
|
|
|
+ return this.$message.warning('请输入手机号');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!this.isMobile(mobile)) {
|
|
|
|
|
+ return this.$message.warning('请输入正确手机号');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!code) {
|
|
|
|
|
+ return this.$message.warning('请输入验证码');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (!first) {
|
|
|
|
|
- this.$message.error('该账号没有权限');
|
|
|
|
|
- } else {
|
|
|
|
|
- this.$nextTick(() => {
|
|
|
|
|
- if (this.$projectName == 'business') {
|
|
|
|
|
- this.$router.push('/');
|
|
|
|
|
- } else {
|
|
|
|
|
- this.$router.push('/');
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ this.saving = true;
|
|
|
|
|
+ try {
|
|
|
|
|
+ const res = await this.$service.common.smsLogin({ mobile, code });
|
|
|
|
|
+ this.$store.commit('SET_TOKEN', res);
|
|
|
|
|
+ await this.afterLoginSuccess();
|
|
|
} catch (err) {
|
|
} catch (err) {
|
|
|
- // this.$refs.captcha.refresh();
|
|
|
|
|
this.saving = false;
|
|
this.saving = false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|