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 [];
}
}