store_id = get_store_id(); $store = Store::findOne($this->store_id); // 获取微信配置 if (is_isv() || \Yii::$app->prod_is_shangmengduli()) { if (in_array($store->business_model, [2, 3, 4])) { $appid = Option::get('platform_appid', 0, 'saas', '')['value']; $secret = Option::get('platform_key', 0, 'saas', '')['value']; $wechat_config = (object)[ 'app_id' => $appid, 'app_secret' => $secret, ]; $config = [ 'app_id' => $wechat_config->app_id, 'secret' => $wechat_config->app_secret, ]; // 获取接口调用凭证 try { $this->access_token = Factory::miniProgram($config)->access_token->getToken()['access_token']; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } else { $mini_id = get_mini_id(); if ($mini_id <= 0) { $wechat_config = WechatConfig::findOne(['store_id' => $this->store_id, 'is_delete' => 0, 'type' => WechatConfig::TYPE_CONFIG_MINI]); if (!$wechat_config) { return [ 'code' => 1, 'msg' => '微信配置错误' ]; } $appid = $wechat_config->app_id; $mini = StoreMini::findOne(['appid' => $appid]); if(!$mini){ return [ 'code' => 1, 'msg' => 'mini_id不存在' ]; } $mini_id = $mini->id; } $mini = StoreMini::findOne($mini_id); if (!$mini) { return [ 'code' => 1, 'msg' => '小程序信息不存在' ]; } try { $config = [ 'app_id' => Option::get("platform_third_appid",0,'saas')['value'], 'secret' => Option::get("platform_third_secret",0,'saas')['value'], 'token' => Option::get("platform_token",0,'saas')['value'], 'aes_key' => Option::get("platform_encodingAesKey",0,'saas')['value'] ]; $openPlatform = Factory::openPlatform($config); $app = $openPlatform->miniProgram($mini->appid, $mini->authorizer_refresh_token); $this->access_token = $app->access_token->getToken()['authorizer_access_token']; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } } else { if (in_array($store->business_model, [2, 3, 4])) { $appid = Option::get('platform_appid', 0, 'saas', '')['value']; $secret = Option::get('platform_key', 0, 'saas', '')['value']; $wechat_config = (object)[ 'app_id' => $appid, 'app_secret' => $secret, ]; } else { $wechat_config = WechatConfig::findOne(['store_id' => $this->store_id, 'is_delete' => 0, 'type' => WechatConfig::TYPE_CONFIG_MINI]); } if (!$wechat_config) { return [ 'code' => 1, 'msg' => '微信配置错误' ]; } $config = [ 'app_id' => $wechat_config->app_id, 'secret' => $wechat_config->app_secret, ]; // 获取接口调用凭证 try { $this->access_token = Factory::miniProgram($config)->access_token->getToken()['access_token']; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } } /** * 调用入口 * @param string $uri 接口地址 * @param array $params 接口参数 * @param array $options 其他参数 * @return string */ protected function request(string $uri, array $params = [], array $options = []) { // 初始化 $this->init(); $res = $this->performRequest($uri . '?access_token=' . $this->access_token, array_merge(['json' => $params], $options)); return $res; } /** * http请求 * @param string $uri * @param array $options * @return string * @throws \GuzzleHttp\Exception\GuzzleException */ protected function performRequest(string $uri, array $options) { $options = $this->fixJsonIssue($options); /** * @var $data \Psr\Http\Message\ResponseInterface */ $data = http_post($uri, $options); return $data->getBody()->getContents(); } /** * @param array $options * * @return array */ protected function fixJsonIssue(array $options) { if (isset($options['json']) && is_array($options['json'])) { $options['headers'] = array_merge($options['headers'] ?? [], ['Content-Type' => 'application/json']); if (empty($options['json'])) { $options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_FORCE_OBJECT); } else { $options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_UNESCAPED_UNICODE); } unset($options['json']); } return $options; } }