CurrencyController.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace backendApi\modules\v1\controllers;
  3. use common\helpers\Form;
  4. use common\models\Currency;
  5. use common\models\CurrencyConversions;
  6. use common\models\forms\CurrenciesConversionsForm;
  7. use yii\db\Exception;
  8. use yii\web\HttpException;
  9. class CurrencyController extends BaseController
  10. {
  11. public $modelClass = Currency::class;
  12. public $currencyConversionsModelClass = CurrencyConversions::class;
  13. public function actionCurrencies()
  14. {
  15. $data = $this->modelClass::getFromCache();
  16. return static::notice(['data' => $data]);
  17. }
  18. public function actionCurrenciesConversions()
  19. {
  20. $data = $this->currencyConversionsModelClass::getFromCache();
  21. return static::notice(['data' => $data]);
  22. }
  23. /**
  24. * @throws Exception
  25. * @throws HttpException
  26. */
  27. public function actionSetCurrenciesConversions()
  28. {
  29. if (\Yii::$app->request->isPost) {
  30. $formModel = new CurrenciesConversionsForm();
  31. $formModel->scenario = 'setCurrenciesConversions';
  32. if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->setCurrenciesConversions()) {
  33. // 更新缓存
  34. $this->currencyConversionsModelClass::updateToCache();
  35. return static::notice(\Yii::t('ctx', 'successfully'));
  36. } else {
  37. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  38. }
  39. }
  40. return static::notice(\Yii::t('ctx', 'illegalRequest'));
  41. }
  42. }