Fastadmin 第三方登录扩展微信小程序模块

Fastadmin 第三方登录扩展微信小程序模块

第一步:执行安装第三方登录插件

第二步:找到目录addons/third/config.php文件 加入下面配置信息

    [
        'name' => 'wechatmini',
        'title' => '微信小程序',
        'type' => 'array',
        'content' => [
            'app_id' => '',
            'app_secret' => '',
            'scope' => 'snsapi_userinfo',
        ],
        'value' => [
            'app_id' => 'wx72e541ff2238317d6',
            'secret' => '81b90qe12q2d3sdf2257e20a79b549fa',
            'scope' => 'get_user_info',
        ],
        'rule' => 'required',
        'msg' => '',
        'tip' => '',
        'ok' => '',
        'extend' => '',
    ],

第三步:修改addons/third/library/Application.php 加入wechatmini

    /**
     * 服务提供者
     * @var array
     */
    private $providers = [
        'qq'      => 'Qq',
        'weibo'   => 'Weibo',
        'wechat'  => 'Wechat',
        **'wechatmini' => 'Wechatmini',**
    ];

第四步:添加addons/third/library/Wechatmini.php 文件

<?php

namespace addons\third\library;

use fast\Http;
use think\Config;
use think\Session;
// use EasyWeChat\Foundation\Application;
use EasyWeChat\Factory;

/**
 * 微信
 */
class Wechatmini
{

    /**
     * 配置信息
     * @var array
     */
    private $config = [];

    public function __construct($options = [])
    {
        if ($config = Config::get('third.wechatmini'))
        {
            $this->config = array_merge($this->config, $config);
        }
        $this->config = array_merge($this->config, is_array($options) ? $options : []);
    }



    /**
     * 获取用户信息
     * @param array $params
     * @return array
     */
    public function getUserInfo($params = [])
    {
        $params = $params ? $params : $_GET;
        if (isset($params['code']))
        {
            if ($params['code'])
            {
                $config = $this->config;
                $app = Factory::miniProgram($config);
                $sns = $app->auth->session($params['code']);
                if (isset($sns['openid'])){
                    if($sns['openid']){
                        $userinfo = $params['rawData'] ? json_decode(stripslashes(html_entity_decode($params['rawData'])),true) : [];
                        $userinfo['avatar'] = isset($userinfo['avatarUrl']) ? $userinfo['avatarUrl'] : '';
                        $userinfo['nickname'] = isset($userinfo['nickName']) ? $userinfo['nickName'] : '';
                        $result = [
                            'access_token'  => '',
                            'refresh_token' => '',
                            'expires_in'    => 0,
                            'openid'        => $sns['openid'],
                            'unionid'       => '',
                            'userinfo'      => $userinfo
                        ];
                        return $result;
                    }
                }
            }
        }
        return [];
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值