SettingController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. namespace addons\Happiness\client\controllers;
  3. use addons\Store\common\enums\StoreEnum;
  4. use addons\Store\common\models\Store;
  5. use addons\Store\common\models\StoreCate;
  6. use addons\Store\common\models\StoreFreightRules;
  7. use addons\Store\common\models\StoreGoodsModel;
  8. use addons\Store\common\models\StoreModel;
  9. use addons\Store\common\models\StoreScoreStoreApply;
  10. use addons\Store\common\models\StoreSettings;
  11. use addons\Store\common\services\StoreSettingService;
  12. use addons\Happiness\common\models\HappinessStore;
  13. use common\enums\AddonsEnum;
  14. use common\enums\GoodsTypeEnum;
  15. use common\enums\StatusEnum;
  16. use common\globals\GlobalInvoke;
  17. use common\logic\common\AddonsLimit;
  18. use common\models\common\Region;
  19. use common\models\goods\FreightRules;
  20. use common\models\user\User;
  21. use Yii;
  22. use addons\Store\common\models\StoreCommunity;
  23. use addons\Happiness\common\enums\HappinessEnum;
  24. use common\models\user\Town;
  25. class SettingController extends BaseController
  26. {
  27. public $enableCsrfValidation = false;
  28. public function actionIndex()
  29. {
  30. if (Yii::$app->request->isAjax) {
  31. if (Yii::$app->request->isGet) {
  32. $detail = HappinessStore::getOne([['id' => $this->store_id], ['mall_id' => $this->mall_id]], true);
  33. $detail['province'] = '';
  34. $detail['city'] = '';
  35. $detail['district'] = '';
  36. $detail['nickname'] = '';
  37. $detail['avatar_url'] = '';
  38. if ($detail) {
  39. $user = User::findOne(['id' => $detail['user_id']]);
  40. $detail['nickname'] = $user['nickname'];
  41. $detail['avatar_url'] = $user['avatar_url'];
  42. $region_id_arr = [$detail['province_id'], $detail['city_id'], $detail['district_id']];
  43. $region_list = Region::lists(['where' => [['id' => $region_id_arr]], 'index' => 'id', 'select' => 'id,name']);
  44. if (!empty($detail['province_id'])) {
  45. $detail['province'] = $region_list[$detail['province_id']]['name'];
  46. $detail['city'] = $region_list[$detail['city_id']]['name'];
  47. $detail['district'] = $region_list[$detail['district_id']]['name'];
  48. }
  49. }
  50. $detail['address_detail'] = $detail['address'] ?? '';
  51. $detail['ll'] = $detail['latitude'] . ',' . $detail['longitude'];
  52. if (!empty($detail['freight_type'])) {
  53. $detail['freight_type'] = json_decode($detail['freight_type'], true);
  54. } else {
  55. $detail['freight_type'] = [
  56. 'express_delivery' => 0,
  57. 'merchant_distribution' => 0,
  58. 'self_mention' => 0,
  59. ];
  60. }
  61. if (!empty($detail['intra_city_distribution'])) {
  62. $detail['intra_city_distribution'] = json_decode($detail['intra_city_distribution'], true);
  63. } else {
  64. $detail['intra_city_distribution'] = [];
  65. }
  66. if (!empty($detail['introduce'])) {
  67. $detail['introduce'] = json_decode($detail['introduce'], true);
  68. } else {
  69. $detail['introduce'] = [];
  70. }
  71. if (!empty($detail['bannar_pic'])) {
  72. $detail['bannar_pic'] = json_decode($detail['bannar_pic'], true);
  73. } else {
  74. $detail['bannar_pic'] = [];
  75. }
  76. if (!isset($detail['show_style'])) {
  77. $detail['show_style'] = "1";
  78. }
  79. if (isset($detail['pickup'])) {
  80. $detail['pickup'] = json_decode($detail['pickup'], true);
  81. } else {
  82. $detail['pickup'] = ['status' => 0];
  83. }
  84. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  85. $configs = [
  86. 'delivery_amount' => $setting['delivery_amount'], //起送金额
  87. 'delivery_fee' => $setting['delivery_fee'], //配送费设置
  88. 'start_limit' => $setting['start_limit'], //初始公里数
  89. 'start_amount' => $setting['start_amount'], //初始配送费
  90. 'add_limit' => $setting['add_limit'], //每次增加配送费公里数
  91. 'add_amount' => $setting['add_amount'], //每次增加配送费数量
  92. ];
  93. if ($setting['is_location_limit'] == 1) {
  94. $configs['localtion_limit'] = $setting['localtion_limit'];
  95. } else {
  96. $configs['localtion_limit'] = '';
  97. }
  98. return $this->success('操作成功', ['detail' => $detail, 'configs' => $configs]);
  99. }
  100. if (Yii::$app->request->isPost) {
  101. $params = Yii::$app->request->post();
  102. $params['form']['id'] = $this->store_id;
  103. $params['form']['mall_id'] = $this->mall_id;
  104. $businessTimeStart = strtotime($params['form']['business_time_start']);
  105. $businessTimeEnd = strtotime($params['form']['business_time_end']);
  106. if ($businessTimeStart >= $businessTimeEnd) {
  107. return $this->error('营业时间,结束时间不能小于开始时间');
  108. }
  109. $res = HappinessStore::setData($params['form']);
  110. if ($res == false) return $this->success(Store::getStaticError());
  111. return $this->success('操作成功');
  112. }
  113. }
  114. return $this->render($this->action->id);
  115. }
  116. public function actionGoodsModel()
  117. {
  118. // 获取组件配置
  119. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
  120. $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
  121. $addons_arr = [];
  122. if (!empty($basic['store_agent_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_AGENT;
  123. if (!empty($basic['store_area_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_AREA;
  124. if (!empty($basic['store_distribution_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_DISTRIBUTION;
  125. if (!empty($basic['store_rebate_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_REBATE;
  126. if ($addons_setting) {
  127. foreach ($addons_setting as $key => $val) {
  128. if (!in_array($key, $addons_arr)) unset($addons_setting[$key]);
  129. }
  130. } else {
  131. $addons_setting = [];
  132. }
  133. if (\Yii::$app->request->isAjax) {
  134. if (\Yii::$app->request->isGet) {
  135. // 获取商品详情
  136. $goods_model = StoreModel::getOne([['mall_id' => $this->mall_id, 'store_id' => $this->store_id]], true);
  137. $content = [];
  138. if (!empty($goods_model['content'])) {
  139. $content = json_decode($goods_model['content'], true);
  140. }
  141. // 获取配置
  142. foreach ($content as $key => &$item) {
  143. if (isset($content[$key])) {
  144. $setting[$key] = $content[$key];
  145. }
  146. }
  147. // 获取等级
  148. $level_params = ['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'level,name'];
  149. $level_params['mall_id'] = $this->mall_id;
  150. $level = GlobalInvoke::exec(GlobalInvoke::GOODS_MODEL_LEVEL, $this->mall_id, $level_params);
  151. $commission_type_map = GoodsTypeEnum::getCommissionTypeMap($this->mall_id);
  152. unset($commission_type_map['digital']);
  153. $status = $goods_model['status'];
  154. return $this->success('操作成功', compact('status', 'level', 'setting', 'commission_type_map'));
  155. } else {
  156. $store_setting = StoreSettings::findOne(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]);
  157. if (!empty($store_setting)) {
  158. if ($store_setting['store_settle_type'] != 3 && $store_setting['store_settle_type'] != 4) {
  159. if (empty($store_setting['is_allow_editing'])) {
  160. return $this->error('分佣由平台承担,门店无权编辑');
  161. }
  162. }
  163. } else {
  164. if ($basic['settle_type'] != 3 && $basic['settle_type'] != 4) {
  165. return $this->error('分佣由平台承担,门店无权编辑');
  166. }
  167. }
  168. $params = Yii::$app->request->post('form');
  169. $status = 0;
  170. if (!empty($params['status'])) {
  171. $status = $params['status'];
  172. unset($params['status']);
  173. }
  174. $res = Yii::$app->services->validate->validate($params, [
  175. [array_keys($addons_setting), 'string']
  176. ]);
  177. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  178. try {
  179. if (isset($params['Agent'])) $params['Agent'] = json_decode($params['Agent'], true);
  180. if (isset($params['Distribution'])) $params['Distribution'] = json_decode($params['Distribution'], true);
  181. if (isset($params['Area'])) $params['Area'] = json_decode($params['Area'], true);
  182. if (isset($params['Rebate'])) $params['Rebate'] = json_decode($params['Rebate'], true);
  183. $param = [
  184. 'mall_id' => $this->mall_id,
  185. 'store_id' => $this->store_id,
  186. 'status' => $status,
  187. 'content' => json_encode($params),
  188. ];
  189. $res = StoreModel::setData($param);
  190. if ($res === false) return $this->error(StoreGoodsModel::getStaticError());
  191. return $this->success("保存成功");
  192. } catch (\Exception $e) {
  193. return $this->error("保存失败,msg=" . $e->getMessage());
  194. }
  195. }
  196. }
  197. return $this->render($this->action->id, ['addons_setting' => $addons_setting]);
  198. }
  199. public function actionFreightRules()
  200. {
  201. if (Yii::$app->request->isAjax) {
  202. if (Yii::$app->request->isGet) {
  203. $keyword = Yii::$app->request->get('keyword', '');
  204. $where = [['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'status' => StatusEnum::ENABLED]];
  205. $keyword && $where[] = ['like', 'name', $keyword . '%', false];
  206. $order = 'id desc';
  207. $params = compact('where', 'order');
  208. $page_data = StoreFreightRules::listPage($params);
  209. if ($page_data === false) return $this->error(StoreFreightRules::getStaticError());
  210. return $this->success('操作成功', $page_data);
  211. } else {
  212. $form = Yii::$app->request->post();
  213. $rules = [[['id'], 'required'], [['id'], 'integer']];
  214. switch ($form['type']) {
  215. case 'destroy':
  216. $form['status'] = StatusEnum::DELETE;
  217. $rules[] = [['status'], 'required'];
  218. break;
  219. case 'default':
  220. $rules[] = [['is_default'], 'required'];
  221. break;
  222. }
  223. $res = Yii::$app->services->validate->validate($form, $rules);
  224. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  225. $form['mall_id'] = $this->mall_id;
  226. $form['store_id'] = $this->store_id;
  227. $res = StoreFreightRules::setData($form);
  228. if ($res === false) return $this->error(FreightRules::getStaticError());
  229. return $this->success('操作成功', []);
  230. }
  231. }
  232. return $this->render($this->action->id);
  233. }
  234. public function actionFreightEdit()
  235. {
  236. if (Yii::$app->request->isAjax) {
  237. if (Yii::$app->request->isGet) {
  238. $get = Yii::$app->request->get();
  239. $res = Yii::$app->services->validate->validate($get, [[['id'], 'required'], [['id'], 'integer']]);
  240. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  241. $detail = StoreFreightRules::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id, 'store_id' => $this->store_id]], true);
  242. $detail['rule'] = json_decode($detail['rule'], true);
  243. return $this->success('操作成功', compact('detail'));
  244. } else {
  245. $form = Yii::$app->request->post('form');
  246. $res = Yii::$app->services->validate->validate($form, [
  247. [['id', 'select_com'], 'integer'],
  248. [['name', 'type', 'rule'], 'safe'],
  249. ]);
  250. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  251. $form['mall_id'] = $this->mall_id;
  252. $form['store_id'] = $this->store_id;
  253. $res = StoreFreightRules::setData($form);
  254. if ($res === false) return $this->error(StoreFreightRules::getStaticError());
  255. return $this->success('操作成功');
  256. }
  257. }
  258. return $this->render($this->action->id);
  259. }
  260. public function actionConfig()
  261. {
  262. if (\Yii::$app->request->isGet) {
  263. $decoration = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'decoration');
  264. $settings = [
  265. 'decoration' => $decoration,
  266. ];
  267. return $this->success('success', $settings);
  268. }
  269. }
  270. //todo 不用验证权限
  271. public function actionGetAddressList()
  272. {
  273. $get = Yii::$app->request->get();
  274. $level = $get['level'] ?? 1;
  275. if ($level > 3) {
  276. if ($level == 4) {
  277. $where[] = ['district_id' => $get['id']];
  278. $params['where'] = $where;
  279. $params['select'] = 'id,name';
  280. $list = Town::lists($params);
  281. } else {
  282. $where[] = ['mall_id' => $this->mall_id, 'street_id' => $get['id'], 'status' => 0];
  283. $params['where'] = $where;
  284. $params['select'] = 'id,community_name as name';
  285. $list = StoreCommunity::lists($params);
  286. $data = ['id' => '-1', 'name' => '其它社区'];
  287. $list[] = $data;
  288. }
  289. } else {
  290. if (!empty($get['id'])) $where[] = ['parent_id' => $get['id']];
  291. $where[] = ['level' => $level];
  292. $params['where'] = $where;
  293. $params['select'] = 'id,name,parent_id,level,lng,lat';
  294. $list = Region::lists($params);
  295. }
  296. return $this->success('操作成功', $list);
  297. }
  298. public function actionStoreCateTree()
  299. {
  300. if (Yii::$app->request->isAjax) {
  301. if (Yii::$app->request->isGet) {
  302. $cond = [['mall_id' => $this->mall_id]];
  303. $list = StoreCate::getCates($cond, [], true, 'id,name,parent_id');
  304. $list = StoreCate::getTreeCate($list);
  305. return $this->success('操作成功', compact('list'));
  306. }
  307. }
  308. }
  309. /**
  310. * 客服管理
  311. * @return string
  312. */
  313. public function actionCustomer()
  314. {
  315. if (Yii::$app->request->isAjax) {
  316. if (Yii::$app->request->isGet) {
  317. $setting = StoreSettings::find()
  318. ->select(['is_customer', 'customer_types'])->where(['mall_id' => $this->mall_id,
  319. 'store_id' => $this->store_id,
  320. 'status' => StatusEnum::ENABLED]
  321. )->asArray()->one();
  322. if (!empty($setting['customer_types'])) $setting['customer_types'] = json_decode($setting['customer_types'], true);
  323. return $this->success("请求成功", ['setting' => $setting]);
  324. }
  325. if (Yii::$app->request->isPost) {// post提交
  326. $is_customer = Yii::$app->request->post('is_customer', 0);
  327. $customer_types = Yii::$app->request->post('customer_types', '');
  328. if (!empty($customer_types)) $customer_types = json_encode($customer_types);
  329. $setting = StoreSettings::findOne(['mall_id' => $this->mall_id,
  330. 'store_id' => $this->store_id,
  331. 'status' => StatusEnum::ENABLED]
  332. );
  333. if (!$setting) {
  334. $setting = new StoreSettings();
  335. }
  336. $setting->mall_id = $this->mall_id;
  337. $setting->store_id = $this->store_id;
  338. $setting->is_customer = $is_customer;
  339. $setting->customer_types = $customer_types;
  340. if (!$setting->save()) {
  341. return $this->error("保存失败");
  342. }
  343. return $this->success("保存成功");
  344. }
  345. }
  346. return $this->render($this->action->id);
  347. }
  348. public function actionScoreDeduction()
  349. {
  350. try {
  351. if (Yii::$app->request->isAjax) {
  352. if (Yii::$app->request->isGet) {
  353. $score_deduction = (new StoreSettingService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]))->getScoreDeduction();
  354. return $this->success("请求成功", $score_deduction);
  355. }
  356. if (Yii::$app->request->isPost) {
  357. $params = Yii::$app->request->post();
  358. if (empty($params['score_give']['give_num'])) {
  359. $params['score_give']['give_num'] = 0;
  360. }
  361. if (!empty($params['score_deduction']['max_deduction_percent'])) {
  362. $platform_score_deduction = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'payment.score_deduction');
  363. $platform_score_deduction = json_decode($platform_score_deduction, true);
  364. $platform_score_deduction['max_deduction_percent'] =
  365. !empty($platform_score_deduction['store_limit_type']) && isset($platform_score_deduction['store_limit_stores_deduction'][$this->store_id]) && is_numeric($platform_score_deduction['store_limit_stores_deduction'][$this->store_id]) ?
  366. $platform_score_deduction['store_limit_stores_deduction'][$this->store_id] : $platform_score_deduction['max_deduction_percent'];
  367. if (isset($platform_score_deduction['max_deduction_percent']) && $params['score_deduction']['max_deduction_percent'] > $platform_score_deduction['max_deduction_percent']) {
  368. return $this->error("独立设置的最高抵扣比例不能大于平台最高抵扣比例");
  369. }
  370. }
  371. (new StoreSettingService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]))->updateScoreDeduction($params);
  372. return $this->success("保存成功");
  373. }
  374. }
  375. return $this->render($this->action->id);
  376. } catch (\Exception $e) {
  377. return $this->error($e->getMessage());
  378. }
  379. }
  380. public function actionScoreGiveApply()
  381. {
  382. try {
  383. if (!Yii::$app->request->isPost) throw new \Exception("非法请求");
  384. $store = Store::findOne(['mall_id' => $this->mall_id, 'id' => $this->store_id, 'status' => StatusEnum::ENABLED]);
  385. if (empty($store)) throw new \Exception("门店信息有误");
  386. StoreScoreStoreApply::saveData([
  387. 'mall_id' => $this->mall_id,
  388. 'store_id' => $this->store_id,
  389. 'store_user_id' => $store->user_id,
  390. 'store_c_id' => $store->c_id,
  391. 'score_method' => 1
  392. ]);
  393. return $this->success("提交申请成功");
  394. } catch (\Exception $e) {
  395. return $this->error($e->getMessage());
  396. }
  397. }
  398. public function actionScoreDeductionApply()
  399. {
  400. try {
  401. if (!Yii::$app->request->isPost) throw new \Exception("非法请求");
  402. $store = Store::findOne(['mall_id' => $this->mall_id, 'id' => $this->store_id, 'status' => StatusEnum::ENABLED]);
  403. if (empty($store)) throw new \Exception("门店信息有误");
  404. StoreScoreStoreApply::saveData([
  405. 'mall_id' => $this->mall_id,
  406. 'store_id' => $this->store_id,
  407. 'store_user_id' => $store->user_id,
  408. 'store_c_id' => $store->c_id,
  409. 'score_method' => 2
  410. ]);
  411. return $this->success("提交申请成功");
  412. } catch (\Exception $e) {
  413. return $this->error($e->getMessage());
  414. }
  415. }
  416. }