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.
99 lines
3.1 KiB
PHP
99 lines
3.1 KiB
PHP
<?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->id,
|
|
'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);
|
|
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 < 2) {
|
|
$question->option = json_decode($question->option, true);
|
|
$item_data = [
|
|
'id' => $question->id,
|
|
'type' => $question->type,
|
|
'question' => $question->question,
|
|
'value' => '',
|
|
];
|
|
if ($question->type == 'input') {
|
|
$item_data['value'] = $question->option['input']['value'];
|
|
$item_data['placeholder'] = $question->option['input']['placeholder'];
|
|
} else {
|
|
$item_data['value'] = '';
|
|
$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);
|
|
$item_data['value'] = $v;
|
|
$list[] = $item_data;
|
|
}
|
|
}
|
|
}
|
|
return $list;
|
|
}
|
|
}
|