Skip to content
关注公众号,获取新课通知
【重要通知】uniapp实战社区交友交流群更换为:602180461,靓仔/靓女可以重新申请加入哦~

用户反馈api


创建相关文件

php
// 创建反馈控制器
php think make:controller api/v1/Feedback
// 创建反馈模型
php think make:model Feedback
// 创建反馈验证器
php think make:validate FeedbackValidate

controller层:application\api\controller\v1\Feedback.php

php
<?php

namespace app\api\controller\v1;

use think\Controller;
use think\Request;
use app\common\controller\BaseController;
use app\common\validate\FeedbackValidate;
use app\common\model\Feedback as FeedbackModel;
class Feedback extends BaseController
{
    // 反馈信息
    public function feedback(){
        (new FeedbackValidate())->goCheck('feedback');
        (new FeedbackModel())->feedback();
        return self::showResCodeWithOutData('反馈成功');
    }

}

route层:route\route.php

php
// 用户操作(绑定手机)
Route::group('api/:v1/',function(){
	// 用户反馈
    Route::post('feedback','api/:v1.Feedback/feedback');
})->middleware(['ApiUserAuth','ApiUserBindPhone','ApiUserStatus']);

validate层:application\common\validate\FeedbackValidate.php

php
protected $rule = [
    'data'=>'require|NotEmpty',
];
protected $scene = [
    'feedback'=>['data'],
];

BaseValidate:application\common\validate\BaseValidate.php

php
// 不能为空
protected function NotEmpty($value, $rule='', $data='', $field='')
{
    if (empty($value)) return $field."不能为空";
    return true;
}

model层:application\common\model\Feedback.php

php
protected $autoWriteTimestamp = true;

// 用户反馈
public function feedback(){
    $param = request()->param();
    $data = [
        'from_id' => request()->userId,
        'to_id' => 0,
        'data' => $param['data']
    ];
    if (!$this -> create($data)) return TApiException();
    return true;
}