Staff.php 11.0 KB
<?php

namespace app\api\controller\inspection;

use app\admin\model\inspection\Depart;
use app\admin\model\inspection\Plan;
use app\common\controller\Api;
use fast\Tree;
use think\Db;

/**
 * 员工接口
 */
class Staff extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['login', 'screen_mng_login'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    public $staffInfo;

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        if ($this->request->action() != "login" && $this->request->action() != "screen_mng_login") {
            $staffModel = new \app\admin\model\inspection\Staff();
            $staffInfo = $staffModel->where(['user_id' => $this->auth->id])->find();
            if (empty($staffInfo)) {
                $this->error('当前员工账号异常');
            }

            $this->staffInfo = $staffInfo;
        }

    }


    /**
     * 获取员工详情
     */
    public function info()
    {
        $staffInfo = $this->staffInfo;
        $departModel = new Depart();
        $departInfo = $departModel->where(['id' => $this->staffInfo['depart_id']])->find();
        $staffInfo['depart_name'] = empty($departInfo['depart_name']) ? '' : $departInfo['depart_name'];
        $staffInfo['reservoir_name'] = \think\Db::name('reservoir_list')->where('id', $staffInfo['reservoir_id'])->value('name');

        if ($this->staffInfo['depart_id'] <= 28) {
            $staffInfo['is_mng_leader'] = 1;
        } else {
            $staffInfo['is_mng_leader'] = 0;
        }
        $staffInfo['avatar'] = $this->auth->avatar;
        if (substr($this->auth->avatar, 0, 4) != 'data') {
            $staffInfo['avatar'] = 'https://qiniu.ynzhsk.cn' . $this->auth->avatar;
        }
        $this->success("请求成功", $staffInfo);
    }

    /**
     * 员工登录
     *
     * @param string $account 账号
     * @param string $password 密码
     */
    public function login()
    {

        $account = $this->request->request('account');
        $password = $this->request->request('password');
        $openid = $this->request->request('openid');

        //unipush用于推送的客户端id(2022.03.16 kevin新增)
        $clientid = $this->request->request("clientid", "");

        // $userInfo = [];
        // $staffInfo = [];
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        // if($openid){
        //     $userModel = new \app\admin\model\User();
        //     $userInfo = $userModel->where(['openid'=>$openid])->find();
        //     $nologin = true;
        // }


        $staffModel = new \app\admin\model\inspection\Staff();
        $staffInfo = $staffModel->where(['is_screen_mng' => '0', "is_regulatory_mng" => '0'])
            ->where("staff_code = '$account' or mobile = '$account'")
            ->find();

        if ($staffInfo) {
            $userModel = new \app\admin\model\User();
            $userInfo = $userModel->where(['id' => $staffInfo['user_id']])->find();
            $departModel = new Depart();
            $departInfo = $departModel->where(['id' => $staffInfo->depart_id])->find();
            if (empty($userInfo)) {
                $this->error('当前员工登录信息异常');
            }
            if ($userInfo->password != $this->auth->getEncryptPassword($password, $userInfo->salt)) {
                $this->error('登录密码不正确');
                return false;
            }

            //kevin 更新客户端唯一标识
            if (!empty($clientid) && $clientid != $userInfo->clientid) {
                $userModel->where("id", $staffInfo['user_id'])->update(["clientid" => $clientid]);
                $userInfo = $userModel->where(['id' => $staffInfo['user_id']])->find();
            }
            $this->auth->direct($userInfo->id);
            $userInfo->openid = $openid;
            $userInfo->save();

            $user = $this->auth->getUserinfo();
            if ('data' != substr($this->auth->getUserinfo()['avatar'], 0, 4)) {
                $user['avatar'] = 'https://qiniu.ynzhsk.cn' . $this->auth->getUserinfo()['avatar'];
            }
            $data = ['userinfo' => $user, 'depart_name' => $departInfo->depart_name, 'duty' => $staffInfo->duty, 'staff' => $staffInfo, 'admin_group_id' => "15288413396"];
            $this->success(__('登录成功!'), $data);
        } else {
            $this->error('当前员工不存在');
        }
    }

    /**
     * 大屏管理员登录
     *
     * @param string $account 账号
     * @param string $password 密码
     */
    public function screen_mng_login()
    {

        $account = $this->request->param('account');
        $password = $this->request->param('password');
        $openid = $this->request->param('openid');

        //unipush用于推送的客户端id(2022.03.16 kevin新增)
        $clientid = $this->request->param("clientid", "");

        // $userInfo = [];
        // $staffInfo = [];
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        // if($openid){
        //     $userModel = new \app\admin\model\User();
        //     $userInfo = $userModel->where(['openid'=>$openid])->find();
        //     $nologin = true;
        // }


        $staffModel = new \app\admin\model\inspection\Staff();
        $staffInfo = $staffModel->where(['is_screen_mng' => '1', "is_regulatory_mng" => '0'])
            ->where("staff_code = '$account' or mobile = '$account'")
            ->find();

        //$staffModel->where(['staff_code' => $account, 'is_screen_mng' => '1'])->find();

        if ($staffInfo) {
            $userModel = new \app\admin\model\User();
            $userInfo = $userModel->where(['id' => $staffInfo['user_id']])->find();
            $departModel = new Depart();
            $departInfo = $departModel->where(['id' => $staffInfo->depart_id])->find();
            if (empty($userInfo)) {
                $this->error('大屏管理员登录信息异常');
            }
            if ($userInfo->password != $this->auth->getEncryptPassword($password, $userInfo->salt)) {
                $this->error('登录密码不正确');
                return false;
            }

            //kevin 更新客户端唯一标识
            if (!empty($clientid) && $clientid != $userInfo->clientid) {
                $userModel->where("id", $staffInfo['user_id'])->update(["clientid" => $clientid]);
                $userInfo = $userModel->where(['id' => $staffInfo['user_id']])->find();
            }
            $this->auth->direct($userInfo->id);
            $userInfo->openid = $openid;
            $userInfo->save();

            $user = $this->auth->getUserinfo();
            if ('data' != substr($this->auth->getUserinfo()['avatar'], 0, 4)) {
                $user['avatar'] = 'https://qiniu.ynzhsk.cn' . $this->auth->getUserinfo()['avatar'];
            }
            $data = ['userinfo' => $user, 'depart_name' => $departInfo->depart_name, 'duty' => $staffInfo->duty, 'staff' => $staffInfo];
            $this->success(__('登录成功'), $data);
        } else {
            $this->error('大屏管理员账号不存在');
        }
    }

    /**
     * 重置密码
     *
     * @param string $mobile 手机号
     * @param string $newpassword 新密码
     * @param string $captcha 验证码
     */
    public function resetpwd()
    {
        $oldpassword = $this->request->request("oldpassword");
        $newpassword = $this->request->request("newpassword");

        if (empty($newpassword)) {
            $this->error('新密码不能为空');
        }

        $userModel = new \app\admin\model\User();
        $userInfo = $userModel->where(['id' => $this->auth->id])->find();
        if (empty($userInfo)) {
            $this->error('当前登录信息异常');
        }
        if ($userInfo->password != $this->auth->getEncryptPassword($oldpassword, $userInfo->salt)) {
            $this->error('原始登录密码不正确');
            return false;
        }

        $ret = $this->auth->changepwd($newpassword, '', true);
        if ($ret) {
            $this->success(__('密码重置成功'));
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 8. 发布事项获取员工列表
     */
    public function getStaffList()
    {

        $curPage = $this->request->param("curPage", 1);
        $pageSize = $this->request->param("pageSize", 99999);
        $pageSize = $curPage == 1 ? $pageSize - 1 : $pageSize;
        $depart_ids = [];
        $data = [];
        $count = 0;
        //查询当前用户所属部门id
        $id = $this->auth->id;
        $depart_id = Db::name("inspection_staff")->where("user_id", $id)->value("depart_id");

        if (!empty($depart_id)) {
            //获取下级所有子部门ID
            $tree = Tree::instance();
            $departModel = new \app\admin\model\inspection\Depart();
            $tree->init(collection($departModel->order('weigh asc,id asc')->select())->toArray(), 'pid');
            $depart_ids = $tree->getChildrenIds($depart_id, true);
        }
        $where = [];
        if (!empty($depart_ids)) {
            $where["s.depart_id"] = ["IN", $depart_ids];
        }
        $where["s.is_regulatory_mng"] = ["=", "1"];
        $where["s.is_screen_mng"] = ["=", "0"];

        $data = Db::name("inspection_staff")->alias("s")
            ->join("user u", "u.id=s.user_id", "LEFT")
            ->join("reservoir_list l", "l.id=s.reservoir_id", "LEFT")
            ->where($where)
            ->order("s.id desc")
            ->field("s.id,s.user_id,s.staff_name,s.staff_code,s.mobile,u.avatar,l.name as reservoir_name")
            ->page($curPage, $pageSize)
            ->select();
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $data[$k]['avatar'] = $v['avatar'] ? cdnurl($v['avatar'], true) : letter_avatar($v['staff_name']);
            }
        }
        $count = count($data);
        if ($curPage == 1) {
            $admin_info = [
                "id" => 0,
                "user_id" => 0,
                "staff_name" => "总后台管理员",
                "avatar" => letter_avatar("总后台管理员"),
            ];
            array_unshift($data, $admin_info);
            $count = $count + 1;
        }

        $this->success("数据获取成功", ["total" => $count, "rows" => $data]);

    }

    /**
     * 2.巡检人员上报经纬度
     */
    public function report_lanlnt()
    {
        $param = $this->request->param();
        if (empty($param['lat']) || empty($param['lng'])) {
            $this->error("经纬度不能为空");
        }
        $insert_data = [
            "inspection_staff_id" => $this->staffInfo['id'],
            "person_type" => "1",//1巡检人员
            "lat" => $param['lat'],
            "lng" => $param['lng'],
            "createtime" => time(),
        ];
        $res = Db::name("inspection_staff_locus")->insertGetId($insert_data);
        if ($res) {
            $this->success("位置上报成功", $res);
        } else {
            $this->error("位置上报失败");
        }
    }

}