Maintain.php 2.7 KB
<?php


namespace app\index\controller;

use app\admin\model\Mainlist;
use app\common\controller\Frontend;
use app\index\model\Area;

class Maintain extends Frontend
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->view->assign('is_search', 0);
        $this->view->assign('is_active', 4);
        $this->view->assign('title', '产品维修');
    }

    public function index() {
        $maintain_model = new \app\index\model\Maintain();
        $maintainer_model = new \app\index\model\Maintainer();
        $maintain = $maintain_model->where('id',1)->find();
        $maintainer = $maintainer_model->select();
        //省市区三级联动
        $areaModel = new Area();
        $province = $areaModel->selectData(['pid'=>0,'level'=>1]);
        $province_id = !empty($province[0]['id']) ? $province[0]['id'] : 0;
        $city = $areaModel->selectData(['pid'=>$province_id,'level'=>2]);
        $city_id = !empty($city[0]['id']) ? $city[0]['id'] : 0;
        $county = $areaModel->selectData(['pid'=>$city_id,'level'=>3]);
        $this->assign('province',$province);
        $this->assign('city',$city);
        $this->assign('county',$county);
        $this->view->assign('maintain',$maintain);
        $this->view->assign('maintainer',$maintainer);
        return $this->view->fetch();
    }

    // 我要维修提交
    public function maintain_submit() {
        if($this->request->isAjax()) {
            $param = $this->request->param();
            $validate = new \think\Validate([
                'name' => 'require',
                'phone' => 'require|regex:^1\d{10}$',
                'province' => 'require',
                'city' => 'require',
                'address' => 'require',
                'content' => 'require',
            ]);
            $validate->message([
                'name.require' => '请输入姓名!',
                'phone.require' => '请输入手机号!',
                'phone.regex' => '手机号格式错误!',
                'province.require' => '请选择省!',
                'city.require' => '请选择市!',
                'county.require' => '请选择区/县!',
                'address.require' => '请输入详细地址!',
                'content.require' => '请输入需求描述!',
            ]);
            if (!$validate->check($param)) {
                $this->error($validate->getError());
            }
            $mainlist_model = new Mainlist();
            $result = $mainlist_model->save($param);
            if(!$result) {
                $this->error('提交失败');
            }
            $this->success('提交成功');
        }
    }
}