Task.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\api\controller\bargain;
  3. use app\api\controller\Controller;
  4. use app\api\model\bargain\Task as TaskModel;
  5. use app\api\model\bargain\Setting as SettingModel;
  6. use app\common\library\Lock;
  7. class Task extends Controller
  8. {
  9. /**
  10. * 我的砍价列表
  11. * @return array
  12. * @throws \app\common\exception\BaseException
  13. * @throws \think\exception\DbException
  14. */
  15. public function lists()
  16. {
  17. $model = new TaskModel;
  18. $myList = $model->getMyList($this->getUser()['user_id']);
  19. return $this->renderSuccess(compact('myList'));
  20. }
  21. /**
  22. * 创建砍价任务
  23. * @param $active_id
  24. * @param $goods_sku_id
  25. * @return array
  26. * @throws \app\common\exception\BaseException
  27. * @throws \think\exception\DbException
  28. */
  29. public function partake($active_id, $goods_sku_id)
  30. {
  31. // 用户信息
  32. $user = $this->getUser();
  33. // 创建砍价任务
  34. $model = new TaskModel;
  35. if (!$model->partake($user['user_id'], $active_id, $goods_sku_id)) {
  36. return $this->renderError($model->getError() ?: '砍价任务创建失败');
  37. }
  38. return $this->renderSuccess([
  39. 'task_id' => $model['task_id']
  40. ]);
  41. }
  42. /**
  43. * 获取砍价任务详情
  44. * @param $task_id
  45. * @return array
  46. * @throws \app\common\exception\BaseException
  47. * @throws \think\exception\DbException
  48. */
  49. public function detail($task_id)
  50. {
  51. $model = new TaskModel;
  52. $detail = $model->getTaskDetail($task_id, $this->getUser(false));
  53. if ($detail === false) {
  54. return $this->renderError($model->getError());
  55. }
  56. // 砍价规则
  57. $setting = SettingModel::getBasic();
  58. return $this->renderSuccess(array_merge($detail, ['setting' => $setting]));
  59. }
  60. /**
  61. * 帮砍一刀
  62. * @param $task_id
  63. * @return array
  64. * @throws \app\common\exception\BaseException
  65. * @throws \think\exception\DbException
  66. */
  67. public function help_cut($task_id)
  68. {
  69. // 加阻塞锁, 防止并发
  70. Lock::lockUp("bargain_help_cut_{$task_id}");
  71. // 砍价任务详情
  72. $model = TaskModel::detail($task_id);
  73. // 砍一刀的金额
  74. $cut_money = $model->getCutMoney();
  75. // 帮砍一刀事件
  76. $status = $model->helpCut($this->getUser());
  77. // 解除并发锁
  78. Lock::unLock("bargain_help_cut_{$task_id}");
  79. if ($status == true) {
  80. return $this->renderSuccess(compact('cut_money'), '砍价成功');
  81. }
  82. return $this->renderError($model->getError() ?: '砍价失败');
  83. }
  84. }