Shipping.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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));
  65. }
  66. /**
  67. * 请求微信API接口
  68. * @param array $apiParam
  69. * @return true
  70. * @throws BaseException
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. private function request(array $apiParam): bool
  76. {
  77. // 小程序配置信息
  78. $wxConfig = WxappSettingModel::getConfigBasic();
  79. // 请求API数据
  80. $WechatShippingApi = new WechatShippingApi($wxConfig['app_id'], $wxConfig['app_secret']);
  81. // 处理返回结果
  82. $response = $WechatShippingApi->uploadShippingInfo($apiParam);
  83. empty($response) && throwError('微信API请求失败:' . $WechatShippingApi->getError());
  84. return true;
  85. }
  86. /**
  87. * 构建发货信息参数
  88. * @param $completed
  89. * @param array $param
  90. * @return array
  91. */
  92. private function buildParam($completed, array $param): array
  93. {
  94. // 设置默认的参数
  95. $param = helper::setQueryDefaultValue($param, [
  96. // 同步至微信小程序《发货信息录入》
  97. 'syncMpWeixinShipping' => 1,
  98. // 物流模式:1物流配送 3虚拟商品 4用户自提
  99. 'logisticsType' => self::DELIVERY_EXPRESS,
  100. // 物流公司ID
  101. 'expressId' => '',
  102. // 物流单号
  103. 'expressNo' => '',
  104. ]);
  105. return $param;
  106. }
  107. /**
  108. * 构建API参数
  109. * @param $completed
  110. * @param array $param
  111. * @return array
  112. * @throws BaseException
  113. */
  114. private function buildApiParam($completed, array $param): array
  115. {
  116. // 获取物流公司编码
  117. $param['expressId'] > 0 && $expressCode = $this->getExpressCode($param['expressId']);
  118. return [
  119. 'order_key' => [
  120. // 订单单号类型:1使用下单商户号和商户侧单号 2使用微信支付单号
  121. 'order_number_type' => 2,
  122. // 支付交易对应的微信订单号
  123. 'transaction_id' => $completed['trade']['trade_no'],
  124. ],
  125. // 物流模式:1物流配送 3虚拟商品 4用户自提
  126. 'logistics_type' => $param['logisticsType'],
  127. // 发货模式:1、UNIFIED_DELIVERY(统一发货)2、SPLIT_DELIVERY(分拆发货)
  128. 'delivery_mode' => 1,
  129. // 用于标识分拆发货模式下是否已全部发货完成 示例值: true/false
  130. 'is_all_delivered' => true,
  131. 'shipping_list' => [
  132. [
  133. 'express_company' => $expressCode ?? '', // 物流公司编码
  134. 'tracking_no' => $param['expressNo'], // 物流单号
  135. 'item_desc' => $completed['goods'][0]['goods_name'], // 商品信息
  136. ]
  137. ],
  138. // 上传时间 (RFC3339格式)
  139. 'upload_time' => \date(DATE_RFC3339),
  140. // 微信用户openid
  141. 'payer' => ['openid' => $this->getUserOpenId($completed['user_id'])],
  142. ];
  143. }
  144. /**
  145. * 获取物流公司编码 [这里用快递鸟编码格式]
  146. * @param int $expressId
  147. * @return mixed
  148. * @throws BaseException
  149. */
  150. private function getExpressCode(int $expressId)
  151. {
  152. $detail = ExpressModel::detail($expressId);
  153. empty($detail) && throwError('很抱歉,未找到指定的物流公司');
  154. return $detail['kdniao_code'];
  155. }
  156. /**
  157. * 获取微信小程序用户openid
  158. * @param int $userId
  159. * @return mixed
  160. * @throws BaseException
  161. */
  162. private function getUserOpenId(int $userId)
  163. {
  164. $openid = UserOauthModel::getOauthIdByUserId($userId, ClientEnum::MP_WEIXIN);
  165. empty($openid) && throwError('很抱歉,未找到当前用户的openid');
  166. return $openid;
  167. }
  168. }