Config.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace app\admin\controller\general;
  3. use app\common\controller\Backend;
  4. /**
  5. * 系统配置
  6. *
  7. * @icon fa fa-circle-o
  8. */
  9. class Config extends Backend
  10. {
  11. protected $model = null;
  12. protected $noNeedRight = ['check'];
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. $this->model = model('Config');
  17. }
  18. public function index()
  19. {
  20. $siteList = [];
  21. $groupList = \app\admin\model\Config::getGroupList();
  22. foreach ($groupList as $k => $v)
  23. {
  24. $siteList[$k]['name'] = $k;
  25. $siteList[$k]['title'] = $v;
  26. $siteList[$k]['list'] = [];
  27. }
  28. foreach ($this->model->all() as $k => $v)
  29. {
  30. if (!isset($siteList[$v['group']]))
  31. {
  32. continue;
  33. }
  34. $value = $v->toArray();
  35. if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio']))
  36. {
  37. $value['value'] = explode(',', $value['value']);
  38. }
  39. if ($value['type'] == 'array')
  40. {
  41. $value['value'] = (array) json_decode($value['value'], TRUE);
  42. }
  43. $value['content'] = json_decode($value['content'], TRUE);
  44. $siteList[$v['group']]['list'][] = $value;
  45. }
  46. $index = 0;
  47. foreach ($siteList as $k => &$v)
  48. {
  49. $v['active'] = !$index ? true : false;
  50. $index++;
  51. }
  52. $this->view->assign('siteList', $siteList);
  53. $this->view->assign('typeList', \app\admin\model\Config::getTypeList());
  54. $this->view->assign('groupList', \app\admin\model\Config::getGroupList());
  55. return $this->view->fetch();
  56. }
  57. /**
  58. * 添加
  59. */
  60. public function add()
  61. {
  62. if ($this->request->isPost())
  63. {
  64. $this->code = -1;
  65. $params = $this->request->post("row/a");
  66. if ($params)
  67. {
  68. foreach ($params as $k => &$v)
  69. {
  70. $v = is_array($v) ? implode(',', $v) : $v;
  71. }
  72. try
  73. {
  74. if ($params['content'] && in_array($params['type'], ['select', 'selects', 'checkbox', 'radio']))
  75. {
  76. $content = explode("\r\n", $params['content']);
  77. $arr = [];
  78. foreach ($content as $k => &$v)
  79. {
  80. if (stripos($v, "|") !== false)
  81. {
  82. $item = explode('|', $v);
  83. $arr[$item[0]] = $item[1];
  84. }
  85. }
  86. $params['content'] = $arr ? json_encode($arr, JSON_UNESCAPED_UNICODE) : '';
  87. }
  88. else
  89. {
  90. $params['content'] = '';
  91. }
  92. $result = $this->model->create($params);
  93. if ($result !== false)
  94. {
  95. try
  96. {
  97. $this->refreshFile();
  98. $this->code = 1;
  99. }
  100. catch (Exception $e)
  101. {
  102. $this->msg = $e->getMessage();
  103. }
  104. }
  105. else
  106. {
  107. $this->msg = $this->model->getError();
  108. }
  109. }
  110. catch (think\Exception $e)
  111. {
  112. $this->msg = $e->getMessage();
  113. }
  114. }
  115. else
  116. {
  117. $this->msg = __('Parameter %s can not be empty', '');
  118. }
  119. return;
  120. }
  121. return $this->view->fetch();
  122. }
  123. public function edit($ids = NULL)
  124. {
  125. $this->code = -1;
  126. if ($this->request->isPost())
  127. {
  128. $params = $this->request->post("row/a");
  129. if ($params)
  130. {
  131. $configList = [];
  132. foreach ($this->model->all() as $k => $v)
  133. {
  134. if (isset($params[$v['name']]))
  135. {
  136. if ($v['type'] == 'array')
  137. {
  138. $fieldarr = $valuearr = [];
  139. $field = $params[$v['name']]['field'];
  140. $value = $params[$v['name']]['value'];
  141. foreach ($field as $m => $n)
  142. {
  143. if ($n != '')
  144. {
  145. $fieldarr[] = $field[$m];
  146. $valuearr[] = $value[$m];
  147. }
  148. }
  149. $params[$v['name']] = array_combine($fieldarr, $valuearr);
  150. $value = json_encode($params[$v['name']], JSON_UNESCAPED_UNICODE);
  151. }
  152. else
  153. {
  154. $value = is_array($params[$v['name']]) ? implode(',', $params[$v['name']]) : $params[$v['name']];
  155. }
  156. $configList[] = ['id' => $v['id'], 'value' => $value];
  157. }
  158. }
  159. $this->model->saveAll($configList);
  160. try
  161. {
  162. $this->refreshFile();
  163. $this->code = 1;
  164. }
  165. catch (Exception $e)
  166. {
  167. $this->msg = $e->getMessage();
  168. }
  169. }
  170. else
  171. {
  172. $this->msg = __('Parameter %s can not be empty', '');
  173. }
  174. return;
  175. }
  176. }
  177. protected function refreshFile()
  178. {
  179. $config = [];
  180. foreach ($this->model->all() as $k => $v)
  181. {
  182. $value = $v->toArray();
  183. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files']))
  184. {
  185. $value['value'] = explode(',', $value['value']);
  186. }
  187. if ($value['type'] == 'array')
  188. {
  189. $value['value'] = (array) json_decode($value['value'], TRUE);
  190. }
  191. $config[$value['name']] = $value['value'];
  192. }
  193. file_put_contents(APP_PATH . 'extra' . DS . 'site.php', '<?php' . "\n\nreturn " . var_export($config, true) . ";");
  194. }
  195. /**
  196. * @internal
  197. */
  198. public function check()
  199. {
  200. $params = $this->request->post("row/a");
  201. if ($params)
  202. {
  203. $config = $this->model->get($params);
  204. if (!$config)
  205. {
  206. return json(['ok' => '']);
  207. }
  208. else
  209. {
  210. return json(['error' => __('Name already exist')]);
  211. }
  212. }
  213. else
  214. {
  215. return json(['error' => __('Invalid parameters')]);
  216. }
  217. }
  218. }