Task.php 2.8 KB

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