Profile.php 2.6 KB
<?php

namespace app\expert\controller\general;

use app\common\controller\ExpertBase;
use app\common\model\User;
use app\expert\model\Experts;
use fast\Random;
use think\Exception;
use think\Session;
use think\Validate;
use app\common\model\Territory;

/**
 * 商家信息配置
 *
 * @icon fa fa-user
 */
class Profile extends ExpertBase
{
    protected $expert = null;
    public function _initialize()
    {
        $this->expert = $this->getSessionData();
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->view->assign("territoryList", Territory::getTerritoryList());
    }

    /**
     * 查看
     */
    public function index()
    {
        $expert = $this->expert;
        $expert = Experts::get($expert['id']);
        $expert['territory_id'] = !empty($expert['territory_id']) ? explode(',',$expert['territory_id']) : '';
//        dump($teacher);die;
        $this->view->assign('statusList',[0=>'待审核',1=>'审核通过',2=>'审核拒绝']);
        $this->view->assign('expert',$expert);
        return $this->view->fetch();
    }

    /**
     * 更新个人信息
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('email', 'nickname', 'password', 'avatar','image','territory_id'))
            ));
            unset($v);
            if (!Validate::is($params['email'], "email")) {
                $this->error(__("Please input correct email"));
            }
            if (isset($params['password'])) {
                if (!Validate::is($params['password'], "/^[\S]{6,16}$/")) {
                    $this->error(__("Please input correct password"));
                }

                $params['salt'] = Random::alnum();
                $params['password'] = md5(md5($params['password']) . $params['salt']);
            }
            if(!empty($params['territory_id'][0])) {
                $params['territory_id'] = implode(',',$params['territory_id']);
            }else{
                $params['territory_id'] = '';
            }
            $exist = User::where('email', $params['email'])->where('id', '<>', $this->expert['id'])->find();
            if ($exist) {
                $this->error(__("邮箱已存在"));
            }
            if ($params) {

                $experts = Experts::get($this->expert['id']);
                $experts->save($params);
                Session::set("expert", $experts->toArray());
                $this->success();
            }
            $this->error();
        }
        return;
    }
}