PHP code example of hlw2326 / think-plugin-mp-shared

1. Go to this page and download the library: Download hlw2326/think-plugin-mp-shared library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hlw2326 / think-plugin-mp-shared example snippets




declare(strict_types=1);

namespace app\mini\service;

use app\mini\model\MiniUser;
use hlw2326\mp\shared\service\UserResolverService;

/**
 * 插件用户解析器注册服务
 * 所有小程序插件通过 UserResolverService 获取当前登录用户 ID
 * @class PluginResolverService
 * @package app\mini\service
 */
class PluginResolverService extends \think\admin\Service
{
    protected function initialize(): void
    {
        // 注册通用解析器('*' 匹配所有 appid)
        UserResolverService::register(function (string $appid): ?string {
            return MiniUser::getCurrentUserId($appid);
        });
    }
}



declare(strict_types=1);

namespace plugin\xxx\controller\api\v1;

use hlw2326\mp\shared\service\UserResolverService;
use think\admin\Controller;

abstract class Base extends Controller
{
    protected string $appid = '';
    protected ?string $userId = null;

    public function __construct()
    {
        parent::__construct();
        $this->appid = $this->request->header('appid', $this->request->get('appid', $this->request->post('appid', '')));
        $this->userId = UserResolverService::getUserId($this->appid);
    }
}