easywechat6 微信小程序获取用户信息登录

前端

uniapp

tempalte

    <button
      class="btn"
  @click="login"
    >
      <text class="gui-grids-icon gui-icons">&#xe63e;</text>
      <span>微信用户快捷登录</span>
    </button>
script`

script

    // 登录
    async login() {
        const handle = {
            desc: "登录和注册",
            success: async res => {
                const user = await this.$api.login({
                    userInfo: res.userInfo, // 用户信息
                    code: await this.getCode()
                })
            },
            fail: res => {
                throw Error(res)
            }
        }
        uni.getUserProfile(handle)
    },

    getCode(){ // 获取code
        return new Promise(resolve => {
            uni.login({
              provider: 'weixin',
              success: res => resolve(res.code)
            })        
        }) 
    }

后端

use EasyWeChat\MiniApp\Application;
$config = [
            'app_id' => env('WECHAT_MiNIAPP_APPID'),
            'secret' => env('WECHAT_MiNIAPP_SECRET'),
        ];

        $app = new Application($config);
        $utils = $app->getUtils();
        $session = $utils->codeToSession($request->input('code'));
        /**
         * $session的信息如下:
         * array(4) {
        ["session_key"]=>
        string(24) "J2uNYxCScVBvIv****"
        ["expires_in"]=>
        int(7200)
        ["openid"]=>
        string(28) "oEwUe0SquhhRJ1yGs7n****"
        ["unionid"]=>
        string(28) "oLpCKv9IW5CWZdkSu6CUf****"
        }
         */

        $user_info = $request->input('userInfo'); // 前端传递过来的用户信息
       // 接下根据业务处理逻辑
本作品采用《CC 协议》,转载必须注明作者和本文链接
专心学习不瞎搞
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
87
粉丝
105
喜欢
480
收藏
717
排名:109
访问:8.7 万
私信
所有博文
社区赞助商