Order.php 12 KB

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