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中使用三方登录插件原有没有小程序登录,需要增加小程序登录

相关推荐
sheji341620 小时前
【开题答辩全过程】以 基于微信小程序的少儿编程学习平台为例,包含答辩的问题和答案
学习·微信小程序·小程序
2401_8442213221 小时前
深入理解K8s中的应用服务:访问、集群与配置
容器·kubernetes·php
const_qiu1 天前
微信小程序自动化测试100%通过率实践
微信小程序·小程序
xiangpanf1 天前
PHP vs Python:30字看透两大语言差异
开发语言·php
xiangpanf1 天前
PHP爬虫框架:Goutte vs Panther
开发语言·c++·vue.js·php
Greg_Zhong1 天前
小程序中登出/切换/重置页面状态的使用
小程序
常利兵1 天前
从0到1:搭建Spring Boot 3企业级认证授权平台
数据库·spring boot·php
一字白首1 天前
微信小程序进阶实战:从 UI 组件库到全局状态管理全解DAY05
ui·微信小程序·小程序
t198751282 天前
使用深度神经网络解决无线网络资源分配问题的MATLAB实现
matlab·php·dnn
m0_459252462 天前
fastadmin动态渲染统计信息
开发语言·前端·javascript·php