h5登录

<script>
	var _this;
export default {
		data() {
			return {
				phone: '', //用户/电话
				password: '', //密码
				code: '' //微信登陆的code
			};
		},
		onLoad(params) {
			let code = this.getCode('code');
			if (code) {
				this.showLoading('正在登陆');
				this.code = code;
				this.wxLoginByCode();
			}
			// if (params.hasOwnProperty('code')) {
			// 	this.showLoading('正在登陆');
			// 	this.code = params.code;
			// 	this.wxLoginByCode();
			// }
		},

		methods: {
			back() {
				this.navigateBack();
			},
			getCode(name) {
				var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
				var r = window.location.search.substr(1).match(reg);
				if (r != null) return decodeURIComponent(r[2]);
				return null;
			},
			// 点击操作,过去微信授权登录
			wxLogin() {
				var params = window.location.href;
				const backUrl = window.encodeURIComponent(params);
				const url = `https://www.c3w.com.cn/authorize/baixiao/get_code?url=${backUrl}`;
				window.location.href = `https://www.c3w.com.cn/authorize/baixiao/get_code?url=${backUrl}`;
			},
			// wxLoginByCode
			async wxLoginByCode() {
				const { data } = await this.loginHttp.reqWxLogin({ code: this.code });
				if (data) {
					this.setData('token', data.token);
					if (this.getData('back_url')) {
						let back_url = this.getData('back_url');
						let origin = window.location.origin;
						window.location.href = origin + back_url
					} else {
						this.switchTab('/pages/index/index');
					}
				}
			},
			async startLogin() {
				const { phone, password } = this;
				if (!this.testPhone()) {
					return;
				}
				if (!password) {
					return this.uniToast('密码不能为空');
				}
				wx.showLoading({
					title: '正在登录'
				});
				const { data } = await this.loginHttp.reqLogin({
					phone,
					password
				});
				wx.hideLoading();
				if (data) {
					this.setData('token', data.token);
					if (this.getData('back_url')) {
						let back_url = this.getData('back_url');
						let origin = window.location.origin;
						window.location.href = origin + back_url
					} else {
						this.switchTab('/pages/index/index');
					}
					// const back_url = this.getData('back_url');
					// const back_tab = this.getData('back_tab');
					// if (back_url) {
					// 	if (back_tab) {
					// 		this.removeData('back_url');
					// 		this.removeData('back_tab');
					// 		this.switchTab(back_url);
					// 	} else {
					// 		this.removeData('back_url');
					// 		this.redirectTo(back_url);
					// 	}
					// } else {
					// 	this.switchTab('/pages/index/index');
					// }
				}
			},
			testPhone() {
				const phone = this.phone;
				if (!phone) {
					this.uniToast('手机号不能为空');
					return false;
				}
				if (!/^1[3456789]\d{9}$/.test(phone)) {
					this.uniToast('手机号格式错误');
					return false;
				}
				return true;
			}
		}
	};
</script>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值