Config.php 8.4 KB

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