SystemGroupData.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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\controller\admin\v1\system\config;
  12. use app\controller\admin\AuthController;
  13. use app\services\other\CacheServices;
  14. use app\services\system\config\SystemGroupDataServices;
  15. use app\services\system\config\SystemGroupServices;
  16. use crmeb\exceptions\AdminException;
  17. use think\exception\ValidateException;
  18. use think\facade\App;
  19. use function request;
  20. /**
  21. * 数据管理
  22. * Class SystemGroupData
  23. * @package app\controller\admin\v1\setting
  24. */
  25. class SystemGroupData extends AuthController
  26. {
  27. /**
  28. * 构造方法
  29. * SystemGroupData constructor.
  30. * @param App $app
  31. * @param SystemGroupDataServices $services
  32. */
  33. public function __construct(App $app, SystemGroupDataServices $services)
  34. {
  35. parent::__construct($app);
  36. $this->services = $services;
  37. }
  38. /**
  39. * 获取数据列表头
  40. * @return mixed
  41. */
  42. public function header(SystemGroupServices $services)
  43. {
  44. [$gid, $config_name] = $this->request->getMore([
  45. ['gid', 0],
  46. ['config_name', '']
  47. ], true);
  48. if (!$gid && !$config_name) return $this->fail('参数错误');
  49. if (!$gid) {
  50. $gid = $services->value(['config_name' => $config_name], 'id');
  51. }
  52. return $this->success($services->getGroupDataTabHeader($gid));
  53. }
  54. /**
  55. * 显示资源列表
  56. *
  57. * @param SystemGroupServices $group
  58. * @return \think\Response
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function index(SystemGroupServices $group)
  64. {
  65. $where = $this->request->getMore([
  66. ['gid', 0],
  67. ['status', ''],
  68. ['config_name', '']
  69. ]);
  70. if (!$where['gid'] && !$where['config_name']) return $this->fail('参数错误');
  71. if (!$where['gid']) {
  72. $where['gid'] = $group->value(['config_name' => $where['config_name']], 'id');
  73. }
  74. unset($where['config_name']);
  75. return $this->success($this->services->getGroupDataList($where));
  76. }
  77. /**
  78. * 显示创建资源表单页.
  79. *
  80. * @return \think\Response
  81. * @throws \FormBuilder\Exception\FormBuilderException
  82. */
  83. public function create()
  84. {
  85. $gid = $this->request->param('gid/d');
  86. if ($this->services->isGroupGidSave($gid, 4, 'index_categy_images')) {
  87. return $this->fail('不能大于四个!');
  88. }
  89. if ($this->services->isGroupGidSave($gid, 7, 'sign_day_num')) {
  90. return $this->fail('签到天数配置不能大于7天');
  91. }
  92. return $this->success($this->services->createForm($gid));
  93. }
  94. /**
  95. * 保存新建的资源
  96. *
  97. * @param SystemGroupServices $services
  98. * @return \think\Response
  99. */
  100. public function save(SystemGroupServices $services)
  101. {
  102. $params = request()->post();
  103. $gid = (int)$params['gid'];
  104. $group = $services->getOne(['id' => $gid], 'id,config_name,fields');
  105. if ($group && $group['config_name'] == 'order_details_images') {
  106. $groupDatas = $this->services->getColumn(['gid' => $gid], 'value', 'id');
  107. foreach ($groupDatas as $groupData) {
  108. $groupData = json_decode($groupData, true);
  109. if (isset($groupData['order_status']['value']) && $groupData['order_status']['value'] == $params['order_status']) {
  110. return $this->fail('已存在请不要重复添加');
  111. }
  112. }
  113. }
  114. $this->checkSeckillTime($services, $gid, $params);
  115. $this->checkSign($services, $gid, $params);
  116. $this->checkRecharge($services, $gid, $params);
  117. $fields = json_decode($group['fields'], true) ?? [];
  118. $value = [];
  119. foreach ($params as $key => $param) {
  120. foreach ($fields as $index => $field) {
  121. if ($key == $field["title"]) {
  122. if ($key != 'give_money' && $param == "")
  123. return $this->fail($field["name"] . "不能为空!");
  124. else {
  125. $value[$key]["type"] = $field["type"];
  126. $value[$key]["value"] = $key == 'give_money' && $param == "" ? 0 : $param;
  127. }
  128. }
  129. }
  130. }
  131. $data = [
  132. "gid" => $params['gid'],
  133. "add_time" => time(),
  134. "value" => json_encode($value),
  135. "sort" => $params["sort"] ?: 0,
  136. "status" => $params["status"]
  137. ];
  138. $this->services->save($data);
  139. $this->services->cacheUpdate($data);
  140. \crmeb\services\CacheService::clear();
  141. return $this->success('添加数据成功!');
  142. }
  143. /**
  144. * 显示指定的资源
  145. *
  146. * @param int $id
  147. * @return \think\Response
  148. */
  149. public function read($id)
  150. {
  151. //
  152. }
  153. /**
  154. * 显示编辑资源表单页.
  155. *
  156. * @param int $id
  157. * @return \think\Response
  158. */
  159. public function edit($id)
  160. {
  161. $gid = $this->request->param('gid/d');
  162. if (!$gid) {
  163. return $this->fail('缺少参数');
  164. }
  165. return $this->success($this->services->updateForm((int)$gid, (int)$id));
  166. }
  167. public function saveAll()
  168. {
  169. $params = request()->post();
  170. if (!isset($params['config_name']) || !isset($params['data'])) {
  171. return $this->fail('缺少参数');
  172. }
  173. $this->services->saveAllData($params['data'], $params['config_name']);
  174. return $this->success('添加数据成功!');
  175. }
  176. /**
  177. * 保存更新的资源
  178. *
  179. * @param \think\Request $request
  180. * @param int $id
  181. * @return \think\Response
  182. */
  183. public function update(SystemGroupServices $services, $id)
  184. {
  185. $groupData = $this->services->get($id);
  186. $fields = $services->getValueFields((int)$groupData["gid"]);
  187. $params = request()->post();
  188. $this->checkSeckillTime($services, $groupData["gid"], $params, $id);
  189. $this->checkSign($services, $groupData["gid"], $params);
  190. $this->checkRecharge($services, $groupData["gid"], $params);
  191. foreach ($params as $key => $param) {
  192. foreach ($fields as $index => $field) {
  193. if ($key == $field["title"]) {
  194. if ($key != 'give_money' && $param == '')
  195. return $this->fail($field["name"] . "不能为空!");
  196. else {
  197. $value[$key]["type"] = $field["type"];
  198. $value[$key]["value"] = $key == 'give_money' && $param == "" ? 0 : $param;
  199. }
  200. }
  201. }
  202. }
  203. $data = [
  204. "value" => json_encode($value),
  205. "sort" => $params["sort"],
  206. "status" => $params["status"]
  207. ];
  208. $this->services->update($id, $data);
  209. $this->services->cacheSaveValue($id, $data);
  210. \crmeb\services\CacheService::clear();
  211. return $this->success('修改成功!');
  212. }
  213. /**
  214. * 删除指定资源
  215. *
  216. * @param int $id
  217. * @return \think\Response
  218. */
  219. public function delete($id)
  220. {
  221. if (!$this->services->delete($id))
  222. return $this->fail('删除失败,请稍候再试!');
  223. else {
  224. \crmeb\services\CacheService::clear();
  225. $this->services->cacheDelById($id);
  226. return $this->success('删除成功!');
  227. }
  228. }
  229. /**
  230. * 修改状态
  231. * @param $id
  232. * @param $status
  233. * @return mixed
  234. */
  235. public function set_status($id, $status)
  236. {
  237. if ($status == '' || $id == 0) return $this->fail('参数错误');
  238. $this->services->update($id, ['status' => $status]);
  239. \crmeb\services\CacheService::clear();
  240. if ($status) {
  241. $this->services->cacheSaveValue($id, 'status', $status);
  242. } else {
  243. $this->services->cacheDelById($id);
  244. }
  245. return $this->success($status == 0 ? '隐藏成功' : '显示成功');
  246. }
  247. /**
  248. * 检查秒杀时间段
  249. * @param SystemGroupServices $services
  250. * @param $gid
  251. * @param $params
  252. * @param int $id
  253. * @return mixed
  254. */
  255. public function checkSeckillTime(SystemGroupServices $services, $gid, $params, $id = 0)
  256. {
  257. $name = $services->value(['id' => $gid], 'config_name');
  258. if ($name == 'routine_seckill_time') {
  259. if ($params['time'] == '') {
  260. throw new AdminException('请输入开始时间');
  261. }
  262. if (!$params['continued']) {
  263. throw new AdminException('请输入持续时间');
  264. }
  265. if (!preg_match('/^(\d|1\d|2[0-3])$/', $params['time'])) {
  266. throw new AdminException('请输入0-23点之前的整点数');
  267. }
  268. if (!preg_match('/^([1-9]|1\d|2[0-4])$/', $params['continued'])) {
  269. throw new AdminException('请输入1-24点之前的持续时间');
  270. }
  271. if (($params['time'] + $params['continued']) > 24) throw new AdminException('开始时间+持续时间不能大于24小时');
  272. $list = $this->services->getColumn(['gid' => $gid], 'value', 'id');
  273. if ($id) unset($list[$id]);
  274. $times = $time = [];
  275. foreach ($list as $item) {
  276. $info = json_decode($item, true);
  277. for ($i = 0; $i < $info['continued']['value']; $i++) {
  278. $times[] = $info['time']['value'] + $i;
  279. }
  280. }
  281. for ($i = 0; $i < $params['continued']; $i++) {
  282. $time[] = $params['time'] + $i;
  283. }
  284. foreach ($time as $v) {
  285. if (in_array($v, $times)) throw new AdminException('时段已占用');
  286. }
  287. }
  288. }
  289. /**
  290. * 检查签到配置
  291. * @param SystemGroupServices $services
  292. * @param $gid
  293. * @param $params
  294. * @param int $id
  295. * @return mixed
  296. */
  297. public function checkSign(SystemGroupServices $services, $gid, $params, $id = 0)
  298. {
  299. $name = $services->value(['id' => $gid], 'config_name');
  300. if ($name == 'sign_day_num') {
  301. if (!$params['sign_num']) {
  302. throw new AdminException('请输入签到赠送积分');
  303. }
  304. if (!preg_match('/^\+?[1-9]\d*$/', $params['sign_num'])) {
  305. throw new AdminException('请输入大于等于0的整数');
  306. }
  307. }
  308. }
  309. /**
  310. * 检查充值金额配置
  311. * @param SystemGroupServices $services
  312. * @param $gid
  313. * @param $params
  314. * @param int $id
  315. * @return mixed
  316. */
  317. public function checkRecharge(SystemGroupServices $services, $gid, $params, $id = 0)
  318. {
  319. $name = $services->value(['id' => $gid], 'config_name');
  320. if ($name == 'user_recharge_quota') {
  321. if (!$params['price']) {
  322. throw new AdminException('请输入售价');
  323. }
  324. if (!preg_match('/^[0-9]{0,8}+(.[0-9]{1,2})?$/', $params['price'])) {
  325. throw new AdminException('售价限制最大10位浮点型且最多两位小数');
  326. }
  327. if ($params['give_money'] && !preg_match('/^[0-9]{0,8}+(.[0-9]{1,2})?$/', $params['give_money'])) {
  328. throw new AdminException('赠送金额限制最大10位浮点型且最多两位小数');
  329. }
  330. if ($this->services->count(['gid' => $gid]) >= 20) {
  331. throw new AdminException('充值优惠条数最多添加20个');
  332. }
  333. }
  334. }
  335. /**
  336. * 获取客服页面广告内容
  337. * @return mixed
  338. */
  339. public function getKfAdv()
  340. {
  341. /** @var CacheServices $cache */
  342. $cache = app()->make(CacheServices::class);
  343. $content = $cache->getDbCache('kf_adv', '');
  344. return $this->success(compact('content'));
  345. }
  346. /**
  347. * 设置客服页面广告内容
  348. * @return mixed
  349. */
  350. public function setKfAdv()
  351. {
  352. $content = $this->request->post('content');
  353. /** @var CacheServices $cache */
  354. $cache = app()->make(CacheServices::class);
  355. $cache->setDbCache('kf_adv', $content);
  356. return $this->success('设置成功');
  357. }
  358. /**
  359. * 获取用户协议内容
  360. * @return mixed
  361. */
  362. public function getUserAgreement($type)
  363. {
  364. /** @var CacheServices $cache */
  365. $cache = app()->make(CacheServices::class);
  366. $content = $cache->getDbCache($type, '');
  367. return $this->success(compact('content'));
  368. }
  369. /**
  370. * 设置用户协议内容
  371. * @return mixed
  372. */
  373. public function setUserAgreement($type)
  374. {
  375. $content = $this->request->post('content');
  376. /** @var CacheServices $cache */
  377. $cache = app()->make(CacheServices::class);
  378. $cache->setDbCache($type, $content);
  379. if ('privacy' === $type) {
  380. $html = <<<HTML
  381. <!doctype html>
  382. <html class="x-admin-sm">
  383. <head>
  384. <meta charset="UTF-8">
  385. <title>隐私协议</title>
  386. <meta name="renderer" content="webkit|ie-comp|ie-stand">
  387. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  388. <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
  389. <meta http-equiv="Cache-Control" content="no-siteapp" />
  390. </head>
  391. <body class="index">
  392. $content
  393. </body>
  394. </html>
  395. HTML;
  396. $file_path = public_path() . 'protocol.html';
  397. if (!file_exists($file_path)) {
  398. try {
  399. mkdir(str_replace('\\', '/', $file_path), 0777, true);
  400. } catch (\Throwable $e) {
  401. throw new ValidateException('请手动在:'. public_path() . '创建空文件:protocol.html,注意检查文件权限');
  402. }
  403. }
  404. file_put_contents(public_path() . 'protocol.html', $html);
  405. }
  406. return $this->success('设置成功');
  407. }
  408. }