ProductController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 文件说明:幻灯片
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: wuwu <15093565100@163.com>
  8. // +----------------------------------------------------------------------
  9. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // | Date: 2017-5-25
  12. // +----------------------------------------------------------------------
  13. namespace api\rob\controller;
  14. use api\activity\model\ActivityJoinModel;
  15. use api\activity\model\ActivityModel;
  16. use api\activity\model\ActivitySiteJoinModel;
  17. use api\applet\model\UserModel;
  18. use app\rob\model\OrderModel;
  19. use app\rob\model\ProductModel;
  20. use cmf\controller\RestBaseController;
  21. class ProductController extends RestBaseController
  22. {
  23. /**
  24. * 列表
  25. */
  26. public function index()
  27. {
  28. $param = $this->request->param();
  29. $page = empty($param['page']) ? 1 : $param['page'];
  30. $size = empty($param['size']) ? 10 : $param['size'];
  31. //搜索条件
  32. $where = [['on_sale', '=', 1]];
  33. $exp = '(CASE WHEN start_time <= NOW() AND NOW() <= end_time THEN 1 ELSE 0 END) DESC,';
  34. $exp .= '(CASE WHEN start_time > NOW() THEN 1 ELSE 0 END) DESC,';
  35. $exp .= '(CASE WHEN end_time < NOW() THEN 1 ELSE 0 END) DESC';
  36. $order = new \think\db\Expression($exp);
  37. $list = ProductModel::where($where)
  38. ->order($order)
  39. ->page($page, $size)
  40. ->select();
  41. //数据处理
  42. if (!$list->isEmpty()) {
  43. foreach ($list as $v) {
  44. $v['image_main'] = cmf_get_image_preview_url($v['image_main']);
  45. $v['status'] = $v['status'];
  46. $v['status_text'] = $v['status_text'];
  47. $v['type_text'] = $v['type_text'];
  48. }
  49. }
  50. $this->success('成功', $list);
  51. }
  52. /**
  53. * 详情
  54. */
  55. public function detail()
  56. {
  57. $id = $this->request->post('id');
  58. $info = ProductModel::get($id);
  59. $info['type_text'] = $info['type_text'];
  60. $info['image_main'] = cmf_get_image_preview_url($info['image_main']);
  61. if (!empty($info['image_list'])) {
  62. $options = [];
  63. foreach ($info['image_list'] as $v) {
  64. $options[] = [
  65. 'url' => cmf_get_file_download_url($v['url']),
  66. 'name' => $v['name'],
  67. ];
  68. }
  69. $info['image_list'] = $options;
  70. }
  71. $this->success('成功', $info);
  72. }
  73. /**
  74. * 购买
  75. */
  76. public function buy()
  77. {
  78. $id = $this->request->post('id');
  79. $model = new ProductModel();
  80. $model->startTrans();
  81. try {
  82. $product = $model->lock(true)->where('id', $id)->find();
  83. if ($this->user['score'] < $product['point']) {
  84. throw new \Exception("积分不足,您的积分只有{$this->user['score']}!");
  85. }
  86. if ($product['quantity'] <= 0) {
  87. throw new \Exception("商品数量不足!");
  88. }
  89. OrderModel::addOrder($this->user, $product, 1);
  90. $product->quantity--;
  91. $product->save();
  92. $model->commit();
  93. } catch (\Exception $e) {
  94. $model->rollback();
  95. $this->error($e->getMessage());
  96. }
  97. $this->success('抢购成功');
  98. }
  99. }