WechatMiniProgramShop.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace app\common\library\easywechatPlus;
  3. use app\common\Service\Order\ShippingInfo\OrderShippingInfo;
  4. use app\commom\model\data\WechatExpress;
  5. use app\common\exception\BusinessException;
  6. use app\common\facade\HttpClient;
  7. use think\Cache;
  8. /**
  9. * 补充 小程序购物订单
  10. */
  11. class WechatMiniProgramShop extends EasywechatPlus
  12. {
  13. /**
  14. * 上送订单信息
  15. *
  16. * @param object|array $order
  17. * @return void
  18. */
  19. public function uploadShippingInfos($order, $express = null, $type = 'send')
  20. {
  21. try {
  22. $orderShippingInfo = new OrderShippingInfo($order);
  23. if ($type == 'change') {
  24. $uploadParams = $orderShippingInfo->getChangeShippingParams($express);
  25. } else {
  26. $uploadParams = $orderShippingInfo->getShippingParams();
  27. }
  28. // 设置消息跳转地址
  29. $this->setMessageJumpPath();
  30. $length = count($uploadParams);
  31. foreach ($uploadParams as $key => $params) {
  32. $params['delivery_mode'] = 1;
  33. $params['is_all_delivered'] = true;
  34. if ($params['logistics_type'] == 1 && count($params['shipping_list']) > 1) {
  35. // 快递物流,并且
  36. $params['delivery_mode'] = 2;
  37. }
  38. if ($length > 1) {
  39. if ($key == ($length - 1)) {
  40. // 最后一条
  41. $params['is_all_delivered'] = true; // 发货完成
  42. } else {
  43. $params['is_all_delivered'] = false; // 发货未完成
  44. }
  45. }
  46. $params['upload_time'] = date(DATE_RFC3339);
  47. \think\Log::info('发货信息录入参数' . json_encode($params));
  48. $result = $this->uploadShippingInfo($params);
  49. if ($result['errcode'] != 0) {
  50. throw new BusinessException('获取失败: errcode:' . $result['errcode'] . '; errmsg:' . $result['errmsg']);
  51. }
  52. }
  53. } catch (\Exception $e) {
  54. format_log_error($e, 'upload_shipping_info', '发货信息录入错误');
  55. }
  56. }
  57. /**
  58. * 将发货信息提交给微信
  59. *
  60. * @param array $params 上送参数
  61. * @return void
  62. */
  63. private function uploadShippingInfo($params)
  64. {
  65. $access_token = $this->getAccessToken();
  66. $add_template_url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info";
  67. $result = HttpClient::request('post', $add_template_url, [
  68. 'body' => json_encode($params, JSON_UNESCAPED_UNICODE),
  69. 'query' => ["access_token" => $access_token['access_token']],
  70. 'headers' => ['Content-Type' => 'application/json']
  71. ]);
  72. $result = $result->getBody()->getContents();
  73. return json_decode($result, true);
  74. }
  75. /**
  76. * 检测并且设置微信消息跳转地址
  77. *
  78. * @param boolean $exception
  79. * @param boolean $is_force
  80. * @return boolean
  81. */
  82. public function checkAndSetMessageJumpPath($exception = false, $is_force = false)
  83. {
  84. try {
  85. // 查询是否又微信发货管理权限
  86. if ($this->isTradeManaged($is_force)) {
  87. // 有权限,设置消息跳转地址
  88. $this->setMessageJumpPath($is_force);
  89. }
  90. return true;
  91. } catch (\Exception $e) {
  92. format_log_error($e, 'checkAndSetMessageJumpPath', '自动设置微信小程序发货信息管理消息跳转路径失败');
  93. if ($exception) {
  94. // 抛出异常
  95. throw new BusinessException($e->getMessage());
  96. }
  97. }
  98. return false;
  99. }
  100. /**
  101. * 查询是否有微信发货信息管理权限 (48001 时当没有权限处理,不抛出异常)
  102. *
  103. * @param boolean $is_force
  104. * @return boolean
  105. */
  106. public function isTradeManaged($is_force = false)
  107. {
  108. $key = 'wechat:is_trade_managed';
  109. if (!$is_force && Cache::get($key)) {
  110. return Cache::get($key); // 直接返回是否有权限
  111. }
  112. $access_token = $this->getAccessToken();
  113. $add_template_url = "https://api.weixin.qq.com/wxa/sec/order/is_trade_managed";
  114. $mini_appid = shop_config('shop.platform.wechat_mini_program.app_id');
  115. if (!$mini_appid) {
  116. // 没有配置微信小程序参数
  117. throw new BusinessException('微信小程序发货管理查询失败,没有配置微信小程序');
  118. }
  119. $params = [
  120. 'appid' => $mini_appid
  121. ];
  122. $result = HttpClient::request('post', $add_template_url, [
  123. 'body' => json_encode($params, JSON_UNESCAPED_UNICODE),
  124. 'query' => ["access_token" => $access_token['access_token']],
  125. 'headers' => ['Content-Type' => 'application/json']
  126. ]);
  127. $result = $result->getBody()->getContents();
  128. $result = json_decode($result, true);
  129. if ($result['errcode'] != 0 && $result['errcode'] != '48001') {
  130. // 48001 时不抛出异常,当没有权限处理
  131. throw new BusinessException('查询是否有微信发货信息管理权限失败: errcode:' . $result['errcode'] . '; errmsg:' . $result['errmsg']);
  132. }
  133. $is_trade_managed = isset($result['is_trade_managed']) ? intval($result['is_trade_managed']) : 0;
  134. Cache::set($key, $is_trade_managed, 7200); // 缓存结果,两小时
  135. return $is_trade_managed;
  136. }
  137. /**
  138. * 设置微信消息跳转路径
  139. *
  140. * @param boolean $is_force
  141. * @return void
  142. */
  143. public function setMessageJumpPath($is_force = false, $path = 'pages/order/detail?comein_type=wechat')
  144. {
  145. if (!$is_force && cache('?wechat:set_message_jump_path')) {
  146. // 已经设置过了,无需再次设置
  147. return true;
  148. }
  149. $access_token = $this->getAccessToken();
  150. $add_template_url = "https://api.weixin.qq.com/wxa/sec/order/set_msg_jump_path";
  151. $params = [
  152. 'path' => $path
  153. ];
  154. $result = HttpClient::request('post', $add_template_url, [
  155. 'body' => json_encode($params, JSON_UNESCAPED_UNICODE),
  156. 'query' => ["access_token" => $access_token['access_token']],
  157. 'headers' => ['Content-Type' => 'application/json']
  158. ]);
  159. $result = $result->getBody()->getContents();
  160. $result = json_decode($result, true);
  161. if ($result['errcode'] != 0) {
  162. throw new BusinessException('设置微信发货消息跳转地址失败: errcode:' . $result['errcode'] . '; errmsg:' . $result['errmsg']);
  163. }
  164. if ($is_force) {
  165. // 充值订单发货时,清掉缓存
  166. cache('wechat:set_message_jump_path', null); // 清除缓存
  167. } else {
  168. cache('wechat:set_message_jump_path', time()); // 永久有效
  169. }
  170. return $result;
  171. }
  172. /**
  173. * 获取微信delivery数据
  174. *
  175. * @param boolean $is_force
  176. * @return void
  177. */
  178. public function getDelivery($is_force = false)
  179. {
  180. if (!$is_force && cache('?wechat:get_delivery_list')) {
  181. // 已经设置过了,无需再次设置
  182. return true;
  183. }
  184. $access_token = $this->getAccessToken();
  185. $get_delivery_url = "https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/get_delivery_list";
  186. $result = HttpClient::request('post', $get_delivery_url, [
  187. 'body' => '{}',
  188. 'query' => ["access_token" => $access_token['access_token']],
  189. 'headers' => ['Content-Type' => 'application/json']
  190. ]);
  191. $result = $result->getBody()->getContents();
  192. $result = json_decode($result, true);
  193. if ($result['errcode'] != 0) {
  194. throw new BusinessException('获取微信 delivery 列表失败: errcode:' . $result['errcode'] . '; errmsg:' . $result['errmsg']);
  195. }
  196. // 存库
  197. $datas = $result['delivery_list'];
  198. foreach ($datas as $data) {
  199. $wechatExpress = WechatExpress::where('code', $data['delivery_id'])->find();
  200. $current = [
  201. 'name' => $data['delivery_name'] ?? '',
  202. 'code' => $data['delivery_id'] ?? '',
  203. ];
  204. if (!$wechatExpress) {
  205. $wechatExpress = new WechatExpress();
  206. }
  207. $wechatExpress->save($current);
  208. }
  209. cache('wechat:get_delivery_list', time()); // 永久有效
  210. return $result;
  211. }
  212. /**
  213. * 方法转发到 easywechat
  214. *
  215. * @param string $funcname
  216. * @param array $arguments
  217. * @return void
  218. */
  219. public function __call($funcname, $arguments)
  220. {
  221. // if ($funcname == 'deletePrivateTemplate') {
  222. // return $this->app->template_message->{$funcname}(...$arguments);
  223. // }
  224. return $this->app->{$funcname}(...$arguments);
  225. }
  226. }