SystemGroupData.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 crmeb\exceptions\AdminException;
  13. use app\services\other\CacheServices;
  14. use think\facade\App;
  15. use app\adminapi\controller\AuthController;
  16. use app\services\system\config\SystemGroupDataServices;
  17. use app\services\system\config\SystemGroupServices;
  18. /**
  19. * 数据管理
  20. * Class SystemGroupData
  21. * @package app\adminapi\controller\v1\setting
  22. */
  23. class SystemGroupData extends AuthController
  24. {
  25. /**
  26. * 构造方法
  27. * SystemGroupData constructor.
  28. * @param App $app
  29. * @param SystemGroupDataServices $services
  30. */
  31. public function __construct(App $app, SystemGroupDataServices $services)
  32. {
  33. parent::__construct($app);
  34. $this->services = $services;
  35. }
  36. /**
  37. * 获取数据列表头
  38. * @return mixed
  39. */
  40. public function header(SystemGroupServices $services)
  41. {
  42. [$gid, $config_name] = $this->request->getMore([
  43. ['gid', 0],
  44. ['config_name', '']
  45. ], true);
  46. if (!$gid && !$config_name) return $this->fail('参数错误');
  47. if (!$gid) {
  48. $gid = $services->value(['config_name' => $config_name], 'id');
  49. }
  50. return app('json')->success($services->getGroupDataTabHeader($gid));
  51. }
  52. /**
  53. * 显示资源列表
  54. *
  55. * @return \think\Response
  56. */
  57. public function index(SystemGroupServices $group)
  58. {
  59. $where = $this->request->getMore([
  60. ['gid', 0],
  61. ['status', ''],
  62. ['config_name', '']
  63. ]);
  64. if (!$where['gid'] && !$where['config_name']) return app('json')->fail('参数错误');
  65. if (!$where['gid']) {
  66. $where['gid'] = $group->value(['config_name' => $where['config_name']], 'id');
  67. }
  68. unset($where['config_name']);
  69. return app('json')->success($this->services->getGroupDataList($where));
  70. }
  71. /**
  72. * 显示创建资源表单页.
  73. *
  74. * @return \think\Response
  75. */
  76. public function create()
  77. {
  78. $gid = $this->request->param('gid/d');
  79. if ($this->services->isGroupGidSave($gid, 4, 'index_categy_images')) {
  80. return app('json')->fail('不能大于四个!');
  81. }
  82. if ($this->services->isGroupGidSave($gid, 7, 'sign_day_num')) {
  83. return app('json')->fail('签到天数配置不能大于7天');
  84. }
  85. return app('json')->success($this->services->createForm($gid));
  86. }
  87. /**
  88. * 保存新建的资源
  89. *
  90. * @return \think\Response
  91. */
  92. public function save(SystemGroupServices $services)
  93. {
  94. $params = request()->post();
  95. $gid = (int)$params['gid'];
  96. $group = $services->getOne(['id' => $gid], 'id,config_name,fields');
  97. if ($group && $group['config_name'] == 'order_details_images') {
  98. $groupDatas = $this->services->getColumn(['gid' => $gid], 'value', 'id');
  99. foreach ($groupDatas as $groupData) {
  100. $groupData = json_decode($groupData, true);
  101. if (isset($groupData['order_status']['value']) && $groupData['order_status']['value'] == $params['order_status']) {
  102. return app('json')->fail('已存在请不要重复添加');
  103. }
  104. }
  105. }
  106. $this->checkSeckillTime($services, $gid, $params);
  107. $this->checkSign($services, $gid, $params);
  108. $fields = json_decode($group['fields'], true) ?? [];
  109. $value = [];
  110. foreach ($params as $key => $param) {
  111. foreach ($fields as $index => $field) {
  112. if ($key == $field["title"]) {
  113. if ($param == "")
  114. return app('json')->fail($field["name"] . "不能为空!");
  115. else {
  116. $value[$key]["type"] = $field["type"];
  117. $value[$key]["value"] = $param;
  118. }
  119. }
  120. }
  121. }
  122. $data = [
  123. "gid" => $params['gid'],
  124. "add_time" => time(),
  125. "value" => json_encode($value),
  126. "sort" => $params["sort"] ?: 0,
  127. "status" => $params["status"]
  128. ];
  129. $this->services->save($data);
  130. \crmeb\services\CacheService::clear();
  131. return app('json')->success('添加数据成功!');
  132. }
  133. /**
  134. * 显示指定的资源
  135. *
  136. * @param int $id
  137. * @return \think\Response
  138. */
  139. public function read($id)
  140. {
  141. //
  142. }
  143. /**
  144. * 显示编辑资源表单页.
  145. *
  146. * @param int $id
  147. * @return \think\Response
  148. */
  149. public function edit($id)
  150. {
  151. $gid = $this->request->param('gid/d');
  152. if (!$gid) {
  153. return app('json')->fail('缺少参数');
  154. }
  155. return app('json')->success($this->services->updateForm((int)$gid, (int)$id));
  156. }
  157. /**
  158. * 保存更新的资源
  159. *
  160. * @param \think\Request $request
  161. * @param int $id
  162. * @return \think\Response
  163. */
  164. public function update(SystemGroupServices $services, $id)
  165. {
  166. $groupData = $this->services->get($id);
  167. $fields = $services->getValueFields((int)$groupData["gid"]);
  168. $params = request()->post();
  169. $this->checkSeckillTime($services, $groupData["gid"], $params, $id);
  170. $this->checkSign($services, $groupData["gid"], $params);
  171. foreach ($params as $key => $param) {
  172. foreach ($fields as $index => $field) {
  173. if ($key == $field["title"]) {
  174. if ($param == '')
  175. return app('json')->fail($field["name"] . "不能为空!");
  176. else {
  177. $value[$key]["type"] = $field["type"];
  178. $value[$key]["value"] = $param;
  179. }
  180. }
  181. }
  182. }
  183. $data = [
  184. "value" => json_encode($value),
  185. "sort" => $params["sort"],
  186. "status" => $params["status"]
  187. ];
  188. $this->services->update($id, $data);
  189. \crmeb\services\CacheService::clear();
  190. return app('json')->success('修改成功!');
  191. }
  192. /**
  193. * 删除指定资源
  194. *
  195. * @param int $id
  196. * @return \think\Response
  197. */
  198. public function delete($id)
  199. {
  200. if (!$this->services->delete($id))
  201. return app('json')->fail('删除失败,请稍候再试!');
  202. else {
  203. \crmeb\services\CacheService::clear();
  204. return app('json')->success('删除成功!');
  205. }
  206. }
  207. /**
  208. * 修改状态
  209. * @param $id
  210. * @param $status
  211. * @return mixed
  212. */
  213. public function set_status($id, $status)
  214. {
  215. if ($status == '' || $id == 0) return app('json')->fail('参数错误');
  216. $this->services->update($id, ['status' => $status]);
  217. \crmeb\services\CacheService::clear();
  218. return app('json')->success($status == 0 ? '隐藏成功' : '显示成功');
  219. }
  220. /**
  221. * 检查秒杀时间段
  222. * @param SystemGroupServices $services
  223. * @param $gid
  224. * @param $params
  225. * @param int $id
  226. * @return mixed
  227. */
  228. public function checkSeckillTime(SystemGroupServices $services, $gid, $params, $id = 0)
  229. {
  230. $name = $services->value(['id' => $gid], 'config_name');
  231. if ($name == 'routine_seckill_time') {
  232. if ($params['time'] == '') {
  233. throw new AdminException('请输入开始时间');
  234. }
  235. if (!$params['continued']) {
  236. throw new AdminException('请输入持续时间');
  237. }
  238. if (!preg_match('/^(\d|1\d|2[0-3])$/', $params['time'])) {
  239. throw new AdminException('请输入0-23点之前的整点数');
  240. }
  241. if (!preg_match('/^([1-9]|1\d|2[0-4])$/', $params['continued'])) {
  242. throw new AdminException('请输入1-24点之前的持续时间');
  243. }
  244. if (($params['time'] + $params['continued']) > 24) throw new AdminException('开始时间+持续时间不能大于24小时');
  245. $list = $this->services->getColumn(['gid' => $gid], 'value', 'id');
  246. if ($id) unset($list[$id]);
  247. $times = $time = [];
  248. foreach ($list as $item) {
  249. $info = json_decode($item, true);
  250. for ($i = 0; $i < $info['continued']['value']; $i++) {
  251. $times[] = $info['time']['value'] + $i;
  252. }
  253. }
  254. for ($i = 0; $i < $params['continued']; $i++) {
  255. $time[] = $params['time'] + $i;
  256. }
  257. foreach ($time as $v) {
  258. if (in_array($v, $times)) throw new AdminException('时段已占用');
  259. }
  260. }
  261. }
  262. /**
  263. * 检查签到配置
  264. * @param SystemGroupServices $services
  265. * @param $gid
  266. * @param $params
  267. * @param int $id
  268. * @return mixed
  269. */
  270. public function checkSign(SystemGroupServices $services, $gid, $params, $id = 0)
  271. {
  272. $name = $services->value(['id' => $gid], 'config_name');
  273. if ($name == 'sign_day_num') {
  274. if (!$params['sign_num']) {
  275. throw new AdminException('请输入签到赠送积分');
  276. }
  277. if (!preg_match('/^\+?[1-9]\d*$/', $params['sign_num'])) {
  278. throw new AdminException('请输入大于等于0的整数');
  279. }
  280. }
  281. }
  282. /**
  283. * 获取客服页面广告内容
  284. * @return mixed
  285. */
  286. public function getKfAdv()
  287. {
  288. /** @var CacheServices $cache */
  289. $cache = app()->make(CacheServices::class);
  290. $content = $cache->getDbCache('kf_adv', '');
  291. return app('json')->success(compact('content'));
  292. }
  293. /**
  294. * 设置客服页面广告内容
  295. * @return mixed
  296. */
  297. public function setKfAdv()
  298. {
  299. $content = $this->request->post('content');
  300. /** @var CacheServices $cache */
  301. $cache = app()->make(CacheServices::class);
  302. $cache->setDbCache('kf_adv', $content);
  303. return app('json')->success('设置成功');
  304. }
  305. public function saveAll()
  306. {
  307. $params = request()->post();
  308. if (!isset($params['config_name']) || !isset($params['data'])) {
  309. return app('json')->fail('缺少参数');
  310. }
  311. $this->services->saveAllData($params['data'], $params['config_name']);
  312. return app('json')->success('添加数据成功!');
  313. }
  314. }