审查视图

app/portal/controller/OrderController.php 6.8 KB
王晓刚 authored
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/5
 * Time: 19:55
 */

namespace app\portal\controller;

王晓刚 authored
12
use app\portal\model\AddressModel;
13 14
use app\portal\model\IndentGoodsModel;
use app\portal\model\IndentModel;
王晓刚 authored
15
use cmf\controller\WeChatBaseController;
4  
anyv authored
16
use think\Db;
王晓刚 authored
17 18 19

class OrderController extends WeChatBaseController
{
20 21 22 23 24 25 26
    /**
     * 全部订单
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
王晓刚 authored
27
    public function get_all(){
28 29 30 31
        $state = $this->request->param('state',0,'intval');
//        if(!empty($state)){
//            $where1['state'] = ['eq',$state];
//        }
王晓刚 authored
32
        $user_id = cmf_get_current_user_id();
33 34 35
        $indentModel = new IndentModel();
        $where1['uid'] = ['eq',$user_id];
        $data = $indentModel->selectData($where1);
王晓刚 authored
36
        if(!empty($data)){
37
            $indentGoodsModel = new IndentGoodsModel();
王晓刚 authored
38
            foreach($data as $key => $vo){
39 40 41
                $where2['indent_id'] = ['eq',$vo['id']];
                $indentGoods = $indentGoodsModel->selectData($where2);
                $data[$key]['indent_goods'] = $indentGoods;
42 43 44 45 46 47 48 49
                if($vo['is_courier'] == 1){
                    if(empty($vo['name'])){
                        unset($data[$key]);
                    }
                }else if($vo['is_courier'] == 2){
                    if(empty($vo['indent_address'])){
                        unset($data[$key]);
                    }
50 51
                }else{
                    unset($data[$key]);
52
                }
王晓刚 authored
53 54
            }
        }
55 56 57 58 59 60 61 62 63 64 65 66 67
        $this->assign('data',$data);
        $this->assign('state',$state);
        return $this->fetch();
    }
    public function get_one(){
        $id = $this->request->param('id',0,'intval');
        if(empty($id)){
            $this->error('缺少必要参数','','','');
        }
        $user_id = cmf_get_current_user_id();
        $where1['uid'] = ['eq',$user_id];
        $where1['id'] = ['eq',$id];
        $indentModel = new IndentModel();
王晓刚 authored
68
        $data = $indentModel->findData($where1)->toArray();
69 70 71 72 73 74
        if(empty($data)){
            $this->error('未查询到该订单','','','');
        }
        $where2['indent_id'] = ['eq',$data['id']];
        $indentGoodsModel = new IndentGoodsModel();
        $indentGoods = $indentGoodsModel->selectData($where2);
王晓刚 authored
75
        $addressModle = new AddressModel();
王晓刚 authored
76
        $address = $addressModle->findData(['id'=>$data['indent_address']]);
王晓刚 authored
77
        $data['address'] = $address;
78 79
        $data['indent_goods'] = $indentGoods;
        $this->assign('data',$data);
4  
anyv authored
80 81 82
        //显示售后咨询手机号
        $phone = Db::name('token') -> where('id',3) -> find();
        $this -> assign('phone',$phone);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        return $this->fetch();
    }
    public function cancel_order(){
        $id = $this->request->param('id',0,'intval');
        if(empty($id)){
            $this->error('缺少必要参数','','','');
        }
        $where1['id'] = ['eq',$id];
        $indentModel = new IndentModel();
        $data = $indentModel->findData($where1);
        if(empty($data)){
            $this->error('缺少必要参数','','','');
        }
        if($data['state'] != 4){
            $this->error('订单不是待支付状态','','','');
        }
        $result = $indentModel->deleteData($where1);
        if(empty($result)){
            $this->error('sql执行失败','','','');
        }
王晓刚 authored
103
        $this->success('SUCCESS');
王晓刚 authored
104
    }
王晓刚 authored
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    public function delivery(){
        $id = $this->request->param('indent_id',0,'intval');
        if(empty($id)){
            $this->error('缺少必要参数');
        }
        $where1['id'] = ['eq',$id];
        $indentModel = new IndentModel();
        $data = $indentModel->findData($where1);
        if(empty($data)){
            $this->error('缺少必要参数','','','');
        }
        $where2['indent_id'] = ['eq',$data['id']];
        $indentGoodsModel = new IndentGoodsModel();
        $indentGoods = $indentGoodsModel->selectData($where2);
        $data['indent_goods'] = $indentGoods;
        $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);
        if($data['state'] != 5){
            $this->error('订单不是待收货状态','','','');
        }
        $result = $indentModel->updateData($where1,['state'=>3]);
        if(empty($result)){
            $this->error('sql执行失败','','','');
        }
        $this->success('SUCCESS','',$data);
    }
4  
anyv authored
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

    /**
     * 判断用户去支付是否为上级业务员所卖书籍
     */
    public function is_my_salesman(){

        $uid = cmf_get_current_user_id();
        $indent_id = $_POST['indent_id'];
        //获取这条订单
        $indent = Db::name('indent') -> where('id',$indent_id) -> find();
        //如果这条订单为平台订单
        if($indent['indent_type'] == 1){
            //获取这条订单下的所有商品
            $pingtai_goods = Db::name('indent_goods') -> where('indent_id',$indent_id) -> select();
            //循环判断商品是否存在是否下架
            foreach ($pingtai_goods as $key => $val){
                $pt_goods = Db::name('goods') -> where('id',$val['goods_id']) -> find();
                //判断商品是否存在
                if(empty($pt_goods)){
                    return false;
                }
                //判断商品是否下架
                if($pt_goods['is_out'] == 0){
                    return false;
                }
            }
            return true;
        }
        //获取当前用户身份
        $my_user = Db::name('my_user') -> where('uid',$uid) -> find();
        //判断用户身份 获取上级业务员
        if($my_user['status'] == 3){
            //当为老师时
            $salesman = Db::name('my_user') -> where('id',$my_user['pid']) -> find();
            if(empty($salesman)){
               return false;
            }
        }
        if($my_user['status'] == 4){
            //当为学生时
            $teacher = Db::name('my_user') -> where('id',$my_user['pid']) -> find();
            if(empty($teacher)){
                return false;
            }
            $salesman = Db::name('my_user') -> where('id',$teacher['pid']) -> find();
        }

        //获取订单下的商品
        $indent_goods = Db::name('indent_goods') -> where('indent_id',$indent_id) -> select();
        foreach ($indent_goods as $key => $val){
            $goods = Db::name('goods') -> where('id',$val['goods_id']) -> find();
            //判断商品是否存在
            if(empty($goods)){
                return false;
            }
            //判断商品是否下架
            if($goods['is_out'] == 0){
                return false;
            }
            if($salesman['uid'] != $goods['uid']){
                return false;
            }

        }
        return true;

    }
































王晓刚 authored
230
}