微信公众号 授权

代码

	onLoad(e) {
			if(e.scene){
				this.regcode = e.scene;
			}
			if(e.regcode){
				this.regcode = e.regcode;
			}

			 // #ifdef H5
		        var app_type = this.isWeiXin() ? 'wechat' : 'h5';
		        var app_type_name = this.isWeiXin() ? '微信公众号' : 'H5';

				if(app_type  == 'wechat'){
					console.log('微信公众号环境')
					this.checkWeChatCode();
				}
			// #endif
			
		},
		methods: {
		checkWeChatCode() {
				// 页面一进来先判断是否有code
				this.code = this.$u.comFun.getUrlCode('code')
				if (this.code) {
				// 如果有,走登录接口
					this.getLogin(this.code)
				} else {
				// 没有调后端接口获取地址
					let url = {
						redirect_url:window.location.href
					}
					this.$u.api.getAuthUrl(url).then(res=>{
						if(res.code == 1){
							window.location.href = res.data.url
						}
					});
				}
			},
			// 微信授权
			async getLogin(code){
				let data = {
					code:code,
					regcode:this.regcode
				}
				const res = await this.$u.api.authByCode(data); // 调登录接口
				if (res.code == 1) {
					this.$u.vuex('vuex_token', res.data.token.access_token)
					this.$u.vuex('vuex_userInfo', res.data.member)
					this.$store.dispatch('material').then(m => {
						this.$refs.uToast.show({
							message: res.msg,
							type: 'success',
							complete: () => {
								this.$u.comFun.losePageUrl()
							}
						})
					})
				}
			},
			isWeiXin() {
		        // #ifndef H5
		        return false;
		        // #endif
		        var ua = navigator.userAgent.toLowerCase();
		        if (ua.match(/MicroMessenger/i) == "micromessenger") {
		            return true;
		        } else {
		            return false;
		        }
		    },
		}

配置

  1. 登录微信公众号后台 https://mp.weixin.qq.com/
  2. 关注相关项目 公众号
  3. 设置网页授权域名,把文件下载下来发给后端,把text文件上传到服务器根目录下在这里插入图片描述
  4. 在这里插入图片描述
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值