153 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			153 lines
		
	
	
		
			4.3 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 PersonalProfile extends Api
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    protected $noNeedLogin = ['*'];
							 | 
						||
| 
								 | 
							
								    protected $noNeedRight = '*';
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    protected $user_id = '';
							 | 
						||
| 
								 | 
							
								    protected $auth_group = '';
							 | 
						||
| 
								 | 
							
								    protected $user_group = '';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    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('用户不存在');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $this->user_id = $user['id'];
							 | 
						||
| 
								 | 
							
								        $this->auth_group = $user['auth_group_id'];
							 | 
						||
| 
								 | 
							
								        $this->user_group = $user['group_id'];
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    public function index(){
							 | 
						||
| 
								 | 
							
								        $auth =  Db::name('auth_group')->where('id', $this->auth_group)->find();
							 | 
						||
| 
								 | 
							
								        $group = Db::name('user_group')->where('id', $this->user_group)->find();
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        if(!$auth){
							 | 
						||
| 
								 | 
							
								          $this->error('角色无效');  
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $where = [];
							 | 
						||
| 
								 | 
							
								        if($auth['level'] == 2){
							 | 
						||
| 
								 | 
							
								            $where['id'] = $group['id'];
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $data = Db::name('user_group')->where($where)->select();
							 | 
						||
| 
								 | 
							
								         // 构建层级结构
							 | 
						||
| 
								 | 
							
								        $tree = $this->buildTree($data);
							 | 
						||
| 
								 | 
							
								        $this->success(__('Logged in successful'), $tree );
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 构建树结构
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param array $data 数据库查询结果数组
							 | 
						||
| 
								 | 
							
								     * @param int $parentId 父ID
							 | 
						||
| 
								 | 
							
								     * @return array
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    private function buildTree(array $data, $parentId = 0)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $tree = [];
							 | 
						||
| 
								 | 
							
								        foreach ($data as $item) {
							 | 
						||
| 
								 | 
							
								            if ($item['pid'] == $parentId) {
							 | 
						||
| 
								 | 
							
								                $children = $this->buildTree($data, $item['id']);
							 | 
						||
| 
								 | 
							
								                if ($children) {
							 | 
						||
| 
								 | 
							
								                    $item['children'] = $children;
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                $tree[] = $item;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return $tree;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								   public function find(){
							 | 
						||
| 
								 | 
							
								        $group_id = $this->request->post('group_id');
							 | 
						||
| 
								 | 
							
								        $page = $this->request->post('page', 1);
							 | 
						||
| 
								 | 
							
								        $size = $this->request->post('size', 10);
							 | 
						||
| 
								 | 
							
								        $nickname = $this->request->post('nickname');
							 | 
						||
| 
								 | 
							
								        $mobile = $this->request->post('mobile');
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        // 基础条件
							 | 
						||
| 
								 | 
							
								        $where = ['group_id' => $group_id];
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        // 模糊搜索处理
							 | 
						||
| 
								 | 
							
								        if ($nickname) {
							 | 
						||
| 
								 | 
							
								            $where['nickname'] = ['like', "%{$nickname}%"]; // 前后模糊匹配
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        if ($mobile) {
							 | 
						||
| 
								 | 
							
								            $where['mobile'] = ['like', "%{$mobile}%"];     // 前后模糊匹配
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								        // 执行查询
							 | 
						||
| 
								 | 
							
								        $userList = Db::name('user')
							 | 
						||
| 
								 | 
							
								            ->where($where)
							 | 
						||
| 
								 | 
							
								            ->page($page, $size)
							 | 
						||
| 
								 | 
							
								            ->order('id asc')
							 | 
						||
| 
								 | 
							
								            ->select();
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								        // 获取总数(需移除分页参数)
							 | 
						||
| 
								 | 
							
								        $count = Db::name('user')
							 | 
						||
| 
								 | 
							
								            ->where($where)
							 | 
						||
| 
								 | 
							
								            ->count();
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								        $data = [
							 | 
						||
| 
								 | 
							
								            'count' => $count,
							 | 
						||
| 
								 | 
							
								            'data'  => $userList,
							 | 
						||
| 
								 | 
							
								        ];
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								        // 业务逻辑判断
							 | 
						||
| 
								 | 
							
								        if ($count == 0) {
							 | 
						||
| 
								 | 
							
								            $this->error('未查询到相关数据');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								        $this->success(__('查询成功'), $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());
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |