DeliveryService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils\Delivery;
  8. use app\models\Store;
  9. use app\models\WechatConfig;
  10. use EasyWeChat\Factory;
  11. use app\models\Option;
  12. use app\models\StoreMini;
  13. /**
  14. * 微信即时配送基础服务类
  15. * Class DeliveryService
  16. * @package app\utils\Delivery
  17. */
  18. class DeliveryService
  19. {
  20. /**
  21. * @var integer $store_id 店铺id
  22. */
  23. protected $store_id;
  24. /**
  25. * @var string $access_token 请求access_token
  26. */
  27. protected $access_token;
  28. /**
  29. * @return array
  30. */
  31. protected function init() {
  32. $this->store_id = get_store_id();
  33. $store = Store::findOne($this->store_id);
  34. // 获取微信配置
  35. if (is_isv() || \Yii::$app->prod_is_shangmengduli()) {
  36. if (in_array($store->business_model, [2, 3, 4])) {
  37. $appid = Option::get('platform_appid', 0, 'saas', '')['value'];
  38. $secret = Option::get('platform_key', 0, 'saas', '')['value'];
  39. $wechat_config = (object)[
  40. 'app_id' => $appid,
  41. 'app_secret' => $secret,
  42. ];
  43. $config = [
  44. 'app_id' => $wechat_config->app_id,
  45. 'secret' => $wechat_config->app_secret,
  46. ];
  47. // 获取接口调用凭证
  48. try {
  49. $this->access_token = Factory::miniProgram($config)->access_token->getToken()['access_token'];
  50. } catch (\Exception $e) {
  51. return [
  52. 'code' => 1,
  53. 'msg' => $e->getMessage()
  54. ];
  55. }
  56. } else {
  57. $mini_id = get_mini_id();
  58. if ($mini_id <= 0) {
  59. $wechat_config = WechatConfig::findOne(['store_id' => $this->store_id, 'is_delete' => 0, 'type' => WechatConfig::TYPE_CONFIG_MINI]);
  60. if (!$wechat_config) {
  61. return [
  62. 'code' => 1,
  63. 'msg' => '微信配置错误'
  64. ];
  65. }
  66. $appid = $wechat_config->app_id;
  67. $mini = StoreMini::findOne(['appid' => $appid]);
  68. if(!$mini){
  69. return [
  70. 'code' => 1,
  71. 'msg' => 'mini_id不存在'
  72. ];
  73. }
  74. $mini_id = $mini->id;
  75. }
  76. $mini = StoreMini::findOne($mini_id);
  77. if (!$mini) {
  78. return [
  79. 'code' => 1,
  80. 'msg' => '小程序信息不存在'
  81. ];
  82. }
  83. try {
  84. $config = [
  85. 'app_id' => Option::get("platform_third_appid",0,'saas')['value'],
  86. 'secret' => Option::get("platform_third_secret",0,'saas')['value'],
  87. 'token' => Option::get("platform_token",0,'saas')['value'],
  88. 'aes_key' => Option::get("platform_encodingAesKey",0,'saas')['value']
  89. ];
  90. $openPlatform = Factory::openPlatform($config);
  91. $app = $openPlatform->miniProgram($mini->appid, $mini->authorizer_refresh_token);
  92. $this->access_token = $app->access_token->getToken()['authorizer_access_token'];
  93. } catch (\Exception $e) {
  94. return [
  95. 'code' => 1,
  96. 'msg' => $e->getMessage()
  97. ];
  98. }
  99. }
  100. } else {
  101. if (in_array($store->business_model, [2, 3, 4])) {
  102. $appid = Option::get('platform_appid', 0, 'saas', '')['value'];
  103. $secret = Option::get('platform_key', 0, 'saas', '')['value'];
  104. $wechat_config = (object)[
  105. 'app_id' => $appid,
  106. 'app_secret' => $secret,
  107. ];
  108. } else {
  109. $wechat_config = WechatConfig::findOne(['store_id' => $this->store_id, 'is_delete' => 0, 'type' => WechatConfig::TYPE_CONFIG_MINI]);
  110. }
  111. if (!$wechat_config) {
  112. return [
  113. 'code' => 1,
  114. 'msg' => '微信配置错误'
  115. ];
  116. }
  117. $config = [
  118. 'app_id' => $wechat_config->app_id,
  119. 'secret' => $wechat_config->app_secret,
  120. ];
  121. // 获取接口调用凭证
  122. try {
  123. $this->access_token = Factory::miniProgram($config)->access_token->getToken()['access_token'];
  124. } catch (\Exception $e) {
  125. return [
  126. 'code' => 1,
  127. 'msg' => $e->getMessage()
  128. ];
  129. }
  130. }
  131. }
  132. /**
  133. * 调用入口
  134. * @param string $uri 接口地址
  135. * @param array $params 接口参数
  136. * @param array $options 其他参数
  137. * @return string
  138. */
  139. protected function request(string $uri, array $params = [], array $options = []) {
  140. // 初始化
  141. $this->init();
  142. $res = $this->performRequest($uri . '?access_token=' . $this->access_token, array_merge(['json' => $params], $options));
  143. return $res;
  144. }
  145. /**
  146. * http请求
  147. * @param string $uri
  148. * @param array $options
  149. * @return string
  150. * @throws \GuzzleHttp\Exception\GuzzleException
  151. */
  152. protected function performRequest(string $uri, array $options) {
  153. $options = $this->fixJsonIssue($options);
  154. /**
  155. * @var $data \Psr\Http\Message\ResponseInterface
  156. */
  157. $data = http_post($uri, $options);
  158. return $data->getBody()->getContents();
  159. }
  160. /**
  161. * @param array $options
  162. *
  163. * @return array
  164. */
  165. protected function fixJsonIssue(array $options)
  166. {
  167. if (isset($options['json']) && is_array($options['json'])) {
  168. $options['headers'] = array_merge($options['headers'] ?? [], ['Content-Type' => 'application/json']);
  169. if (empty($options['json'])) {
  170. $options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_FORCE_OBJECT);
  171. } else {
  172. $options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_UNESCAPED_UNICODE);
  173. }
  174. unset($options['json']);
  175. }
  176. return $options;
  177. }
  178. }