TransportationController.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace backendApi\modules\v1\controllers;
  3. use common\helpers\Form;
  4. use common\models\Countries;
  5. use common\models\Currency;
  6. use common\models\CurrencyConversions;
  7. use common\models\forms\CurrenciesConversionsForm;
  8. use common\models\forms\FreeTemplateForm;
  9. use common\models\FreeTemplate;
  10. use yii\db\Exception;
  11. use yii\web\HttpException;
  12. class TransportationController extends BaseController
  13. {
  14. public $countiesModelClass = Countries::class;
  15. public $modelClass = FreeTemplate::class;
  16. public function actionTransportation()
  17. {
  18. // 国家列表
  19. $countries = $this->countiesModelClass::getFromCache();
  20. // 运费列表
  21. $transportation = $this->modelClass::getFromCache();
  22. $transportation = array_column($transportation, NULL, 'country_id');
  23. foreach ($countries as &$country) {
  24. $country['freight'] = $transportation[$country['country_id']]['freight'] ?? 0;
  25. $country['free_shipping'] = $transportation[$country['country_id']]['free_shipping'] ?? 0;
  26. $country['currency_id'] = $transportation[$country['country_id']]['currency_id'] ?? 0;
  27. }
  28. return static::notice(['data' => $countries]);
  29. }
  30. public function actionSetTransportation()
  31. {
  32. if (\Yii::$app->request->isPost) {
  33. $formModel = new FreeTemplateForm();
  34. $formModel->scenario = 'setTransportation';
  35. if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->setTransportation()) {
  36. // 更新缓存
  37. $this->modelClass::updateToCache();
  38. return static::notice(\Yii::t('ctx', 'successfully'));
  39. } else {
  40. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  41. }
  42. }
  43. return static::notice(\Yii::t('ctx', 'illegalRequest'));
  44. }
  45. }