DictConfigLogic.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\setting\dict;
  15. use app\common\logic\BaseLogic;
  16. use app\common\model\dict\DictConfig;
  17. /**
  18. * 字典数据逻辑
  19. * Class DictConfigLogic
  20. * @package app\adminapi\logic\DictConfig
  21. */
  22. class DictConfigLogic extends BaseLogic
  23. {
  24. /**
  25. * @notes 添加编辑
  26. * @param array $params
  27. * @return DictConfig|\think\Model
  28. */
  29. public static function save(array $params)
  30. {
  31. try {
  32. $data = [
  33. 'name' => $params['name'],
  34. 'value' => $params['value'],
  35. 'content' => $params['content'] ? json_encode($params['content']) : null,
  36. 'status' => $params['status'],
  37. 'remark' => $params['remark'] ?? '',
  38. ];
  39. //校验字段类型是否重复
  40. $id = !empty($params['id'])? $params['id'] : 0;
  41. if (DictConfig::where(['value' => $params['value']])->where('id', '<>', $id)->value('id')) {
  42. throw new \think\Exception('字段类型已存在'.$params['id']);
  43. }
  44. if (!empty($params['id'])) {
  45. return DictConfig::where(['id' => $params['id']])->update($data);
  46. } else {
  47. return DictConfig::create($data);
  48. }
  49. } catch (\Exception $e) {
  50. self::setError($e->getMessage());
  51. return false;
  52. }
  53. }
  54. /**
  55. * @notes 删除字典数据
  56. * @param array $params
  57. * @return bool
  58. */
  59. public static function delete(array $params)
  60. {
  61. return DictConfig::destroy($params['id']);
  62. }
  63. /**
  64. * @notes 获取字典数据详情
  65. * @param $params
  66. * @return array
  67. */
  68. public static function detail($params): array
  69. {
  70. $detail = DictConfig::findOrEmpty($params['id'])->toArray();
  71. $detail['content'] = $detail['content'] ? json_decode($detail['content'], true) : [];
  72. return $detail;
  73. }
  74. }