ConfigService.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace app\admin\service\sys_manage;
  3. use app\model\SysConfig;
  4. use support\Db;
  5. use support\Request;
  6. class ConfigService
  7. {
  8. public static function configList(Request $request)
  9. {
  10. $page = $request->get('page', 1);
  11. $pageSize = $request->get('pageSize', 20);
  12. $keyword = $request->get('keyword', '');
  13. $rows = SysConfig::select('*')
  14. ->when($keyword != '', function ($query) use ($keyword) {
  15. $query->where('config_name', 'like', '%' . $keyword . '%');
  16. })
  17. ->orderBy('config_sort', 'DESC')
  18. ->forPage($page, $pageSize)
  19. ->get()
  20. ->toArray();
  21. $total = SysConfig::when($keyword != '', function ($query) use ($keyword) {
  22. $query->where('config_name', 'like', '%' . $keyword . '%');
  23. })->count();
  24. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  25. }
  26. public static function configInfo($id)
  27. {
  28. $config = SysConfig::where('config_id', $id)->first();
  29. if (!$config) {
  30. return json_fail('配置不存在');
  31. }
  32. return json_success('', $config->toArray());
  33. }
  34. /**
  35. * @Desc 添加配置
  36. * @Author Gorden
  37. * @Date 2024/2/22 15:57
  38. *
  39. * @param $params
  40. * @return \support\Response
  41. */
  42. public static function addConfig($params)
  43. {
  44. try {
  45. $data = [
  46. 'config_status' => $params['config_status'],
  47. 'config_category' => $params['config_category'],
  48. 'config_key' => $params['config_key'],
  49. 'config_name' => $params['config_name'],
  50. 'config_alias' => $params['config_alias'],
  51. 'config_value' => $params['config_value'],
  52. 'config_value_json' => !empty($params['config_value_json']) ? $params['config_value_json'] : '{}',
  53. 'config_sort' => $params['config_sort'] ?? 0,
  54. 'config_groupby' => $params['config_groupby'] ?? '',
  55. 'config_remark' => isset($params['config_remark']) ? format_string($params['config_remark']) : '',
  56. 'config_extend_json' => !empty($params['config_extend_json']) ? $params['config_extend_json'] : '{}',
  57. 'config_addtimes' => time()
  58. ];
  59. SysConfig::insert($data);
  60. } catch (\Exception $e) {
  61. return json_fail('添加配置失败');
  62. }
  63. return json_success('添加配置成功');
  64. }
  65. /**
  66. * @Desc 修改配置
  67. * @Author Gorden
  68. * @Date 2024/2/22 16:24
  69. *
  70. * @param $id
  71. * @param $params
  72. * @return \support\Response
  73. */
  74. public static function updateConfig($id, $params)
  75. {
  76. try {
  77. $data = [
  78. 'config_status' => $params['config_status'],
  79. 'config_category' => $params['config_category'],
  80. 'config_key' => $params['config_key'],
  81. 'config_name' => $params['config_name'],
  82. 'config_alias' => $params['config_alias'],
  83. 'config_value' => $params['config_value'],
  84. 'config_value_json' => !empty($params['config_value_json']) ? $params['config_value_json'] : '{}',
  85. 'config_sort' => $params['config_sort'],
  86. 'config_groupby' => $params['config_groupby'],
  87. 'config_remark' => format_string($params['config_remark']),
  88. 'config_extend_json' => !empty($params['config_extend_json']) ? $params['config_extend_json'] : '{}',
  89. ];
  90. SysConfig::where('config_id', $id)->update($data);
  91. } catch (\Exception $e) {
  92. return json_fail('修改配置失败');
  93. }
  94. return json_success('修改配置成功');
  95. }
  96. /**
  97. * @Desc 配置修改
  98. * @Author Gorden
  99. * @Date 2024/2/22 16:38
  100. *
  101. * @param $id
  102. * @param $params
  103. * @return \support\Response
  104. */
  105. public static function updateStatus($id, $params)
  106. {
  107. try {
  108. SysConfig::where('config_id', $id)->update(['config_status' => $params['config_status']]);
  109. } catch (\Exception $e) {
  110. return json_fail('配置状态修改失败');
  111. }
  112. return json_success('配置状态修改成功');
  113. }
  114. /**
  115. * @Desc 删除字段
  116. * @Author Gorden
  117. * @Date 2024/2/22 15:08
  118. *
  119. * @param $id
  120. * @return \support\Response
  121. */
  122. public static function delConfig(Request $request)
  123. {
  124. $ids = $request->post('config_id');
  125. if (!$ids) {
  126. return json_fail("数据错误");
  127. }
  128. try {
  129. if (is_array($ids)) {
  130. SysConfig::whereIn('config_id', $ids)->delete();
  131. } else {
  132. SysConfig::where('config_id', $ids)->delete();
  133. }
  134. } catch (\Exception $e) {
  135. return json_fail('配置删除失败');
  136. }
  137. return json_success('配置删除成功');
  138. }
  139. /**
  140. * @Desc APP 参数读取
  141. * @Author Gorden
  142. * @Date 2024/4/15 13:54
  143. *
  144. * @param $type
  145. * @return \support\Response|void
  146. */
  147. public static function getParams($type)
  148. {
  149. if ($type == 'sys') {
  150. $res = SysConfig::where('config_key', 'app-name')
  151. ->orWhere('config_key', 'app-version')
  152. ->orWhere('config_key', 'app-common-css')
  153. ->orWhere('config_key', 'app-license')
  154. ->orWhere('config_key', 'app-hide')
  155. ->get()
  156. ->toArray();
  157. $data = [];
  158. foreach ($res as $item) {
  159. $key = str_replace('-', '_', $item['config_key']);
  160. $data[$key] = $item['config_value'];
  161. }
  162. return json_success('', $data);
  163. } else {
  164. $res = SysConfig::where('config_key', $type)->select('config_status', 'config_value_json', 'config_key', 'config_value')->first();
  165. if (!empty($res)) {
  166. $res = $res->toArray();
  167. $valueJson = json_decode($res['config_value_json'], true);
  168. if (!in_array($res['config_key'], ['app-menu-shortcut', 'app-menu-navigation', 'app-menu-footer'])) {
  169. $data['status'] = $res['config_status'];
  170. $data['config_name'] = $res['config_value'];
  171. foreach ($valueJson as $key => $item) {
  172. $key = str_replace('-', '_', $key);
  173. $data[$key] = $item;
  174. }
  175. } else {
  176. $data = $valueJson;
  177. }
  178. return json_success('', $data);
  179. }
  180. }
  181. }
  182. public static function paramsSave(Request $request, $type)
  183. {
  184. $params = $request->post();
  185. try {
  186. Db::beginTransaction();
  187. if ($params['type'] == 'sys') {
  188. SysConfig::where('config_key', 'app-name')->update(['config_value' => $params['app_name']]);
  189. SysConfig::where('config_key', 'app-version')->update(['config_value' => $params['app_version']]);
  190. SysConfig::where('config_key', 'app-common-css')->update(['config_value' => $params['app_common_css']]);
  191. SysConfig::where('config_key', 'app-license')->update(['config_value' => $params['app_license']]);
  192. SysConfig::where('config_key', 'app-hide')->update(['config_value' => $params['app_hide']]);
  193. } else if (!in_array($params['type'], ['shortcutMenu', 'navigationMenu', 'footerMenu'])) {
  194. $data = [
  195. 'config_status' => $params['status'],
  196. 'config_value' => $params['config_name'],
  197. ];
  198. unset($params['status'], $params['type'], $params['config_name']);
  199. $data['config_value_json'] = json_encode($params);
  200. SysConfig::where('config_key', $type)->update($data);
  201. } else {
  202. foreach ($params['config_value_json'] as $item) {
  203. unset($item['isSet']);
  204. $data['config_value_json'][] = $item;
  205. }
  206. SysConfig::where('config_key', $type)->update($data);
  207. }
  208. Db::commit();
  209. return json_success('success');
  210. } catch (\Exception $e) {
  211. Db::rollBack();
  212. return json_fail('保存失败');
  213. }
  214. }
  215. }