System.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * lemocms
  4. * ============================================================================
  5. * 版权所有 2018-2027 lemocms,并保留所有权利。
  6. * 网站地址: https://www.lemocms.com
  7. * ----------------------------------------------------------------------------
  8. * 采用最新Thinkphp6实现
  9. * ============================================================================
  10. * Author: yuege
  11. * Date: 2019/8/2
  12. */
  13. namespace app\admin\controller;
  14. use app\common\controller\Backend;
  15. use app\common\model\Config as ConfigModel;
  16. use app\common\model\ConfigGroup as ConfigGroupModel;
  17. use app\common\model\FieldType;
  18. use think\facade\Request;
  19. use think\facade\Db;
  20. use think\facade\View;
  21. class System extends Backend {
  22. public $rules = [
  23. ['name'=>'email','title'=>'邮件'],
  24. ['name'=>'url','title'=>'网址'],
  25. ['name'=>'number','title'=>'有效的数值'],
  26. ['name'=>'ip','title'=>'IP'],
  27. ['name'=>'date','title'=>'日期'],
  28. ['name'=>'phone','title'=>'手机号'],
  29. ['name'=>'qq','title'=>'QQ'],
  30. ['name'=>'identity','title'=>'身份证号'],
  31. ];
  32. public function initialize()
  33. {
  34. parent::initialize(); // TODO: Change the autogenerated stub
  35. }
  36. public function index(){
  37. $group = ['site','email','upload','sms'];
  38. $list = Db::name('config')
  39. ->where('type','in',$group)
  40. ->field('code,value')
  41. ->column('value','code');
  42. View::assign('config',json_encode($list));
  43. return View::fetch();
  44. }
  45. //配置设置
  46. public function site()
  47. {
  48. if (Request::isPost()) {
  49. $data = Request::post();
  50. foreach ($data as $k=>$v){
  51. $res = Db::name('config')->where('code',$k)->update(['value'=>$v]);
  52. }
  53. $this->success('save success');
  54. } else {
  55. $this->error('invalid options');
  56. }
  57. }
  58. // 配置列表
  59. public function configlist(){
  60. if (Request::isPost()) {
  61. $keys = Request::post('keys', '', 'trim');
  62. $page = Request::post('page') ? Request::post('page') : 1;
  63. $list = Db::name('config')
  64. ->where('code', 'like', '%' . $keys . '%')
  65. ->paginate(['list_rows' => $this->pageSize, 'page' => $page])
  66. ->toArray();
  67. return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['data'], 'count' => $list['total']];
  68. }
  69. return View::fetch();
  70. }
  71. //添加配置
  72. public function configAdd(){
  73. if(Request::isPost()){
  74. $data = Request::post();
  75. $info = ConfigModel::where('code',$data['code'])->find();
  76. if($info){
  77. $this->error(lang('field already exist'));
  78. }else{
  79. if(ConfigModel::create($data)){
  80. $this->success(lang('edit success'));
  81. }else{
  82. $this->error(lang('edit fail'));
  83. }
  84. }
  85. }
  86. $info = '';
  87. $configGroup = Db::name('config_group')->select();
  88. $fieldType = FieldType::select();
  89. $view = ['title'=>lang('add'),'info'=>$info,'configGroup'=>$configGroup,'fieldType'=>$fieldType,'rules'=>$this->rules];
  90. View::assign($view);
  91. return View::fetch('add');
  92. }
  93. // 编辑配置
  94. public function configEdit(){
  95. $id = Request::param('id');
  96. if(Request::isPost()){
  97. $info = ConfigModel::find($id);
  98. $data = Request::post();
  99. if($info->update($data)){
  100. $this->success(lang('edit success'));
  101. }else{
  102. $this->error(lang('edit fail'));
  103. }
  104. }
  105. $configGroup = Db::name('config_group')->select();
  106. $info = ConfigModel::find($id);
  107. $fieldType = FieldType::select();
  108. $view = ['title'=>lang('edit'),'info'=>$info,'configGroup'=>$configGroup,'fieldType'=>$fieldType,'rules'=>$this->rules];
  109. View::assign($view);
  110. return View::fetch('add');
  111. }
  112. // 删除配置
  113. public function configDel(){
  114. if (Request::isPost()) {
  115. $id = Request::post('id');
  116. if(ConfigModel::destroy($id))
  117. {
  118. $this->success('delete success');
  119. } else{
  120. $this->error('delete fail');
  121. }
  122. } else {
  123. $this->error('invalid options');
  124. }
  125. }
  126. // 配置状态
  127. public function configState(){
  128. $id = Request::post('id');
  129. if (empty($id)) {
  130. $this->error('data not exist');
  131. }
  132. $info = ConfigModel::find($id);
  133. $status = $info['status'] == 1 ? 0 : 1;
  134. $info->status = $status;
  135. $info->save();
  136. $this->success(lang('edit success'));
  137. }
  138. public function configGroup(){
  139. $keys = Request::post('keys', '', 'trim');
  140. $page = Request::post('page') ? Request::post('page') : 1;
  141. $list = Db::name('config_group')
  142. ->where('name', 'like', '%' . $keys . '%')
  143. ->select();
  144. $info =$list;
  145. $view = ['title'=>lang('config group'),'info'=>$info];
  146. View::assign($view);
  147. return View::fetch();
  148. }
  149. // 配置分组
  150. public function configGroupAdd(){
  151. if(Request::isPost()){
  152. $data = Request::post();
  153. $info = ConfigGroupModel::where('name',$data['name'])->find();
  154. if($info){
  155. $this->error(lang('field already exist'));
  156. }else {
  157. if (ConfigGroupModel::create($data)) {
  158. $this->success(lang('edit success'));
  159. } else {
  160. $this->error(lang('edit fail'));
  161. }
  162. }
  163. }
  164. $view = ['title'=>lang('config group'),'info'=>''];
  165. View::assign($view);
  166. return View::fetch('groupadd');
  167. }
  168. public function configGroupDel(){
  169. $id = Request::post('id');
  170. $info = ConfigGroupModel::find($id);
  171. if(!$info){
  172. $this->error(lang('id is not exist'));
  173. }
  174. $config = ConfigModel::where('type',$info->name)->find();
  175. if($config){
  176. $this->error(lang('group has config').lang('delete fail'));
  177. }
  178. if( ConfigGroupModel::destroy($id)){
  179. $this->success(lang('delete success'));
  180. }else{
  181. $this->error(lang('delete fail'));
  182. }
  183. }
  184. //
  185. }