SystemGroupData.php 10 KB

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