['except' => ['index', 'ceshi2'] ] ]; public function ceshi2() { $config = [ 'app_id' => 'wx0b3defb62f0f910b', 'secret' => 'c02aa7ad9e4a5c423862e068b6cb4ad4', 'token' => 'e5511202e4104e0aa9963e244b3e3785', 'aes_key' => '', // 明文模式请勿填写 EncodingAESKey /** * OAuth 配置 * * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login * callback:OAuth授权完成后的回调页地址 */ 'oauth' => [ 'scopes' => ['snsapi_userinfo'], 'callback' => '/examples/oauth_callback.php', ], /** * 接口请求相关配置,超时时间等,具体可用参数请参考: * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php */ 'http' => [ 'timeout' => 5.0, // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri 'retry' => true, // 使用默认重试配置 // 'retry' => [ // // 仅以下状态码重试 // 'http_codes' => [429, 500] // // 最大重试次数 // 'max_retries' => 3, // // 请求间隔 (毫秒) // 'delay' => 1000, // // 如果设置,每次重试的等待时间都会增加这个系数 // // (例如. 首次:1000ms; 第二次: 3 * 1000ms; etc.) // 'multiplier' => 3 // ], ], ]; $app = new wxApplication($config); $utils = $app->getUtils(); $config = $utils->buildJsSdkConfig( 'https://h5.lihaink.cn', ['updateAppMessageShareData', 'updateTimelineShareData'], [], false ); return $this->apiSuccess('ok', $config); } public function index($code) { $config = [ 'app_id' => 'wxfb4695444ab8d0d0', 'secret' => '9d73b21c39586ccb55c716546a0b999e', 'token' => 'easywechat', /** * 接口请求相关配置,超时时间等,具体可用参数请参考: * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php */ 'http' => [ 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启 'timeout' => 5.0, // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri 'retry' => true, // 使用默认重试配置 // 'retry' => [ // // 仅以下状态码重试 // 'http_codes' => [429, 500] // // 最大重试次数 // 'max_retries' => 3, // // 请求间隔 (毫秒) // 'delay' => 1000, // // 如果设置,每次重试的等待时间都会增加这个系数 // // (例如. 首次:1000ms; 第二次: 3 * 1000ms; etc.) // 'multiplier' => 3 // ], ], ]; $app = new Application($config); $utils = $app->getUtils(); $response = $utils->codeToSession($code); if ($response['openid']) { $user = Db::table('fa_user')->where('openid', $response['openid'])->field('id,nickname,group_id,avatar,group_id')->find(); if ($user) { $find = Db::table('fa_szxc_information_usermsg')->where('user_id', $user['id'])->find(); if ($find) { $user['no_update'] = 0; } else { $user['no_update'] = 1; } //创建新Token $token = self::getToken($user['id']); $user['token'] = $token; $user['expires_in'] = 2222222; return $this->apiSuccess('ok', ['user_info' => $user]); } else { $time = time(); $data = [ 'openid' => $response['openid'], 'group_id' => 1, 'username' => 'wx' . $time, 'nickname' => '微信用户' . $time, 'avatar' => 'https://lihai001.oss-cn-chengdu.aliyuncs.com/uploads/20230111/58c84995456bc63b2660d9526f7062fc.png', 'level' => 1, 'createtime' => $time, 'updatetime' => $time, 'status' => 'normal' ]; $id = Db::table('fa_user')->insertGetId($data); // 写入商城关系表 $nk_user['n_user_id'] = $id; $is_nk_user_id = Db::connect('shop')->name('nk_user')->where($nk_user)->find(); if (!$is_nk_user_id) { Db::connect('shop')->name('nk_user')->insert($nk_user); } //创建新Token $token = self::getToken($user['id']); $userinfo = ['user_id' => $id, 'no_update' => 1, 'token' => $token, 'expires_in' => 2222222, 'nickname' => $data['nickname'], 'avatar' => $data['avatar'], 'group_id' => 1]; return $this->apiSuccess('ok', ['user_info' => $userinfo]); } } halt($response); } /** * @param $user_id * @return string */ public function getToken($user_id){ $time = time(); //当前时间 $conf = $this->jwt_conf; $token = [ 'iss' => $conf['iss'], //签发者 可选 'aud' => $conf['aud'], //接收该JWT的一方,可选 'iat' => $time, //签发时间 'nbf' => $time-1 , //(Not Before):某个时间点后才能访问,比如设置time+30,表示当前时间30秒后才能使用 'exp' => $time+$conf['exptime'], //过期时间,这里设置2个小时 'data' => [ //自定义信息,不要定义敏感信息 'userid' =>$user_id, ] ]; return JWT::encode($token, $conf['secrect'], 'HS256'); //输出Token 默认'HS256' } public function getopenid() { $code = get_params('code'); if (empty($code)) { $this->apiError('缺少参数'); } $config = [ 'app_id' => 'wx6e14cb98394e36bc', 'secret' => 'd8b7c06bbdb29309fed11fe62fe6b022', 'token' => 'easywechat', /** * 接口请求相关配置,超时时间等,具体可用参数请参考: * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php */ 'http' => [ 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启 'timeout' => 5.0, // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri 'retry' => true, // 使用默认重试配置 // 'retry' => [ // // 仅以下状态码重试 // 'http_codes' => [429, 500] // // 最大重试次数 // 'max_retries' => 3, // // 请求间隔 (毫秒) // 'delay' => 1000, // // 如果设置,每次重试的等待时间都会增加这个系数 // // (例如. 首次:1000ms; 第二次: 3 * 1000ms; etc.) // 'multiplier' => 3 // ], ], ]; $app = new Application($config); $utils = $app->getUtils(); $response = $utils->codeToSession($code); if ($response['openid']) { $where['user_id'] = JWT_UID; $user = Db::table('fa_user_openid')->where($where)->find(); if ($user) { if (!empty($user['openid']) && $user['openid'] == $response['openid']) { $this->apiError('您已授权'); } else { $data['openid'] = $response['openid']; Db::table('fa_user_openid')->where($where)->update($data); } } else { $time = time(); $data = [ 'openid' => $response['openid'], 'user_id' => JWT_UID, 'createtime' => $time, ]; $res = Db::table('fa_user_openid')->insert($data); if ($res) { $this->apiSuccess('授权成功'); } else { $this->apiError('授权失败'); } } } else { $this->apiError('操作失败'); } } public function getoffopenid() { $code = get_params('code'); if (empty($code)) { $this->apiError('缺少参数'); } $config = [ 'app_id' => 'wx0b3defb62f0f910b', 'secret' => 'c02aa7ad9e4a5c423862e068b6cb4ad4', 'token' => 'easywechat', /** * 接口请求相关配置,超时时间等,具体可用参数请参考: * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php */ 'http' => [ 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启 'timeout' => 5.0, // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri 'retry' => true, // 使用默认重试配置 // 'retry' => [ // // 仅以下状态码重试 // 'http_codes' => [429, 500] // // 最大重试次数 // 'max_retries' => 3, // // 请求间隔 (毫秒) // 'delay' => 1000, // // 如果设置,每次重试的等待时间都会增加这个系数 // // (例如. 首次:1000ms; 第二次: 3 * 1000ms; etc.) // 'multiplier' => 3 // ], ], ]; $app = new Application($config); $utils = $app->getUtils(); $response = $utils->codeToSession($code); if ($response['openid']) { $where['user_id'] = JWT_UID; $user = Db::table('fa_user_openid')->where($where)->find(); if ($user) { if (!empty($user['official_account_openid']) && $user['official_account_openid'] == $response['openid']) { $this->apiError('您已授权'); } else { $data['official_account_openid'] = $response['openid']; Db::table('fa_user_openid')->where($where)->update($data); } } else { $time = time(); $data = [ 'official_account_openid' => $response['openid'], 'user_id' => JWT_UID, 'createtime' => $time, ]; $res = Db::table('fa_user_openid')->insert($data); if ($res) { $this->apiSuccess('授权成功'); } else { $this->apiError('授权失败'); } } } else { $this->apiError('操作失败'); } } }