TransportationController.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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_id'] = $transportation[$country['ID']]['currency_id'] ?? 0;
  31. // $country['currency'] = $currencies[$transportation[$country['ID']]['currency_id']]['NAME'] ?? '';
  32. }
  33. return static::notice(['data' => $countries]);
  34. }
  35. public function actionSetTransportation()
  36. {
  37. if (\Yii::$app->request->isPost) {
  38. $formModel = new FreeTemplateForm();
  39. $formModel->scenario = 'setTransportation';
  40. if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->setTransportation()) {
  41. // 更新缓存
  42. $this->modelClass::updateToCache();
  43. return static::notice(\Yii::t('ctx', 'successfully'));
  44. } else {
  45. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  46. }
  47. }
  48. return static::notice(\Yii::t('ctx', 'illegalRequest'));
  49. }
  50. }