Shipping.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\service\order;
  13. use app\common\library\helper;
  14. use app\common\model\Express as ExpressModel;
  15. use app\common\model\UserOauth as UserOauthModel;
  16. use app\common\model\wxapp\Setting as WxappSettingModel;
  17. use app\common\enum\Client as ClientEnum;
  18. use app\common\enum\payment\Method as PaymentMethodEnum;
  19. use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum;
  20. use app\common\library\wechat\Shipping as WechatShippingApi;
  21. use app\common\service\BaseService;
  22. use cores\exception\BaseException;
  23. /**
  24. * 微信小程序-发货信息管理
  25. * Class Shipping
  26. * @package app\common\service\order
  27. */
  28. class Shipping extends BaseService
  29. {
  30. // 物流配送
  31. const DELIVERY_EXPRESS = 1;
  32. // 虚拟商品
  33. const DELIVERY_VIRTUAL = 3;
  34. /**
  35. * 发货信息同步微信平台
  36. * @param mixed $completed 订单详情
  37. * @param array $param 发货信息参数
  38. * @return bool
  39. * @throws BaseException
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function syncMpWeixinShipping($completed, array $param): bool
  45. {
  46. // 构建发货信息参数
  47. $param = $this->buildParam($completed, $param);
  48. // 仅微信小程序端并且使用微信支付的订单才可以同步
  49. if (
  50. !$param['syncMpWeixinShipping']
  51. || $completed['pay_method'] !== PaymentMethodEnum::WECHAT
  52. || $completed['platform'] !== ClientEnum::MP_WEIXIN
  53. ) {
  54. return false;
  55. }
  56. // 订单全部发货时再同步, 分包发货时不同步
  57. if ($completed['delivery_status'] !== DeliveryStatusEnum::DELIVERED) {
  58. return false;
  59. }
  60. if (empty($completed['trade'])) {
  61. throwError('很抱歉,该订单不存在微信支付交易记录');
  62. }
  63. // 请求微信API接口
  64. return $this->request($this->buildApiParam($completed, $param), $completed['store_id']);
  65. }
  66. /**
  67. * 请求微信API接口
  68. * @param array $apiParam
  69. * @param int $storeId
  70. * @return true
  71. * @throws BaseException
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. private function request(array $apiParam, int $storeId): bool
  77. {
  78. // 小程序配置信息
  79. $wxConfig = WxappSettingModel::getConfigBasic($storeId);
  80. // 请求API数据
  81. $WechatShippingApi = new WechatShippingApi($wxConfig['app_id'], $wxConfig['app_secret']);
  82. // 处理返回结果
  83. $response = $WechatShippingApi->uploadShippingInfo($apiParam);
  84. empty($response) && throwError('微信API请求失败:' . $WechatShippingApi->getError());
  85. return true;
  86. }
  87. /**
  88. * 构建发货信息参数
  89. * @param $completed
  90. * @param array $param
  91. * @return array
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. private function buildParam($completed, array $param): array
  97. {
  98. // 设置默认的参数
  99. $param = helper::setQueryDefaultValue($param, [
  100. // 同步至微信小程序《发货信息管理》
  101. 'syncMpWeixinShipping' => 1,
  102. // 物流模式:1物流配送 3虚拟商品 4用户自提
  103. 'logisticsType' => self::DELIVERY_EXPRESS,
  104. // 物流公司ID
  105. 'expressId' => '',
  106. // 物流单号
  107. 'expressNo' => '',
  108. ]);
  109. // 是否开启发货信息管理
  110. !WxappSettingModel::isEnableShipping($completed['store_id']) && $param['syncMpWeixinShipping'] = 0;
  111. return $param;
  112. }
  113. /**
  114. * 构建API参数
  115. * @param $completed
  116. * @param array $param
  117. * @return array
  118. * @throws BaseException
  119. */
  120. private function buildApiParam($completed, array $param): array
  121. {
  122. // 获取物流公司编码
  123. $param['expressId'] > 0 && $expressCode = $this->getExpressCode($param['expressId']);
  124. return [
  125. 'order_key' => [
  126. // 订单单号类型:1使用下单商户号和商户侧单号 2使用微信支付单号
  127. 'order_number_type' => 2,
  128. // 支付交易对应的微信订单号
  129. 'transaction_id' => $completed['trade']['trade_no'],
  130. ],
  131. // 物流模式:1物流配送 3虚拟商品 4用户自提
  132. 'logistics_type' => $param['logisticsType'],
  133. // 发货模式:1、UNIFIED_DELIVERY(统一发货)2、SPLIT_DELIVERY(分拆发货)
  134. 'delivery_mode' => 1,
  135. // 用于标识分拆发货模式下是否已全部发货完成 示例值: true/false
  136. 'is_all_delivered' => true,
  137. 'shipping_list' => [
  138. [
  139. 'express_company' => $expressCode ?? '', // 物流公司编码
  140. 'tracking_no' => $param['expressNo'], // 物流单号
  141. 'item_desc' => $completed['goods'][0]['goods_name'], // 商品信息
  142. ]
  143. ],
  144. // 上传时间 (RFC3339格式)
  145. 'upload_time' => \date(DATE_RFC3339),
  146. // 微信用户openid
  147. 'payer' => ['openid' => $this->getUserOpenId($completed['user_id'])],
  148. ];
  149. }
  150. /**
  151. * 获取物流公司编码 [这里用快递鸟编码格式]
  152. * @param int $expressId
  153. * @return mixed
  154. * @throws BaseException
  155. */
  156. private function getExpressCode(int $expressId)
  157. {
  158. $detail = ExpressModel::detail($expressId);
  159. empty($detail) && throwError('很抱歉,未找到指定的物流公司');
  160. return $detail['kdniao_code'];
  161. }
  162. /**
  163. * 获取微信小程序用户openid
  164. * @param int $userId
  165. * @return mixed
  166. * @throws BaseException
  167. */
  168. private function getUserOpenId(int $userId)
  169. {
  170. $openid = UserOauthModel::getOauthIdByUserId($userId, ClientEnum::MP_WEIXIN);
  171. empty($openid) && throwError('很抱歉,未找到当前用户的openid');
  172. return $openid;
  173. }
  174. }