190 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			190 lines
		
	
	
		
			5.5 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 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')
 | 
						|
                ->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')->where('id',$id)->find();
 | 
						|
        $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('user_id', $id)
 | 
						|
                    ->where('YEAR(createtime)', $time)
 | 
						|
                    ->where('status',2)
 | 
						|
                    ->order('a.id', 'desc')
 | 
						|
                    ->select();
 | 
						|
        $array = [
 | 
						|
                'user' => $ret,
 | 
						|
                'date' => $date,
 | 
						|
            ];
 | 
						|
        if ($array) {
 | 
						|
            $this->success(__('Logged in successful'), $array);
 | 
						|
        } else {
 | 
						|
            $this->error($this->auth->getError());
 | 
						|
        }
 | 
						|
        
 | 
						|
    }
 | 
						|
}
 |