SettingController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace addons\Mch\client\controllers;
  3. use addons\Mch\common\enums\MchEnum;
  4. use addons\Mch\common\models\Mch;
  5. use addons\Mch\common\models\MchModel;
  6. use addons\Mch\common\models\MchSettings;
  7. use addons\Store\common\models\StoreCate;
  8. use common\enums\AddonsEnum;
  9. use common\enums\GoodsTypeEnum;
  10. use common\enums\StatusEnum;
  11. use common\globals\GlobalInvoke;
  12. use common\logic\common\AddonsLimit;
  13. use common\models\common\Region;
  14. use common\models\goods\FreightRules;
  15. use Yii;
  16. class SettingController extends BaseController
  17. {
  18. public $enableCsrfValidation = false;
  19. public function actionIndex()
  20. {
  21. if (Yii::$app->request->isAjax) {
  22. if (Yii::$app->request->isGet) {
  23. $detail = Mch::getOne([['id' => $this->mch_id], ['mall_id' => $this->mall_id]], true);
  24. $detail['cIds'] = [];
  25. $detail['province'] = 0;
  26. $detail['city'] = 0;
  27. $detail['district'] = 0;
  28. $detail['cIds'][] = [];
  29. $detail['address_detail'] = '';
  30. $detail['ll'] = $detail['latitude'] . ',' . $detail['longitude'];
  31. if (!empty($detail['freight_type'])){
  32. $detail['freight_type'] = json_decode($detail['freight_type'], true);
  33. }else{
  34. $detail['freight_type'] =[
  35. 'express_delivery'=>0,
  36. ];
  37. }
  38. if (!empty($detail['intra_city_distribution'])) $detail['intra_city_distribution'] = json_decode($detail['intra_city_distribution'], true);
  39. $detail['customer_service'] = !empty($detail['customer_service']) ? json_decode($detail['customer_service'], true) : ['web_service_url' => ''];
  40. return $this->success('操作成功', ['detail' => $detail]);
  41. }
  42. if (Yii::$app->request->isPost) {
  43. $params = Yii::$app->request->post();
  44. $params['form']['id'] = $this->mch_id;
  45. $params['form']['mall_id'] = $this->mall_id;
  46. // $params['form']['check_status'] = 0;
  47. $res = Mch::setData($params['form']);
  48. if ($res == false) return $this->success('操作失败');
  49. return $this->success('操作成功');
  50. }
  51. }
  52. return $this->render($this->action->id);
  53. }
  54. public function actionGoodsModel()
  55. {
  56. // 获取组件配置
  57. $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
  58. $addons_arr = [];
  59. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, MchEnum::ADDONS_NAME, 'basic');
  60. if(!empty($basic['store_agent_status'])) $addons_arr[] =AddonsEnum::ADDONS_NAME_AGENT;
  61. if(!empty($basic['store_area_status'])) $addons_arr[] =AddonsEnum::ADDONS_NAME_AREA;
  62. if(!empty($basic['store_distribution_status'])) $addons_arr[] =AddonsEnum::ADDONS_NAME_DISTRIBUTION;
  63. if(!empty($basic['store_boss_status'])) $addons_arr[] =AddonsEnum::ADDONS_NAME_BOSS;
  64. if($addons_setting){
  65. foreach ($addons_setting as $key=>$val){
  66. if(!in_array($key,$addons_arr)) unset($addons_setting[$key]);
  67. }
  68. }else{
  69. $addons_setting = [];
  70. }
  71. if (\Yii::$app->request->isAjax) {
  72. if (\Yii::$app->request->isGet) {
  73. // 获取商品详情
  74. $goods_model = MchModel::getOne([['mall_id' => $this->mall_id, 'mch_id' => $this->mch_id]], true);
  75. $content = [];
  76. if (!empty($goods_model['content'])) {
  77. $content = json_decode($goods_model['content'], true);
  78. }
  79. // 获取配置
  80. foreach ($content as $key => &$item) {
  81. if (isset($content[$key])) {
  82. $setting[$key] = json_decode($content[$key], true);
  83. }
  84. }
  85. // 获取等级
  86. $level_params = ['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'level,name'];
  87. $level_params['mall_id'] = $this->mall_id;
  88. $level = GlobalInvoke::exec(GlobalInvoke::GOODS_MODEL_LEVEL, $this->mall_id, $level_params);
  89. $commission_type_map = GoodsTypeEnum::getCommissionTypeMap($this->mall_id);
  90. unset($commission_type_map['digital']);
  91. $status = $goods_model['status']??0;
  92. return $this->success('操作成功', compact('status', 'level', 'setting', 'commission_type_map'));
  93. } else {
  94. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, MchEnum::ADDONS_NAME, 'basic');
  95. $mch_setting = MchSettings::findOne(['mch_id'=>$this->mch_id,'mall_id'=>$this->mall_id]);
  96. if(!empty($mch_setting)){
  97. if($mch_setting['store_settle_type']!=4){
  98. return $this->error('分佣由平台承担,您无权编辑');
  99. }
  100. }else{
  101. if($basic['settle_type']!=4){
  102. return $this->error('分佣由平台承担,您无权编辑');
  103. }
  104. }
  105. $params = Yii::$app->request->post('form');
  106. $status = 0;
  107. if (!empty($params['status'])) {
  108. $status = $params['status'];
  109. unset($params['status']);
  110. }
  111. $res = Yii::$app->services->validate->validate($params, [
  112. [array_keys($addons_setting), 'string']
  113. ]);
  114. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  115. try {
  116. $param = [
  117. 'mall_id' => $this->mall_id,
  118. 'mch_id' => $this->mch_id,
  119. 'status' => $status,
  120. 'content' => json_encode($params),
  121. ];
  122. $res = MchModel::setData($param);
  123. if ($res === false) return $this->error(MchModel::getStaticError());
  124. return $this->success("保存成功");
  125. } catch (\Exception $e) {
  126. return $this->error("保存失败,msg=" . $e->getMessage());
  127. }
  128. }
  129. }
  130. return $this->render($this->action->id, ['addons_setting' => $addons_setting]);
  131. }
  132. public function actionFreightRules()
  133. {
  134. if (Yii::$app->request->isAjax) {
  135. if (Yii::$app->request->isGet) {
  136. $keyword = Yii::$app->request->get('keyword', '');
  137. $where = [['mall_id' => $this->mall_id, 'mch_id' => $this->mch_id, 'status' => StatusEnum::ENABLED]];
  138. $keyword && $where[] = ['like', 'name', $keyword . '%', false];
  139. $order = 'id desc';
  140. $params = compact('where', 'order');
  141. $page_data = FreightRules::listPage($params);
  142. if($page_data === false) return $this->error(FreightRules::getStaticError());
  143. return $this->success('操作成功',$page_data);
  144. }else{
  145. $form = Yii::$app->request->post();
  146. $rules = [[['id'], 'required'], [['id'], 'integer']];
  147. switch ($form['type']) {
  148. case 'destroy':
  149. $form['status'] = StatusEnum::DELETE;
  150. $rules[] = [['status'], 'required'];
  151. break;
  152. case 'default':
  153. $rules[] = [['is_default'], 'required'];
  154. break;
  155. }
  156. $res = Yii::$app->services->validate->validate($form, $rules);
  157. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  158. $form['mall_id'] = $this->mall_id;
  159. $form['mch_id'] = $this->mch_id;
  160. $res = FreightRules::setData($form);
  161. if ($res === false) return $this->error(FreightRules::getStaticError());
  162. return $this->success('操作成功',[]);
  163. }
  164. }
  165. return $this->render($this->action->id);
  166. }
  167. public function actionFreightEdit(){
  168. if (Yii::$app->request->isAjax) {
  169. if (Yii::$app->request->isGet) {
  170. $get = Yii::$app->request->get();
  171. $res = Yii::$app->services->validate->validate($get, [[['id'], 'required'], [['id'], 'integer']]);
  172. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  173. $detail = FreightRules::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id, 'mch_id' => $this->mch_id]], true);
  174. $detail['rule'] = json_decode($detail['rule'],true);
  175. return $this->success('操作成功',compact('detail'));
  176. } else {
  177. $form = Yii::$app->request->post('form');
  178. $res = Yii::$app->services->validate->validate($form, [
  179. [['id','select_com','compute_type'], 'integer'],
  180. [['name', 'type', 'rule'], 'safe'],
  181. ]);
  182. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  183. $form['rule'] = json_decode($form['rule'], true);
  184. $form['mall_id'] = $this->mall_id;
  185. $form['mch_id'] = $this->mch_id;
  186. $res = FreightRules::setData($form);
  187. if ($res === false) return $this->error(FreightRules::getStaticError());
  188. return $this->success('操作成功');
  189. }
  190. }
  191. return $this->render($this->action->id);
  192. }
  193. //todo 不用验证权限
  194. public function actionGetAddressList()
  195. {
  196. $get = Yii::$app->request->get();
  197. if (!empty($get['id'])) $where[] = ['parent_id' => $get['id']];
  198. $where[] = ['level' => $get['level'] ?? 1];
  199. $params['where'] = $where;
  200. $params['select'] = 'id,name,parent_id,level';
  201. $list = Region::lists($params);
  202. return $this->success('操作成功', $list);
  203. }
  204. public function actionStoreCateTree()
  205. {
  206. if (Yii::$app->request->isAjax) {
  207. if (Yii::$app->request->isGet) {
  208. $cond = [['mall_id' => $this->mall_id]];
  209. $list = StoreCate::getCates($cond, [], true, 'id,name,parent_id');
  210. $list = StoreCate::getTreeCate($list);
  211. return $this->success('操作成功', compact('list'));
  212. }
  213. }
  214. }
  215. //修改密码
  216. public function actionUpdatePassword(){
  217. $params = Yii::$app->request->post();
  218. $res = Yii::$app->services->validate->validate($params, [
  219. [['old_pass','new_pass','check_pass'], 'required'],
  220. ]);
  221. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  222. if($params['new_pass']!=$params['check_pass']) return $this->error('新密码和确认密码不一致');
  223. if(!$this->admin->validatePassword($params['old_pass'])){
  224. return $this->error('旧密码不正确');
  225. }
  226. $this->admin->password = Yii::$app->security->generatePasswordHash($params['new_pass']);
  227. $res = $this->admin->save();
  228. if ($res === false) return $this->error('修改密码失败');
  229. return $this->success('操作成功');
  230. }
  231. }