SystemGroupData.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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 app('json')->fail(100100);
  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(100100);
  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(400298);
  81. }
  82. if ($this->services->isGroupGidSave($gid, 7, 'sign_day_num')) {
  83. return app('json')->fail(400299);
  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(400188);
  103. }
  104. }
  105. }
  106. $this->services->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(400297);
  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(400189);
  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(100100);
  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->services->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(400297);
  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(100001);
  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(100008);
  203. else {
  204. \crmeb\services\CacheService::clear();
  205. return app('json')->success(100002);
  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(100100);
  217. $this->services->update($id, ['status' => $status]);
  218. \crmeb\services\CacheService::clear();
  219. return app('json')->success(100014);
  220. }
  221. /**
  222. * 检查签到配置
  223. * @param SystemGroupServices $services
  224. * @param $gid
  225. * @param $params
  226. * @param int $id
  227. * @return mixed
  228. */
  229. public function checkSign(SystemGroupServices $services, $gid, $params, $id = 0)
  230. {
  231. $name = $services->value(['id' => $gid], 'config_name');
  232. if ($name == 'sign_day_num') {
  233. if (!$params['sign_num']) {
  234. throw new AdminException(400196);
  235. }
  236. if (!preg_match('/^\+?[1-9]\d*$/', $params['sign_num'])) {
  237. throw new AdminException(400197);
  238. }
  239. }
  240. }
  241. /**
  242. * 获取客服页面广告内容
  243. * @return mixed
  244. */
  245. public function getKfAdv()
  246. {
  247. /** @var CacheServices $cache */
  248. $cache = app()->make(CacheServices::class);
  249. $content = $cache->getDbCache('kf_adv', '');
  250. return app('json')->success(compact('content'));
  251. }
  252. /**
  253. * 设置客服页面广告内容
  254. * @return mixed
  255. */
  256. public function setKfAdv()
  257. {
  258. $content = $this->request->post('content');
  259. /** @var CacheServices $cache */
  260. $cache = app()->make(CacheServices::class);
  261. $cache->setDbCache('kf_adv', $content);
  262. return app('json')->success(100014);
  263. }
  264. public function saveAll()
  265. {
  266. $params = request()->post();
  267. if (!isset($params['config_name']) || !isset($params['data'])) {
  268. return app('json')->fail(100100);
  269. }
  270. $this->services->saveAllData($params['data'], $params['config_name']);
  271. return app('json')->success(400295);
  272. }
  273. /**
  274. * 获取用户协议内容
  275. * @return mixed
  276. */
  277. public function getUserAgreement()
  278. {
  279. /** @var CacheServices $cache */
  280. $cache = app()->make(CacheServices::class);
  281. $content = $cache->getDbCache('user_agreement', '');
  282. return app('json')->success(compact('content'));
  283. }
  284. /**
  285. * 设置用户协议内容
  286. * @return mixed
  287. */
  288. public function setUserAgreement()
  289. {
  290. $content = $this->request->post('content');
  291. /** @var CacheServices $cache */
  292. $cache = app()->make(CacheServices::class);
  293. $cache->setDbCache('user_agreement', $content);
  294. return app('json')->success(100014);
  295. }
  296. }