AdminServiceController.php 4.3 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\portal\controller;

use cmf\controller\AdminBaseController;
use app\portal\model\ServiceModel;
use app\portal\model\ServiceRuleModel;
use app\portal\model\CompanyModel;
use think\Db;
use think\db\Query;

class AdminServiceController extends AdminBaseController
{

    //列表页
    public function index(){
        $list = Db::name('service')
            ->where(function (Query $query) {
                $data = $this->request->param();
                if (!empty($data['name'])) {
                    $keyword = $data['name'];
                    $query->where('name', 'like', "%$keyword%");
                }

            })
            ->order("id desc")
            ->paginate(10,false,['query'=>request()->param()]);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }

    //新增页面
    public function add(){
        return $this->fetch();
    }

    //新增提交
    public function addPost(){
        $data      = $this->request->param();
        $result    = $this->validate($data, 'Service');
        if ($result !== true) {
            $this->error($result);
        }
//        $companyModel = new CompanyModel();
//        if(strpos($companyModel::HOST,$data['icon']) === false){
//            $data['icon'] = $companyModel::HOST.$data['icon'];
//        }
        $serviceModel = new ServiceModel();
        $data['create_time'] = time();
        $serviceModel->allowField(true)->save($data);
        $this->success("添加成功!", url("AdminService/index"));
    }

    //设置服务页面
    public function edit(){
        $id        = $this->request->param('id', 0, 'intval');
        $serviceModel = new ServiceModel();
        $service      = $serviceModel->get($id);
        $this->assign('post', $service);

        //查询企业名称
        $resCompany = $this->getCompany();
        $this->assign('company_list',$resCompany);

        //查询该服务权限的企业
        $rule = $this->getRule($id);
        $this->assign('rule',$rule);
        return $this->fetch();
    }

    //获取企业名称
    private function getCompany(){
        $res = Db::name('company')
            ->field('id,company_name')
            ->select()
            ->toArray();
        return $res;
    }

    //获取服务权限
    private function getRule($id){
        $res = Db::name('service_rule')
            ->where('s_id',$id)
            ->field('id,c_id')
            ->find();
        $arr['c_id'] = explode(',',trim($res['c_id'],','));
        $arr['id'] = $res['id'];
        return $arr;
    }

    //设置保存页面
    public function editPost(){
        $data      = $this->request->param();
//        $companyModel = new CompanyModel();
//        if(strpos($data['icon'],$companyModel::HOST) === false){
//            $data['icon'] = $companyModel::HOST.$data['icon'];
//        }
        $res['s_id'] = $data['id'];
        if(isset($data['c_id']) && !empty($data['c_id'])){
            $res['c_id'] = ','.implode($data['c_id'],',').',';
        }else{
            $res['c_id'] = '';
        }
        $res['create_time'] = time();
        $ruleModel = new ServiceRuleModel();
        if(isset($data['rule_id']) && !empty($data['rule_id'])){
            $ruleModel->where('id',$data['rule_id'])->update($res);
        }else{
            $ruleModel->allowField(true)->save($res);
        }
        $serviceModel = new ServiceModel();
        $serviceModel->where('id',$data['id'])->update(['english_name'=>$data['english_name'],'icon'=>$data['icon']]);
        $this->success("设置成功!", url("AdminService/index"));
    }
}