Config.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\admin\controller\unishop;
  3. use app\common\controller\Backend;
  4. use \app\admin\model\unishop\Config as ConfigModel;
  5. use think\Exception;
  6. /**
  7. * 系统配置
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Config extends Backend
  12. {
  13. /**
  14. * Config模型对象
  15. * @var \app\admin\model\unishop\Config
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new ConfigModel;
  22. }
  23. /**
  24. * 查看
  25. */
  26. public function index()
  27. {
  28. $siteList = [];
  29. $groupList = ConfigModel::getGroupList();
  30. foreach ($groupList as $k => $v) {
  31. $siteList[$k]['name'] = $k;
  32. $siteList[$k]['title'] = $v;
  33. $siteList[$k]['list'] = [];
  34. }
  35. foreach ($this->model->all() as $k => $v) {
  36. if (!isset($siteList[$v['group']])) {
  37. continue;
  38. }
  39. $value = $v->toArray();
  40. $value['title'] = __($value['title']);
  41. if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
  42. $value['value'] = explode(',', $value['value']);
  43. }
  44. $value['content'] = json_decode($value['content'], TRUE);
  45. $value['tip'] = htmlspecialchars($value['tip']);
  46. $siteList[$v['group']]['list'][] = $value;
  47. }
  48. $index = 0;
  49. foreach ($siteList as $k => &$v) {
  50. $v['active'] = !$index ? true : false;
  51. $index++;
  52. }
  53. $this->view->assign('siteList', $siteList);
  54. $this->view->assign('typeList', ConfigModel::getTypeList());
  55. $this->view->assign('groupList', ConfigModel::getGroupList());
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 添加
  60. */
  61. public function add()
  62. {
  63. if ($this->request->isPost()) {
  64. $params = $this->request->post("row/a");
  65. if ($params) {
  66. foreach ($params as $k => &$v) {
  67. $v = is_array($v) ? implode(',', $v) : $v;
  68. }
  69. try {
  70. if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
  71. $params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
  72. } else {
  73. $params['content'] = '';
  74. }
  75. $result = $this->model->create($params);
  76. if ($result !== false) {
  77. $this->success();
  78. } else {
  79. $this->error($this->model->getError());
  80. }
  81. } catch (Exception $e) {
  82. $this->error($e->getMessage());
  83. }
  84. }
  85. $this->error(__('Parameter %s can not be empty', ''));
  86. }
  87. return $this->view->fetch();
  88. }
  89. /**
  90. * 编辑
  91. * @param null $ids
  92. */
  93. public function edit($ids = NULL)
  94. {
  95. if ($this->request->isPost()) {
  96. $row = $this->request->post("row/a");
  97. if ($row) {
  98. $configList = [];
  99. foreach ($this->model->all() as $v) {
  100. if (isset($row[$v['name']])) {
  101. $value = $row[$v['name']];
  102. if (is_array($value) && isset($value['field'])) {
  103. $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
  104. } else {
  105. $value = is_array($value) ? implode(',', $value) : $value;
  106. }
  107. $v['value'] = $value;
  108. $configList[] = $v->toArray();
  109. }
  110. }
  111. $this->model->allowField(true)->saveAll($configList);
  112. $this->success();
  113. }
  114. $this->error(__('Parameter %s can not be empty', ''));
  115. }
  116. }
  117. public function del($ids = "")
  118. {
  119. $name = $this->request->request('name');
  120. $config = ConfigModel::getByName($name);
  121. if ($config) {
  122. try {
  123. $config->delete();
  124. } catch (Exception $e) {
  125. $this->error($e->getMessage());
  126. }
  127. $this->success();
  128. } else {
  129. $this->error(__('Invalid parameters'));
  130. }
  131. }
  132. /**
  133. * 检测配置项是否存在
  134. * @internal
  135. */
  136. public function check()
  137. {
  138. $params = $this->request->post("row/a");
  139. if ($params) {
  140. $config = $this->model->get($params);
  141. if (!$config) {
  142. return $this->success();
  143. } else {
  144. return $this->error(__('Name already exist'));
  145. }
  146. } else {
  147. return $this->error(__('Invalid parameters'));
  148. }
  149. }
  150. }