SettingController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace addons\ValetOrder\backend\controllers;
  3. use addons\ValetOrder\common\service\SettingService;
  4. use common\enums\StatusEnum;
  5. use common\models\user\UserLevel;
  6. use Yii;
  7. /**
  8. * 默认控制器
  9. *
  10. * Class DefaultController
  11. * @package addons\WangDianTong\backend\controllers
  12. */
  13. class SettingController extends BaseController
  14. {
  15. public $enableCsrfValidation = false;
  16. /**
  17. * 首页
  18. *
  19. * @return string
  20. */
  21. public function actionIndex()
  22. {
  23. if (\Yii::$app->request->isAjax) {
  24. $service = new SettingService();
  25. if (\Yii::$app->request->isGet) {
  26. $ret = $service->get($this->mall_id);
  27. if (empty($ret)) {
  28. $ret = [
  29. 'is_enable' => 0,
  30. 'is_enable_sms_code' => 0,
  31. 'is_enable_order_refund' => 0,
  32. ];
  33. }
  34. $ret['allow_all_level_valet_order'] = $ret['allow_all_level_valet_order'] ?? 1;
  35. $ret['is_limit_same_team'] = $ret['is_limit_same_team'] ?? 0;
  36. $ret['allow_valet_order_user_levels'] = $ret['allow_valet_order_user_levels'] ?? '';
  37. $ret['allow_valet_order_user_levels'] = array_filter(explode(',', $ret['allow_valet_order_user_levels']), function ($val) {
  38. return is_numeric($val);
  39. });
  40. return $this->success('success', $ret);
  41. } else if (\Yii::$app->request->isPost) {
  42. $data = Yii::$app->request->post();
  43. $valid = Yii::$app->services->validate->validate($data, [
  44. [['is_enable', 'is_enable_sms_code', 'is_enable_order_refund', 'allow_all_level_valet_order', 'is_limit_same_team', 'is_hide_order_price', 'is_enable_other_identity_price'], 'integer'],
  45. [['allow_valet_order_user_levels'], 'safe'],
  46. ]);
  47. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  48. if (isset($data['allow_valet_order_user_levels'])) {
  49. $data['allow_valet_order_user_levels'] = implode(',', array_filter($data['allow_valet_order_user_levels'], function ($val) {
  50. return is_numeric($val);
  51. }));
  52. }
  53. // 保存"使用代下单用户的等级价格"开关配置
  54. if (!empty($data['is_enable_other_identity_price'])) {
  55. try {
  56. $service->createValetOrderDefaultUser($this->mall_id);
  57. }catch (\Exception $e) {
  58. return $this->error($e->getMessage());
  59. }
  60. }
  61. return $service->save($this->mall_id, $data) ? $this->success("保存成功", $data) : $this->error('保存失败');
  62. }
  63. }
  64. return $this->render('index');
  65. }
  66. /**
  67. * @return mixed|null
  68. */
  69. public function actionGetData()
  70. {
  71. $userLevelList = UserLevel::getUserLevel([['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], [], 1);
  72. return $this->success('success', ['user_level_list' => $userLevelList]);
  73. }
  74. }