SystemConfig.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\setting;
  12. use app\adminapi\controller\AuthController;
  13. use app\Request;
  14. use app\services\system\config\SystemConfigServices;
  15. use app\services\system\config\SystemConfigTabServices;
  16. use think\facade\App;
  17. /**
  18. * 系统配置
  19. * Class SystemConfig
  20. * @package app\adminapi\controller\v1\setting
  21. */
  22. class SystemConfig extends AuthController
  23. {
  24. /**
  25. * SystemConfig constructor.
  26. * @param App $app
  27. * @param SystemConfigServices $services
  28. */
  29. public function __construct(App $app, SystemConfigServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 显示资源列表
  36. *
  37. * @return \think\Response
  38. */
  39. public function index()
  40. {
  41. $where = $this->request->getMore([
  42. ['tab_id', 0],
  43. ['status', -1]
  44. ]);
  45. if (!$where['tab_id']) {
  46. return app('json')->fail('参数错误');
  47. }
  48. if ($where['status'] == -1) {
  49. unset($where['status']);
  50. }
  51. return app('json')->success($this->services->getConfigList($where));
  52. }
  53. /**
  54. * 显示创建资源表单页.
  55. * @param $type
  56. * @return \think\Response
  57. */
  58. public function create()
  59. {
  60. [$type, $tabId] = $this->request->getMore([
  61. [['type', 'd'], ''],
  62. [['tab_id', 'd'], 1]
  63. ], true);
  64. return app('json')->success($this->services->createFormRule($type, $tabId));
  65. }
  66. /**
  67. * 保存新建的资源
  68. *
  69. * @return \think\Response
  70. */
  71. public function save()
  72. {
  73. $data = $this->request->postMore([
  74. 'menu_name',
  75. 'type',
  76. 'input_type',
  77. 'config_tab_id',
  78. 'parameter',
  79. 'upload_type',
  80. 'required',
  81. 'width',
  82. 'high',
  83. 'value',
  84. 'info',
  85. 'desc',
  86. 'sort',
  87. 'status',
  88. ]);
  89. if (!$data['info']) return app('json')->fail('请输入配置名称');
  90. if (!$data['menu_name']) return app('json')->fail('请输入字段名称');
  91. if (!$data['desc']) return app('json')->fail('请输入配置简介');
  92. if ($data['sort'] < 0) {
  93. $data['sort'] = 0;
  94. }
  95. if ($data['type'] == 'text') {
  96. if (!$data['width']) return app('json')->fail('请输入文本框的宽度');
  97. if ($data['width'] <= 0) return app('json')->fail('请输入正确的文本框的宽度');
  98. }
  99. if ($data['type'] == 'textarea') {
  100. if (!$data['width']) return app('json')->fail('请输入多行文本框的宽度');
  101. if (!$data['high']) return app('json')->fail('请输入多行文本框的高度');
  102. if ($data['width'] < 0) return app('json')->fail('请输入正确的多行文本框的宽度');
  103. if ($data['high'] < 0) return app('json')->fail('请输入正确的多行文本框的宽度');
  104. }
  105. if ($data['type'] == 'radio' || $data['type'] == 'checkbox') {
  106. if (!$data['parameter']) return app('json')->fail('请输入配置参数');
  107. $this->services->valiDateRadioAndCheckbox($data);
  108. }
  109. $data['value'] = json_encode($data['value']);
  110. $config = $this->services->getOne(['menu_name' => $data['menu_name']]);
  111. if ($config) {
  112. $this->services->update($config['id'], $data, 'id');
  113. } else {
  114. $this->services->save($data);
  115. }
  116. \crmeb\services\CacheService::clear();
  117. return app('json')->success('添加配置成功!');
  118. }
  119. /**
  120. * 显示指定的资源
  121. *
  122. * @param int $id
  123. * @return \think\Response
  124. */
  125. public function read($id)
  126. {
  127. if (!$id) {
  128. return app('json')->fail('参数错误,请重新打开');
  129. }
  130. $info = $this->services->getReadList((int)$id);
  131. return app('json')->success(compact('info'));
  132. }
  133. /**
  134. * 显示编辑资源表单页.
  135. *
  136. * @param int $id
  137. * @return \think\Response
  138. */
  139. public function edit($id)
  140. {
  141. return app('json')->success($this->services->editConfigForm((int)$id));
  142. }
  143. /**
  144. * 保存更新的资源
  145. *
  146. * @param int $id
  147. * @return \think\Response
  148. */
  149. public function update($id)
  150. {
  151. $type = request()->post('type');
  152. if ($type == 'text' || $type == 'textarea' || $type == 'radio' || ($type == 'upload' && (request()->post('upload_type') == 1 || request()->post('upload_type') == 3))) {
  153. $value = request()->post('value');
  154. } else {
  155. $value = request()->post('value/a');
  156. }
  157. if (!$value) $value = request()->post(request()->post('menu_name'));
  158. $data = $this->request->postMore(['status', 'info', 'desc', 'sort', 'config_tab_id', 'required', 'parameter', ['value', $value], 'upload_type', 'input_type']);
  159. if (!$this->services->get($id)) {
  160. return app('json')->fail('编辑的记录不存在!');
  161. }
  162. $data['value'] = json_encode($data['value']);
  163. $this->services->update($id, $data);
  164. \crmeb\services\CacheService::clear();
  165. return app('json')->success('修改成功!');
  166. }
  167. /**
  168. * 删除指定资源
  169. *
  170. * @param int $id
  171. * @return \think\Response
  172. */
  173. public function delete($id)
  174. {
  175. if (!$this->services->delete($id))
  176. return app('json')->fail('删除失败,请稍候再试!');
  177. else {
  178. \crmeb\services\CacheService::clear();
  179. return app('json')->success('删除成功!');
  180. }
  181. }
  182. /**
  183. * 修改状态
  184. * @param $id
  185. * @param $status
  186. * @return mixed
  187. */
  188. public function set_status($id, $status)
  189. {
  190. if ($status == '' || $id == 0) {
  191. return app('json')->fail('参数错误');
  192. }
  193. $this->services->update($id, ['status' => $status]);
  194. \crmeb\services\CacheService::clear();
  195. return app('json')->success($status == 0 ? '隐藏成功' : '显示成功');
  196. }
  197. /**
  198. * 基础配置
  199. * */
  200. public function edit_basics(Request $request)
  201. {
  202. $tabId = $this->request->param('tab_id', 1);
  203. if (!$tabId) {
  204. return app('json')->fail('参数错误');
  205. }
  206. $url = $request->baseUrl();
  207. return app('json')->success($this->services->getConfigForm($url, $tabId));
  208. }
  209. /**
  210. * 保存数据 true
  211. * */
  212. public function save_basics(Request $request)
  213. {
  214. $post = $this->request->post();
  215. foreach ($post as $k => $v) {
  216. if (is_array($v)) {
  217. $res = $this->services->getUploadTypeList($k);
  218. foreach ($res as $kk => $vv) {
  219. if ($kk == 'upload') {
  220. if ($vv == 1 || $vv == 3) {
  221. $post[$k] = $v[0];
  222. }
  223. }
  224. }
  225. }
  226. }
  227. validate(\app\adminapi\validate\setting\SystemConfigValidata::class)->check($post);
  228. $this->services->checkParam($request->baseUrl(), $post);
  229. if (isset($post['store_brokerage_ratio']) && isset($post['store_brokerage_two'])) {
  230. $num = $post['store_brokerage_ratio'] + $post['store_brokerage_two'];
  231. if ($num > 100) {
  232. return app('json')->fail('一二级返佣比例不能大于100%');
  233. }
  234. }
  235. if (isset($post['spread_banner'])) {
  236. $num = count($post['spread_banner']);
  237. if ($num > 5) {
  238. return app('json')->fail('分销海报不能多于5张');
  239. }
  240. }
  241. foreach ($post as $k => $v) {
  242. if ($k === 'user_extract_min_price' && !preg_match('/[0-9]$/', $v)) {
  243. return app('json')->fail('提现最低金额只能为数字!');
  244. }
  245. $config_one = $this->services->getOne(['menu_name' => $k]);
  246. if ($config_one) {
  247. $config_one['value'] = $v;
  248. $this->services->valiDateValue($config_one);
  249. $this->services->update($k, ['value' => json_encode($v)], 'menu_name');
  250. }
  251. }
  252. if (isset($post['wss_open'])) {
  253. $this->services->saveSslFilePath((int)$post['wss_open'], $post['wss_local_pk'] ?? '', $post['wss_local_cert'] ?? '');
  254. }
  255. \crmeb\services\CacheService::clear();
  256. return app('json')->success('修改成功');
  257. }
  258. /**
  259. * 获取系统设置头部分类
  260. * @param SystemConfigTabServices $services
  261. * @return mixed
  262. * @throws \think\db\exception\DataNotFoundException
  263. * @throws \think\db\exception\DbException
  264. * @throws \think\db\exception\ModelNotFoundException
  265. */
  266. public function header_basics(SystemConfigTabServices $services)
  267. {
  268. [$type, $pid] = $this->request->getMore([
  269. [['type', 'd'], 0],
  270. [['pid', 'd'], 0]
  271. ], true);
  272. if ($type == 3) {//其它分类
  273. $config_tab = [];
  274. } else {
  275. $config_tab = $services->getConfigTab($pid);
  276. }
  277. return app('json')->success(compact('config_tab'));
  278. }
  279. }