Order.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model\locked;
  13. use app\api\model\Setting as SettingModel;
  14. use app\api\model\User;
  15. use app\api\service\User as UserService;
  16. use app\common\enum\locked\order\PayType;
  17. use app\common\library\helper;
  18. use app\common\model\UnlockOrder as OrderModel;
  19. use app\common\service\Order as OrderService;
  20. use app\common\enum\locked\order\PayStatus as PayStatusEnum;
  21. use app\common\exception\BaseException;
  22. /**
  23. * 用户解锁订单模型
  24. * Class Order
  25. * @package app\api\model\locked
  26. */
  27. class Order extends OrderModel
  28. {
  29. /**
  30. * 隐藏字段
  31. * @var array
  32. */
  33. protected $hidden = [
  34. 'transaction_id',
  35. 'create_time',
  36. 'update_time',
  37. ];
  38. /**
  39. * 获取订单列表
  40. * @return \think\Paginator
  41. * @throws BaseException
  42. * @throws \think\db\exception\DbException
  43. */
  44. public function getList()
  45. {
  46. // 当前用户ID
  47. $userId = UserService::getCurrentLoginUserId();
  48. // 获取列表数据
  49. return $this->where('user_id', '=', $userId)
  50. ->where('status', '>=', PayStatusEnum::PENDING)
  51. ->order(['create_time' => 'desc'])
  52. ->paginate(15);
  53. }
  54. /**
  55. * 获取订单详情(待付款状态)
  56. * @param $orderNo
  57. * @return array|null|static
  58. */
  59. public static function getPayDetail(string $orderNo)
  60. {
  61. return self::detail(['order_no' => $orderNo, 'status' => PayStatusEnum::PENDING]);
  62. }
  63. /**
  64. * 创建充值订单
  65. * @param int|null $planId
  66. * @param float $customMoney
  67. * @return bool|int
  68. * @throws BaseException
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function createOrder(?int $planId = null, float $customMoney = 0.00)
  74. {
  75. }
  76. /**
  77. * 保存订单记录
  78. * @param $data
  79. * @return bool|false|int
  80. */
  81. private function saveOrder(array $data)
  82. {
  83. return true;
  84. }
  85. /**
  86. * 生成订单
  87. * @param int $payType 支付方式
  88. * @return array|array[]|bool
  89. * @throws BaseException
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. private function getOrderData(int $payType, float $price)
  95. {
  96. // 订单信息
  97. $data = [
  98. 'order' => [
  99. 'user_id' => UserService::getCurrentLoginUserId(),
  100. 'order_no' => 'RC' . OrderService::createOrderNo(),
  101. 'recharge_type' => $payType,
  102. 'price' => 0.00,
  103. 'store_id' => self::$storeId,
  104. ],
  105. 'users' => [] // 解锁用户
  106. ];
  107. // 实际到账金额
  108. $data['order']['total_money'] = 0;
  109. return $data;
  110. }
  111. /**
  112. * 创建套餐充值订单数据
  113. * @param array $order
  114. * @param int $planId
  115. * @return array
  116. * @throws BaseException
  117. */
  118. private function createDataByUsers(array $order, int $userId)
  119. {
  120. // 获取用户详情
  121. $info = User::detail($userId);
  122. if (empty($info)) {
  123. throwError('解锁用户不存在');
  124. }
  125. $order['info'] = $info;
  126. return $order;
  127. }
  128. /**
  129. * 表单验证
  130. * @param int $payType
  131. * @param float $price
  132. * @return bool
  133. * @throws \think\db\exception\DataNotFoundException
  134. * @throws \think\db\exception\DbException
  135. * @throws \think\db\exception\ModelNotFoundException
  136. */
  137. private function validateForm(int $payType, float $price)
  138. {
  139. if ($price <= 0) {
  140. $this->error = '请先后台设置解锁费用';
  141. return false;
  142. }
  143. return true;
  144. }
  145. /**
  146. * 表单验证 [自定义充值]
  147. * @param float $customMoney 充值金额
  148. * @return bool
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\DbException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. */
  153. private function validateFormCustom(float $customMoney)
  154. {
  155. // 充值设置
  156. $setting = SettingModel::getItem('recharge');
  157. if ($setting['is_custom'] == false) {
  158. $this->error = '很抱歉,当前不允许充值自定义金额';
  159. return false;
  160. }
  161. if ($customMoney <= 0) {
  162. $this->error = '请输入正确的充值金额';
  163. return false;
  164. }
  165. // 验证最低充值金额
  166. if (helper::bccomp($customMoney, $setting['lowest_money']) === -1) {
  167. $this->error = "很抱歉,当前最低充值金额不能低于{$setting['lowest_money']}元";
  168. return false;
  169. }
  170. return true;
  171. }
  172. }