luck-draw/app/controller/Feedback.php

75 lines
2.4 KiB
PHP

<?php
namespace app\controller;
use app\model\Feedback as FeedbackModel;
use app\repository\AccountRepository;
use think\captcha\facade\Captcha;
/**
* 留言与意见反馈
*
* Class Feedback
* @package app\controller
*/
class Feedback extends Base
{
/**
* 用户提交反馈信息
*/
public function submitFeedback()
{
if (!$this->request->isPost()) {
return $this->json(4001, '请求方式错误!');
}
$params = [
'user_name' => $this->request->post('user_name/s', ''),
'user_tel' => $this->request->post('user_tel/s', ''),
'user_email' => $this->request->post('user_email/s', ''),
'content' => $this->request->post('content/s', ''),
'code' => $this->request->post('code/s', ''),
];
try{
$validate = $this->validateByApi($params, [
'code|验证码'=>'require|captcha',
'user_name|姓名' => 'chs|min:2|max:30',
'user_tel|联系电话' => 'min:5|max:20|mobile',
'user_email|邮箱地址' => 'email',
'content|留言内容' => 'require|min:6|max:500',
]);
if ($validate !== true) {
return $validate;
}
if ($this->authId > 0) {
$account = AccountRepository::getInstance()->findById($this->authId);
if ($account) {
$params['user_name'] = $params['user_name'] ?: ($account->nickname ?? '');
$params['user_tel'] = $params['user_tel'] ?: ($account->mobile ?? '');
}
}
FeedbackModel::create([
'account_id' => $this->authId,
'user_name' => $params['user_name'] ?: '',
'user_tel' => $params['user_tel'] ?: '',
'user_email' => $params['user_email'],
'content' => $params['content'],
'created_at' => date('Y-m-d H:i:s'),
]);
return $this->json(0, '感谢你的留言!');
} catch (\Exception $e) {
return $this->json(5001, '服务器异常,留言信息提交失败!');
}
}
public function code()
{
return Captcha::create('verify');
}
}