SuggestController.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace api\modules\v1\controllers;
  3. use api\common\behaviors\QueryParamAuth;
  4. use api\common\controllers\Controller;
  5. use common\enums\CodeEnum;
  6. use common\models\Suggest;
  7. use Yii;
  8. use yii\base\Exception;
  9. use yii\helpers\ArrayHelper;
  10. class SuggestController extends Controller
  11. {
  12. public function behaviors()
  13. {
  14. return ArrayHelper::merge(parent::behaviors(), [
  15. [
  16. 'class' => QueryParamAuth::className(),
  17. 'tokenParam' => 'token',
  18. ]
  19. ]);
  20. }
  21. /**
  22. * @return array|string[]
  23. * @throws \yii\db\Exception
  24. * @author jiang
  25. */
  26. public function actionCreate()
  27. {
  28. $transaction = Yii::$app->db->beginTransaction();
  29. try {
  30. $model = new Suggest();
  31. $model->load(request()->post(), '');
  32. $model->save();
  33. if ($model->hasErrors()) {
  34. throw new Exception(current($model->getErrors())[0]);
  35. }
  36. $transaction->commit();
  37. } catch (\Exception $e) {
  38. $transaction->rollBack();
  39. return ['errcode' => CodeEnum::CODE_ERROR, 'errmsg' => '提交失败:' . $e->getMessage()];
  40. }
  41. return ['errmsg' => '提交成功'];
  42. }
  43. }