作者 李忠强

更新

... ... @@ -5,6 +5,8 @@ namespace app\admin\controller\litestore;
use app\common\controller\Backend;
use app\admin\model\litestore\Litestorespec as SpecModel;
use app\admin\model\litestore\Litestorespecvalue as SpecValueModel;
use think\Db;
/**
*
*
... ... @@ -233,6 +235,10 @@ class Litestoregoods extends Backend
//成功之后 存储商品规格
$spec_many_params = $this->request->post("spec_many/a");
$row->addGoodsSpec($params,$spec_many_params,$this->request->post("spec/a"), true);
// 删除购物车
Db::name('cart')->where('goods_id',$row->goods_id)->delete();
//删除我常买
Db::name('user_buylist')->where('goods_id',$row->goods_id)->delete();
$this->success();
} else {
$this->error($row->getError());
... ...
<?php
namespace app\api\controller;
use app\api\model\GoodsSpec;
use app\common\controller\Api;
use think\Db;
/**
* 我常买
*/
class Buy extends Api
{
protected $noNeedRight = ['*'];
/**
* @ApiTitle (我常买列表)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function buyList()
{
$model = new \app\api\model\UserBuylist();
$list = $model
->with(['goods','sku'])
->where('user_id',$this->auth->id)
->select();
foreach ($list as $key => $value){
$value->getRelation('goods')->visible(['goods_name','goods_id']);
$value->getRelation('sku')->visible(['goods_spec_id','goods_price','spec_sku_id']);
}
$list = collection($list)->toArray();
foreach ($list as $k => &$val){
$skuids = explode('_', $val['sku']['spec_sku_id']);
$sku = Db::name('litestore_spec_value')->where('id','in',$skuids)->column('spec_value');
$skuname = !empty($sku)?implode(' ',$sku):'';
$val['sku']['sku_name'] = $skuname;
}
$this->success('SUCCESS',$list);
}
/**
* @ApiTitle (价格计算)
* @ApiSummary ([{goods_id:22 goods_sku_id:106 number:2} {goods_id:23 goods_sku_id:66 number:2}])
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="data_json", type="string", required=true, description="下单的商品json数据")
* @ApiParams (name="goods_id", type="integer", required=false, description="商品id 此值不传 json数组注释用")
* @ApiParams (name="goods_sku_id", type="integer", required=false, description="规格id 此值不传 json数组注释用")
* @ApiParams (name="number", type="integer", required=false, description="购买数量 此值不传 json数组注释用")
* @ApiReturn ({
'code':'1',
'msg':'购物车页面价格'
'data':
"price": "37574.00" 总价
})
*/
public function priceCalculation()
{
$json = $this->request->post('data_json');
if (!$json) $this->error('data_json参数不能为空');
$data = json_decode($json,true);
$goodsmodel = new \app\api\model\Goods();
$skumodel = new GoodsSpec();
$sum_price = 0; //总价格
foreach ($data as $key => $value){
if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])){
$this->error('参数不合法');
}
$goods = $goodsmodel->where('goods_id',$value['goods_id'])->field('goods_id,goods_name,image,spec_type')->find();
if (!$goods) $this->error('商品不存在');
$sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])
->field('goods_spec_id,spec_sku_id,goods_price')->find();
if (!$sku) $this->error('商品规格不存在');
$sum_price = bcadd($sum_price,bcmul($sku['goods_price'],$value['number'],2),2);
}
$this->success('购物车页面价格',['price'=>$sum_price]);
}
/**
* @ApiTitle (删除列表)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="cart_ids", type="integer", required=true, description="列表id 逗号隔开")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
})
*/
public function delList()
{
$ids = $this->request->post('cart_ids');
if (!$ids) $this->error('参数不可为空');
$ids = explode(',',$ids);
foreach ($ids as $key => $value){
if (!is_numeric($value)) $this->error('参数不合法');
}
$model = new \app\api\model\UserBuylist();
$model->whereIn('id',$ids)->delete();
$this->success('SUCCESS');
}
/**
* @ApiTitle (增加购买数量)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="列表id")
* @ApiParams (name="number", type="integer", required=true, description="增加数量")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
})
*/
public function addBuyListNumber()
{
$id = $this->request->post('id');
$number = $this->request->post('number');
if (!is_numeric($id) || !is_numeric($number)) $this->error('参数不合法');
$model = new \app\api\model\UserBuylist();
$model->where('id',$id)->setInc('number',$number);
$this->success('SUCCESS');
}
/**
* @ApiTitle (减少购买数量)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="列表id")
* @ApiParams (name="number", type="integer", required=true, description="减少数量")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
})
*/
public function subBuyListNumber()
{
$id = $this->request->post('id');
$number = $this->request->post('number');
if (!is_numeric($id) || !is_numeric($number)) $this->error('参数不合法');
$model = new \app\api\model\UserBuylist();
if ($model->where('id',$id)->value('number') ==1){
$this->error('购买数量不可再减少');
}else{
$model->where('id',$id)->setDec('number',$number);
}
$this->success('SUCCESS');
}
}
\ No newline at end of file
... ...
... ... @@ -53,6 +53,30 @@ class Cart extends Api
$this->success('SUCCESS');
}
/**
* @ApiTitle (删除购物车)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="cart_ids", type="integer", required=true, description="购物车id 逗号隔开")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
})
*/
public function delCart()
{
$ids = $this->request->post('cart_ids');
if (!$ids) $this->error('参数不可为空');
$ids = explode(',',$ids);
foreach ($ids as $key => $value){
if (!is_numeric($value)) $this->error('参数不合法');
}
$model = new \app\api\model\Cart();
$model->whereIn('id',$ids)->delete();
$this->success('SUCCESS');
}
/**
* @ApiTitle (增加购物车数量)
* @ApiMethod (POST)
... ...
... ... @@ -34,6 +34,8 @@ class Notify extends Api
$goodsmodel = new \app\api\model\OrderGoods();
$skumodel = new \app\api\model\GoodsSpec();
$goods_model = new \app\api\model\Goods();
$cartmodel = new \app\api\model\Cart();
$buymodel = new \app\api\model\UserBuylist();
try {
$out_trade_no = $data['out_trade_no'];
$order = $model
... ... @@ -80,6 +82,16 @@ class Notify extends Api
Db::name('user_coupon')->insertAll($data);
}
}
// 加入我常买
$data = [];
foreach ($list as $key => $value){
$data[] = [
'user_id' => $order['user_id'],
'goods_id' => $value['goods_id'],
'sku_id' => $value['goods_spec_id'],
];
}
$buymodel->isUpdate(false)->saveAll($data);
} catch (Exception $e) {
}
echo $pay->success()->send();
... ...
... ... @@ -389,6 +389,7 @@ class Order extends Api
$ordermodel = new \app\api\model\Order();
$ordergoodsmodel = new \app\api\model\OrderGoods();
$orderaddressmodel = new \app\api\model\OrderAddress();
$cartmodel = new \app\api\model\Cart();
$specmodel = new SpecValue();
$goods_array = []; //商品列表
$sum_price = 0; //总价格
... ... @@ -483,6 +484,17 @@ class Order extends Api
$ordergoodsmodel->saveAll($goods_array);
$order_address['order_id'] = $ordermodel->id;
$orderaddressmodel->save($order_address);
// 删除购物车
$goodsids = [];
$skuids = [];
foreach ($goods_array as $key => $value){
$goodsids[] = $value['goods_id'];
$skuids[] = $value['goods_spec_id'];
}
$cartmodel->whereIn('goods_id',$goodsids)
->whereIn('sku_id',$skuids)
->where('user_id',$this->auth->id)
->delete();
Db::commit();
}catch (PDOException $e){
Db::rollback();
... ...
<?php
namespace app\api\model;
use think\Model;
class UserBuylist extends Model
{
protected $autoWriteTimestamp = 'int';
protected $createTime = 'createtime';
protected $updateTime = false;
public function goods()
{
return $this->belongsTo('Goods','goods_id','goods_id');
}
public function sku()
{
return $this->belongsTo('GoodsSpec','sku_id','goods_spec_id');
}
}
\ No newline at end of file
... ...