getWechatConfig($arguments[0])); } if ($type === 'ThinkService' && $class === 'Config') { return ConfigService::instance()->init($arguments[0]); } if ($type === 'WeOpen') { return new $classname(self::instance()->getServiceConfig()); } throw new \think\admin\Exception("class {$classname} not defined."); } /** * 生成公众号授权信息. * @param array $info 生成授权信息 */ public static function buildAuthData(array $info): array { $info = array_change_key_case($info); $info['business_info'] = serialize($info['business_info']); $info['verify_type'] = $info['verify_type_info']['id'] != 0 ? '未认证' : '已认证'; if (isset($info['func_info']) && is_array($info['func_info'])) { $funcinfo = array_column($info['func_info'], 'funcscope_category'); $info['func_info'] = join(',', array_column($funcinfo, 'id')); } if (empty($info['miniprograminfo'])) { $info['service_type'] = $info['service_type_info']['id'] == 2 ? '服务号' : '订阅号'; $info['miniprograminfo'] = ''; } else { $info['service_type'] = '小程序'; $info['miniprograminfo'] = serialize($info['miniprograminfo']); } $data = [ 'user_name' => $info['user_name'], 'user_alias' => $info['alias'], 'user_company' => $info['principal_name'], 'user_signature' => $info['signature'], 'user_nickname' => $info['nick_name'], 'service_type' => $info['service_type'], 'service_verify' => $info['verify_type'], 'qrcode_url' => $info['qrcode_url'], 'businessinfo' => $info['business_info'], 'miniprograminfo' => $info['miniprograminfo'], ]; if (isset($info['head_img'])) { $data['user_headimg'] = $info['head_img']; } $keys = 'func_info,expires_in,authorizer_appid,authorizer_access_token,authorizer_refresh_token'; foreach (explode(',', $keys) as $key) { if (isset($info[$key])) { $data[$key] = $info[$key]; } } return $data; } /** * 获取公众号配置参数. * @throws \think\admin\Exception */ public function getWechatConfig(string $appid): array { $conifg = [ 'appid' => $appid, 'token' => sysconf('service.component_token'), 'appsecret' => sysconf('service.component_appsecret'), 'encodingaeskey' => sysconf('service.component_encodingaeskey'), 'cache_path' => $this->getCachePath(), ]; $conifg['GetAccessTokenCallback'] = function ($authorizerAppid) { $map = ['authorizer_appid' => $authorizerAppid]; $refreshToken = WechatAuth::mk()->where($map)->value('authorizer_refresh_token'); if (empty($refreshToken)) { throw new \think\admin\Exception('The WeChat information is not configured.', '404'); } // 刷新公众号原授权 AccessToken $result = AuthService::WeOpenService()->refreshAccessToken($authorizerAppid, $refreshToken); if (empty($result['authorizer_access_token']) || empty($result['authorizer_refresh_token'])) { throw new Exception($result['errmsg']); } // 更新公众号授权信息 WechatAuth::mk()->where($map)->update([ 'authorizer_access_token' => $result['authorizer_access_token'], 'authorizer_refresh_token' => $result['authorizer_refresh_token'], ]); return $result['authorizer_access_token']; }; return $conifg; } /** * 获取服务平台配置参数. * @throws \think\admin\Exception */ public function getServiceConfig(): array { return [ 'cache_path' => $this->getCachePath(), 'component_appid' => sysconf('service.component_appid'), 'component_token' => sysconf('service.component_token'), 'component_appsecret' => sysconf('service.component_appsecret'), 'component_encodingaeskey' => sysconf('service.component_encodingaeskey'), ]; } /** * 获取缓存目录. */ private function getCachePath(): string { return syspath('safefile/cache'); } }