123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- // +----------------------------------------------------------------------
- // | 文件说明:幻灯片
- // +----------------------------------------------------------------------
- // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Author: wuwu <15093565100@163.com>
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Date: 2017-5-25
- // +----------------------------------------------------------------------
- namespace api\rob\controller;
- use api\activity\model\ActivityJoinModel;
- use api\activity\model\ActivityModel;
- use api\activity\model\ActivitySiteJoinModel;
- use api\applet\model\UserModel;
- use app\rob\model\OrderModel;
- use app\rob\model\ProductModel;
- use cmf\controller\RestBaseController;
- class ProductController extends RestBaseController
- {
- /**
- * 列表
- */
- public function index()
- {
- $param = $this->request->param();
- $page = empty($param['page']) ? 1 : $param['page'];
- $size = empty($param['size']) ? 10 : $param['size'];
- //搜索条件
- $where = [['on_sale', '=', 1]];
- $exp = '(CASE WHEN start_time <= NOW() AND NOW() <= end_time THEN 1 ELSE 0 END) DESC,';
- $exp .= '(CASE WHEN start_time > NOW() THEN 1 ELSE 0 END) DESC,';
- $exp .= '(CASE WHEN end_time < NOW() THEN 1 ELSE 0 END) DESC';
- $order = new \think\db\Expression($exp);
- $list = ProductModel::where($where)
- ->order($order)
- ->page($page, $size)
- ->select();
- //数据处理
- if (!$list->isEmpty()) {
- foreach ($list as $v) {
- $v['image_main'] = cmf_get_image_preview_url($v['image_main']);
- $v['status'] = $v['status'];
- $v['status_text'] = $v['status_text'];
- $v['type_text'] = $v['type_text'];
- }
- }
- $this->success('成功', $list);
- }
- /**
- * 详情
- */
- public function detail()
- {
- $id = $this->request->post('id');
- $info = ProductModel::get($id);
- $info['type_text'] = $info['type_text'];
- $info['image_main'] = cmf_get_image_preview_url($info['image_main']);
- if (!empty($info['image_list'])) {
- $options = [];
- foreach ($info['image_list'] as $v) {
- $options[] = [
- 'url' => cmf_get_file_download_url($v['url']),
- 'name' => $v['name'],
- ];
- }
- $info['image_list'] = $options;
- }
- $this->success('成功', $info);
- }
- /**
- * 购买
- */
- public function buy()
- {
- $id = $this->request->post('id');
- $model = new ProductModel();
- $model->startTrans();
- try {
- $product = $model->lock(true)->where('id', $id)->find();
- if ($this->user['score'] < $product['point']) {
- throw new \Exception("积分不足,您的积分只有{$this->user['score']}!");
- }
- if ($product['quantity'] <= 0) {
- throw new \Exception("商品数量不足!");
- }
- OrderModel::addOrder($this->user, $product, 1);
- $product->quantity--;
- $product->save();
- $model->commit();
- } catch (\Exception $e) {
- $model->rollback();
- $this->error($e->getMessage());
- }
- $this->success('抢购成功');
- }
- }
|