Config.php 8.3 KB

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