SettingController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. namespace backend\modules\admin\controllers;
  3. use backend\controllers\AdminController;
  4. use common\enums\StatusEnum;
  5. use common\enums\SettingEnum;
  6. use common\models\common\PlatformSetting;
  7. use common\models\mall\Mall;
  8. use common\models\notice\Notice;
  9. use Yii;
  10. use yii\helpers\Json;
  11. class SettingController extends AdminController
  12. {
  13. public function actionIndex()
  14. {
  15. if (\Yii::$app->request->isAjax) {
  16. if (\Yii::$app->request->post()) {
  17. $setting = \Yii::$app->request->post('setting');
  18. $trans = \Yii::$app->db->beginTransaction();
  19. try {
  20. // 重新组合配置
  21. foreach ($setting as $group => $value) {
  22. foreach ($value as $name => $val) {
  23. $data[$group][$name] = $val['value'];
  24. }
  25. }
  26. // 检查发薪猫域名是否变化,如果变化修改nginx vhosts
  27. $beforeCatH5Url = PlatformSetting::getConfByGroup('system', true)['cat_h5_url']['value'] ?? '';
  28. if (!empty($data['system']['cat_h5_url']) && $beforeCatH5Url != $data['system']['cat_h5_url']) {
  29. //
  30. $isChangeSalaryUrl = true;
  31. }
  32. \Yii::$app->services->setting->platform->set($data);
  33. $trans->commit();
  34. // 创建发薪猫nginx vhosts文件
  35. if (!empty($isChangeSalaryUrl)) {
  36. try {
  37. \services\common\UpgradeService::makeSalaryVhosts();
  38. }catch (\Exception $e) {
  39. $data['system']['cat_h5_url'] = $beforeCatH5Url;
  40. \Yii::$app->services->setting->platform->set($data);
  41. $this->success('配置修改成功,发薪猫H5链接修改失败,原因:' . $e->getMessage());
  42. return;
  43. }
  44. }
  45. $this->success('操作成功');
  46. } catch (\Exception $e) {
  47. $trans->rollBack();
  48. \Yii::error('edit platformSetting error:' . $e->getMessage());
  49. $this->error('编辑失败');
  50. }
  51. } else {
  52. $system = PlatformSetting::getConfByGroup('system', true);
  53. $storage = PlatformSetting::getConfByGroup('storage', true);
  54. $storage_driver = PlatformSetting::getConfByGroup('storage_driver', true);
  55. $limit = PlatformSetting::getConfByGroup('limit', true);
  56. $mall_create_syn = PlatformSetting::getConfByGroup('mall_create_syn', true);
  57. $map = PlatformSetting::getConfByGroup('map', true);
  58. $sms = PlatformSetting::getConfByGroup('sms', true);
  59. $backups = PlatformSetting::getConfByGroup('backups', true);
  60. if(!empty($mall_create_syn['mode'])){
  61. $res = json_decode($mall_create_syn['mode']['value'],true);
  62. if(is_array($res)){
  63. $mall_create_syn['mode']['value'] = $res;
  64. }else{
  65. $mall_create_syn['mode']['value'] = [];
  66. }
  67. }else{
  68. $mall_create_syn['mode']['value'] = [];
  69. }
  70. $data = compact('system', 'storage', 'storage_driver', 'limit','mall_create_syn', 'map','sms', 'backups');
  71. $mallList = Mall::lists(['where'=>[
  72. ['status'=>StatusEnum::ENABLED],
  73. ['or',
  74. ['expired_at'=>0],
  75. ['>','expired_at',time()],
  76. ]
  77. ],'order'=>'id asc','select'=>'id,name']);
  78. $data['mallList'] = $mallList;
  79. $this->success('操作成功', $data);
  80. }
  81. }
  82. return $this->render($this->action->id);
  83. }
  84. /**
  85. * 支付设置
  86. * @Author: lun
  87. * @DateTime: 2022/2/24 0024 18:29
  88. * @Copyright: copyright (c) 2021 广东七件事集团
  89. * @return string
  90. */
  91. public function actionPay()
  92. {
  93. if (\Yii::$app->request->isAjax) {
  94. if (\Yii::$app->request->post()) {
  95. $setting = \Yii::$app->request->post('setting');
  96. // 重新组合配置
  97. $data = self::makeUpParams($setting);
  98. $res = Yii::$app->services->setting->set($data);
  99. if($res === false) return $this->error(Yii::$app->services->setting->mall->getError());
  100. $this->success('保存成功');
  101. } else {
  102. $params = \Yii::$app->request->get();
  103. // 检查提交的参数
  104. $res = Yii::$app->services->validate->validate($params,[
  105. [['type'], 'required'],
  106. [['type'], 'string']
  107. ]);
  108. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  109. // 获取分组
  110. $groups = SettingEnum::getPlatformType($params['type']);
  111. $data = PlatformSetting::getConfByGroup($groups, true);
  112. $return = self::returnArray($data);
  113. $this->success('获取成功', $return);
  114. }
  115. }
  116. return $this->render($this->action->id);
  117. }
  118. // 编辑或获取配置信息
  119. public function actionEdit()
  120. {
  121. if (\Yii::$app->request->isAjax) {
  122. if (\Yii::$app->request->post()) {
  123. $setting = \Yii::$app->request->post('setting');
  124. $trans = \Yii::$app->db->beginTransaction();
  125. try {
  126. // 重新组合配置
  127. foreach ($setting as $group => $value) {
  128. foreach ($value as $name => $val) {
  129. $data[$group][$name] = $val['value'];
  130. }
  131. }
  132. \Yii::$app->services->setting->platform->set($data);
  133. $trans->commit();
  134. $this->success('操作成功');
  135. } catch (\Exception $e) {
  136. $trans->rollBack();
  137. \Yii::error('edit platformSetting error:' . $e->getMessage());
  138. $this->error('编辑失败');
  139. }
  140. } else {
  141. $system = PlatformSetting::getConfByGroup('system', true);
  142. $storage = PlatformSetting::getConfByGroup('storage', true);
  143. $storage_driver = PlatformSetting::getConfByGroup('storage_driver', true);
  144. $limit = PlatformSetting::getConfByGroup('limit', true);
  145. $mall_create_syn = PlatformSetting::getConfByGroup('mall_create_syn', true);
  146. if(!empty($mall_create_syn['mode'])){
  147. $res = json_decode($mall_create_syn['mode']['value'],true);
  148. if(is_array($res)){
  149. $mall_create_syn['mode']['value'] = $res;
  150. }else{
  151. $mall_create_syn['mode']['value'] = [];
  152. }
  153. }else{
  154. $mall_create_syn['mode']['value'] = [];
  155. }
  156. $map = PlatformSetting::getConfByGroup('map', true);
  157. $data = compact('system', 'storage', 'storage_driver', 'limit','mall_create_syn','map','sms');
  158. $mallList = Mall::lists(['where'=>[
  159. ['status'=>StatusEnum::ENABLED],
  160. ['or',
  161. ['expired_at'=>0],
  162. ['>','expired_at',time()],
  163. ]
  164. ],'order'=>'id asc','select'=>'id,name']);
  165. $data['mallList'] = $mallList;
  166. $this->success('操作成功', $data);
  167. }
  168. }
  169. }
  170. /**
  171. * 教程中心
  172. * @Author: ltm
  173. * @DateTime: 2022/2/24 0024 18:30
  174. * @Copyright: copyright (c) 2021 广东七件事集团
  175. * @return string
  176. */
  177. public function actionCourse(){
  178. if (\Yii::$app->request->isAjax) {
  179. if (\Yii::$app->request->post()) {
  180. $setting = \Yii::$app->request->post('setting');
  181. $trans = \Yii::$app->db->beginTransaction();
  182. try {
  183. // 重新组合配置
  184. foreach ($setting as $group => $value) {
  185. foreach ($value as $name => $val) {
  186. $data[$group][$name] = $val['value'];
  187. }
  188. }
  189. \Yii::$app->services->setting->platform->set($data);
  190. $trans->commit();
  191. $this->success('操作成功');
  192. } catch (\Exception $e) {
  193. $trans->rollBack();
  194. \Yii::error('edit platformSetting error:' . $e->getMessage());
  195. $this->error('编辑失败');
  196. }
  197. } else {
  198. $system = PlatformSetting::getConfByGroup('system', true);
  199. $video_tutorials_map = PlatformSetting::getConfByGroup('video_tutorial', true);
  200. $video_tutorials=\Yii::$app->params['video_tutorial'];
  201. foreach ($video_tutorials as &$item){
  202. if(isset($video_tutorials_map['type'.$item['id']])){
  203. $value = json_decode($video_tutorials_map['type'.$item['id']]['value'],true);
  204. $item['name'] = $value['name'];
  205. $item['url'] = $value['url'];
  206. }
  207. }
  208. $system['video_tutorials'] = $video_tutorials;
  209. $this->success('操作成功', $system);
  210. }
  211. }
  212. return $this->render($this->action->id);
  213. }
  214. public function actionCourseVideo(){
  215. if (\Yii::$app->request->isAjax) {
  216. if (\Yii::$app->request->post()) {
  217. $post = \Yii::$app->request->post();
  218. try {
  219. $temp= ['id'=>$post['id']];
  220. $temp['url'] = $post['url']??'';
  221. $temp['name'] = $post['name']??'';
  222. $data['video_tutorial']['type'.$post['id']] = $temp;
  223. \Yii::$app->services->setting->platform->set($data);
  224. return $this->success('操作成功');
  225. } catch (\Exception $e) {
  226. \Yii::error('edit platformSetting error:' . $e->getMessage());
  227. $this->error('编辑失败');
  228. }
  229. }
  230. }
  231. }
  232. /**
  233. * @Author: ltm
  234. * @DateTime: 2022/3/16 0016 18:42
  235. * @Copyright: copyright (c) 2021 广东七件事集团
  236. * @param $data
  237. * @return string
  238. */
  239. public function actionNotice(){
  240. if (\Yii::$app->request->isAjax) {
  241. $get = \Yii::$app->request->get();
  242. // 检查提交的参数
  243. $res = \Yii::$app->services->validate->validate($get,[
  244. [['keyword'], 'safe'],
  245. [['limit'],'default','value' => '15'],
  246. ]);
  247. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  248. $where = [];
  249. if($get['keyword']) $where[] = ['like', 'content', $get['keyword']];
  250. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  251. $order = 'created_at desc';
  252. $params = array('where' => $where, 'order'=>$order, 'limit' => $get['limit']);
  253. $list = Notice::listPage($params, false, true);
  254. $this->success('获取成功', $list);
  255. }
  256. return $this->render($this->action->id);
  257. }
  258. public function actionNoticeEdit(){
  259. if (\Yii::$app->request->isAjax) {
  260. if (\Yii::$app->request->post()) {
  261. $post = \Yii::$app->request->post();
  262. // 检查提交的参数
  263. $res = \Yii::$app->services->validate->validate($post,[
  264. [['content'],'required'],
  265. [['id','status'], 'safe'],
  266. ]);
  267. if($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  268. $post = Notice::exceptNullVal($post);
  269. $res = Notice::setData($post);
  270. if($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  271. $this->success('操作成功');
  272. }
  273. }
  274. }
  275. public function actionNoticeDel(){
  276. if (\Yii::$app->request->isAjax) {
  277. if (\Yii::$app->request->post()) {
  278. $post = \Yii::$app->request->post();
  279. // 检查提交的参数
  280. $res = \Yii::$app->services->validate->validate($post,[
  281. [['id'],'required'],
  282. ]);
  283. if($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  284. $post['status'] = StatusEnum::DELETE;
  285. $post = Notice::exceptNullVal($post);
  286. $res = Notice::setData($post);
  287. if($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  288. $this->success('操作成功');
  289. }
  290. }
  291. }
  292. /**
  293. * @Author: ltm
  294. * @DateTime: 2022/3/16 0016 18:42
  295. * @Copyright: copyright (c) 2021 广东七件事集团
  296. * @param $data
  297. * @return string
  298. */
  299. public function actionNoticeList()
  300. {
  301. $retuest = \Yii::$app->request;
  302. if ($retuest->isAjax) {
  303. if ($retuest->isGet) {
  304. $get = \Yii::$app->request->get();
  305. // 检查提交的参数
  306. $res = \Yii::$app->services->validate->validate($get,[
  307. [['keyword'], 'safe'],
  308. [['limit'],'default','value' => '15'],
  309. ]);
  310. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  311. $where = [];
  312. if($get['keyword']) $where[] = ['like', 'content', $get['keyword']];
  313. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  314. $order = 'created_at desc';
  315. $params = array('where' => $where, 'order'=>$order, 'limit' => $get['limit']);
  316. $list = Notice::listPage($params, false, true);
  317. $this->success('获取成功', $list);
  318. }
  319. } else {
  320. return $this->render($this->action->id);
  321. }
  322. }
  323. // 编辑或获取配置信息
  324. public function actionSettingEdit()
  325. {
  326. if (\Yii::$app->request->isAjax) {
  327. if (\Yii::$app->request->post()) {
  328. $setting = \Yii::$app->request->post('setting');
  329. // 重新组合配置
  330. $data = self::makeUpParams($setting);
  331. $res = Yii::$app->services->setting->set($data);
  332. if($res === false) return $this->error(Yii::$app->services->setting->mall->getError());
  333. $this->success('保存成功');
  334. } else {
  335. $params = \Yii::$app->request->get();
  336. // 检查提交的参数
  337. $res = Yii::$app->services->validate->validate($params,[
  338. [['type'], 'required'],
  339. [['type'], 'string']
  340. ]);
  341. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  342. // 获取分组
  343. $groups = SettingEnum::getPlatformType($params['type']);
  344. $data = PlatformSetting::getConfByGroup($groups, true);
  345. $return = self::returnArray($data);
  346. $this->success('获取成功', $return);
  347. }
  348. }
  349. }
  350. /**
  351. * 数组重组
  352. * @param array $params
  353. * @return array
  354. */
  355. private static function makeUpParams(array $params)
  356. {
  357. $data = [];
  358. // 重新组合配置
  359. foreach ($params as $group => $value) {
  360. foreach ($value as $name => $val) {
  361. $data[$group][$name] = $val['value'];
  362. }
  363. }
  364. return $data;
  365. }
  366. /**
  367. * 字符串转数组
  368. * @param array $data
  369. * @param string $changeText
  370. * @return array
  371. */
  372. private static function returnArray(array $data, $changeText = 'checkbox')
  373. {
  374. foreach ($data as $key => $item) {
  375. foreach ($item as $k => $value) {
  376. $type = $value['type'] ?? '';
  377. if ($type == $changeText) {
  378. $data[$key][$k]['value'] = Json::decode($value['value'], true);
  379. }
  380. }
  381. }
  382. return $data;
  383. }
  384. }