WechatMiniProgramShop.php 9.0 KB

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