294 lines
		
	
	
		
			8.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			294 lines
		
	
	
		
			8.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace app\api\controller\backend;
 | 
						|
 | 
						|
use app\common\controller\Api;
 | 
						|
use app\common\library\Ems;
 | 
						|
use app\common\library\Sms;
 | 
						|
use fast\Random;
 | 
						|
use think\Config;
 | 
						|
use think\Validate;
 | 
						|
use think\Db;
 | 
						|
 | 
						|
/**
 | 
						|
 * 会员接口
 | 
						|
 */
 | 
						|
class User extends Api
 | 
						|
{
 | 
						|
    protected $noNeedLogin = ['*'];
 | 
						|
    protected $noNeedRight = '*';
 | 
						|
    
 | 
						|
    public function _initialize()
 | 
						|
    {
 | 
						|
        parent::_initialize();
 | 
						|
        $id = $this->request->header('Token');
 | 
						|
        if(!$id){
 | 
						|
            return $this->error('缺少参数');
 | 
						|
        }
 | 
						|
        $user = Db::name('user')->where('token', $id)->find();
 | 
						|
        if(!$user){
 | 
						|
            return $this->error('用户不存在','',99998);
 | 
						|
        }
 | 
						|
    }
 | 
						|
    
 | 
						|
    public function getEvaluation()
 | 
						|
    {
 | 
						|
        $id = $this->request->header('Token');
 | 
						|
        if(!$id){
 | 
						|
            return $this->error('缺少参数');
 | 
						|
        }
 | 
						|
        $group_id = Db::name('user')->where('token', $id)->find();
 | 
						|
        if(!$group_id){
 | 
						|
            return $this->error('未查询到该用户科室');
 | 
						|
        }
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * 用户列表
 | 
						|
     */
 | 
						|
    public function getUserData()
 | 
						|
    {   
 | 
						|
        $group_id  = $this->request->post('group_id');
 | 
						|
        $page = $this->request->post('page',1);
 | 
						|
        $size = $this->request->post('size',10);
 | 
						|
        $where = [];
 | 
						|
        if($group_id){
 | 
						|
            $id = Db::name('user_group')->where('id', $group_id)->find();
 | 
						|
            if($id['pid'] == 0){
 | 
						|
                $info = Db::name('user_group')->where('pid', $id['id'])->column('id');
 | 
						|
                array_push($info,intval($group_id));
 | 
						|
                $info_string = implode(',', array_map('intval', $info));
 | 
						|
                $where['a.group_id'] = ['in',$info_string];
 | 
						|
            }else{
 | 
						|
                $where['a.group_id'] = $group_id;
 | 
						|
            }
 | 
						|
           
 | 
						|
        }
 | 
						|
        // 从数据库中获取所有数据
 | 
						|
        $data = Db::name('user')
 | 
						|
                ->field('a.*,w.name as party_group_name,p.name as user_group_name')
 | 
						|
                ->alias('a')
 | 
						|
                ->join('party_group w','a.party_id = w.id','LEFT')
 | 
						|
                ->join('user_group p','a.group_id = p.id','LEFT')
 | 
						|
                ->where($where)
 | 
						|
                ->page($page,$size)
 | 
						|
                ->order('a.id asc')
 | 
						|
                ->select();
 | 
						|
        $count = Db::name('user')
 | 
						|
                ->field('a.*,w.name as party_group_name,p.name as user_group_name')
 | 
						|
                ->alias('a')
 | 
						|
                ->join('party_group w','a.party_id = w.id','LEFT')
 | 
						|
                ->join('user_group p','a.group_id = p.id','LEFT')
 | 
						|
                ->where($where)
 | 
						|
                ->order('a.id asc')
 | 
						|
                ->count();
 | 
						|
        $res = [
 | 
						|
                'count' => $count,
 | 
						|
                'data' => $data,
 | 
						|
                ];
 | 
						|
        return $this->success('请求成功',$res);
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * 单个用户查询
 | 
						|
     */
 | 
						|
    public function getUserFind()
 | 
						|
    {
 | 
						|
        $id = $this->request->post('id');
 | 
						|
        if (!$id) {
 | 
						|
            $this->error(__('Invalid parameters'));
 | 
						|
        }
 | 
						|
        $ret = Db::name('user')->where('id',$id)->find();
 | 
						|
        if ($ret) {
 | 
						|
            $this->success(__('Logged in successful'), $ret);
 | 
						|
        } else {
 | 
						|
            $this->error($this->auth->getError());
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public function create()
 | 
						|
    {
 | 
						|
        $data = $this->request->post();
 | 
						|
        if($data['password']){
 | 
						|
            $data['password'] = md5($data['password']);
 | 
						|
        }
 | 
						|
        $data['createtime'] = date('Y-m-d H:i:s');
 | 
						|
        $data['updatetime'] = date('Y-m-d H:i:s');
 | 
						|
        $result = Db::name('user')->insert($data);
 | 
						|
        if ($result) {
 | 
						|
            return $this->success('添加成功',$result);
 | 
						|
        } else {
 | 
						|
            return $this->error('添加失败',$result);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 更新记录
 | 
						|
     *
 | 
						|
     * @param Request $request
 | 
						|
     * @param int $id
 | 
						|
     * @return \think\Response
 | 
						|
     */
 | 
						|
    public function update()
 | 
						|
    {   
 | 
						|
        $id = $this->request->post('id');
 | 
						|
        $data = $this->request->post();
 | 
						|
        if($data['password']){
 | 
						|
            $data['password'] = md5($data['password']);
 | 
						|
        }
 | 
						|
        if(!$data['password']){
 | 
						|
            unset($data['password']);
 | 
						|
        }
 | 
						|
        $data['updatetime'] = date('Y-m-d H:i:s');
 | 
						|
        $result = Db::name('user')->where('id', $id)->strict(false)->update($data);
 | 
						|
        if ($result) {
 | 
						|
            return $this->success('更新成功',$result);
 | 
						|
        } else {
 | 
						|
            return $this->error('更新失败',$result);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 删除记录
 | 
						|
     *
 | 
						|
     * @param int $id
 | 
						|
     * @return \think\Response
 | 
						|
     */
 | 
						|
    public function delete()
 | 
						|
    {   
 | 
						|
        $id = $this->request->post('id');
 | 
						|
        if(!$id){
 | 
						|
            return $this->error('缺少参数');
 | 
						|
        }
 | 
						|
        $result = Db::name('user')->delete($id);
 | 
						|
        if ($result) {
 | 
						|
            return $this->success('删除成功',$result);
 | 
						|
        } else {
 | 
						|
            return $this->error('删除失败',$result);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取所有医生的姓名和id
 | 
						|
     */
 | 
						|
    public function getDoctorName()
 | 
						|
    {
 | 
						|
        $data = Db::name('user')
 | 
						|
                ->field('id,nickname,code')
 | 
						|
                ->where('id', '<>', 1) // 添加条件 id 不等于 1
 | 
						|
                ->select();
 | 
						|
        if ($data) {
 | 
						|
            return $this->success('请求成功',$data);
 | 
						|
        } else {
 | 
						|
            return $this->error('请求失败',$data);
 | 
						|
        }
 | 
						|
    }
 | 
						|
    
 | 
						|
    public function personalProfile(){
 | 
						|
        $id = $this->request->post('user_id');
 | 
						|
        $time = $this->request->post('time', date('Y'));
 | 
						|
        if (!$id) {
 | 
						|
            $this->error(__('Invalid parameters'));
 | 
						|
        }
 | 
						|
        $ret = Db::name('user')
 | 
						|
                ->field('a.*, u.name as gorup_name, d.name as party_name')
 | 
						|
                ->alias('a')
 | 
						|
                ->join('user_group u','a.group_id = u.id','LEFT')
 | 
						|
                ->join('party_group d','a.party_id = d.id','LEFT')
 | 
						|
                ->where('a.id',$id)
 | 
						|
                ->find();
 | 
						|
       $startDate = $time . '-01-01';
 | 
						|
        $endDate = $time . '-12-31';
 | 
						|
         
 | 
						|
        $date = Db::name('addition_and_subtraction_records')
 | 
						|
            ->field('a.*,p.project_name,p.scoring_criteria')
 | 
						|
            ->alias('a')
 | 
						|
            ->join('plus_minus_scoring p','a.assessment_project = p.id','LEFT')
 | 
						|
            ->where('a.user_id', $id)
 | 
						|
            ->where('a.status', 2)
 | 
						|
            ->whereBetween('a.createtime', [$startDate, $endDate]) // 使用日期范围
 | 
						|
            ->order('a.id', 'desc')
 | 
						|
            ->select();
 | 
						|
 | 
						|
        if(!$date){
 | 
						|
            $date = [];
 | 
						|
        }
 | 
						|
        $array = [
 | 
						|
                'user' => $ret,
 | 
						|
                'date' => $date,
 | 
						|
            ];
 | 
						|
        if ($array) {
 | 
						|
            $this->success(__('Logged in successful'), $array);
 | 
						|
        } else {
 | 
						|
            $this->error($this->auth->getError());
 | 
						|
        }
 | 
						|
        
 | 
						|
    }
 | 
						|
    
 | 
						|
    
 | 
						|
     /**
 | 
						|
     * 单个用户查询
 | 
						|
     */
 | 
						|
    public function getH5UserFind()
 | 
						|
    {
 | 
						|
        $id = $this->request->post('user_id');
 | 
						|
        if (!$id) {
 | 
						|
            $this->error(__('Invalid parameters'));
 | 
						|
        }
 | 
						|
        $ret = Db::name('user')
 | 
						|
                ->field('a.nickname,a.mobile,a.position,a.certificate_code,a.license_code,a.license_image,p.name as user_group_name')
 | 
						|
                ->alias('a')
 | 
						|
                ->join('user_group p','a.group_id = p.id','LEFT')
 | 
						|
                ->where('a.id',$id)
 | 
						|
                ->find();
 | 
						|
        if ($ret) {
 | 
						|
            $this->success(__('Logged in successful'), $ret);
 | 
						|
        } else {
 | 
						|
            $this->error($this->auth->getError());
 | 
						|
        }
 | 
						|
    }
 | 
						|
    
 | 
						|
    //修改密码
 | 
						|
    public function updatePassword()
 | 
						|
    {
 | 
						|
        $id = $this->request->post('id');
 | 
						|
        $old_password = $this->request->post('old_password');
 | 
						|
        $new_password = $this->request->post('new_password');
 | 
						|
        $confirm_password = $this->request->post('confirm_password');
 | 
						|
        if (!$id) {
 | 
						|
            $this->error(__('Invalid parameters'));
 | 
						|
        }
 | 
						|
        if (!$old_password) {
 | 
						|
            $this->error(__('Invalid parameters'));
 | 
						|
        }
 | 
						|
        if (!$new_password) {
 | 
						|
            $this->error(__('Invalid parameters'));
 | 
						|
        }
 | 
						|
        if (!$confirm_password) {
 | 
						|
            $this->error(__('Invalid parameters'));
 | 
						|
        }
 | 
						|
        if($new_password != $confirm_password){
 | 
						|
            $this->error(__('两次输入密码不一致'));
 | 
						|
        }
 | 
						|
        
 | 
						|
        $ret = Db::name('user')
 | 
						|
                ->where('id',$id)
 | 
						|
                ->where('password', md5($old_password))
 | 
						|
                ->find();
 | 
						|
        if(!$ret){
 | 
						|
             $this->error(__('该用户不存在'));
 | 
						|
        }
 | 
						|
        if($ret['password'] == md5($new_password)){
 | 
						|
            $this->error(__('新旧密码不能一致'));
 | 
						|
        }
 | 
						|
        $data = [
 | 
						|
                'password' => md5($new_password),
 | 
						|
            ];
 | 
						|
        $update = $result = Db::name('user')->where('id', $id)->strict(false)->update($data);
 | 
						|
        if ($update) {
 | 
						|
            $this->success(__('修改成功'), $update);
 | 
						|
        } else {
 | 
						|
            $this->error($this->auth->getError());
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |