474 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			474 lines
		
	
	
		
			15 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;
 | 
						||
use think\Model;
 | 
						||
use fast\Tree;
 | 
						||
/**
 | 
						||
 * 调查问卷接口
 | 
						||
 */
 | 
						||
class Questionnaire extends Api
 | 
						||
{
 | 
						||
    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
 | 
						||
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
 | 
						||
    //如果接口已经设置无需登录,那也就无需鉴权了
 | 
						||
    //
 | 
						||
    // 无需登录的接口,*表示全部
 | 
						||
    protected $noNeedLogin = ['*','searchById', 'search'];
 | 
						||
    // 无需鉴权的接口,*表示全部
 | 
						||
    protected $noNeedRight = ['test2'];
 | 
						||
 | 
						||
    protected $dataLimit = 'personal';
 | 
						||
 | 
						||
      /**
 | 
						||
     * 
 | 
						||
     * 问卷列表
 | 
						||
     * 
 | 
						||
    */
 | 
						||
    public function questionnaireIndex(){
 | 
						||
        $title = input('questionnaire');
 | 
						||
        $page = input("page",1); 
 | 
						||
        $size = input("size",10); 
 | 
						||
        $Token = $this->request->header('Token');
 | 
						||
        if(!$Token){
 | 
						||
            return $this->error('缺少参数');
 | 
						||
        }
 | 
						||
        $user_id = Db::name('user')->where('token', $Token)->value('id');
 | 
						||
        if(!$user_id){
 | 
						||
            return $this->error('该用户不存在');
 | 
						||
        }
 | 
						||
        
 | 
						||
        $where = [];
 | 
						||
        if($title){
 | 
						||
           $where['title'] = ['like',"%$title%"];
 | 
						||
        }
 | 
						||
        $data = Db::name('questionnaire')
 | 
						||
                    ->where($where)
 | 
						||
                    ->order('releasetime desc')
 | 
						||
                    ->page($page,$size)
 | 
						||
                    ->select();
 | 
						||
        foreach ($data as $key => $val){
 | 
						||
            $res = Db::name('questionnaire_log')
 | 
						||
                    ->where('questionnaire_id', $val['id'])
 | 
						||
                    ->where('user_id', $user_id)
 | 
						||
                    ->select();
 | 
						||
            $data[$key]['type'] = '未填写';
 | 
						||
            if($res){
 | 
						||
                $data[$key]['type'] = '已填写';
 | 
						||
            }
 | 
						||
            
 | 
						||
        }
 | 
						||
        
 | 
						||
        $count = Db::name('questionnaire')
 | 
						||
                    ->where($where)
 | 
						||
                    ->count();
 | 
						||
        $data = [
 | 
						||
                'count' => $count,
 | 
						||
                'data' => $data
 | 
						||
                ];
 | 
						||
        $this->success('返回成功',$data);
 | 
						||
    }
 | 
						||
   
 | 
						||
    //单挑查询
 | 
						||
    public function questionnairefind()
 | 
						||
{
 | 
						||
    $questionnaireId = input('id');
 | 
						||
    
 | 
						||
    // 1. 查询问卷所有题目(带权重排序)
 | 
						||
    $topics = Db::name('questionnaire_topic')
 | 
						||
        ->where('questionnaire_id', $questionnaireId)
 | 
						||
        ->order('id desc')  // 注意:原代码中的"wight"应为"weight"的拼写错误
 | 
						||
        ->select();
 | 
						||
 | 
						||
    // 2. 预处理题目数据:解码选项
 | 
						||
    $processedTopics = [];
 | 
						||
    foreach ($topics as $topic) {
 | 
						||
        $topic['option'] = json_decode($topic['option'], true);
 | 
						||
        $processedTopics[$topic['id']] = $topic; // 使用ID作为键方便后续匹配
 | 
						||
    }
 | 
						||
 | 
						||
    // 3. 查询该问卷所有题目的答题记录
 | 
						||
    $topicIds = array_keys($processedTopics);
 | 
						||
    $answerLogs = Db::name('questionnaire_log')
 | 
						||
        ->where('topic_id', 'in', $topicIds)
 | 
						||
        ->select();
 | 
						||
 | 
						||
    // 4. 合并答题记录到题目数据
 | 
						||
    foreach ($answerLogs as $log) {
 | 
						||
        $topicId = $log['topic_id'];
 | 
						||
        if (isset($processedTopics[$topicId])) {
 | 
						||
            // 添加 type 标识和答案数据
 | 
						||
            // $processedTopics[$topicId]['type'] = 1;
 | 
						||
            $processedTopics[$topicId]['topic'] = $log['option'];
 | 
						||
        }
 | 
						||
    }
 | 
						||
 | 
						||
    // 5. 转换为数组(保持顺序)
 | 
						||
    $result = array_values($processedTopics);
 | 
						||
 | 
						||
    $this->success('查询成功', $result);
 | 
						||
}
 | 
						||
    
 | 
						||
    //问卷统计查询
 | 
						||
    public function questionnaireStatistics(){
 | 
						||
        $id = input('id');
 | 
						||
        // 题目
 | 
						||
        $topic = Db::name('questionnaire_topic')
 | 
						||
                    ->where('activity_id', $id)
 | 
						||
                    ->select();
 | 
						||
         
 | 
						||
        $jianda = Db::name('questionnaire_topic')
 | 
						||
                    ->where('questionnaire_id', $id)
 | 
						||
                    ->order('wight desc')
 | 
						||
                    ->select();
 | 
						||
        foreach($jianda as $key => $val){
 | 
						||
            $jianda[$key]['option'] = json_decode($val['option'], true);
 | 
						||
        }
 | 
						||
        $jianda_log = Db::name('questionnaire_topic')
 | 
						||
                    ->where('questionnaire_id', $id)
 | 
						||
                    ->column('id');
 | 
						||
        $topic_id = implode(',',$jianda_log);
 | 
						||
        $jiandaer = Db::name('questionnaire_log')
 | 
						||
                    ->where('topic_id','in' ,$topic_id)
 | 
						||
                    ->select();
 | 
						||
        foreach($jianda as $topic_key => $topic_val){
 | 
						||
                $log_jianda = [];
 | 
						||
                foreach($jiandaer as $log_key => $log_val){
 | 
						||
 | 
						||
                    if($topic_val['id'] == $log_val['topic_id']){
 | 
						||
                        array_push($log_jianda, $log_val);
 | 
						||
                    }
 | 
						||
 | 
						||
                }
 | 
						||
               
 | 
						||
                $jianda[$topic_key]['list'] = $log_jianda;
 | 
						||
                $jianda[$topic_key]['quantity'] = count($jianda[$topic_key]['list']);
 | 
						||
        }
 | 
						||
            // 遍历 $jianda 数组  
 | 
						||
            foreach ($jianda as &$val) { // 使用引用 & 来直接修改原数组  
 | 
						||
                // 遍历 'option' 数组  
 | 
						||
                foreach ($val['option'] as &$option) { // 使用引用 & 来直接修改原数组  
 | 
						||
                    // 初始化 'num' 为 0(以防 'list' 中没有任何匹配的项) 
 | 
						||
                    $option['num'] = 0;  
 | 
						||
                      
 | 
						||
                    // 遍历 'list' 数组  
 | 
						||
                    foreach ($val['list'] as $b) {  
 | 
						||
                        // 检查 'list' 中的 'option' 是否包含当前 'option' 的 'name'  
 | 
						||
                        if (strpos($b['option'], $option['name']) !== false) {  
 | 
						||
                            // 如果找到匹配项,增加 'num' 并退出循环(因为不需要继续检查)  
 | 
						||
                            $option['num']++;  
 | 
						||
                            // break; // 退出当前 'list' 的循环  
 | 
						||
                        }  
 | 
						||
                    }  
 | 
						||
                }  
 | 
						||
                  
 | 
						||
                // 取消对 $val 的引用,避免后续潜在问题  
 | 
						||
                unset($val);  
 | 
						||
            } 
 | 
						||
        $this->success('返回成功',$jianda);
 | 
						||
      
 | 
						||
    }
 | 
						||
 | 
						||
    public function questionnaireAdd(){
 | 
						||
        $data = input();
 | 
						||
        $data['createtime'] = time();
 | 
						||
        $adddata = Db::name('questionnaire')->strict(false)->insert($data);
 | 
						||
        if ($adddata) {
 | 
						||
            $this->success('已添加');
 | 
						||
        } else {
 | 
						||
            $this->error('添加失败');
 | 
						||
        }
 | 
						||
    }
 | 
						||
 | 
						||
    
 | 
						||
    public function questionnaireEdit(){
 | 
						||
        $data = input();
 | 
						||
        $id = $data['id'];
 | 
						||
        // $this->success('已更新',$data);
 | 
						||
        
 | 
						||
        $update = Db::name('questionnaire')->where('id',  $id)->strict(false)->update($data);
 | 
						||
        if ($update) {
 | 
						||
            $this->success('已更新');
 | 
						||
        } else {
 | 
						||
            $this->error('更新失败');
 | 
						||
        }
 | 
						||
    }
 | 
						||
    
 | 
						||
 | 
						||
    public function questionnaireDel(){
 | 
						||
        $data = input();
 | 
						||
        $id = $data['id'];
 | 
						||
        $delete = Db::name('questionnaire')->where('id',  $id)->delete();
 | 
						||
        if ($delete) {
 | 
						||
            $this->success('已删除');
 | 
						||
        } else {
 | 
						||
            $this->error('删除失败');
 | 
						||
        }
 | 
						||
    }
 | 
						||
 | 
						||
    public function topicIndex(){
 | 
						||
        $data = input();
 | 
						||
        $id = $data['id'];
 | 
						||
        $topic = Db::name('questionnaire_topic')->where('questionnaire_id',$id)->select();
 | 
						||
        if ($topic) {
 | 
						||
            $this->success('查询成功',$topic);
 | 
						||
        } else {
 | 
						||
            $this->success('查询成功',[]);
 | 
						||
        }
 | 
						||
    }
 | 
						||
 | 
						||
    public function topicAdd(){
 | 
						||
        $data = $_POST['topic'];
 | 
						||
        $activity_id = $this->request->post('questionnaire_id');
 | 
						||
    
 | 
						||
        if (!$activity_id) {
 | 
						||
            $this->error(__('Invalid parameters'));
 | 
						||
        }
 | 
						||
     
 | 
						||
        // 解析 JSON 并校验
 | 
						||
        $array = json_decode($data, true);
 | 
						||
        if ($array === null) {
 | 
						||
            $error = json_last_error_msg();
 | 
						||
            $this->error("JSON 解析失败: " . $error);
 | 
						||
        }
 | 
						||
     
 | 
						||
        // 遍历处理数据
 | 
						||
        foreach ($array as $key => $val) {
 | 
						||
            $array[$key]['questionnaire_id'] = $activity_id;
 | 
						||
            $array[$key]['createtime'] = date('Y-m-d H:i:s');
 | 
						||
            $array[$key]['option'] = json_encode($val['option'], JSON_UNESCAPED_UNICODE); // 避免中文转义
 | 
						||
        }
 | 
						||
     
 | 
						||
        // 插入数据库
 | 
						||
        $adddata = Db::name('questionnaire_topic')->strict(false)->insertAll($array);
 | 
						||
        
 | 
						||
        if ($adddata) {
 | 
						||
            $this->success('已添加');
 | 
						||
        } else {
 | 
						||
            $this->error('添加失败');
 | 
						||
        }
 | 
						||
    }
 | 
						||
 | 
						||
    
 | 
						||
    public function topicEdit(){
 | 
						||
        $data = $_POST['topic'];
 | 
						||
        $array = json_decode($data, true);
 | 
						||
        //  $this->success('已更新', $array);
 | 
						||
        $id = $array['id'];
 | 
						||
        $array['option'] = json_encode( $array['option'],true);
 | 
						||
        $update = Db::name('questionnaire_topic')->where('id',  $id)->strict(false)->update($array);
 | 
						||
 | 
						||
        if ($update) {
 | 
						||
            $this->success('已更新');
 | 
						||
        } else {
 | 
						||
            $this->error('内容未更新');
 | 
						||
        }
 | 
						||
    }
 | 
						||
    
 | 
						||
 | 
						||
    public function topicDel(){
 | 
						||
        $id = $this->request->post('id');
 | 
						||
        if(!$id){
 | 
						||
            $this->error(__('Invalid parameters'));
 | 
						||
        }
 | 
						||
        
 | 
						||
        $delete = Db::name('questionnaire_topic')->where('id',  $id)->delete();
 | 
						||
        if ($delete) {
 | 
						||
            $this->success('已删除');
 | 
						||
        } else {
 | 
						||
            $this->error('删除失败');
 | 
						||
        }
 | 
						||
    }
 | 
						||
// --------------------------------------------------------------------------------------------
 | 
						||
// 移动端
 | 
						||
    public function appIndex(){
 | 
						||
        $title = input('questionnaire');
 | 
						||
        $page = input("page",1); 
 | 
						||
        $size = input("size",10); 
 | 
						||
        $where = [];
 | 
						||
        if($title){
 | 
						||
        $where['title'] = ['like',"%$title%"];
 | 
						||
        }
 | 
						||
 | 
						||
        $data = Db::name('questionnaire')
 | 
						||
                ->where($where)
 | 
						||
                ->order('releasetime desc')
 | 
						||
                ->page($page,$size)
 | 
						||
                ->select();
 | 
						||
        $one = [];
 | 
						||
        $two = [];
 | 
						||
        $there = [];
 | 
						||
        foreach($data as $key => $val){
 | 
						||
                if(strtotime($val['releasetime']) < time() && strtotime($val['endtime']) > time()){
 | 
						||
                    $id = $val['id'];
 | 
						||
                    $val['log'] = Db::name('questionnaire_log')
 | 
						||
                                    ->where('questionnaire_id', $id)
 | 
						||
                                    ->group('member_id')
 | 
						||
                                    ->count();
 | 
						||
                array_push($two,$val);
 | 
						||
                }
 | 
						||
              
 | 
						||
            } 
 | 
						||
        $this->success('返回成功',$two);
 | 
						||
    }
 | 
						||
       
 | 
						||
    //单挑查询
 | 
						||
    public function appFind(){
 | 
						||
        $id = input('id');
 | 
						||
 | 
						||
        $data = Db::name('questionnaire')
 | 
						||
                ->where('id', $id)
 | 
						||
                ->order('releasetime desc')
 | 
						||
                ->select();
 | 
						||
        // 题目
 | 
						||
        $topic = Db::name('questionnaire_topic')
 | 
						||
                    ->where('questionnaire_id', $id)
 | 
						||
                    ->select();
 | 
						||
        
 | 
						||
        $jianda = Db::name('questionnaire_topic')
 | 
						||
                    ->where('questionnaire_id', $id)
 | 
						||
                    ->where('type',1)
 | 
						||
                    ->select();
 | 
						||
 | 
						||
        $jianda_log = Db::name('questionnaire_topic')
 | 
						||
                    // ->field('id')   
 | 
						||
                    ->where('questionnaire_id', $id)
 | 
						||
                    ->where('type',1)
 | 
						||
                    ->column('id');
 | 
						||
        $topic_id = implode(',',$jianda_log);
 | 
						||
        $jiandaer = Db::name('questionnaire_log')
 | 
						||
                    // ->field('id')   
 | 
						||
                    ->where('topic_id','in' ,$topic_id)
 | 
						||
                    ->select();
 | 
						||
 | 
						||
                    // var_dump($jiandaer);die();
 | 
						||
        
 | 
						||
        
 | 
						||
        if($jianda_log){
 | 
						||
           foreach($jianda as $topic_key => $topic_val){
 | 
						||
 | 
						||
                $log_jianda = [];
 | 
						||
 | 
						||
                foreach($jiandaer as $log_key => $log_val){
 | 
						||
 | 
						||
                    if($topic_val['id'] == $log_val['topic_id']){
 | 
						||
                        array_push($log_jianda, $log_val);
 | 
						||
                    }
 | 
						||
 | 
						||
                }
 | 
						||
               
 | 
						||
                $jianda[$topic_key]['list'] = $log_jianda;
 | 
						||
           }
 | 
						||
        }else{
 | 
						||
            $jianda_log = [];
 | 
						||
        }   
 | 
						||
 | 
						||
        foreach($topic as $key => $val){
 | 
						||
          
 | 
						||
            $topic[$key]['count'] = Db::name('questionnaire_topic')
 | 
						||
                                ->where('questionnaire_id', $id)
 | 
						||
                                ->count();
 | 
						||
           
 | 
						||
            $option = json_decode($val['option'],true);
 | 
						||
 | 
						||
            $b = [];
 | 
						||
            $c = [];
 | 
						||
                foreach($option as $k => $v){
 | 
						||
 | 
						||
                    $a = Db::name('questionnaire_log')
 | 
						||
                                ->where('topic_id', $val['id'])
 | 
						||
                                ->where('option','like',"%$k%")
 | 
						||
                                ->count();
 | 
						||
                    
 | 
						||
                    $b = [
 | 
						||
                        'name' => $v['name'],
 | 
						||
                        'xvanxiang' => $k,
 | 
						||
                        'mun' => $a
 | 
						||
                    ];
 | 
						||
                    array_push($c,$b);
 | 
						||
 | 
						||
                }
 | 
						||
 | 
						||
            $topic[$key]['canyurenshu'] = $c;
 | 
						||
            if($val['type'] == 1){
 | 
						||
                unset($topic[$key]);
 | 
						||
            }
 | 
						||
            
 | 
						||
        }
 | 
						||
        $topic = array_values($topic);
 | 
						||
        $info = [
 | 
						||
            'data' => $data,
 | 
						||
            'topic' =>$topic,
 | 
						||
            'jianda'=>$jianda
 | 
						||
        ];
 | 
						||
        $this->success('返回成功',$info);
 | 
						||
    }
 | 
						||
 | 
						||
    public function appAdd(){
 | 
						||
        $option = $_POST['option'];
 | 
						||
        $Token = $this->request->header('Token');
 | 
						||
        if(!$Token){
 | 
						||
            return $this->error('缺少参数');
 | 
						||
        }
 | 
						||
        $user_id = Db::name('user')->where('token', $Token)->value('id');
 | 
						||
        if(!$user_id){
 | 
						||
            return $this->error('该用户不存在');
 | 
						||
        }
 | 
						||
        $member_id = $user_id;
 | 
						||
        $option = json_decode($option, true);
 | 
						||
        
 | 
						||
        foreach($option as $key => $val){
 | 
						||
            $option[$key]['user_id'] = $user_id;
 | 
						||
            $option[$key]['createtime'] = date('Y-m-d H:i:s');
 | 
						||
            $where = [];
 | 
						||
            if($member_id){
 | 
						||
               $where['user_id'] = $member_id;
 | 
						||
            }
 | 
						||
            if($val['questionnaire_id']){
 | 
						||
                $where['questionnaire_id'] = $val['questionnaire_id'];
 | 
						||
            }
 | 
						||
            
 | 
						||
            $if = Db::name('questionnaire_log')
 | 
						||
                    ->where($where)
 | 
						||
                    ->find();
 | 
						||
            if($if){
 | 
						||
                $this->success('问卷只能填写一次哦');
 | 
						||
            }
 | 
						||
        }
 | 
						||
    //   $this->success('提交成功',$option);
 | 
						||
        // $data = json_decode($option,true);
 | 
						||
        $adddata = Db::name('questionnaire_log')
 | 
						||
                ->strict(false)
 | 
						||
                ->insertAll($option);
 | 
						||
 | 
						||
        $this->success('提交成功',$adddata);
 | 
						||
    }
 | 
						||
 | 
						||
    public function appFindLog(){
 | 
						||
        
 | 
						||
        $member_id = input('member_id');
 | 
						||
        $questionnaire_id = input('questionnaire_id');
 | 
						||
        $where = [];
 | 
						||
        if($member_id){
 | 
						||
           $where['member_id'] = $member_id;
 | 
						||
        }
 | 
						||
        if($questionnaire_id){
 | 
						||
            $where['questionnaire_id'] = $questionnaire_id;
 | 
						||
        }
 | 
						||
        $if = Db::name('questionnaire_log') 
 | 
						||
                ->where($where)
 | 
						||
                ->select();
 | 
						||
        if($if){
 | 
						||
            $this->success('返回成功',$if);
 | 
						||
        }
 | 
						||
        $this->error('未查询到');
 | 
						||
    }
 | 
						||
}
 |