generated from php/site_api
177 lines
4.8 KiB
PHP
177 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\Faq;
|
|
|
|
use app\BaseController;
|
|
use app\common\model\Faq\Faq as ModelFaq;
|
|
use app\Request;
|
|
use think\Validate;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Filesystem;
|
|
use app\common\arw\adjfut\src\Excel;
|
|
use app\common\arw\adjfut\src\UploadFile;
|
|
use app\common\exception\Tool;
|
|
use think\facade\Db;
|
|
use think\facade\Env;
|
|
|
|
|
|
class Faq extends BaseController
|
|
{
|
|
/**
|
|
* 获取常见问题列表
|
|
*/
|
|
public function getFaqList(Request $request, $isExport = false): array
|
|
{
|
|
$params = $request->param();
|
|
$con = [];
|
|
|
|
$con = Tool::getOptionalQuery(['faq_questions', 'LIKE'], ['c.i18n_lang_type_guid', '=']);
|
|
|
|
$query = ModelFaq::where($con)
|
|
->alias('a')
|
|
->leftjoin('i18n_lang_type c', 'a.i18n_lang_type_guid = c.i18n_lang_type_guid')
|
|
->field([
|
|
'faq_id',
|
|
'faq_guid',
|
|
'faq_questions',
|
|
'faq_answer',
|
|
'faq_sort',
|
|
'a.i18n_lang_type_guid',
|
|
'c.i18n_lang_type_name',
|
|
'c.i18n_lang_type_code',
|
|
])
|
|
->order('faq_sort', 'asc');
|
|
|
|
return $isExport ? $query->select()->toArray() : msg("获取常见问题列表成功!", $query);
|
|
}
|
|
|
|
/**
|
|
* 添加常见问题
|
|
*/
|
|
public function addFaq(Request $request): array
|
|
{
|
|
Db::startTrans();
|
|
Tool::adminLockTableWrite('faq');
|
|
try {
|
|
$params = $request->param();
|
|
$this->validate($params, [
|
|
'faq_questions|问题' => 'require',
|
|
'faq_answer|答案' => 'require',
|
|
]);
|
|
$model = ModelFaq::create($params, [
|
|
'i18n_lang_type_guid',
|
|
'tdk_create_user_guid',
|
|
'tdk_update_user_guid',
|
|
'faq_questions',
|
|
'faq_answer',
|
|
'faq_sort',
|
|
'faq_guid',
|
|
'faq_create_user_guid',
|
|
'faq_update_user_guid'
|
|
]);
|
|
Db::commit();
|
|
Tool::unlockTable();
|
|
return msg('添加成功!');
|
|
} catch (\Throwable $th) {
|
|
Db::rollback();
|
|
Tool::unlockTable();
|
|
throw $th;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 编辑常见问题
|
|
*/
|
|
public function editFaq(Request $request): array
|
|
{
|
|
Db::startTrans();
|
|
Tool::adminLockTableWrite('faq');
|
|
try {
|
|
$params = $request->param();
|
|
$this->validate($params, [
|
|
'faq_questions|问题' => 'require',
|
|
'faq_answer|答案' => 'require',
|
|
'faq_sort|顺序' => 'require'
|
|
]);
|
|
$model = ModelFaq::where('faq_guid', $params['faq_guid'])->find();
|
|
if (!$model) throwErrorMsg("该常见问题不存在", 1);
|
|
$model->allowField([
|
|
'i18n_lang_type_guid',
|
|
'faq_questions',
|
|
'faq_answer',
|
|
'faq_sort',
|
|
'faq_update_user_guid'
|
|
])->save($params);
|
|
Db::commit();
|
|
Tool::unlockTable();
|
|
return msg('编辑成功!');
|
|
} catch (\Throwable $th) {
|
|
Db::rollback();
|
|
Tool::unlockTable();
|
|
throw $th;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除常见问题
|
|
*/
|
|
public function deleteFaq(Request $request): array
|
|
{
|
|
Db::startTrans();
|
|
Tool::adminLockTableWrite('faq');
|
|
try {
|
|
$params = $request->param();
|
|
$this->validate($params, [
|
|
'faq_guid' => 'require',
|
|
]);
|
|
$faq = ModelFaq::where([
|
|
'faq_guid' => explode(',', $params['faq_guid'])
|
|
])->select();
|
|
$faq->delete();
|
|
Db::commit();
|
|
Tool::unlockTable();
|
|
return msg('删除成功!');
|
|
} catch (\Throwable $th) {
|
|
Db::rollback();
|
|
Tool::unlockTable();
|
|
throw $th;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 导出Excel
|
|
*/
|
|
public function exportExcel(Request $request): void
|
|
{
|
|
ModelFaq::exportExcel(self::getFaqList($request, true));
|
|
}
|
|
|
|
/**
|
|
* 下载导入模板
|
|
*/
|
|
public function downloadTemplate(Request $request): void
|
|
{
|
|
$params = $request->param();
|
|
$data = [
|
|
array_values(ModelFaq::EXCELFIELD),
|
|
['zh', '默认值1', '默认值2', '默认值3',]
|
|
];
|
|
$excel = (new Excel())->exporTsheet($data);
|
|
$excel->save('常见问题导入模板.xlsx');
|
|
}
|
|
|
|
/**
|
|
* 导入excel
|
|
*/
|
|
public function importExcel(Request $request): array
|
|
{
|
|
$file = new UploadFile('uploads', 'fileExt:xlsx');
|
|
$file->putFile('faq');
|
|
$msg = ModelFaq::importExcel($file);
|
|
return [
|
|
'code' => 0,
|
|
'msg' => $msg
|
|
];
|
|
}
|
|
}
|