SettingController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace addons\QiShangTong\backend\controllers;
  3. use addons\QiShangTong\common\service\GoodsService;
  4. use addons\QiShangTong\common\service\SettingService;
  5. use common\enums\StatusEnum;
  6. use common\models\common\PlatformSetting;
  7. use common\models\mall\Mall;
  8. use Yii;
  9. use yii\helpers\Json;
  10. /**
  11. * 默认控制器
  12. *
  13. * Class DefaultController
  14. * @package addons\WechatVideoShop\backend\controllers
  15. */
  16. class SettingController extends BaseController
  17. {
  18. public $enableCsrfValidation = false;
  19. /**
  20. * 首页
  21. *
  22. * @return string
  23. */
  24. public function actionIndex()
  25. {
  26. if (Yii::$app->request->isAjax) {
  27. $service = (new SettingService());
  28. if (Yii::$app->request->isPost) {
  29. $data = Yii::$app->request->post();
  30. $valid = Yii::$app->services->validate->validate($data, [
  31. [['is_enable', 'is_default', 'auto_update_price'], 'integer'],
  32. [['app_key', 'secret_key', 'contact_image'], 'string'],
  33. [['market_price_rate', 'price_rate', 'min_balance'], 'number'],
  34. [['price', 'mobile'], 'safe'],
  35. ]);
  36. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  37. $data['price'] = !empty($data['price']) ? Json::encode($data['price']) : '[]';
  38. if (!empty($data['is_default']) && $this->admin->admin_type == StatusEnum::ENABLED) {
  39. // 需要将其他的更新为非默认
  40. foreach (Mall::getAllMallIds() as $mallId) {
  41. if ($mallId != $this->mall_id) {
  42. $config = $service->get($mallId);
  43. $config['is_default'] = StatusEnum::DISABLED;
  44. $service->save($mallId, $config);
  45. }
  46. }
  47. }
  48. if (!empty($data['auto_update_price'])) {
  49. $ret = $service->get($this->mall_id);
  50. // 判断订单是否更改
  51. if (!empty($ret['price']) && (md5($ret['price']) != md5($data['price']))) {
  52. // 需要更新商品价格
  53. try {
  54. (new GoodsService())->changePrice($this->mall_id);
  55. } catch (\Exception $e) {
  56. return $this->error($e->getMessage());
  57. }
  58. }
  59. }
  60. return $service->save($this->mall_id, $data) ? $this->success("保存成功", $data) : $this->error('保存失败');
  61. } else {
  62. $ret = $service->get($this->mall_id);
  63. if (!empty($ret['price'])) {
  64. $ret['price'] = Json::decode($ret['price']);
  65. if (!is_array($ret['price']['price']['rate'])) $ret['price'] = null;
  66. }
  67. return $this->success('success', $ret);
  68. }
  69. }
  70. $get_system_setting = PlatformSetting::getConfByGroup('system', true);
  71. return $this->render('index',[
  72. 'order_ship_callback_url' => "{$get_system_setting['api_url']['value']}/qi-shang-tong/v1/order/delivery",
  73. 'order_ready_callback_url' => "{$get_system_setting['api_url']['value']}/qi-shang-tong/v1/order/ready",
  74. 'order_refund_callback_url' => "{$get_system_setting['api_url']['value']}/qi-shang-tong/v1/refund/event",
  75. 'goods_callback_url' => "{$get_system_setting['api_url']['value']}/qi-shang-tong/v1/goods/event",
  76. 'admin_type' => $this->admin->admin_type]);
  77. }
  78. }