travel/service/app/admin/controller/QaController.php

205 lines
6.9 KiB
PHP
Raw Permalink Normal View History

2024-06-24 20:58:23 +08:00
<?php
namespace app\admin\controller;
2024-06-27 17:34:53 +08:00
use app\model\QaCitys;
2024-06-28 17:30:56 +08:00
use app\model\QaQuestions;
2024-06-24 20:58:23 +08:00
use app\model\Qas;
2024-06-27 19:53:53 +08:00
use support\Log;
2024-06-24 20:58:23 +08:00
use support\Request;
2024-06-28 17:30:56 +08:00
use think\facade\Db;
2024-06-24 20:58:23 +08:00
class QaController extends base
{
public function getQaList(Request $request)
{
2024-06-27 17:34:53 +08:00
$city_id = $request->get('city_id');
2024-06-27 19:53:53 +08:00
$keyword = $request->get('keyword');
2024-07-03 16:38:30 +08:00
$limit = $request->get('limit', 10);
2024-06-27 17:34:53 +08:00
2024-07-03 16:38:30 +08:00
$listQuery = Qas::query();
2024-06-27 19:53:53 +08:00
2024-07-03 16:38:30 +08:00
if (!empty($city_id)) {
$listQuery->where(['status' => 1, 'city_id' => $city_id]);
2024-06-27 19:53:53 +08:00
}
2024-07-03 16:38:30 +08:00
if (!empty($keyword)) {
$qaIds = QaQuestions::whereRaw("title LIKE ? OR content LIKE ?", ['%'.$keyword.'%', '%'.$keyword.'%'])
->column('qa_id');
$listQuery->when(!empty($qaIds), function ($query) use ($qaIds) {
2024-07-03 18:01:39 +08:00
$query->whereIn('id', $qaIds);
2024-07-03 16:38:30 +08:00
});
2024-06-27 19:53:53 +08:00
}
2024-07-03 16:38:30 +08:00
$list = $listQuery->order('id', 'desc')
2024-07-03 18:01:39 +08:00
->with(['qaQuestions' => function ($query) use ($keyword) {
$query->whereRaw("title LIKE ? OR content LIKE ?", ['%'.$keyword.'%', '%'.$keyword.'%']);
2024-07-03 16:38:30 +08:00
$query->order('sort', 'desc');
}])
->paginate($limit);
2024-06-27 17:34:53 +08:00
2024-08-09 15:21:06 +08:00
$list = json_decode(json_encode($list), true);
if (isset($list['data']) && is_array($list['data'])) {
foreach ($list['data'] as &$item) {
if (isset($item['img_zip']) && !empty($item['img_zip'])) {
$item['img_zip'] = json_decode($item['img_zip'], true);
}
if (isset($item['trip_zip']) && !empty($item['trip_zip'])) {
$item['trip_zip'] = json_decode($item['trip_zip'], true);
}
}
}
2024-06-27 17:34:53 +08:00
return $this->success($list);
}
/**
* cms list
* @param Request $request
* @return \support\Response
* @throws \think\db\exception\DbException
*/
public function getQa(Request $request)
{
$title = $request->get('title');
$status = $request->get('status');
2024-07-01 11:25:55 +08:00
$list = Qas::with(['qaCitys','qaQuestions'=>function ($query) {
2024-07-03 16:38:30 +08:00
$query->order('sort desc');
2024-07-01 11:25:55 +08:00
}])->order('create_time desc');
2024-06-27 17:34:53 +08:00
if (!empty($title)){
$list = $list->where('title','like','%'.$title.'%');
}
2024-07-03 17:03:50 +08:00
if (!is_null($status)){
2024-06-27 17:34:53 +08:00
$list = $list->where('status',$status);
}
2024-07-31 10:59:19 +08:00
$list = $list->paginate($request->get('limit',10))->toArray();
foreach ($list['data'] as &$item) {
2024-08-09 15:38:49 +08:00
if (isset($item['img_zip']) && !empty($item['img_zip'])) {
$item['img_zip'] = json_decode($item['img_zip'], true);
}
if (isset($item['trip_zip']) && !empty($item['trip_zip'])) {
$item['trip_zip'] = json_decode($item['trip_zip'], true);
}
2024-07-31 10:59:19 +08:00
}
2024-06-24 20:58:23 +08:00
return $this->success($list);
}
public function getQaDetail(Request $request)
{
2024-06-26 15:38:38 +08:00
$id = $request->get('id');
if (empty($id)) return $this->error(2001, 'id data cannot be empty!');
2024-06-24 20:58:23 +08:00
2024-07-01 11:25:55 +08:00
$data = Qas::with(['qaCitys','qaQuestions'=>function ($query) {
$query->order('sort asc');
}])->where('status',1)->find();
2024-06-24 20:58:23 +08:00
return $this->success($data);
}
public function addQa(Request $request)
{
$post = $request->post();
2024-06-27 19:53:53 +08:00
if (empty($post['city_id'])) return $this->error(2001, 'city_id data cannot be empty!');
2024-06-24 20:58:23 +08:00
if (empty($post['title'])) return $this->error(2001, 'title data cannot be empty!');
2024-06-28 17:30:56 +08:00
2024-08-09 11:15:10 +08:00
if ($post['trip_zip'] && is_array($post['trip_zip'])) {
$post['trip_zip'] = json_encode($post['trip_zip']);
2024-07-31 10:59:19 +08:00
}
2024-08-09 11:15:10 +08:00
if ($post['img_zip'] && is_array($post['img_zip'])) {
$post['img_zip'] = json_encode($post['img_zip']);
2024-07-31 10:59:19 +08:00
}
2024-07-01 16:05:56 +08:00
$qaQuestion = $post['qaQuestions'] ?? null;
2024-08-29 18:00:36 +08:00
unset($post['qaQuestions']);
if (isset($post['update_time'])) {
unset($post['update_time']);
}
2024-08-29 18:01:27 +08:00
if (isset($post['id'])) {
2024-08-29 18:00:36 +08:00
unset($post['id']);
}
2024-08-09 15:21:06 +08:00
$post['create_time'] = time();
2024-06-24 20:58:23 +08:00
try {
2024-06-28 17:30:56 +08:00
Db::transaction(function () use ($post,$qaQuestion){
$data = Qas::create($post);
if (!empty($qaQuestion)){
foreach ($qaQuestion as $k => $v){
$qaQuestion = new QaQuestions();
$qaQuestion->qa_id = $data->id;
$qaQuestion->title = $v['title'];
2024-07-17 15:38:58 +08:00
$qaQuestion->content = urldecode($v['content']);
2024-06-28 17:30:56 +08:00
$qaQuestion->sort = $v['sort'];
$qaQuestion->save();
}
$data->qaQuestions = $qaQuestion;
}
});
2024-06-24 20:58:23 +08:00
} catch (\Exception $e) {
return $this->error(2002, $e->getMessage());
}
2024-06-28 17:30:56 +08:00
return $this->success(null);
2024-06-24 20:58:23 +08:00
}
public function editQa(Request $request)
{
$post = $request->post();
if (empty($post['id'])) return $this->error(2001, 'id data cannot be empty!');
2024-07-01 16:03:54 +08:00
$qaQuestion = $post['qaQuestions'] ?? null;
2024-06-27 17:34:53 +08:00
unset($post['create_time']);
unset($post['update_time']);
2024-06-28 17:30:56 +08:00
unset($post['qaQuestions']);
2024-06-24 20:58:23 +08:00
2024-08-09 15:21:06 +08:00
if (isset($post['trip_zip']) && $post['trip_zip']) {
$post['trip_zip'] = json_encode($post['trip_zip']);
2024-07-31 10:59:19 +08:00
}
2024-08-09 15:21:06 +08:00
if (isset($post['img_zip']) && $post['img_zip']) {
$post['img_zip'] = json_encode($post['img_zip']);
2024-07-31 10:59:19 +08:00
}
2024-06-24 20:58:23 +08:00
try {
2024-06-28 17:30:56 +08:00
Db::transaction(function () use ($post,$qaQuestion){
$data = Qas::update($post);
if (!empty($qaQuestion)){
QaQuestions::where('qa_id',$post['id'])->delete();
foreach ($qaQuestion as $k => $v){
$qaQuestion = new QaQuestions();
$qaQuestion->qa_id = $data->id;
$qaQuestion->title = $v['title'];
2024-07-17 15:38:58 +08:00
$qaQuestion->content = urldecode($v['content']);
2024-06-28 17:30:56 +08:00
$qaQuestion->sort = $v['sort'];
$qaQuestion->save();
}
$data->qa_question = $qaQuestion;
}
});
2024-06-24 20:58:23 +08:00
} catch (\Exception $e) {
return $this->error(2002, $e->getMessage());
}
2024-06-28 17:30:56 +08:00
return $this->success(null);
2024-06-24 20:58:23 +08:00
}
public function delQa(Request $request)
{
2024-06-27 17:34:53 +08:00
$id = $request->post('id');
2024-06-24 20:58:23 +08:00
if (empty($id)) return $this->error(2001, 'id data cannot be empty!');
try {
2024-06-28 18:33:51 +08:00
Db::transaction(function () use ($id){
Qas::destroy($id);
QaQuestions::where('qa_id',$id)->delete();
});
2024-06-24 20:58:23 +08:00
} catch (\Exception $e) {
return $this->error(2002, $e->getMessage());
}
2024-06-28 18:33:51 +08:00
return $this->success(null);
2024-06-24 20:58:23 +08:00
}
2024-06-27 17:34:53 +08:00
public function getQaCityList()
{
2024-07-02 16:35:41 +08:00
$list = QaCitys::order('sort desc')->select();
2024-06-27 17:34:53 +08:00
return $this->success($list);
}
2024-06-24 20:58:23 +08:00
}