123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace app\api\controller;
- use app\api\model\BuycarModel;
- use app\api\model\Commoditycolor;
- use app\common\controller\Api;
- /**
- * 购物车
- */
- class Buycar extends APi
- {
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
- /**
- * 购物车显示
- * @param string $user_id 用户id
- */
- public function buyCarIndex()
- {
- $parames = $this->request->post();
- $rules = [
- 'user_id' => 'require|number'
- ];
- $msg = [
- 'user_id.require' => '网络错误',
- 'user_id.number' => '网络错误',
- ];
- $validata = $this->validate($parames, $rules, $msg);
- if (is_string($validata)) {
- return $this->result($validata, [], 100);
- }
- $where = array(
- 'user_id' => $parames['user_id']
- );
- $data = BuycarModel::with('commodity')
- ->where($where)
- ->select();
- // $data = BuycarModel::where('user_id',$parames['user_id'])
- // ->alias('b')
- // ->join('commodity c','b.c_id=c.c_id')
- // ->select();
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('暂无数据', [], 100);
- }
- }
- /**
- * 购物车添加
- * @param string $user_id 用户id
- * @param string $c_id 商品c_id
- * @param string $p_id 规格id
- * @param string $p_name 规格名
- * @param string $colorid 颜色id
- * @param string $name 颜色名
- * @param string $number 数量
- * @param string $color 颜色的图片
- */
- public function buyCarAdd()
- {
- $params = $this->request->post();
- $rules = [
- 'user_id' => 'require|number',
- 'c_id' => 'require|number',
- 'p_id' => 'require|number',
- 'colorid' => 'require|number',
- 'p_name' => 'require',
- 'name' => 'require',
- 'number' => 'require|number',
- 'color' => 'require',
- ];
- $msg = [
- 'user_id.require' => '网络错误',
- 'user_id.number' => '网络错误',
- 'c_id.require' => '网络错误',
- 'c_id.number' => '网络错误',
- 'p_id.require' => '网络错误',
- 'p_id.number' => '网络错误',
- 'colorid.number' => '网络错误',
- 'colorid.require' => '网络错误',
- 'p_name.require' => '网络错误',
- 'name.require' => '网络错误',
- 'number.require' => '网络错误',
- 'number.number' => '网络错误',
- 'color.require' => '网络错误',
- ];
- $validata = $this->validate($params, $rules, $msg);
- if (is_string($validata)) {
- return $this->result($validata, [], 100);
- }
- // 查出颜色信息
- $color = Commoditycolor::where('colorid',$params['colorid'])->find();
- $list = $params;
- unset($list['number']);
- unset($list['token']);
- $issetcar = BuycarModel::where($list)->find(); //判断购物车是否已存在此商品
- $params['money'] = $color['money'];
- $params['kucun'] = $color['kucun'];
- if ($issetcar) {
- return $this->result('购物车已经存在此商品了','',100);
- // //存在的话把数量加上
- // $addnumber = BuycarModel::where('k_id',$issetcar['k_id'])->setInc('number',$params['number']);
- // //存在的话把金额加上
- // $addmoney = BuycarModel::where('k_id',$issetcar['k_id'])->setInc('money',$color['money']);
- // if ($addnumber&&$addmoney) {
- // return $this->result('添加成功', '', 200);
- // } else {
- // return $this->result('添加失败', '', 100);
- // }
- }
- $model = new BuycarModel();
- $addCar = $model->allowField(true)->save($params);
- if ($addCar) {
- return $this->result('添加成功', '', 200);
- } else {
- return $this->result('添加失败', '', 100);
- }
- }
- /**
- * 购物车批量删除
- * @param string $list 购物车id(数组)
- */
- public function carDel () {
- $params = $this->request->post();
- $rules = [
- 'list' => 'require'
- ];
- $msg = [
- 'list.require' => '网络错误'
- ];
- $validata = $this->validate($params,$rules,$msg);
- if (is_string($validata)) {
- return $this->result($validata,[],100);
- }
- foreach ($params['list'] as $v) {
- $del = BuycarModel::where('k_id',$v)->delete();
- }
- return $this->result('删除成功',[],200);
- }
- }
|