首页 > 编程语言 >uni-app+php:微信小程序登录:用code得到openid/unionid(hbuilderx 3.7.3)

uni-app+php:微信小程序登录:用code得到openid/unionid(hbuilderx 3.7.3)

时间:2023-03-14 15:46:22浏览次数:64  
标签:openid code console com res 微信 data

一,js代码:

<template>
    <view>
         <button class="login-wxpng" open-type="getUserInfo" @getuserinfo="xcxWxLogin">
                    微信小程序登录
         </button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                
            }
        },
        methods: {
           xcxWxLogin() {
                uni.login({
                    provider: 'weixin',
                    success: function(res) {
                        console.log("微信登录的res结果:");
                        console.log(res);
                        
                        //开始获取openid
                        if (res.code) {
                            //发起网络请求
                            uni.request({
                                method: 'GET',
                                url: 'http://api.lhdtest.com/auth/xcxuserinfo',
                                data: {
                                        app_key: 'wxxcx',
                                        v: '2.75',
                                        code: res.code,
                                },
                                success(res) {
                                        console.log('xcxuserinfo:');
                                        console.log(res);
                                }
                            })
                        } else {
                            console.log('登录失败!' + res.errMsg)
                        }
                        
                    },
                });
            },
        }
    }
</script>

<style>

</style>

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: [email protected]

二,服务端php代码:

class Auth extends BaseController
{
   private string $appId = 'wxnideappid';
   private string $appSecret = 'nideappsecret';

    //根据code得到微信用户的信息
    public function xcxUserInfo(){
        $code = $this->request->param('code','','string');
        $result=$this->getSession($code);
        var_dump($result);
    }

    //用code得到微信的session信息,包含用户id
    public function getSession($code){
        $s_data = array();
        $s_data['appid'] = $this->appId;
        $s_data['secret'] = $this->appSecret;
        $s_data['js_code'] = $code;
        $s_data['grant_type']="authorization_code";
        $session_url = 'https://api.weixin.qq.com/sns/jscode2session?' . http_build_query ( $s_data );
        $content = file_get_contents ( $session_url );
        $content = json_decode ( $content, true );
        $content['appid']=$s_data['appid'];
        return $content;
    }
}

三,测试效果

 

 

四,查看hbuilderx的版本: 

 

标签:openid,code,console,com,res,微信,data
From: https://www.cnblogs.com/architectforest/p/17215144.html

相关文章

  • LeetCode 15. 三数之和
    classSolution{public:vector<vector<int>>threeSum(vector<int>&nums){vector<vector<int>>res;sort(nums.begin(),nums.end());......
  • uni-app:微信小程序登录:获取code时返回the code is a mock one(hbuilderx 3.7.3)
    一,微信小程序:wx.login时报错返回的code值为:thecodeisamockone如图:说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest ......
  • Code Generate 代码生成器 V1.0
    CodeGenerateV1.0代码生成器根据配置的模板,根据建表语句,生成Code。例如java代码、vue代码、jsp代码以及html代码等等,均可根据自己的代码写作习惯进行配置。缺点:配置......
  • vscode-vim键盘操作配置
    vscode(win)+vim键盘操作配置参考了知乎博主云崖君的vscode+vim全键盘操作高效搭配方案,仅用以记录个人使用配置,内容较为简略。配置文件如下详见后续说明。s......
  • APP-uniapp-uni.login实现微信登陆
    直接上代码登录按钮,单击方法weixinLogin(){console.log("进入微信登录");uni.login({provider:'weixin',success:functio......
  • 微信小程序封装一个带有图片的Tab栏
    前言最近在做一个微信小程序,看到有许多软件上都有一种带有图片可以滑动的Tab栏,然后我就去看了一些组件库,包括Vant,WeUI,发现他们所提供的Tab栏好像都只有标题(title)的功能,假......
  • 什么是SAAS微信小程序?
    SAAS小程序是一种基于云计算、软件即服务(SoftwareasaService,SAAS)的小程序。SAAS小程序提供基于云端的应用程序服务,用户可以通过互联网访问和使用这些应用程序,而不必自己......
  • [思维提升|干货All in]6种算法解决LeetCode困难题:滑动窗口最大值
    为了更好的阅读体验,欢迎阅读原文:[思维提升|干货Allin]6种算法解决LeetCode困难题:滑动窗口最大值(eriktse.com)最近在leetcode遇到一道非常经典的题目:239.滑动窗口最......
  • [LeetCode] 62. 不同路径 java 动态规划解法
    classSolution{publicintuniquePaths(intm,intn){//确定dp数组以及下标的含义//dp[i][j]:表⽰从(0,0)出发,到(i,j)有dp[i][j]条不同的路径......
  • atcoder ABC
    Ex-OptimalPathDecomposition题目只能给链染色,问你最短的(两点距离最大值),距离为不同颜色个数f[u],g[u],f表示u可以和father同一个颜色,g表示不可以。转移记录三个值。......