You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.0 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\API\H5;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class QuestionnaireController extends Controller
{
public function list(Request $request)
{
$db = DB::table('question_lists');
$question = $db->orderBy('order', 'desc')->get();
$list = [];
foreach ($question as $key => $value) {
$list[] = [
'id' => $value->question,
'title' => $value->name,
'icon' => $value->icon,
'desc' => $value->desc,
];
}
return \Yz::Return(true, '操作完成', [
'list' => $list
]);
}
public function get(Request $request)
{
$id = $request->post('id');
$person = $request->post('person');
$question = DB::table('questionnaires')->where('id', $id)->first();
if (!$question) {
return \Yz::echoError('问卷不存在');
}
$question_ids = json_decode($question->questions, true);
$person_info = DB::table('web_user_person')->where('id', $person)->first();
$list = self::getList($person_info, $question_ids);
//如果是男性屏蔽id为99的问题
$nvxingQuestion=[99,103];
if($person_info->sex==1){
$temp=[];
foreach ($list as $key => $item) {
if(!in_array($item['id'],$nvxingQuestion)){
$temp[]=$item;
}
}
$list=$temp;
}
return \Yz::Return(true, '操作完成', [
'info' => $question,
'list' => $list
]);
}
public function getList($person, $ids, $level = 0)
{
$list = [];
$replace_array = ['name', 'birthday', 'phone', 'id_number'];
foreach ($ids as $id) {
$question = DB::table('question_questions')->where('id', $id)->first();
if (!!$question) {
if ($level < 3) {
$question->option = json_decode($question->option, true);
$item_data = [
'id' => $question->id,
'type' => $question->type,
'question' => $question->question,
'value' => '',
];
if (in_array($question->type, ['input', 'date', 'city'])) {
$v = $question->option['input']['value'];
$item_data['value'] = $v;
$item_data['placeholder'] = $question->option['input']['placeholder'];
} else {
if($question->question=='性别'){
if($person->sex==1) $vl=0;
if($person->sex==2) $vl=1;
}else{
$vl='';
}
$item_data['value'] = $vl;
$select = $question->option['select']['value'];
$select_array = [];
foreach ($select as $select_value) {
$push_item = [
'content' => $select_value['content'],
'children' => []
];
if ($select_value['type'] == 'questions') {
$children = self::getList($person, $select_value['questions'], $level + 1);
if (count($children) > 0) {
$push_item['children'] = $children;
}
}
$select_array[] = $push_item;
}
$item_data['select'] = $select_array;
}
$v = $item_data['value'];
foreach ($replace_array as $replace_item) {
if (!!$person) {
$v = str_replace('${' . $replace_item . '}', $person->$replace_item, $v);
} else {
$v = str_replace('${' . $replace_item . '}', '', $v);
}
}
$v = str_replace('${体重}', '', $v);
$v = str_replace('${身高}', '', $v);
$v = str_replace('${今日日期}',$person->birthday , $v);
if ($v == '${省市县}') {
$v = [[
'value' => '46',
'text' => '海南省'
], [
'value' => '4601',
'text' => '海口市'
], [
'value' => '460105',
'text' => '秀英区'
]];
}
$item_data['value'] = $v;
$list[] = $item_data;
}
}
}
return $list;
}
}