login.js 3.8 KB

import { mapGetters, mapActions, mapState } from "vuex";

import api from '@/api/api'
import {
	httpGet,
	httpPost
} from '@/api/fetch-api.js'
import { reject } from '_any-promise@1.3.0@any-promise';

let urls

let PK = 'B5FE03847F02046C47292AF0FF2DE88977241483DD40C123046EB39CBE4C48167B120096CFF12CD16559322884A3C56FA92B07B89AB51FC8C91A75127622151DDD730DFF1F993D5A290CEAC0BBA7FC88285D8994ACBAFF50101EDE9A0925AD5DFFAFE96D53C370E9C5B37DF2F871F81C4D7CA6B7EC37FF459C07975AD9A74A95';
let E = '10001';

export default {
	data() {
		return {
			key: 'value',
			type: 1, // 1:帐密登陆 2:OTP登陆,
			values: {
				// 返回的token,串连整个流程,后台安全校验使用
				token: "",
				deviceId: "",
			},
			loginForm: {
				userId: "18334783910",
				password: "qweqwe123"
			},
			loginCheck: {
				showImageCode: false,
				agreeProtocol: false
			},
			PK: process.env.RSA_PUBLIC_KEY || PK,
			E: process.env.RSA_KEY_INDEX || E,
		}
	},
	components: {},
	computed: {
		...mapState({
			userInfo: state => state.userInfo
		}),
		locale() {
			return this.$i18n.locale || 'tc';
		},
		i18n() {
			return this.$i18n.messages && this.$i18n.locale ? this.$i18n.messages[this.$i18n.locale] : {};
		}
	},
	methods: {
		onCheckHandler() {

		},
		onProtocolHandler() {
			this.$router.push({
				path: "/protocol"
			})
		},
		onSubmitHandler() {

		},
		onRegisterHandler() {
			this.$router.push({
				path: "/register"
			})
		},
		onForgetHandler() {

		},
		onLoginTypeHandler(val) {
			this.type = val;
		},
		initData() { },
		handlerIsShowImageVcode() {
			return new Promise((resolve, reject) => {
				httpPost({
					url: api.stdIsShowImageVcode,
					data: {
						deviceId: this.values.deviceId,
						userId: this.loginForm.userId
					}
				}).then(response => {
					// 判断是否显示图形验证码
					if (this._handlerIsShowImageVcodeResponse(response)) {
						resolve(response);
					}
				})
			});
		},
		_handlerIsShowImageVcodeResponse(response) {
			if (response.returnCode == "0" && response.data.isShowVcode == "N") {
				return true;
			}
			this.values.token = response.data.token;
			return false;
		},
		handlerRefreshVcode() {
			// 刷新图形二维码
		},
		handlerLogin() {
			this.refreshDeviceId();
			// console.log("this.loginCheck.agreeProtocol  == ", this.loginCheck.agreeProtocol);
			if (!this.loginCheck.agreeProtocol) {
				this._showAgreeProtocalTips();
				return;
			}
			// 刷新图形二维码
			this.handlerIsShowImageVcode().then(() => {
				httpPost({
					url: api.gsLogin,
					data: {
						deviceId: this.values.deviceId,
						loginName: this.loginForm.userId,
						loginPwd: this._passwordEncrypt(this.loginForm.password)
					}
				}).then(response => {
					this._handlerLoginResponse(response);
				})
			});
		},
		// 处理登录结果
		_handlerLoginResponse(response) {
			// let res = response.content;
			if (response.returnCode == 0) {
				this.$store.commit("SET_USER_INFO", response.data);
				this._redirectTo();
			} else {
				let msg = response.returnMsg;
				this._showLoginErrorMessage(msg);
			}
		},
		_showAgreeProtocalTips() {
			alert("请同意《平安一账通会员服务协议》");
		},
		_showLoginErrorMessage(message) {
			alert(message);
		},
		_redirectTo() {
			let path = this.$route.query.callback || "/index";
			this.$router.push({
				path: path
			});
		},
		handlerSendOTP() {
			// 发送短信
		},
		handlerValidateOTPandRepeat() {
			// 验证短信验证码
		},
		refreshDeviceId() {
			this.values.deviceId = this.values.deviceId || (Math.random() + "").substring(2)
		},
		_passwordEncrypt(rawPwd) {
			let rsa = new RSAKey();
			rsa.setPublic(this.PK, this.E);
			let res = rsa.encrypt(rawPwd);
			if (res == null) return rawPwd;
			return res;
		}
	},
	mounted() {
		// console.log("PK ===  ", this.PK)
		// console.log("E ===  ", this.E)
	},
	created() { }
}