SystemGroupData.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2021 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. $value = [];
  172. foreach ($params as $key => $param) {
  173. foreach ($fields as $index => $field) {
  174. if ($key == $field["title"]) {
  175. if ($param == '')
  176. return app('json')->fail($field["name"] . "不能为空!");
  177. else {
  178. $value[$key]["type"] = $field["type"];
  179. $value[$key]["value"] = $param;
  180. }
  181. }
  182. }
  183. }
  184. $data = [
  185. "value" => json_encode($value),
  186. "sort" => $params["sort"],
  187. "status" => $params["status"]
  188. ];
  189. $this->services->update($id, $data);
  190. \crmeb\services\CacheService::clear();
  191. return app('json')->success('修改成功!');
  192. }
  193. /**
  194. * 删除指定资源
  195. *
  196. * @param int $id
  197. * @return \think\Response
  198. */
  199. public function delete($id)
  200. {
  201. if (!$this->services->delete($id))
  202. return app('json')->fail('删除失败,请稍候再试!');
  203. else {
  204. \crmeb\services\CacheService::clear();
  205. return app('json')->success('删除成功!');
  206. }
  207. }
  208. /**
  209. * 修改状态
  210. * @param $id
  211. * @param $status
  212. * @return mixed
  213. */
  214. public function set_status($id, $status)
  215. {
  216. if ($status == '' || $id == 0) return app('json')->fail('参数错误');
  217. $this->services->update($id, ['status' => $status]);
  218. \crmeb\services\CacheService::clear();
  219. return app('json')->success($status == 0 ? '隐藏成功' : '显示成功');
  220. }
  221. /**
  222. * 检查秒杀时间段
  223. * @param SystemGroupServices $services
  224. * @param $gid
  225. * @param $params
  226. * @param int $id
  227. * @return mixed
  228. */
  229. public function checkSeckillTime(SystemGroupServices $services, $gid, $params, $id = 0)
  230. {
  231. $name = $services->value(['id' => $gid], 'config_name');
  232. if ($name == 'routine_seckill_time') {
  233. if ($params['time'] == '') {
  234. throw new AdminException('请输入开始时间');
  235. }
  236. if (!$params['continued']) {
  237. throw new AdminException('请输入持续时间');
  238. }
  239. if (!preg_match('/^(\d|1\d|2[0-3])$/', $params['time'])) {
  240. throw new AdminException('请输入0-23点之前的整点数');
  241. }
  242. if (!preg_match('/^([1-9]|1\d|2[0-4])$/', $params['continued'])) {
  243. throw new AdminException('请输入1-24点之前的持续时间');
  244. }
  245. if (($params['time'] + $params['continued']) > 24) throw new AdminException('开始时间+持续时间不能大于24小时');
  246. $list = $this->services->getColumn(['gid' => $gid], 'value', 'id');
  247. if ($id) unset($list[$id]);
  248. $times = $time = [];
  249. foreach ($list as $item) {
  250. $info = json_decode($item, true);
  251. for ($i = 0; $i < $info['continued']['value']; $i++) {
  252. $times[] = $info['time']['value'] + $i;
  253. }
  254. }
  255. for ($i = 0; $i < $params['continued']; $i++) {
  256. $time[] = $params['time'] + $i;
  257. }
  258. foreach ($time as $v) {
  259. if (in_array($v, $times)) throw new AdminException('时段已占用');
  260. }
  261. }
  262. }
  263. /**
  264. * 检查签到配置
  265. * @param SystemGroupServices $services
  266. * @param $gid
  267. * @param $params
  268. * @param int $id
  269. * @return mixed
  270. */
  271. public function checkSign(SystemGroupServices $services, $gid, $params, $id = 0)
  272. {
  273. $name = $services->value(['id' => $gid], 'config_name');
  274. if ($name == 'sign_day_num') {
  275. if (!$params['sign_num']) {
  276. throw new AdminException('请输入签到赠送积分');
  277. }
  278. if (!preg_match('/^\+?[1-9]\d*$/', $params['sign_num'])) {
  279. throw new AdminException('请输入大于等于0的整数');
  280. }
  281. }
  282. }
  283. /**
  284. * 获取客服页面广告内容
  285. * @return mixed
  286. */
  287. public function getKfAdv()
  288. {
  289. /** @var CacheServices $cache */
  290. $cache = app()->make(CacheServices::class);
  291. $content = $cache->getDbCache('kf_adv', '');
  292. return app('json')->success(compact('content'));
  293. }
  294. /**
  295. * 设置客服页面广告内容
  296. * @return mixed
  297. */
  298. public function setKfAdv()
  299. {
  300. $content = $this->request->post('content');
  301. /** @var CacheServices $cache */
  302. $cache = app()->make(CacheServices::class);
  303. $cache->setDbCache('kf_adv', $content);
  304. return app('json')->success('设置成功');
  305. }
  306. public function saveAll()
  307. {
  308. $params = request()->post();
  309. if (!isset($params['config_name']) || !isset($params['data'])) {
  310. return app('json')->fail('缺少参数');
  311. }
  312. $this->services->saveAllData($params['data'], $params['config_name']);
  313. return app('json')->success('添加数据成功!');
  314. }
  315. /**
  316. * 获取用户协议内容
  317. * @return mixed
  318. */
  319. public function getUserAgreement()
  320. {
  321. /** @var CacheServices $cache */
  322. $cache = app()->make(CacheServices::class);
  323. $content = $cache->getDbCache('user_agreement', '');
  324. return app('json')->success(compact('content'));
  325. }
  326. /**
  327. * 设置用户协议内容
  328. * @return mixed
  329. */
  330. public function setUserAgreement()
  331. {
  332. $content = $this->request->post('content');
  333. /** @var CacheServices $cache */
  334. $cache = app()->make(CacheServices::class);
  335. $cache->setDbCache('user_agreement', $content);
  336. return app('json')->success('设置成功');
  337. }
  338. }