weChatOffAcc.vue 4.44 KB
<template>
	<view class="container">
		<div>微信公众号-授权认证中</div>
	</view>
</template>

<script>
	const {
		Base64
	} = require('js-base64');
	export default {
		data() {
			return {
				currentModeIndex: 2, //1账号密码 2验证码 3忘记密码
				inputType: 'password',
				codeDisabled: false,
				codeTips: '',
				formData: {
					mobile: '',
					password: '',
				},
				flag: true,
				rules: {
					mobile: [{
							type: 'string',
							required: true,
							message: '请输入手机号',
							trigger: ['blur', 'change']
						},
						// {
						//   // 自定义验证函数,见上说明
						//   validator: (rule, value, callback) => {
						//     // 上面有说,返回true表示校验通过,返回false表示不通过
						//     // uni.$u.test.mobile()就是返回true或者false的
						//     return uni.$u.test.mobile(value)
						//   },
						//   message: '手机号码不正确',
						//   // 触发器可以同时用blur和change
						//   trigger: ['change', 'blur']
						// }
					],
					password: {
						type: 'string',
						min: 4,
						max: 16,
						required: true,
						message: '密码长度4-16位密码',
						trigger: ['blur', 'change']
					},
					code: {
						type: 'integer',
						len: 4,
						required: true,
						message: '请填写4位验证码',
						trigger: ['blur', 'change']
					}
				},
				isAutoLogin: [], //是否自动登录
				hasForgot: false, //忘记密码
				loginText: "短信登录",
				btnText: "发送验证码",
				checkValue: ['agree'],
				accountTile: "短信验证码登录",
				accountTip: "未注册手机号登录后自动生成账号",
				accountPlaceholder: "请输入手机号",
				appid: this.$config.appid,
				openId: null
			}
		},
		created() {
			this.handleAuthorization();
		},
		methods: {
			handleAuthorization() {
				console.log("================================");
				this.code = "";
				console.log("window.location.href", window.location.href);
				let local = window.location.href;
				console.log("local", local);
				let scope = "snsapi_base"; //静默授权 用户无感知
				console.log("this.scope", scope);
				this.code = this.getParameters('code');
				console.log("this.code", this.code);
				
				// 本地调试可用 设置好地址后无需重定向
				// let jumpToUri =
				// 	`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.$config.appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=${scope}&state=hotent#wechat_redirect`
				// console.log("jumpToUri", jumpToUri);
				// if (this.code == null || this.code === "") {
				// 	window.location.href = jumpToUri;
				// } else {
				// 	this.getOpenId(this.code);
				// }
				
				this.getOpenId(this.code);
				console.log("================================");
			},
			getOpenId(code) {
				let that = this;
				this.$api.loginApi.getWechatOpenId(code).then(res => {
					console.log("res", res);
					if (res.openid) {
						that.openId = res.openid;
						//首次登录未绑定账号去登录页登录后触发绑定
						console.log("获取openId", that.openId);
						uni.setStorageSync('openId', this.openId)
						uni.navigateTo({
							url:'/pages/login'
						})
					} else {
						//已经绑定直接缓存token 跳转到主页
						this.$store.dispatch('wxAuthLogin', res).then(e => {
							uni.switchTab({
								url: '/pages/home/home'
							});
						}).catch(err => {
							uni.$u.toast(err)
						})
					}
				});
			},
			getUrlCode() {
				let fullUrl = window.location.href;
				console.log("fullUrl ", fullUrl);
				let theRequest = new Object();
				if (fullUrl.indexOf("?") != -1) {
					var temp = fullUrl.split("?");
					let str = temp[1];
					let strs = str.split("&");
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
					}
				}
				return theRequest;
			},
			//获取url中的参数
			getParameters(name) {
				var locUrl = decodeURI(window.location.search.substr(1))
				var aryParams = locUrl.split('&')
				var rtn = ''
				for (var i = 0; i < aryParams.length; i++) {
					var pair = aryParams[i]
					var aryEnt = pair.split('=')
					var key = aryEnt[0]
					var val = aryEnt[1]
					if (key != name) continue
					if (rtn == '') {
						rtn = val
					} else {
						rtn += ',' + val
					}
				}
				return rtn
			},
		},
	};
</script>

<style lang="scss" scoped>
	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100vh;
	}
</style>