123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace App\Http\Controllers\Admin\Invite;
- use App\Http\Resources\Invite\QuestionTagCollection;
- use App\Models\Invite\QuestionTemplateModel;
- use App\Models\Invite\QuestionTagModel;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- class QuestionTemplateController extends Controller
- {
- // 创建问题
- public function store(Request $request)
- {
- $this->validate($request, [
- 'type' => 'required|in:text,music',
- 'question' => 'required',
- 'tag_id' => 'integer',
- 'author' => 'max:8'
- ]);
- $attribute = $request->only('type', 'question', 'tag_id', 'author');
- switch ($request->post('type', 'text')) {
- case "music":
- $attribute['question'] = json_encode($request->post('question'), JSON_UNESCAPED_UNICODE);
- break;
- }
- QuestionTemplateModel::create($attribute);
- return response([
- 'code' => 200,
- 'message' => 'OK'
- ]);
- }
- // 修改问题
- public function update(Request $request, $id)
- {
- $this->validate($request, [
- 'type' => 'in:text,music',
- 'question' => '',
- 'tag_id' => 'integer',
- 'author' => 'max:8',
- 'hide' => '',
- ]);
- $attribute = $request->only('type', 'question', 'tag_id', 'author');
- switch ($request->post('type', 'text')) {
- case "music":
- $attribute['question'] = json_encode($request->post('question'), JSON_UNESCAPED_UNICODE);
- break;
- }
- $question = QuestionTemplateModel::findOrFail($id);
- $question->update($attribute);
- return response([
- 'code' => 200,
- 'message' => 'OK'
- ]);
- }
- // 标签列表
- public function index(Request $request)
- {
- $tags = QuestionTagModel::where('type', $request->get('type', 'text'))->get();
- foreach ($tags as $tag) {
- $questions = QuestionTemplateModel::where('tag_id', $tag->id)->get();
- foreach ($questions as $question) {
- switch ($question->type) {
- case "music":
- $question->question = json_decode($question->question, true);
- break;
- }
- }
- $tag->questions = $questions;
- }
- return new QuestionTagCollection($tags);
- }
- // 创建标签
- public function createTag(Request $request)
- {
- $this->validate($request, [
- 'type' => 'required|in:text,music',
- 'tag' => 'required|max:8'
- ]);
- QuestionTagModel::create($request->all());
- return response([
- 'code' => 200,
- 'message' => 'OK'
- ]);
- }
- // 修改标签
- public function updateTag(Request $request, $id)
- {
- $this->validate($request, [
- 'type' => 'in:text,music',
- 'tag' => 'max:8'
- ]);
- $questionTag = QuestionTagModel::findOrFail($id);
- $questionTag->update($request->all());
- return response([
- 'code' => 200,
- 'message' => 'OK'
- ]);
- }
- }
|