uniapp开发H5网页授权步骤

第一步是微信公众平台申请测试账号
在这里插入图片描述
第二部在网页授权(修改)填写回调域名,测试可以使用自己本地的端口,注意:这里不可以添加http:// 只准添加例如:baidu.com
在这里插入图片描述
在这里插入图片描述
第三部按照里面的只是即可
在这里插入图片描述
代码如下

1// 获取code
			getCode() {
				this.code = ''
				let origin = 'http://192.168.1.115:8082/mycenter/mycenter' //网页授权的回调域名,这里设置的是本地端口
				let urlNow = encodeURIComponent(origin); //处理域名
				let scope = "snsapi_userinfo"; //弹框显示授权
				let appid = "公众平台申请的id";
				this.code = this.codeurl  
				// 截取code
				if (this.code == null || this.code === '') { //未授权qu授权
					let url =
				`https://open.weixin.qq.com/connect/oauth2/authorize?
				appid=${appid}&redirect_uri=${urlNow}&
				response_type=code&scope=${scope}&
				state=123#wechat_redirect`;
					window.location.href = url;
				}
			},
			getUrlCode() { // 截取url中的code方法
				var url = location.search
				var theRequest = new Object()
				if (url.indexOf("?") != -1) {
					var str = url.substr(1)
					var strs = str.split("&")
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
					}
				}
				return theRequest
			},

2.// 拿code换取accessToken和openid最后获取用户信息
			async saveOpenID() {
				let data = {
					code: this.codeurl,
					initiationID: this.rankvalue(32)
				}
				if (this.codeurl) {
					const res = await this.myrequest.post('/dlt/user/access_token', data)
					if (res.result) {
						this.accessToken = res.result.accessToken
						this.openid = res.result.openid
						this.login() //获取用户登录信息
					}
				}
			},
			// 获取登录用户信息
			async login() {
				let loginData = {
					loginCode: this.openid,
					initiationID: this.rankvalue(32),
					accessToken: this.accessToken
				}
				const userInfo = await this.myrequest.post("/dlt/user/login", loginData)
				this.headImage =userInfo.result.headImage
				this.userName = userInfo.result.userName
			},


created() {	
	 this.codeurl = this.getUrlCode().code //获取code
	 this.getCode() 	
	},
  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值