Order.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::MASTER);
  238. $OrderCompleteService->complete([$this], static::$wxapp_id);
  239. return $status;
  240. });
  241. }
  242. /**
  243. * 获取订单总数
  244. * @param $user
  245. * @param string $type
  246. * @return int|string
  247. * @throws \think\Exception
  248. */
  249. public function getCount($user, $type = 'all')
  250. {
  251. if ($user === false) {
  252. return false;
  253. }
  254. // 筛选条件
  255. $filter = [];
  256. // 订单数据类型
  257. switch ($type) {
  258. case 'all':
  259. break;
  260. case 'payment';
  261. $filter['pay_status'] = PayStatusEnum::PENDING;
  262. break;
  263. case 'received';
  264. $filter['pay_status'] = PayStatusEnum::SUCCESS;
  265. $filter['delivery_status'] = 20;
  266. $filter['receipt_status'] = 10;
  267. break;
  268. case 'comment';
  269. $filter['order_status'] = 30;
  270. $filter['is_comment'] = 0;
  271. break;
  272. }
  273. return $this->where('user_id', '=', $user['user_id'])
  274. ->where('order_status', '<>', 20)
  275. ->where($filter)
  276. ->where('is_delete', '=', 0)
  277. ->count();
  278. }
  279. /**
  280. * 订单详情
  281. * @param $order_id
  282. * @param null $user_id
  283. * @return null|static
  284. * @throws BaseException
  285. * @throws \think\exception\DbException
  286. */
  287. public static function getUserOrderDetail($order_id, $user_id)
  288. {
  289. if (!$order = self::get([
  290. 'order_id' => $order_id,
  291. 'user_id' => $user_id,
  292. ], [
  293. 'goods' => ['image', 'goods', 'refund'],
  294. 'address', 'express', 'extract_shop'
  295. ])
  296. ) {
  297. throw new BaseException(['msg' => '订单不存在']);
  298. }
  299. return $order;
  300. }
  301. /**
  302. * 判断当前订单是否允许核销
  303. * @param static $order
  304. * @return bool
  305. */
  306. public function checkExtractOrder(&$order)
  307. {
  308. if (
  309. $order['pay_status']['value'] == PayStatusEnum::SUCCESS
  310. && $order['delivery_type']['value'] == DeliveryTypeEnum::EXTRACT
  311. && $order['delivery_status']['value'] == 10
  312. ) {
  313. return true;
  314. }
  315. $this->setError('该订单不能被核销');
  316. return false;
  317. }
  318. /**
  319. * 当前订单是否允许申请售后
  320. * @return bool
  321. */
  322. public function isAllowRefund()
  323. {
  324. // 必须是已发货的订单
  325. if ($this['delivery_status']['value'] != 20) {
  326. return false;
  327. }
  328. // 允许申请售后期限(天)
  329. $refundDays = SettingModel::getItem('trade')['order']['refund_days'];
  330. // 不允许售后
  331. if ($refundDays == 0) {
  332. return false;
  333. }
  334. // 当前时间超出允许申请售后期限
  335. if (
  336. $this['receipt_status'] == 20
  337. && time() > ($this['receipt_time'] + ((int)$refundDays * 86400))
  338. ) {
  339. return false;
  340. }
  341. return true;
  342. }
  343. /**
  344. * 设置错误信息
  345. * @param $error
  346. */
  347. protected function setError($error)
  348. {
  349. empty($this->error) && $this->error = $error;
  350. }
  351. /**
  352. * 是否存在错误
  353. * @return bool
  354. */
  355. public function hasError()
  356. {
  357. return !empty($this->error);
  358. }
  359. }