• uniapp开发的h5,使用微信授权登录(前置条件+具体代码)


    原文

    • 微信内嵌浏览器运行H5版时,可以调起微信登录
    • 普通浏览器调起微信登陆是不开放的,只有个别开发者才有,比如京东

    前置条件

    在微信内嵌浏览器运行H5版时,调起微信登录,需要配置回调域名 (请注意,这里填写的是域名(是一个字符串),而不是URL,因此请勿加 http:// 等协议头;),具体步骤如下

    1. 打开微信公众平台,登录上去
    2. 点击【公众号设置】
    3. 点击【功能设置】
    4. 找到【网页授权域名】,点击旁边的【设置】
    5. 在修改业务域名和JS接口域名时,已经上传过这个文件的话,那么请直接跳过这一步。如果还没上传的,直接点击文件下载,然后上传到服务器。 (这个可以找后台人员去干)
    6. 上传成功后,直接输入授权域名,点击【确认】即可

    具体代码如下

    微信开发文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
    scope为snsapi_base时, 为静默登录
    scope为snsapi_userinfo时,会弹出“xxx申请获得你的微信头像、昵称、地区和性别信息”这样的弹出框,需要经过用户同意
    


    <!-- 注册页面 -->
    <template>
    	<view class="bottom-side-otherLogin" @click="getWeChatCode" v-if="isWeixin">
    		<text>其他社交账号登录</text>
    		<image src="https://xuezhifu-resource.oss-cn-hangzhou.aliyuncs.com/newxuefu/mwx/wx.png"></image>
    	</view>
    </template>
    <script>
    	export default {
    		data() {
    			return {
    				isWeixin: false,
    			};
    		},
    		onLoad() {
    			this.isWeixin = this.isWechat()
    			if(this.isWeixin){
    				this.checkWeChatCode()//通过微信官方接口获取code之后,会重新刷新设置的回调地址【redirect_uri】
    			}
    		},
    		onShow() {
    		},
    		mounted() {
     
    		},
    		methods: {
    			/*微信登录相关  start*/
    			//方法:用来判断是否是微信内置的浏览器
    			isWechat() {
    			        return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
    			},
    			//方法:用来提取code
    			getUrlCode(name) {
    				return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1]
    					.replace(/\+/g, '%20')) || null
    			},
    			//检查浏览器地址栏中微信接口返回的code
    			checkWeChatCode() {
    				let code = this.getUrlCode('code')
    				uni.showToast({
    					title:`微信code=${code}`
    				})
    				if (code) {
    					
    					this.getOpenidAndUserinfo(code)
    				}
    			},
    			//请求微信接口,用来获取code
    			getWeChatCode() {
    				let local = encodeURIComponent(window.location.href); //获取当前页面地址作为回调地址
    				let appid = '自己的appid'
    				
    				//通过微信官方接口获取code之后,会重新刷新设置的回调地址【redirect_uri】
    				window.location.href =
    					"https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
    					appid + 
    					"&redirect_uri=" +
    					local +
    					"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
    			},
    			//把code传递给后台接口,静默登录
    			getOpenidAndUserinfo(code) {
    				this.$http({
    					url:'api/login',
    					data:{
    						code:code
    					}
    				}).then((res) => {
    					console.log(res)
    					if (res.code != 0) {
    						uni.showToast({
    							title: res.msg,
    							duration: 3000,
    							icon: 'none'
    						});
    						return
    					}else{
    						this.afterLogin(res)
    					}
    				})
    			},
    			/*微信登录相关  end*/
    			afterLogin(res){
    				let user = res.data.user
    				uni.setStorageSync('token', res.data.token);
    				let u = {
    					avatar: user.avatar ? user.avatar : this.avatar,
    					mobile: user.mobile,
    					nickname: user.nickname ? user.nickname : '土肥圆'
    				}
    				uni.setStorage({
    					key: 'u',
    					data: u,
    					success: () => {
    						
    						let url = uni.getStorageSync('redirect')
    						uni.reLaunch({
    							url: url ? url : '/pages/index'
    						})
    					}
    				});
    			},
    		},
     
    	}
    </script>
     
    
  • 相关阅读:
    百度脑图源码
    H5与Native交互的实现
    【GOF23设计模式】建造者模式
    【GOF23设计模式】工厂模式
    【GOF23设计模式】单例模式
    Linux符设备驱动编程
    linux多线程编程——读者优先、写者优先问题
    建立makefile
    构建交叉开发环境
    Failed to create the part's controls解决方法
  • 原文地址:https://www.cnblogs.com/kerin/p/15718162.html
Copyright © 2020-2023  润新知