Fastadmin中使用小程序登录

在fastadmin中使用三方登录插件原有没有小程序登录,需要增加小程序登录

目录

增加小程序登录

安装

修改配置

增加服务提供者

注册服务提供者

登录调用

总结


增加小程序登录

安装

首先在插件管理中找到三方登录插件安装

安装最新的1.4.7

修改配置

在third/config.php文件中,weibo配置下仿照上面的配置增加wechatmini配置,

如下:

php 复制代码
[
    'name' => 'wechatmini',
    'title' => '微信小程序',
    'type' => 'array',
    'content' => [
        'app_id' => '',
        'app_secret' => '',
        'scope' => 'snsapi_userinfo',
    ],
    'value' => [
        'app_id' => '',
        'secret' => '',
        'scope' => 'get_user_info',
    ],
    'rule' => 'required',
    'msg' => '',
    'tip' => '',
    'ok' => '',
    'extend' => '',
],

修改后保存后,在三方登录配置中增加微信小程序配置内容,如下:

增加服务提供者

在third/library中增加wechatmini.php文件,为小程序登录处理。

文件内容如下:

php 复制代码
<?php

namespace addons\third\library;

use fast\Http;
use think\Config;
use think\Session;
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 ?: $_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']) && $sns['openid']) {
                    return $sns;
                }
            }
        }
        return [];
    }
}

注册服务提供者

在third/library/Application.php中注册服务提供者。

登录调用

在api/User.php的third方法中处理登录时,可通过platform参数传递wechatmini,

调用小程序登录方法,如下:

总结

在fastadmin中使用三方登录插件原有没有小程序登录,需要增加小程序登录

相关推荐
darkb1rd2 小时前
二、PHP 5.4-7.4版本演进与安全改进
安全·php·webshell
程序 代码狂人2 小时前
CentOS7初始化配置操作
linux·运维·开发语言·php
m0_748233173 小时前
PHP版本演进:从7.x到8.x全解析
java·开发语言·php
zhengfei6113 小时前
精选的优秀法证分析工具和资源列表
开发语言·php
说私域3 小时前
开源链动2+1模式商城小程序在深度分销数字化转型中的应用研究
人工智能·小程序·开源·流量运营·私域运营
REDcker4 小时前
RFC1918私有IP地址空间详解
网络协议·tcp/ip·php
咖啡の猫4 小时前
微信小程序案例 - 自定义 tabBar
微信小程序·小程序·notepad++
咖啡の猫5 小时前
微信小程序全局数据共享
微信小程序·小程序
桐溪漂流5 小时前
微信小程序cli脚本预览上传
微信小程序·小程序
咖啡の猫5 小时前
微信小程序使用 npm 包
微信小程序·小程序·npm