TransportationController.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 $modelClass = FreeTemplate::class;
  15. public $countiesModelClass = Countries::class;
  16. public $currencyModelClass = Currency::class;
  17. public function actionTransportation()
  18. {
  19. // 国家列表
  20. $countries = $this->countiesModelClass::getFromCache();
  21. // 运费列表
  22. $transportation = $this->modelClass::getFromCache();
  23. $transportation = array_column($transportation, NULL, 'country_id');
  24. // 货币列表
  25. $currencies = $this->currencyModelClass::getFromCache();
  26. $currencies = array_column($currencies, NULL, 'ID');
  27. foreach ($countries as &$country) {
  28. $country['freight'] = $transportation[$country['ID']]['freight'] ?? 0;
  29. $country['free_shipping'] = $transportation[$country['ID']]['free_shipping'] ?? 0;
  30. $country['currency'] = $this->currencyModelClass::getById($country['LOCAL_CURRENCY_ID']);
  31. }
  32. return static::notice(['data' => $countries]);
  33. }
  34. public function actionSetTransportation()
  35. {
  36. if (\Yii::$app->request->isPost) {
  37. $formModel = new FreeTemplateForm();
  38. $formModel->scenario = 'setTransportation';
  39. if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->setTransportation()) {
  40. // 更新缓存
  41. $this->modelClass::updateToCache();
  42. return static::notice(\Yii::t('ctx', 'successfully'));
  43. } else {
  44. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  45. }
  46. }
  47. return static::notice(\Yii::t('ctx', 'illegalRequest'));
  48. }
  49. }