uniapp(小程序)+php获取openid

这段代码展示了如何在小程序中实现登录并获取Openid的过程。首先通过uni.login获取code,然后将code发送到后台,后台通过调用微信API将code转换为openid。这个过程对于实现用户身份验证至关重要。
摘要由CSDN通过智能技术生成
			//获取code
			getcode(){
				let that = this
				uni.login({
					success:function(res){
						let code = res.code
						//console.log(res,"获取的code ")
						that.getopenid(code)
					}
				})
			},
			
			//获取openid
			getopenid(code){
				console.log(code,"传递的code")
				this.$axios.axios(
					'POST',
					this.$paths.indexGetOpid, {
						code:code
					}
				).then(res => {
					if (res.code == 1) {
						console.log(res.data)
						this.openid = res.data.openid
					}
					console.log(res, "openid获取")
				})
			},
//获取Openid
    public function getOpid()
    {
        $appid = 'xxx'; // 小程序APPID
        $secret = 'xxx'; // 小程序secret
        $code = Request()->param('code');//code
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 5000);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);

        $data = json_decode($res,true);
        $this->success('成功',$data);
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值