OrderService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace addons\Area\common\service;
  3. use addons\Area\common\models\AreaCommunity;
  4. use addons\Area\common\models\AreaOrder;
  5. use addons\Area\common\enums\AreaEnum;
  6. use common\enums\StatusEnum;
  7. use common\models\order\Order;
  8. use Exception;
  9. class OrderService
  10. {
  11. public function preview($params)
  12. {
  13. try {
  14. $configs = \Yii::$app->services->setting->addons->get($params['mall_id'], AreaEnum::ADDONS_NAME, 'basic');
  15. if ($configs['is_buy_area'] != 1) throw new Exception('未开启区代申请条件按钮');
  16. if ($params['level'] != 3) throw new Exception('非申请区代身份无需下单');
  17. //检查是否存在已购买订单
  18. $order = AreaOrder::getOne([
  19. ['mall_id' => $params['mall_id']],
  20. ['user_id' => $params['user_id']],
  21. ['level' => $params['level']],
  22. ['area_id' => $params['district_id']],
  23. ['is_pay' => StatusEnum::ENABLED],
  24. ['status' => StatusEnum::ENABLED],
  25. ], true);
  26. if (!empty($order)) return ['is_pay' => 1, 'order_id' => $order['id']];
  27. //获取最近购买该区代身份的订单
  28. $near_order = AreaOrder::getOne([
  29. ['mall_id' => $params['mall_id']],
  30. ['level' => $params['level']],
  31. ['area_id' => $params['district_id']],
  32. ['is_pay' => StatusEnum::ENABLED],
  33. ['status' => StatusEnum::ENABLED],
  34. ], true);
  35. //处理底价
  36. $price = $configs['area_buy_price'];
  37. if (!empty($near_order)) {
  38. $price = bcmul($near_order['price'] + ($near_order['price'] * $configs['add_price_pre']/100), 1, 2);
  39. }
  40. return ['total_pay_money' => $price];
  41. } catch (\Exception $e) {
  42. throw new Exception($e->getMessage());
  43. }
  44. }
  45. public function created($params)
  46. {
  47. try {
  48. $configs = \Yii::$app->services->setting->addons->get($params['mall_id'], AreaEnum::ADDONS_NAME, 'basic');
  49. if ($configs['is_buy_area'] != 1) throw new Exception('未开启区代申请条件按钮');
  50. if ($params['level'] != 3) throw new Exception('非申请区代身份无需下单');
  51. //检查是否存在已购买订单
  52. $order = AreaOrder::getOne([
  53. ['mall_id' => $params['mall_id']],
  54. ['user_id' => $params['user_id']],
  55. ['level' => $params['level']],
  56. ['area_id' => $params['district_id']],
  57. ['is_pay' => StatusEnum::ENABLED],
  58. ['status' => StatusEnum::ENABLED],
  59. ], true);
  60. if (!empty($order)) return ['is_pay' => 1, 'order_id' => $order['id']];
  61. //获取最近购买该区代身份的订单
  62. $near_order = AreaOrder::getOne([
  63. ['mall_id' => $params['mall_id']],
  64. ['level' => $params['level']],
  65. ['area_id' => $params['district_id']],
  66. ['is_pay' => StatusEnum::ENABLED],
  67. ['status' => StatusEnum::ENABLED],
  68. ], true);
  69. //处理底价
  70. $price = $configs['area_buy_price'];
  71. if (!empty($near_order)) {
  72. $price = bcmul($near_order['price'] + ($near_order['price'] * $configs['add_price_pre']/100), 1, 2);
  73. }
  74. $order_data = [
  75. 'mall_id' => $params['mall_id'],
  76. 'user_id' => $params['user_id'],
  77. 'level' => $params['level'],
  78. 'area_id' => $params['district_id'],
  79. 'price' => $price,
  80. 'order_no' => self::generateOrderNo('sa'),
  81. ];
  82. $order = AreaOrder::setData($order_data);
  83. if (!$order) throw new Exception(AreaOrder::$static_error);
  84. $order_trade_info[] = [
  85. 'order_id' => $order->id,
  86. 'order_type' => 'Area',
  87. 'order_no' => $order->order_no,
  88. 'amount' => $order->price,
  89. 'title' => '购买区代申请资格',
  90. 'notify_class' => 'addons\Area\common\logic\notify\Notify',
  91. ];
  92. $res = \Yii::$app->services->pay->createTrade($params['mall_id'],$params['user_id'], $order_trade_info);
  93. $res['order_id'] = $order->id;
  94. $arr['transaction'] = $res;
  95. $payment_expire_time = \Yii::$app->services->setting->mall->get($params['mall_id'], 'order.payment_expire_time');
  96. $arr['surplus_second'] = 30*60;//剩余秒数
  97. if($payment_expire_time){
  98. $arr['surplus_second'] = intval($payment_expire_time) * 60;//剩余秒数
  99. }
  100. return $arr;
  101. } catch (\Exception $e) {
  102. throw new Exception($e->getMessage());
  103. }
  104. }
  105. public static function generateOrderNo($prefix = ''){
  106. // 优化唯一性保证:使用random_int()代替rand(),并结合微调以生成更唯一的ID
  107. $randInt = random_int(100000, 999999);
  108. // 日期时间戳处理
  109. $dateTimeStr = date('YmdHis');
  110. // 保证前缀不为空字符串且不超过一定长度(例如20个字符)
  111. $prefix = trim($prefix, '0');
  112. if (strlen($prefix) > 20) {
  113. $prefix = substr($prefix, 0, 20);
  114. }
  115. // 生成订单号,保证总长度为17(前缀+日期时间戳+随机数)
  116. $orderNo = $prefix . $dateTimeStr . str_pad($randInt, 6, '0', STR_PAD_LEFT);
  117. return $orderNo;
  118. }
  119. }