SystemConfigTab.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace app\adminapi\controller\v1\setting;
  3. use app\adminapi\controller\AuthController;
  4. use think\Request;
  5. use think\facade\Route as Url;
  6. use crmeb\services\{
  7. FormBuilder as Form, UtilService as Util
  8. };
  9. use app\models\system\{
  10. SystemConfigTab as ConfigTabModel, SystemConfig as ConfigModel
  11. };
  12. /**
  13. * 配置分类
  14. * Class SystemConfigTab
  15. * @package app\adminapi\controller\v1\setting
  16. */
  17. class SystemConfigTab extends AuthController
  18. {
  19. /**
  20. * 显示资源列表
  21. *
  22. * @return \think\Response
  23. */
  24. public function index()
  25. {
  26. $where = Util::getMore([
  27. ['page', 1],
  28. ['limit', 15],
  29. ['status', ''],
  30. ['title', '']
  31. ], $this->request);
  32. $list = ConfigTabModel::getSystemConfigTabPage($where);
  33. return $this->success($list);
  34. }
  35. /**
  36. * 显示创建资源表单页.
  37. *
  38. * @return \think\Response
  39. */
  40. public function create()
  41. {
  42. $form[] = Form::select('pid', '父级分类', 0)->setOptions(function () {
  43. $menuList = ConfigTabModel::field(['id', 'pid', 'title'])->select()->toArray();//var_dump($menuList);
  44. $list = sort_list_tier($menuList, '顶级', 'pid', 'id');//var_dump($list);
  45. $menus = [['value' => 0, 'label' => '顶级按钮']];
  46. foreach ($list as $menu) {
  47. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
  48. }
  49. return $menus;
  50. })->filterable(1);
  51. $form[] = Form::input('title', '分类昵称');
  52. $form[] = Form::input('eng_title', '分类字段英文');
  53. $form[] = Form::frameInputOne('icon', '图标', Url::buildUrl('admin/widget.widgets/icon', array('fodder' => 'icon')))->icon('ios-ionic')->height('435px');
  54. $form[] = Form::radio('type', '类型', 0)->options(self::getConfigType());
  55. $form[] = Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  56. $form[] = Form::number('sort', '排序', 0);
  57. return $this->makePostForm('添加配置分类', $form, Url::buildUrl('/setting/config_class'), 'POST');
  58. }
  59. /**
  60. * 保存新建的资源
  61. *
  62. * @param \think\Request $request
  63. * @return \think\Response
  64. */
  65. public function save(Request $request)
  66. {
  67. $data = Util::postMore([
  68. 'eng_title',
  69. 'status',
  70. 'title',
  71. 'icon',
  72. ['type', 0],
  73. ['sort', 0],
  74. ['pid', 0],
  75. ]);
  76. if (!$data['title']) return $this->fail('请输入按钮名称');
  77. ConfigTabModel::create($data);
  78. return $this->success('添加配置分类成功!');
  79. }
  80. /**
  81. * 显示指定的资源
  82. *
  83. * @param int $id
  84. * @return \think\Response
  85. */
  86. public function read($id)
  87. {
  88. //
  89. }
  90. /**
  91. * 显示编辑资源表单页.
  92. *
  93. * @param int $id
  94. * @return \think\Response
  95. */
  96. public function edit($id)
  97. {
  98. $menu = ConfigTabModel::get($id)->getData();
  99. if (!$menu) return $this->fail('数据不存在!');
  100. $form[] = Form::select('pid', '父级分类', (string)$menu['pid'])->setOptions(function () {
  101. $menuList = ConfigTabModel::field(['id', 'pid', 'title'])->select()->toArray();//var_dump($menuList);
  102. $list = sort_list_tier($menuList, '顶级', 'pid', 'id');//var_dump($list);
  103. $menus = [['value' => 0, 'label' => '顶级按钮']];
  104. foreach ($list as $menu) {
  105. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
  106. }
  107. return $menus;
  108. })->filterable(1);
  109. $form[] = Form::input('title', '分类昵称', $menu['title']);
  110. $form[] = Form::input('eng_title', '分类字段英文', $menu['eng_title']);
  111. $form[] = Form::frameInputOne('icon', '图标', Url::buildUrl('admin/widget.widgets/icon', array('fodder' => 'icon')), $menu['icon'])->icon('ios-ionic')->height('435px');
  112. $form[] = Form::radio('type', '类型', $menu['type'])->options(self::getConfigType());
  113. $form[] = Form::radio('status', '状态', $menu['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  114. $form[] = Form::number('sort', '排序', $menu['sort']);
  115. return $this->makePostForm('编辑配置分类', $form, Url::buildUrl('/setting/config_class/' . $id), 'PUT');
  116. }
  117. /**
  118. * 保存更新的资源
  119. *
  120. * @param \think\Request $request
  121. * @param int $id
  122. * @return \think\Response
  123. */
  124. public function update(Request $request, $id)
  125. {
  126. $data = Util::postMore([
  127. 'title',
  128. 'status',
  129. 'eng_title',
  130. 'icon',
  131. ['type', 0],
  132. ['sort', 0],
  133. ['pid', 0],
  134. ]);
  135. if (!$data['title']) return $this->fail('请输入分类昵称');
  136. if (!$data['eng_title']) return $this->fail('请输入分类字段');
  137. if (!ConfigTabModel::get($id)) return $this->fail('编辑的记录不存在!');
  138. ConfigTabModel::edit($data, $id);
  139. return $this->success('修改成功!');
  140. }
  141. /**
  142. * 删除指定资源
  143. *
  144. * @param int $id
  145. * @return \think\Response
  146. */
  147. public function delete($id)
  148. {
  149. if (ConfigModel::where('config_tab_id', $id)->count()) return $this->fail('存在下级配置,无法删除!');
  150. if (!ConfigTabModel::del($id))
  151. return $this->fail(ConfigTabModel::getErrorInfo('删除失败,请稍候再试!'));
  152. else
  153. return $this->success('删除成功!');
  154. }
  155. /** 定义配置分类,需要添加分类可以手动添加
  156. * @return array
  157. */
  158. public function getConfigType()
  159. {
  160. return [
  161. ['value' => 0, 'label' => '系统']
  162. , ['value' => 1, 'label' => '应用']
  163. , ['value' => 2, 'label' => '支付']
  164. , ['value' => 3, 'label' => '其它']
  165. ];
  166. // return [
  167. // ['value'=>0,'label'=>'系统']
  168. // ,['value'=>1,'label'=>'公众号']
  169. // ,['value'=>2,'label'=>'小程序']
  170. // ,['value'=>3,'label'=>'其它']
  171. // ];
  172. }
  173. /**
  174. * 修改状态
  175. * @param $id
  176. * @param $status
  177. * @return mixed
  178. */
  179. public function set_status($id, $status)
  180. {
  181. if ($status == '' || $id == 0) return $this->fail('参数错误');
  182. ConfigTabModel::where(['id' => $id])->update(['status' => $status]);
  183. return $this->success($status == 0 ? '隐藏成功' : '显示成功');
  184. }
  185. }