截流自动化的商城平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

WechatMiniExpressSendSyncServer.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace app\common\server;
  3. use app\common\enum\OrderEnum;
  4. use app\common\model\Client_;
  5. use app\common\model\Delivery;
  6. use app\common\model\Express;
  7. use app\common\model\order\Order;
  8. use app\common\model\order\OrderGoods;
  9. use app\common\model\order\OrderTrade;
  10. use app\common\model\RechargeOrder;
  11. use app\common\model\user\UserAuth;
  12. use EasyWeChat\Factory;
  13. use Psr\SimpleCache\InvalidArgumentException;
  14. use think\facade\Log;
  15. use think\helper\Str;
  16. /**
  17. * @notes 小程序 发货信息录入
  18. * @notes https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html
  19. * author lbzy
  20. * @datetime 2023-07-14 14:51:23
  21. * @class WechatMiniExpressSendSyncService
  22. * @package app\common\service
  23. */
  24. class WechatMiniExpressSendSyncServer
  25. {
  26. static private $miniApp;
  27. private static function getMiniApp()
  28. {
  29. static::$miniApp = static::$miniApp ? : Factory::miniProgram(WeChatServer::getMnpConfig());
  30. return static::$miniApp;
  31. }
  32. /**
  33. * @notes 订单录入
  34. * @param array $order
  35. * @return bool
  36. * @author lbzy
  37. * @datetime 2023-07-14 14:34:05
  38. */
  39. static function _sync_order(array $order) : bool
  40. {
  41. try {
  42. $time = time();
  43. $log = 'wechat_mini_express_sync_order';
  44. $app = static::getMiniApp();
  45. $user = UserAuth::where('user_id', $order['user_id'])->where('client', Client_::mnp)->findOrEmpty();
  46. if (empty($user->openid)) {
  47. Log::write("用户:{$order['user_id']} openid不存在,订单ID:{$order['id']}", $log);
  48. Order::update([ 'wechat_mini_express_sync' => 2, 'wechat_mini_express_sync_time' => time() ], [
  49. [ 'id', '=', $order['id'] ],
  50. ]);
  51. return false;
  52. }
  53. $order_goods = OrderGoods::where('order_id', $order['id'])->select()->toArray();
  54. $goods_names = implode(" ", array_column($order_goods, 'goods_name'));
  55. $shipping_item = [
  56. 'item_desc' => mb_strlen($goods_names) > 128 ? (mb_substr($goods_names, 0, 124, 'UTF-8') . '...') : $goods_names,
  57. ];
  58. $data = [
  59. 'order_key' => [
  60. 'order_number_type' => 2,
  61. 'transaction_id' => $order['transaction_id'] ? : OrderTrade::where('id', $order['trade_id'])->value('transaction_id', ''),
  62. ],
  63. 'delivery_mode' => 1,
  64. 'upload_time' => date(DATE_RFC3339, $time),
  65. 'payer' => [
  66. 'openid' => $user->openid,
  67. ],
  68. ];
  69. $delivery = Delivery::where('order_id', $order['id'])->findOrEmpty();
  70. $express = Express::where('id', $delivery['shipping_id'] ?? 0)->findOrEmpty();
  71. if (! empty($express->code) && ! empty($delivery->invoice_no) && ! empty($delivery->mobile)) {
  72. $shipping_item['tracking_no'] = $delivery->invoice_no;
  73. // 微信小程序 物流公司delivery_id
  74. $shipping_item['express_company'] = $express->code;
  75. $shipping_item['contact']['receiver_contact'] = substr_replace($delivery->mobile, '****', 3, 4);
  76. } else {
  77. // 无需物流 改为门店自提
  78. $data['logistics_type'] = 4;
  79. }
  80. $data['shipping_list'][] = $shipping_item;
  81. switch ($order['delivery_type']) {
  82. // 快递发货
  83. case OrderEnum::DELIVERY_TYPE_EXPRESS:
  84. $data['logistics_type'] = $data['logistics_type'] ?? 1;
  85. break;
  86. // 门店自提
  87. case OrderEnum::DELIVERY_TYPE_SELF:
  88. $data['logistics_type'] = 4;
  89. break;
  90. // 虚拟发货
  91. case OrderEnum::DELIVERY_TYPE_VIRTUAL:
  92. $data['logistics_type'] = 3;
  93. break;
  94. default:
  95. break;
  96. }
  97. // dump($data);
  98. $token = static::getToken($app);
  99. $result_content = $app->http_client->post("/wxa/sec/order/upload_shipping_info?access_token={$token}", [
  100. 'body' => json_encode($data, JSON_UNESCAPED_UNICODE),
  101. ])->getBody()->getContents();
  102. $result = json_decode($result_content, true);
  103. // dump($result);
  104. if (! isset($result['errcode']) || $result['errcode'] != 0) {
  105. // token失效 不标记失败 等下次执行
  106. if (isset($result['errcode']) && $result['errcode'] == 40001) {
  107. Log::write("等待下次执行" . ($result_content ? : "发货录入发生错误"), $log);
  108. return false;
  109. }
  110. Log::write($result_content ? : "发货录入发生错误", $log);
  111. Order::update([ 'wechat_mini_express_sync' => 2, 'wechat_mini_express_sync_time' => $time ], [
  112. [ 'id', '=', $order['id'] ],
  113. ]);
  114. return false;
  115. }
  116. Order::update([ 'wechat_mini_express_sync' => 1, 'wechat_mini_express_sync_time' => $time ], [
  117. [ 'id', '=', $order['id'] ],
  118. ]);
  119. return true;
  120. } catch(\Throwable $e) {
  121. // dump($e->__toString());
  122. Log::write($e->__toString(), $log);
  123. return false;
  124. }
  125. }
  126. /**
  127. * @notes 充值录入
  128. * @param array $recharge
  129. * @return bool
  130. * @throws InvalidArgumentException
  131. * @author lbzy
  132. * @datetime 2023-07-17 15:18:18
  133. */
  134. static function _sync_recharge(array $recharge) : bool
  135. {
  136. try {
  137. $time = time();
  138. $log = 'wechat_mini_express_sync_recharge';
  139. $app = static::getMiniApp();
  140. $user = UserAuth::where('user_id', $recharge['user_id'])->where('client', Client_::mnp)->findOrEmpty();
  141. if (empty($user->openid)) {
  142. Log::write("用户:{$recharge['user_id']} openid不存在,订单ID:{$recharge['id']}", $log);
  143. RechargeOrder::update([ 'wechat_mini_express_sync' => 2, 'wechat_mini_express_sync_time' => time() ], [
  144. [ 'id', '=', $recharge['id'] ],
  145. ]);
  146. return false;
  147. }
  148. $shipping_item = [
  149. 'item_desc' => '余额充值',
  150. ];
  151. $data = [
  152. 'order_key' => [
  153. 'order_number_type' => 2,
  154. 'transaction_id' => $recharge['transaction_id'],
  155. ],
  156. 'delivery_mode' => 1,
  157. 'upload_time' => date(DATE_RFC3339, $time),
  158. 'payer' => [
  159. 'openid' => $user->openid,
  160. ],
  161. 'logistics_type' => 3,
  162. ];
  163. $data['shipping_list'][] = $shipping_item;
  164. // dump($data);
  165. $token = static::getToken($app);
  166. $result_content = $app->http_client->post("/wxa/sec/order/upload_shipping_info?access_token={$token}", [
  167. 'body' => json_encode($data, JSON_UNESCAPED_UNICODE),
  168. ])->getBody()->getContents();
  169. $result = json_decode($result_content, true);
  170. // dump($result);
  171. if (! isset($result['errcode']) || $result['errcode'] != 0) {
  172. // token失效 不标记失败 等下次执行
  173. if (isset($result['errcode']) && $result['errcode'] == 40001) {
  174. Log::write("等待下次执行" . ($result_content ? : "发货录入发生错误"), $log);
  175. return false;
  176. }
  177. Log::write($result_content ? : "发货录入发生错误", $log);
  178. RechargeOrder::update([ 'wechat_mini_express_sync' => 2, 'wechat_mini_express_sync_time' => $time ], [
  179. [ 'id', '=', $recharge['id'] ],
  180. ]);
  181. return false;
  182. }
  183. RechargeOrder::update([ 'wechat_mini_express_sync' => 1, 'wechat_mini_express_sync_time' => $time ], [
  184. [ 'id', '=', $recharge['id'] ],
  185. ]);
  186. return true;
  187. } catch(\Throwable $e) {
  188. // dump($e->__toString());
  189. Log::write($e->__toString(), $log);
  190. return false;
  191. }
  192. }
  193. private static function getToken($app)
  194. {
  195. return $app->access_token->getToken()['access_token'];
  196. }
  197. static function wechatSyncCheck($order)
  198. {
  199. $app = static::getMiniApp();
  200. $data = [
  201. 'transaction_id' => $order['transaction_id'] ?? '',
  202. ];
  203. $token = static::getToken($app);
  204. $result_content = $app->http_client->post("/wxa/sec/order/get_order?access_token={$token}", [
  205. 'body' => json_encode($data, JSON_UNESCAPED_UNICODE),
  206. ])->getBody()->getContents();
  207. $result = json_decode($result_content, true);
  208. if (! isset($result['errcode']) || $result['errcode'] != 0) {
  209. // token失效 不标记失败 等下次执行
  210. if (isset($result['errcode']) && $result['errcode'] == 40001) {
  211. Log::write("等待下次执行" . ($result_content ? : "发货录入发生错误"), 'wechat_mini_express_sync_check');
  212. return false;
  213. }
  214. Log::write($result_content ? : "发货录入发生错误", 'wechat_mini_express_sync_check');
  215. return [];
  216. }
  217. return $result;
  218. }
  219. }