Order.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. namespace app\api\model;
  3. use app\api\model\User as UserModel;
  4. use app\api\model\Goods as GoodsModel;
  5. use app\api\model\Setting as SettingModel;
  6. use app\api\model\UserCoupon as UserCouponModel;
  7. use app\api\service\order\PaySuccess;
  8. use app\api\service\Payment as PaymentService;
  9. use app\api\service\order\source\Factory as OrderSourceFactory;
  10. use app\common\model\Order as OrderModel;
  11. use app\common\enum\OrderType as OrderTypeEnum;
  12. use app\common\enum\DeliveryType as DeliveryTypeEnum;
  13. use app\common\enum\order\Status as OrderStatusEnum;
  14. use app\common\enum\order\PayType as PayTypeEnum;
  15. use app\common\enum\order\PayStatus as PayStatusEnum;
  16. use app\common\service\goods\source\Factory as FactoryStock;
  17. use app\common\service\order\Complete as OrderCompleteService;
  18. use app\common\exception\BaseException;
  19. use app\common\library\helper;
  20. /**
  21. * 订单模型
  22. * Class Order
  23. * @package app\api\model
  24. */
  25. class Order extends OrderModel
  26. {
  27. /**
  28. * 隐藏字段
  29. * @var array
  30. */
  31. protected $hidden = [
  32. 'wxapp_id',
  33. 'update_time'
  34. ];
  35. /**
  36. * 待支付订单详情
  37. * @param $orderNo
  38. * @return null|static
  39. * @throws \think\exception\DbException
  40. */
  41. public static function getPayDetail($orderNo)
  42. {
  43. return self::get(['order_no' => $orderNo, 'pay_status' => 10, 'is_delete' => 0], ['goods', 'user']);
  44. }
  45. /**
  46. * 订单支付事件
  47. * @param int $payType
  48. * @return bool
  49. * @throws \think\exception\DbException
  50. */
  51. public function onPay($payType = PayTypeEnum::WECHAT)
  52. {
  53. // 判断订单状态
  54. $orderSource = OrderSourceFactory::getFactory($this['order_source']);
  55. if (!$orderSource->checkOrderStatusOnPay($this)) {
  56. $this->error = $orderSource->getError();
  57. return false;
  58. }
  59. // 余额支付
  60. if ($payType == PayTypeEnum::BALANCE) {
  61. return $this->onPaymentByBalance($this['order_no']);
  62. }
  63. return true;
  64. }
  65. /**
  66. * 构建支付请求的参数
  67. * @param $user
  68. * @param $order
  69. * @param $payType
  70. * @return array
  71. * @throws BaseException
  72. * @throws \think\exception\DbException
  73. */
  74. public function onOrderPayment($user, $order, $payType)
  75. {
  76. if ($payType == PayTypeEnum::WECHAT) {
  77. return $this->onPaymentByWechat($user, $order);
  78. }
  79. return [];
  80. }
  81. /**
  82. * 构建微信支付请求
  83. * @param $user
  84. * @param $order
  85. * @return array
  86. * @throws BaseException
  87. * @throws \think\exception\DbException
  88. */
  89. protected function onPaymentByWechat($user, $order)
  90. {
  91. return PaymentService::wechat(
  92. $user,
  93. $order['order_id'],
  94. $order['order_no'],
  95. $order['pay_price'],
  96. OrderTypeEnum::MASTER
  97. );
  98. }
  99. /**
  100. * 立即购买:获取订单商品列表
  101. * @param $goodsId
  102. * @param $goodsSkuId
  103. * @param $goodsNum
  104. * @return array
  105. */
  106. public function getOrderGoodsListByNow($goodsId, $goodsSkuId, $goodsNum)
  107. {
  108. // 商品详情
  109. /* @var GoodsModel $goods */
  110. $goods = GoodsModel::detail($goodsId);
  111. // 商品sku信息
  112. $goods['goods_sku'] = GoodsModel::getGoodsSku($goods, $goodsSkuId);
  113. // 商品列表
  114. $goodsList = [$goods->hidden(['category', 'content', 'image', 'sku'])];
  115. foreach ($goodsList as &$item) {
  116. // 商品单价
  117. $item['goods_price'] = $item['goods_sku']['goods_price'];
  118. // 商品购买数量
  119. $item['total_num'] = $goodsNum;
  120. $item['spec_sku_id'] = $item['goods_sku']['spec_sku_id'];
  121. // 商品购买总金额
  122. $item['total_price'] = helper::bcmul($item['goods_price'], $goodsNum);
  123. }
  124. return $goodsList;
  125. }
  126. /**
  127. * 余额支付标记订单已支付
  128. * @param $orderNo
  129. * @return bool
  130. * @throws \think\exception\DbException
  131. */
  132. public function onPaymentByBalance($orderNo)
  133. {
  134. // 获取订单详情
  135. $PaySuccess = new PaySuccess($orderNo);
  136. // 发起余额支付
  137. $status = $PaySuccess->onPaySuccess(PayTypeEnum::BALANCE);
  138. if (!$status) {
  139. $this->error = $PaySuccess->getError();
  140. }
  141. return $status;
  142. }
  143. /**
  144. * 用户中心订单列表
  145. * @param $user_id
  146. * @param string $type
  147. * @return \think\Paginator
  148. * @throws \think\exception\DbException
  149. */
  150. public function getList($user_id, $type = 'all')
  151. {
  152. // 筛选条件
  153. $filter = [];
  154. // 订单数据类型
  155. switch ($type) {
  156. case 'all':
  157. break;
  158. case 'payment';
  159. $filter['pay_status'] = PayStatusEnum::PENDING;
  160. $filter['order_status'] = 10;
  161. break;
  162. case 'delivery';
  163. $filter['pay_status'] = PayStatusEnum::SUCCESS;
  164. $filter['delivery_status'] = 10;
  165. $filter['order_status'] = 10;
  166. break;
  167. case 'received';
  168. $filter['pay_status'] = PayStatusEnum::SUCCESS;
  169. $filter['delivery_status'] = 20;
  170. $filter['receipt_status'] = 10;
  171. $filter['order_status'] = 10;
  172. break;
  173. case 'comment';
  174. $filter['is_comment'] = 0;
  175. $filter['order_status'] = 30;
  176. break;
  177. }
  178. return $this->with(['goods.image'])
  179. ->where('user_id', '=', $user_id)
  180. ->where($filter)
  181. ->where('is_delete', '=', 0)
  182. ->order(['create_time' => 'desc'])
  183. ->paginate(15, false, [
  184. 'query' => \request()->request()
  185. ]);
  186. }
  187. /**
  188. * 取消订单
  189. * @param UserModel $user
  190. * @return bool|mixed
  191. */
  192. public function cancel($user)
  193. {
  194. if ($this['delivery_status']['value'] == 20) {
  195. $this->error = '已发货订单不可取消';
  196. return false;
  197. }
  198. // 订单取消事件
  199. return $this->transaction(function () use ($user) {
  200. // 订单是否已支付
  201. $isPay = $this['pay_status']['value'] == PayStatusEnum::SUCCESS;
  202. // 未付款的订单
  203. if ($isPay == false) {
  204. // 回退商品库存
  205. FactoryStock::getFactory($this['order_source'])->backGoodsStock($this['goods'], $isPay);
  206. // 回退用户优惠券
  207. $this['coupon_id'] > 0 && UserCouponModel::setIsUse($this['coupon_id'], false);
  208. // 回退用户积分
  209. $describe = "订单取消:{$this['order_no']}";
  210. $this['points_num'] > 0 && $user->setIncPoints($this['points_num'], $describe);
  211. }
  212. // 更新订单状态
  213. return $this->save(['order_status' => $isPay ? OrderStatusEnum::APPLY_CANCEL : OrderStatusEnum::CANCELLED]);
  214. });
  215. }
  216. /**
  217. * 确认收货
  218. * @return bool|mixed
  219. */
  220. public function receipt()
  221. {
  222. // 验证订单是否合法
  223. // 条件1: 订单必须已发货
  224. // 条件2: 订单必须未收货
  225. if ($this['delivery_status']['value'] != 20 || $this['receipt_status']['value'] != 10) {
  226. $this->error = '该订单不合法';
  227. return false;
  228. }
  229. return $this->transaction(function () {
  230. // 更新订单状态
  231. $status = $this->save([
  232. 'receipt_status' => 20,
  233. 'receipt_time' => time(),
  234. 'order_status' => 30
  235. ]);
  236. // // 获取已完成的订单
  237. // $completed = self::detail($this['order_id'], [
  238. // 'user', 'address', 'goods', 'express', // 用于好物圈
  239. // ]);
  240. // 执行订单完成后的操作
  241. $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::MASTER);
  242. $OrderCompleteService->complete([$this], static::$wxapp_id);
  243. return $status;
  244. });
  245. }
  246. /**
  247. * 获取订单总数
  248. * @param $user
  249. * @param string $type
  250. * @return int|string
  251. * @throws \think\Exception
  252. */
  253. public function getCount($user, $type = 'all')
  254. {
  255. if ($user === false) {
  256. return false;
  257. }
  258. // 筛选条件
  259. $filter = [];
  260. // 订单数据类型
  261. switch ($type) {
  262. case 'all':
  263. break;
  264. case 'payment';
  265. $filter['pay_status'] = PayStatusEnum::PENDING;
  266. break;
  267. case 'received';
  268. $filter['pay_status'] = PayStatusEnum::SUCCESS;
  269. $filter['delivery_status'] = 20;
  270. $filter['receipt_status'] = 10;
  271. break;
  272. case 'comment';
  273. $filter['order_status'] = 30;
  274. $filter['is_comment'] = 0;
  275. break;
  276. }
  277. return $this->where('user_id', '=', $user['user_id'])
  278. ->where('order_status', '<>', 20)
  279. ->where($filter)
  280. ->where('is_delete', '=', 0)
  281. ->count();
  282. }
  283. /**
  284. * 订单详情
  285. * @param $order_id
  286. * @param null $user_id
  287. * @return null|static
  288. * @throws BaseException
  289. * @throws \think\exception\DbException
  290. */
  291. public static function getUserOrderDetail($order_id, $user_id)
  292. {
  293. if (!$order = self::get([
  294. 'order_id' => $order_id,
  295. 'user_id' => $user_id,
  296. ], [
  297. 'goods' => ['image', 'goods', 'refund'],
  298. 'address', 'express', 'extract_shop'
  299. ])
  300. ) {
  301. throw new BaseException(['msg' => '订单不存在']);
  302. }
  303. return $order;
  304. }
  305. /**
  306. * 判断当前订单是否允许核销
  307. * @param static $order
  308. * @return bool
  309. */
  310. public function checkExtractOrder(&$order)
  311. {
  312. if (
  313. $order['pay_status']['value'] == PayStatusEnum::SUCCESS
  314. && $order['delivery_type']['value'] == DeliveryTypeEnum::EXTRACT
  315. && $order['delivery_status']['value'] == 10
  316. ) {
  317. return true;
  318. }
  319. $this->setError('该订单不能被核销');
  320. return false;
  321. }
  322. /**
  323. * 当前订单是否允许申请售后
  324. * @return bool
  325. */
  326. public function isAllowRefund()
  327. {
  328. // 必须是已发货的订单
  329. if ($this['delivery_status']['value'] != 20) {
  330. return false;
  331. }
  332. // 允许申请售后期限(天)
  333. $refundDays = SettingModel::getItem('trade')['order']['refund_days'];
  334. // 不允许售后
  335. if ($refundDays == 0) {
  336. return false;
  337. }
  338. // 当前时间超出允许申请售后期限
  339. if (
  340. $this['receipt_status'] == 20
  341. && time() > ($this['receipt_time'] + ((int)$refundDays * 86400))
  342. ) {
  343. return false;
  344. }
  345. return true;
  346. }
  347. /**
  348. * 设置错误信息
  349. * @param $error
  350. */
  351. protected function setError($error)
  352. {
  353. empty($this->error) && $this->error = $error;
  354. }
  355. /**
  356. * 是否存在错误
  357. * @return bool
  358. */
  359. public function hasError()
  360. {
  361. return !empty($this->error);
  362. }
  363. }