Config.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace app\cmgadm\controller\general;
  3. use app\common\controller\Backend;
  4. use app\common\library\Email;
  5. use app\common\model\Config as ConfigModel;
  6. use think\Exception;
  7. use think\Validate;
  8. use Think\Db;
  9. /**
  10. * 系统配置
  11. *
  12. * @icon fa fa-cogs
  13. * @remark 可以在此增改系统的变量和分组,也可以自定义分组和变量,如果需要删除请从数据库中删除
  14. */
  15. class Config extends Backend
  16. {
  17. /**
  18. * @var \app\common\model\Config
  19. */
  20. protected $model = null;
  21. protected $noNeedRight = ['check', 'rulelist'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('Config');
  26. }
  27. /**
  28. * 查看
  29. */
  30. public function index()
  31. {
  32. $siteList = [];
  33. $groupList = ConfigModel::getGroupList();
  34. foreach ($groupList as $k => $v) {
  35. $siteList[$k]['name'] = $k;
  36. $siteList[$k]['title'] = $v;
  37. $siteList[$k]['list'] = [];
  38. }
  39. foreach ($this->model->all() as $k => $v) {
  40. if (!isset($siteList[$v['group']])) {
  41. continue;
  42. }
  43. $value = $v->toArray();
  44. $value['title'] = __($value['title']);
  45. if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
  46. $value['value'] = explode(',', $value['value']);
  47. }
  48. $value['content'] = json_decode($value['content'], true);
  49. $value['tip'] = htmlspecialchars($value['tip']);
  50. $siteList[$v['group']]['list'][] = $value;
  51. }
  52. $index = 0;
  53. foreach ($siteList as $k => &$v) {
  54. $v['active'] = !$index ? true : false;
  55. $index++;
  56. }
  57. $this->view->assign('siteList', $siteList);
  58. $this->view->assign('typeList', ConfigModel::getTypeList());
  59. $this->view->assign('ruleList', ConfigModel::getRegexList());
  60. $this->view->assign('groupList', ConfigModel::getGroupList());
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 添加
  65. */
  66. public function add()
  67. {
  68. if ($this->request->isPost()) {
  69. $this->token();
  70. $params = $this->request->post("row/a");
  71. if ($params) {
  72. foreach ($params as $k => &$v) {
  73. $v = is_array($v) ? implode(',', $v) : $v;
  74. }
  75. try {
  76. if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
  77. $params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
  78. } else {
  79. $params['content'] = '';
  80. }
  81. $result = $this->model->create($params);
  82. if ($result !== false) {
  83. try {
  84. $this->refreshFile();
  85. } catch (Exception $e) {
  86. $this->error($e->getMessage());
  87. }
  88. $this->success();
  89. } else {
  90. $this->error($this->model->getError());
  91. }
  92. } catch (Exception $e) {
  93. $this->error($e->getMessage());
  94. }
  95. }
  96. $this->error(__('Parameter %s can not be empty', ''));
  97. }
  98. return $this->view->fetch();
  99. }
  100. /**
  101. * 编辑
  102. * @param null $ids
  103. */
  104. public function edit($ids = null)
  105. {
  106. if ($this->request->isPost()) {
  107. $this->token();
  108. $row = $this->request->post("row/a");
  109. if ($row) {
  110. $configList = [];
  111. foreach ($this->model->all() as $v) {
  112. if (isset($row[$v['name']])) {
  113. $value = $row[$v['name']];
  114. if (is_array($value) && isset($value['field'])) {
  115. $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
  116. } else {
  117. $value = is_array($value) ? implode(',', $value) : $value;
  118. }
  119. $v['value'] = $value;
  120. $configList[] = $v->toArray();
  121. }
  122. }
  123. $this->model->allowField(true)->saveAll($configList);
  124. try {
  125. $this->refreshFile();
  126. } catch (Exception $e) {
  127. $this->error($e->getMessage());
  128. }
  129. $this->success();
  130. }
  131. $this->error(__('Parameter %s can not be empty', ''));
  132. }
  133. }
  134. /**
  135. * 删除
  136. * @param string $ids
  137. */
  138. public function del($ids = "")
  139. {
  140. $name = $this->request->post('name');
  141. $config = ConfigModel::getByName($name);
  142. if ($name && $config) {
  143. try {
  144. $config->delete();
  145. $this->refreshFile();
  146. } catch (Exception $e) {
  147. $this->error($e->getMessage());
  148. }
  149. $this->success();
  150. } else {
  151. $this->error(__('Invalid parameters'));
  152. }
  153. }
  154. /**
  155. * 刷新配置文件
  156. */
  157. protected function refreshFile()
  158. {
  159. $config = [];
  160. foreach ($this->model->all() as $k => $v) {
  161. $value = $v->toArray();
  162. if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
  163. $value['value'] = explode(',', $value['value']);
  164. }
  165. if ($value['type'] == 'array') {
  166. $value['value'] = (array)json_decode($value['value'], true);
  167. }
  168. $config[$value['name']] = $value['value'];
  169. }
  170. file_put_contents(
  171. APP_PATH . 'extra' . DS . 'site.php',
  172. '<?php' . "\n\nreturn " . var_export($config, true) . ";"
  173. );
  174. }
  175. /**
  176. * 检测配置项是否存在
  177. * @internal
  178. */
  179. public function check()
  180. {
  181. $params = $this->request->post("row/a");
  182. if ($params) {
  183. $config = $this->model->get($params);
  184. if (!$config) {
  185. return $this->success();
  186. } else {
  187. return $this->error(__('Name already exist'));
  188. }
  189. } else {
  190. return $this->error(__('Invalid parameters'));
  191. }
  192. }
  193. /**
  194. * 规则列表
  195. * @internal
  196. */
  197. public function rulelist()
  198. {
  199. //主键
  200. $primarykey = $this->request->request("keyField");
  201. //主键值
  202. $keyValue = $this->request->request("keyValue", "");
  203. $keyValueArr = array_filter(explode(',', $keyValue));
  204. $regexList = \app\common\model\Config::getRegexList();
  205. $list = [];
  206. foreach ($regexList as $k => $v) {
  207. if ($keyValueArr) {
  208. if (in_array($k, $keyValueArr)) {
  209. $list[] = ['id' => $k, 'name' => $v];
  210. }
  211. } else {
  212. $list[] = ['id' => $k, 'name' => $v];
  213. }
  214. }
  215. return json(['list' => $list]);
  216. }
  217. /**
  218. * 发送测试邮件
  219. * @internal
  220. */
  221. public function emailtest()
  222. {
  223. $row = $this->request->post('row/a');
  224. $receiver = $this->request->post("receiver");
  225. if ($receiver) {
  226. if (!Validate::is($receiver, "email")) {
  227. $this->error(__('Please input correct email'));
  228. }
  229. \think\Config::set('site', array_merge(\think\Config::get('site'), $row));
  230. $email = new Email;
  231. $result = $email
  232. ->to($receiver)
  233. ->subject(__("This is a test mail"))
  234. ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content') . '</div>')
  235. ->send();
  236. if ($result) {
  237. $this->success();
  238. } else {
  239. $this->error($email->getError());
  240. }
  241. } else {
  242. return $this->error(__('Invalid parameters'));
  243. }
  244. }
  245. /* 清空数据 */
  246. function Initialize_data()
  247. {
  248. Db::execute('truncate table ct_user');
  249. Db::execute('truncate table ct_user_parent');
  250. Db::execute('truncate table ct_detailed_bonus');
  251. Db::execute('truncate table ct_detailed_cash');
  252. Db::execute('truncate table ct_detailed_shopping');
  253. Db::execute('truncate table ct_detailed_integral');
  254. Db::execute('truncate table ct_detailed_score');
  255. Db::execute('truncate table ct_finance');
  256. Db::execute('truncate table ct_recharge');
  257. Db::execute('truncate table ct_withdrawals');
  258. Db::execute('truncate table ct_bonus_count');
  259. Db::execute('truncate table ct_order');
  260. Db::execute('truncate table ct_order_comment');
  261. Db::execute('truncate table ct_order_goods');
  262. Db::execute('truncate table ct_order_log');
  263. Db::execute('truncate table ct_goods_cart');
  264. Db::execute('truncate table ct_user_address');
  265. Db::execute('truncate table ct_trade');
  266. Db::execute('truncate table ct_studio');
  267. Db::execute('truncate table ct_admin_log');
  268. db()->execute('update ct_goods set price1=price,istrade=0,userid=1 where id>0');
  269. $this->success("执行完毕",url('user/user/index'));
  270. }
  271. }