| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils\Delivery;
- use app\models\Store;
- use app\models\WechatConfig;
- use EasyWeChat\Factory;
- use app\models\Option;
- use app\models\StoreMini;
- /**
- * 微信即时配送基础服务类
- * Class DeliveryService
- * @package app\utils\Delivery
- */
- class DeliveryService
- {
- /**
- * @var integer $store_id 店铺id
- */
- protected $store_id;
- /**
- * @var string $access_token 请求access_token
- */
- protected $access_token;
- /**
- * @return array
- */
- protected function init() {
- $this->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;
- }
- }
|