Viporder.php 15.9 KB
<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Form;
use think\Db;
use think\Validate;
use fast\Random;

/**
 * 开通会员订单管理
 *
 * @icon fa fa-circle-o
 */
class Viporder extends Backend
{
    
    /**
     * Viporder模型对象
     * @var \app\admin\model\Viporder
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Viporder;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            foreach ($list as &$v){
                $v['province_ids'] = explode(',',$v['province_ids']);
                $v['province_ids'] = Db::name('province')->whereIn('id',$v['province_ids'])->column('name');
                if($v['vip_type'] == 2){
                    $v['province_ids'] = '全部';
                }
            }


            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    if($params['audit'] == 2){
                        if(empty($params['property'])){
                            $this->error('请选择店铺属性');
                        }
                        $user = Db::name('user')->where(['id'=>$row['user_id']])->find();
                        //查出审核信息
                        if($row['type'] == 1){
                            $list = Db::name('minestore')
                                ->where('user_id',$row['user_id'])
                                ->find();
                        }else{
                            //企业店铺
                            $list = Db::name('firmstores')
                                ->where('user_id',$row['user_id'])
                                ->find();
                        }
                        $data1 = Db::name('admin')->where(['username'=>$params['username']])->find();
                        if(!empty($data1)){
                            $this->error('账号不能重复');
                        }

                        //创建后台用户参数
                        $admin['salt'] = Random::alnum();
                        $admin['pwd'] = $params['password'];
                        $admin['password'] = md5(md5($params['password']) .  $admin['salt']);
                        $admin['nickname'] = $params['name'];
                        $admin['username'] = $params['username'];
                        $admin['phone'] = $list['phone'];
                        $admin['avatar'] = '/assets/img/avatar.png';
                        $admin['createtime'] = time();
                        $admin['updatetime'] = time();
                        $b = Db::name('admin')->insertGetId($admin);    //创建后台账号
                        $c = Db::name('auth_group_access')->insert(['uid'=>$b,'group_id'=>6]);

                        //创建店铺参数
                        $store['user_id'] = $row['user_id'];//用户id
                        $store['name'] = $params['name'];//店铺名称
                        $store['type'] = $row['type'];//店铺分类(1个人店铺2企业店铺)
                        $store['admin_id'] = $b;//店铺后台用户id
                        $store['property'] = implode(',',$params['property']);//店铺属性
                        $store['content'] = $params['content'];//店铺简介
                        $store['thumbnail'] = $params['thumbnail'];//店铺图片
                        $store['money'] = $params['money'];//店铺免运费金额
                        $store['freight'] = $params['freight'];//运费
                        $store['phone'] = $params['phone'];//联系电话
                        $store['address'] = $params['address'];//店铺店址
                        $store['lng'] = $params['lng'];//经度
                        $store['lat'] = $params['lat'];//纬度
//                        $store['province_id'] = $params['province_id'];//省份
//                        $store['city_id'] = $params['city_id'];//城市
//                        $store['county_id'] = $params['county_id'];//区/县
                        $store['viptype'] = $row['vip_type'];//店铺会员类型(1普通会员2超级会员)
                        $store['province_ids'] = $row['province_ids'];//开通会员的省份id
//                        $store['expiration'] = $viporder['expirationtime'];//会员到期时间
                        $store['vip_passtime'] = $user['vip_passtime'];
                        $store['svip_passtime'] = $user['svip_passtime'];
                        $store['createtime'] = time();//创建时间
                        $store_id = Db::name('store')->insertGetId($store);//创建店铺
                        Db::name('admin')->where(['id'=>$b])->update(['store_id'=>$store_id]);
                    }
                    $result = Db::name('viporder')->where(['id'=>$ids])->update(['audit'=>$params['audit']]);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $data = Db::name('firmstores')->where(['user_id'=>$row['user_id']])->find();
        if(empty($data)){
            $data = Db::name('minestore')->where(['user_id'=>$row['user_id']])->find();
        }
        $this->view->assign("data",$data);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit2($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;

                if (!Validate::is($params['password'], '\S{6,16}')) {
                    $this->error(__("Please input correct password"));
                }
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    if($params['audit'] == 1){
                        if(empty($params['property'])){
                            $this->error('请选择店铺属性');
                        }
                        //查询出该订单信息
                        $viporder = Db::name('viporder')
                            ->where('id',$ids)
                            ->find();
                        $user = Db::name('user')->where(['id'=>$viporder['user_id']])->find();
                        //查出审核信息
                        if($viporder['type'] == 1){
                            $list = Db::name('minestore')
                                ->where('id',$viporder['stores_id'])
                                ->where('user_id',$viporder['user_id'])
                                ->find();
                        }else{
                            //企业店铺
                            $list = Db::name('firmstores')
                                ->where('id',$viporder['stores_id'])
                                ->where('user_id',$viporder['user_id'])
                                ->find();
                        }
                        $data1 = Db::name('admin')->where(['username'=>$params['username']])->find();
                        if(!empty($data1)){
                            $this->error('账号不能重复');
                        }

                        //创建后台用户参数
                        $admin['salt'] = Random::alnum();
                        $admin['pwd'] = $params['password'];
                        $admin['password'] = md5(md5($params['password']) .  $admin['salt']);
                        $admin['nickname'] = $params['name'];
                        $admin['username'] = $params['username'];
                        $admin['phone'] = $list['phone'];
                        $admin['avatar'] = '/assets/img/avatar.png';
                        $admin['createtime'] = time();
                        $admin['updatetime'] = time();
                        $b = Db::name('admin')->insertGetId($admin);    //创建后台账号
                        $c = Db::name('auth_group_access')->insert(['uid'=>$b,'group_id'=>6]);

                        //创建店铺参数
                        $store['user_id'] = $viporder['user_id'];           //用户id
                        $store['name'] = $params['name'];                   //店铺名称
                        $store['type'] = $viporder['type'];                 //店铺分类(1个人店铺2企业店铺)
                        $store['admin_id'] = $b;                            //店铺后台用户id
                        $store['property'] = implode(',',$params['property']);           //店铺属性
//                    $store['content'] = '';                             //店铺简介
//                    $store['thumbnail'] = '';                           //店铺图片
//                    $store['money'] = '';                               //店铺免运费金额
//                    $store['phone'] = '';                               //联系电话
                        $store['address'] = $list['address'];               //店铺店址
                        $store['lng'] = $list['lng'];                       //经度
                        $store['lat'] = $list['lat'];                       //纬度
                        $store['viptype'] = $viporder['vip_type'];          //店铺会员类型(1普通会员2超级会员)
                        $store['province_ids'] = $viporder['province_ids']; //开通会员的省份id
//                    $store['expiration'] = $viporder['expirationtime']; //会员到期时间
                        $store['vip_passtime'] = $user['vip_passtime'];
                        $store['svip_passtime'] = $user['svip_passtime'];
                        $store['createtime'] = time();                      //创建时间
                        $store_id = Db::name('store')->insertGetId($store);    //创建店铺
                        Db::name('admin')->where(['id'=>$b])->update(['store_id'=>$store_id]);
                    }
                    $result = Db::name('viporder')->where(['id'=>$ids])->update(['audit'=>$params['audit']]);
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    Db::commit();
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    //审核信息详情
    public function detatil($ids){
        $data = Db::name('viporder')
            ->where('id',$ids)
            ->find();
        //个人店铺
        if($data['type'] == 1){
            $list = Db::name('minestore')
//                ->where('id',$data['stores_id'])
                ->where('user_id',$data['user_id'])
                ->find();
            $list['thumbnail'] = explode(',',$list['thumbnail']);
            $list['card_images'] = explode(',',$list['card_images']);
            $this->assign('data',$list);
            return $this->view->fetch();
        }else{
            //企业店铺
            $list = Db::name('firmstores')
//                ->where('id',$data['stores_id'])
                ->where('user_id',$data['user_id'])
                ->find();

            $list['business'] = explode(',',$list['business']);
            $this->assign('data',$list);
            return $this->view->fetch('detatil1');
        }
    }

    //店铺信息详情
    public function storedetatil($ids){
        $user = Db::name('viporder')
            ->where('id',$ids)
            ->find();
        $data = Db::name('store')
            ->where('user_id',$user['user_id'])
            ->find();
        $admin = Db::name('admin')
            ->where('id',$data['admin_id'])
            ->find();
        $data['username'] = $admin['username'];
        $data['pwd'] = $admin['pwd'];
        $this->assign('data',$data);
        return $this->view->fetch();
    }

}