作者 何书鹏

11

<?php
namespace app\api\controller;
use app\common\controller\Api;
class Order extends Api
{
/**
* 我的订单列表
* @param $dataType
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function index()
{
$model = new OrderModel;
$list = $model->getList($this->user['user_id'], $dataType);
return $this->renderSuccess(compact('list'));
}
/**
* 订单详情信息
* @param $order_id
* @return array
* @throws \app\common\exception\BaseException
* @throws \think\exception\DbException
*/
public function detail($order_id)
{
$order = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
return $this->renderSuccess(['order' => $order]);
}
/**
* 取消订单
* @param $order_id
* @return array
* @throws \Exception
* @throws \app\common\exception\BaseException
* @throws \think\exception\DbException
*/
public function cancel($order_id)
{
$model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
if ($model->cancel()) {
return $this->renderSuccess();
}
return $this->renderError($model->getError());
}
/**
* 确认收货
* @param $order_id
* @return array
* @throws \app\common\exception\BaseException
* @throws \think\exception\DbException
*/
public function receipt($order_id)
{
$model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
if ($model->receipt()) {
return $this->renderSuccess();
}
return $this->renderError($model->getError());
}
}
... ...
... ... @@ -41,7 +41,7 @@ class Cart extends Model
public function getGoodsSpecTextAttr($value,$data){
$goods_spec_text = [];
if(!empty($data['goods_spec'])){
$goods_spec = json_decode($data['goods_spec'],true);
$goods_spec = json_decode(str_replace('\'','"',$data['goods_spec']),true);
foreach($goods_spec as $k => $v){
$spec = Spec::where('id',$k)->field('spec_name,spec_type')->find()->toArray();
$spec['spec_value'] = $spec['spec_type'] == '1' ? SpecValue::where('id',$v)->value('spec_value') : $v;
... ... @@ -216,6 +216,7 @@ class Cart extends Model
$tree = Tree::instance()->init(collection(Category::order('weigh desc,id desc')->select())->toArray(), 'pid');
foreach($new_cart_list as $goods_id => $v){
$goods_total_price = array_sum(array_column($v, 'goods_total_price'));
// 商品所属分类
$filter = [];
foreach(explode(',',$v[0]['goods']['category_ids']) as $v){
$id_arr = $tree->getParentsIds($v,true);
... ... @@ -223,7 +224,7 @@ class Cart extends Model
$filter[] = "find_in_set($val,category_ids)";
}
}
$where = implode(' or ',$filter);
$where = implode(' or ',$filter)." or category_ids = '' or category_ids = 0";
$coupon_id_arr = array_unique(
array_merge(
... ...
... ... @@ -77,4 +77,36 @@ class Order extends Model
Db::commit();
return true;
}
/**
* 用户中心订单列表
*/
public function getList($user_id, $type = 'all')
{
// 筛选条件
$filter = [];
// 订单数据类型
switch ($type) {
case 'all':
break;
case 'payment';
$filter['pay_status'] = 10;
break;
case 'delivery';
$filter['pay_status'] = 20;
$filter['delivery_status'] = 10;
break;
case 'received';
$filter['pay_status'] = 20;
$filter['delivery_status'] = 20;
$filter['receipt_status'] = 10;
break;
}
return $this->with(['goods.image'])
->where('user_id', '=', $user_id)
->where('order_status', '<>', 20)
->where($filter)
->order(['create_time' => 'desc'])
->select();
}
}
... ...