Order.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model\recharge;
  13. use app\api\service\User as UserService;
  14. use app\api\model\Setting as SettingModel;
  15. use app\api\model\recharge\Plan as PlanModel;
  16. use app\api\model\recharge\OrderPlan as OrderPlanModel;
  17. use app\common\model\recharge\Order as OrderModel;
  18. use app\common\service\Order as OrderService;
  19. use app\common\enum\recharge\order\PayStatus as PayStatusEnum;
  20. use app\common\enum\recharge\order\RechargeType as RechargeTypeEnum;
  21. use cores\exception\BaseException;
  22. use app\common\library\helper;
  23. /**
  24. * 用户充值订单模型
  25. * Class Order
  26. * @package app\api\model\recharge
  27. */
  28. class Order extends OrderModel
  29. {
  30. /**
  31. * 隐藏字段
  32. * @var array
  33. */
  34. protected $hidden = [
  35. 'transaction_id',
  36. 'store_id',
  37. 'create_time',
  38. 'update_time',
  39. ];
  40. /**
  41. * 获取订单列表
  42. * @return \think\Paginator
  43. * @throws BaseException
  44. * @throws \think\db\exception\DbException
  45. */
  46. public function getList(): \think\Paginator
  47. {
  48. // 当前用户ID
  49. $userId = UserService::getCurrentLoginUserId();
  50. // 获取列表数据
  51. return $this->where('user_id', '=', $userId)
  52. ->where('pay_status', '=', PayStatusEnum::SUCCESS)
  53. ->order(['create_time' => 'desc'])
  54. ->paginate(15);
  55. }
  56. /**
  57. * 获取订单详情 (待付款状态)
  58. * @param string $orderNo 订单号
  59. * @return array|null|static
  60. */
  61. public static function getPayDetail(string $orderNo)
  62. {
  63. return self::detail(['order_no' => $orderNo]);
  64. }
  65. /**
  66. * 创建充值订单
  67. * @param int|null $planId 方案ID
  68. * @param string|null $customMoney 自定义金额
  69. * @return bool|int
  70. * @throws BaseException
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. public function createOrder(?int $planId = null, ?string $customMoney = null)
  76. {
  77. // 确定充值方式
  78. $rechargeType = $planId > 0 ? RechargeTypeEnum::PLAN : RechargeTypeEnum::CUSTOM;
  79. // 验证用户输入
  80. if (!$this->validateForm($rechargeType, $planId, $customMoney)) {
  81. $this->error = $this->error ?: '数据验证错误';
  82. return false;
  83. }
  84. // 获取订单数据
  85. $data = $this->getOrderData($rechargeType, $planId, $customMoney);
  86. // 记录订单信息
  87. return $this->saveOrder($data);
  88. }
  89. /**
  90. * 保存订单记录
  91. * @param array $data
  92. * @return bool
  93. */
  94. private function saveOrder(array $data): bool
  95. {
  96. // 写入订单记录
  97. $this->save($data['order']);
  98. // 记录订单套餐快照
  99. if (!empty($data['plan'])) {
  100. $PlanModel = new OrderPlanModel;
  101. return $PlanModel->add($this['order_id'], $data['plan']);
  102. }
  103. return true;
  104. }
  105. /**
  106. * 生成充值订单
  107. * @param int $rechargeType 充值方式
  108. * @param int $planId 方案ID
  109. * @param string $customMoney 自定义金额
  110. * @return array|array[]|bool
  111. * @throws BaseException
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\DbException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. */
  116. private function getOrderData(int $rechargeType, int $planId, string $customMoney)
  117. {
  118. // 订单信息
  119. $data = [
  120. 'order' => [
  121. 'user_id' => UserService::getCurrentLoginUserId(),
  122. 'order_no' => 'RC' . OrderService::createOrderNo(),
  123. 'recharge_type' => $rechargeType,
  124. 'gift_money' => 0.00,
  125. 'platform' => \getPlatform(),
  126. 'store_id' => self::$storeId,
  127. ],
  128. 'plan' => [] // 订单套餐快照
  129. ];
  130. // 自定义金额充值
  131. if ($rechargeType == RechargeTypeEnum::CUSTOM) {
  132. $data = $this->createDataByCustom($data, $customMoney);
  133. }
  134. // 套餐充值
  135. if ($rechargeType == RechargeTypeEnum::PLAN) {
  136. $data = $this->createDataByPlan($data, $planId);
  137. }
  138. // 实际到账金额
  139. $data['order']['actual_money'] = helper::bcadd($data['order']['pay_price'], $data['order']['gift_money']);
  140. return $data;
  141. }
  142. /**
  143. * 创建套餐充值订单数据
  144. * @param array $order 订单数据
  145. * @param int $planId 方案ID
  146. * @return array
  147. * @throws BaseException
  148. */
  149. private function createDataByPlan(array $order, int $planId): array
  150. {
  151. // 获取套餐详情
  152. $planInfo = PlanModel::detail($planId);
  153. if (empty($planInfo)) {
  154. throwError('充值套餐不存在');
  155. }
  156. $order['plan'] = $planInfo;
  157. $order['order']['plan_id'] = $planInfo['plan_id'];
  158. $order['order']['gift_money'] = $planInfo['gift_money'];
  159. $order['order']['pay_price'] = $planInfo['money'];
  160. return $order;
  161. }
  162. /**
  163. * 创建自定义充值订单数据
  164. * @param array $order
  165. * @param string $customMoney 自定义金额
  166. * @return array|bool
  167. * @throws \think\db\exception\DataNotFoundException
  168. * @throws \think\db\exception\DbException
  169. * @throws \think\db\exception\ModelNotFoundException
  170. */
  171. private function createDataByCustom(array $order, string $customMoney)
  172. {
  173. // 用户支付金额
  174. $order['order']['pay_price'] = $customMoney;
  175. // 充值设置
  176. $setting = SettingModel::getItem('recharge');
  177. if (!$setting['is_custom']) {
  178. return true;
  179. }
  180. // 根据自定义充值金额匹配满足的套餐
  181. if ($setting['is_match_plan']) {
  182. $matchPlanInfo = (new PlanModel)->getMatchPlan($customMoney);
  183. if (!empty($matchPlanInfo)) {
  184. $order['plan'] = $matchPlanInfo;
  185. $order['order']['plan_id'] = $matchPlanInfo['plan_id'];
  186. $order['order']['gift_money'] = $matchPlanInfo['gift_money'];
  187. }
  188. }
  189. return $order;
  190. }
  191. /**
  192. * 表单验证
  193. * @param int $rechargeType
  194. * @param int $planId 方案ID
  195. * @param string $customMoney 自定义金额
  196. * @return bool
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\DbException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. */
  201. private function validateForm(int $rechargeType, int $planId, string $customMoney): bool
  202. {
  203. if (empty($planId) && $customMoney <= 0) {
  204. $this->error = '请选择充值套餐或输入充值金额';
  205. return false;
  206. }
  207. // 验证自定义的金额
  208. if ($rechargeType == RechargeTypeEnum::CUSTOM && !$this->validateFormCustom($customMoney)) {
  209. return false;
  210. }
  211. return true;
  212. }
  213. /**
  214. * 表单验证 [自定义充值]
  215. * @param string $customMoney 充值金额
  216. * @return bool
  217. * @throws \think\db\exception\DataNotFoundException
  218. * @throws \think\db\exception\DbException
  219. * @throws \think\db\exception\ModelNotFoundException
  220. */
  221. private function validateFormCustom(string $customMoney): bool
  222. {
  223. // 充值设置
  224. $setting = SettingModel::getItem('recharge');
  225. if (!$setting['is_custom']) {
  226. $this->error = '很抱歉,当前不允许充值自定义金额';
  227. return false;
  228. }
  229. if ($customMoney <= 0) {
  230. $this->error = '请输入正确的充值金额';
  231. return false;
  232. }
  233. // 验证最低充值金额
  234. if (helper::bccomp($customMoney, $setting['lowest_money']) === -1) {
  235. $this->error = "很抱歉,当前最低充值金额不能低于{$setting['lowest_money']}元";
  236. return false;
  237. }
  238. return true;
  239. }
  240. }