Order.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Config;
  5. use app\common\model\Goods;
  6. use app\common\model\MoneyLog;
  7. use app\common\model\Order AS OrderModel;
  8. use app\common\model\Users;
  9. use think\Db;
  10. use think\Exception;
  11. /**
  12. * 首页接口
  13. */
  14. class Order extends Api
  15. {
  16. protected $noNeedLogin = [];
  17. protected $noNeedRight = ['*'];
  18. /**
  19. * 订单列表
  20. * @return void
  21. * @throws \think\exception\DbException
  22. */
  23. public function base()
  24. {
  25. $user = $this->auth->getUser();
  26. $data['banner_list'] = 'https://dapp-static.oss-cn-shenzhen.aliyuncs.com/jue-jin-lu/3.pnggS02ouwrJfiF65979ec74db73';
  27. $data['day_tasks_num'] = (new Config())->getValue('day_tasks_num');//每日任务数
  28. $data['task_income'] = (new Config())->getValue('task_income');//单次任务收益
  29. $data['task_num'] = $user['task_num'];//今日已做任务
  30. $data['balance'] = $user['balance'];
  31. $data['bonus_sum'] = $user['bonus_sum'];
  32. $data['bonus_today'] = (new OrderModel())
  33. ->where('user_id', $user['id'])
  34. ->where('status', OrderModel::Success)
  35. ->whereTime('create_time', '>=', strtotime('today'))
  36. ->sum('bonus');
  37. $this->success('', $data);
  38. }
  39. /**
  40. * 订单列表
  41. * @return void
  42. * @throws \think\exception\DbException
  43. */
  44. public function list()
  45. {
  46. $user = $this->auth->getUser();
  47. $paginator = OrderModel::where('user_id', $user['id'])
  48. ->field('order_no,amount,bonus,status,create_time')
  49. ->order('id DESC')
  50. ->paginate($this->pageSize);
  51. foreach ($paginator as $k => $v) {
  52. $paginator[$k]['status_name'] = (new OrderModel())->getStatusNames($v['status']);
  53. }
  54. $res_data = $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items());
  55. $res_data['bonus_sum'] = $user['bonus_sum'];
  56. $this->success('', $res_data);
  57. }
  58. /**
  59. * 获取订单
  60. * @return void
  61. * @throws \think\exception\DbException
  62. */
  63. public function get()
  64. {
  65. $user = $this->auth->getUser();
  66. if($user['open_task'] != 1){
  67. $this->error(__('暂停抢单'));
  68. }
  69. if(!($user['freeze'] < 0)){
  70. //冻结金额《0时,需要充值
  71. $this->error(__('余额不足'));
  72. }
  73. if(!($user['balance'] > 0)){
  74. $this->error(__('余额不足'));
  75. }
  76. $day_tasks_num = (new Config())->getValue('day_tasks_num');//单日任务数
  77. $task_income = (new Config())->getValue('task_income');//单次收益
  78. $amount_mini = $user['balance'] * 0.4;
  79. $amount_max = $user['balance'] * 0.8;
  80. if($user['task_num'] >= $day_tasks_num){
  81. if(!empty($user['task_last_time']) && (date('md',time()) != date('md', $user['task_last_time']))){
  82. //当日接单量 >= 任务数时,最后一次接单时间和当前不是同一天,则重置当日接单量
  83. $user['task_num'] = 0;
  84. Users::where('id', $user['id'])->update([
  85. 'task_num' => 0
  86. ]);
  87. }else{
  88. $this->error(__('今日任务已完成'));
  89. }
  90. }
  91. $check_order = (new OrderModel)
  92. ->where('user_id', $user['id'])
  93. ->where('status', '<', OrderModel::Success)
  94. ->count();
  95. if($check_order){
  96. $this->error(__('有未完成订单'));
  97. }
  98. if($user['is_limit_task'] == 1){
  99. //卡单 limit_task 字段值为json {"which_start":"7","min_amount":"100","max_amount":"1000","income_multiple":"2"}
  100. $limit_task = json_decode($user['limit_task'], true);
  101. if(empty($limit_task)){
  102. $this->error(__('参数错误'));
  103. }
  104. $val_arr = explode(',', $limit_task['which_start']);
  105. if(in_array($user['task_num'] + 1, $val_arr)){
  106. //从这单开始卡单
  107. if(!($user['freeze'] > 0)){
  108. //冻结现有余额,并
  109. }
  110. $task_income = $task_income * $limit_task['income_multiple'];//单次收益
  111. $amount_mini = $limit_task['min_amount'];
  112. $amount_max = $limit_task['max_amount'];
  113. }
  114. }
  115. $goods_info = (new Goods())
  116. ->fetchSql(false)
  117. ->whereBetween('price', [$amount_mini, $amount_max])
  118. ->where('status', 1)
  119. ->orderRaw("RAND()")
  120. ->find();
  121. if(empty($goods_info)){
  122. $this->error(__('未匹配到商品'));
  123. }
  124. $order_data = [
  125. 'order_no' => 'O' . $user['id'] . time(),
  126. 'user_id' => $user['id'],
  127. 'title' => $goods_info['title'],
  128. 'amount' => $goods_info['price'],
  129. 'bonus' => $goods_info['price'] * $task_income,
  130. 'user_type' => $user['user_type'],
  131. 'status' => OrderModel::Pending,
  132. ];
  133. // 启动事务
  134. Db::startTrans();
  135. try {
  136. // 创建订单
  137. OrderModel::create($order_data);
  138. //账变
  139. (new MoneyLog())->change($user['id'], -$goods_info['price'], MoneyLog::Pay, '', '');
  140. // 提交事务
  141. Db::commit();
  142. } catch (Exception $e) {
  143. // 回滚事务
  144. Db::rollback();
  145. $this->error($e->getMessage());
  146. }
  147. unset($order_data['user_type']);
  148. unset($order_data['status']);
  149. $order_data['img_url'] = $goods_info['img_url'];
  150. $this->success('', $order_data);
  151. }
  152. /**
  153. * 提交订单
  154. * @return void
  155. * @throws \think\exception\DbException
  156. */
  157. public function submit()
  158. {
  159. $user = $this->auth->getUser();
  160. $order_no = $this->request->post('order_no');
  161. if(empty($order_no)){
  162. $this->error(__('参数有误'));
  163. }
  164. $order_info = (new OrderModel)
  165. ->where('user_id', $user['id'])
  166. ->where('order_no', $order_no)
  167. ->find();
  168. if(empty($order_info)){
  169. $this->error(__('参数有误'));
  170. }
  171. if($user['balance'] < 0){
  172. $this->error(__('余额不足'));
  173. }
  174. if($order_info['status'] == OrderModel::Success){
  175. $this->success('');
  176. }
  177. // 启动事务
  178. Db::startTrans();
  179. try {
  180. // 创建订单
  181. (new OrderModel)
  182. ->where('id', $order_info['id'])
  183. ->update([
  184. 'status' => OrderModel::Success
  185. ]);
  186. //账变
  187. (new MoneyLog())->change($user['id'], $order_info['amount'], MoneyLog::PayBack, '', '');
  188. //订单佣金
  189. (new MoneyLog())->change($user['id'], $order_info['bonus'], MoneyLog::OrderBonus, '', '');
  190. //累积佣金和任务次数
  191. (new Users())
  192. ->where('id', $user['id'])
  193. ->update([
  194. 'task_num' => Db::raw('task_num + 1'),
  195. 'bonus_sum' => Db::raw('bonus_sum + ' .$order_info['bonus']),
  196. 'task_last_time'=> time()
  197. ]);
  198. //向上级发放
  199. // 提交事务
  200. Db::commit();
  201. } catch (Exception $e) {
  202. // 回滚事务
  203. Db::rollback();
  204. $this->error($e->getMessage());
  205. }
  206. $this->success('');
  207. }
  208. }