Order.php 11 KB

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