SystemGroupData.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. if ($group && $group['config_name'] == 'user_recharge_quota') {
  107. if ($params['price'] <= 0) return app('json')->fail('售价必须大于0');
  108. if ($params['give_money'] < 0) return app('json')->fail('赠送不能小于0');
  109. }
  110. $this->services->checkSeckillTime($services, $gid, $params);
  111. $this->checkSign($services, $gid, $params);
  112. $fields = json_decode($group['fields'], true) ?? [];
  113. $value = [];
  114. foreach ($params as $key => $param) {
  115. foreach ($fields as $index => $field) {
  116. if ($key == $field["title"]) {
  117. if ($param == "")
  118. return app('json')->fail(400297);
  119. else {
  120. $value[$key]["type"] = $field["type"];
  121. $value[$key]["value"] = $param;
  122. }
  123. }
  124. }
  125. }
  126. $data = [
  127. "gid" => $params['gid'],
  128. "add_time" => time(),
  129. "value" => json_encode($value),
  130. "sort" => $params["sort"] ?: 0,
  131. "status" => $params["status"]
  132. ];
  133. $this->services->save($data);
  134. \crmeb\services\CacheService::clear();
  135. return app('json')->success(400189);
  136. }
  137. /**
  138. * 显示指定的资源
  139. *
  140. * @param int $id
  141. * @return \think\Response
  142. */
  143. public function read($id)
  144. {
  145. //
  146. }
  147. /**
  148. * 显示编辑资源表单页.
  149. *
  150. * @param int $id
  151. * @return \think\Response
  152. */
  153. public function edit($id)
  154. {
  155. $gid = $this->request->param('gid/d');
  156. if (!$gid) {
  157. return app('json')->fail(100100);
  158. }
  159. return app('json')->success($this->services->updateForm((int)$gid, (int)$id));
  160. }
  161. /**
  162. * 保存更新的资源
  163. *
  164. * @param \think\Request $request
  165. * @param int $id
  166. * @return \think\Response
  167. */
  168. public function update(SystemGroupServices $services, $id)
  169. {
  170. $groupData = $this->services->get($id);
  171. $fields = $services->getValueFields((int)$groupData["gid"]);
  172. $params = request()->post();
  173. $group = $services->getOne(['id' => $params['gid']], 'id,config_name,fields');
  174. if ($group && $group['config_name'] == 'user_recharge_quota') {
  175. if ($params['price'] <= 0) return app('json')->fail('售价必须大于0');
  176. if ($params['give_money'] < 0) return app('json')->fail('赠送不能小于0');
  177. }
  178. $this->services->checkSeckillTime($services, $groupData["gid"], $params, $id);
  179. $this->checkSign($services, $groupData["gid"], $params);
  180. $value = [];
  181. foreach ($params as $key => $param) {
  182. foreach ($fields as $index => $field) {
  183. if ($key == $field["title"]) {
  184. if ($param == '')
  185. return app('json')->fail(400297);
  186. else {
  187. $value[$key]["type"] = $field["type"];
  188. $value[$key]["value"] = $param;
  189. }
  190. }
  191. }
  192. }
  193. $data = [
  194. "value" => json_encode($value),
  195. "sort" => $params["sort"],
  196. "status" => $params["status"]
  197. ];
  198. $this->services->update($id, $data);
  199. \crmeb\services\CacheService::clear();
  200. return app('json')->success(100001);
  201. }
  202. /**
  203. * 删除指定资源
  204. *
  205. * @param int $id
  206. * @return \think\Response
  207. */
  208. public function delete($id)
  209. {
  210. if (!$this->services->delete($id))
  211. return app('json')->fail(100008);
  212. else {
  213. \crmeb\services\CacheService::clear();
  214. return app('json')->success(100002);
  215. }
  216. }
  217. /**
  218. * 修改状态
  219. * @param $id
  220. * @param $status
  221. * @return mixed
  222. */
  223. public function set_status($id, $status)
  224. {
  225. if ($status == '' || $id == 0) return app('json')->fail(100100);
  226. $this->services->update($id, ['status' => $status]);
  227. \crmeb\services\CacheService::clear();
  228. return app('json')->success(100014);
  229. }
  230. /**
  231. * 检查签到配置
  232. * @param SystemGroupServices $services
  233. * @param $gid
  234. * @param $params
  235. * @param int $id
  236. * @return mixed
  237. */
  238. public function checkSign(SystemGroupServices $services, $gid, $params, $id = 0)
  239. {
  240. $name = $services->value(['id' => $gid], 'config_name');
  241. if ($name == 'sign_day_num') {
  242. if (!$params['sign_num']) {
  243. throw new AdminException(400196);
  244. }
  245. if (!preg_match('/^\+?[1-9]\d*$/', $params['sign_num'])) {
  246. throw new AdminException(400197);
  247. }
  248. }
  249. }
  250. /**
  251. * 获取客服页面广告内容
  252. * @return mixed
  253. */
  254. public function getKfAdv()
  255. {
  256. /** @var CacheServices $cache */
  257. $cache = app()->make(CacheServices::class);
  258. $content = $cache->getDbCache('kf_adv', '');
  259. return app('json')->success(compact('content'));
  260. }
  261. /**
  262. * 设置客服页面广告内容
  263. * @return mixed
  264. */
  265. public function setKfAdv()
  266. {
  267. $content = $this->request->post('content');
  268. /** @var CacheServices $cache */
  269. $cache = app()->make(CacheServices::class);
  270. $cache->setDbCache('kf_adv', $content);
  271. return app('json')->success(100014);
  272. }
  273. public function saveAll()
  274. {
  275. $params = request()->post();
  276. if (!isset($params['config_name']) || !isset($params['data'])) {
  277. return app('json')->fail(100100);
  278. }
  279. $this->services->saveAllData($params['data'], $params['config_name']);
  280. return app('json')->success(400295);
  281. }
  282. /**
  283. * 获取用户协议内容
  284. * @return mixed
  285. */
  286. public function getUserAgreement()
  287. {
  288. /** @var CacheServices $cache */
  289. $cache = app()->make(CacheServices::class);
  290. $content = $cache->getDbCache('user_agreement', '');
  291. return app('json')->success(compact('content'));
  292. }
  293. /**
  294. * 设置用户协议内容
  295. * @return mixed
  296. */
  297. public function setUserAgreement()
  298. {
  299. $content = $this->request->post('content');
  300. /** @var CacheServices $cache */
  301. $cache = app()->make(CacheServices::class);
  302. $cache->setDbCache('user_agreement', $content);
  303. return app('json')->success(100014);
  304. }
  305. }