<?php

namespace api\modules\v1\controllers;


use api\common\behaviors\QueryParamAuth;
use api\common\controllers\Controller;
use common\enums\CodeEnum;
use common\models\Suggest;
use Yii;
use yii\base\Exception;
use yii\helpers\ArrayHelper;


class SuggestController extends Controller
{

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => QueryParamAuth::className(),
                'tokenParam' => 'token',
            ]
        ]);
    }
    /**
     * @return array|string[]
     * @throws \yii\db\Exception
     * @author jiang
     */
    public function actionCreate()
    {
        $transaction = Yii::$app->db->beginTransaction();
        try {

            $model = new Suggest();
            $model->load(request()->post(), '');
            $model->save();

            if ($model->hasErrors()) {
                throw new Exception(current($model->getErrors())[0]);
            }

            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
            return ['errcode' => CodeEnum::CODE_ERROR, 'errmsg' => '提交失败：' . $e->getMessage()];
        }

        return ['errmsg' => '提交成功'];
    }
}