Style.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace app\admin\controller\enterprise;
  3. use think\facade\Route as Url;
  4. use app\admin\controller\AuthController;
  5. use app\admin\model\system\SystemAttachment;
  6. use app\admin\model\enterprise\EnterCategory as ArticleCategoryModel;
  7. use crmeb\services\{FormBuilder as Form,UtilService as Util,JsonService as Json};
  8. /**
  9. * 企业类型管理 控制器
  10. * */
  11. class Style extends AuthController
  12. {
  13. /**
  14. * 类型管理
  15. * */
  16. public function index()
  17. {
  18. $where = Util::getMore([
  19. ['status', ''],
  20. ['title', ''],
  21. ], $this->request);
  22. $this->assign('where', $where);
  23. $this->assign(ArticleCategoryModel::systemPage($where));
  24. return $this->fetch();
  25. }
  26. /**
  27. * 添加类型管理
  28. * */
  29. public function create()
  30. {
  31. $f = array();
  32. $f[] = Form::select('pid', '父级id')->setOptions(function () {
  33. $list = ArticleCategoryModel::getTierList();
  34. $menus[] = ['value' => 0, 'label' => '顶级类型'];
  35. foreach ($list as $menu) {
  36. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
  37. }
  38. return $menus;
  39. })->filterable(1);
  40. $f[] = Form::input('title', '类型名称');
  41. $f[] = Form::input('intr', '类型简介')->type('textarea');
  42. $f[] = Form::frameImageOne('image', '类型图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')))->icon('image')->width('100%')->height('500px');
  43. $f[] = Form::number('sort', '排序', 0);
  44. $f[] = Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  45. $f[] = Form::radio('is_type', '是否第三方', 1)->options([['value' => 1, 'label' => '是'], ['value' => 0, 'label' => '否']]);
  46. $form = Form::make_post_form('添加类型', $f, Url::buildUrl('save'));
  47. $this->assign(compact('form'));
  48. return $this->fetch('public/form-builder');
  49. }
  50. /**
  51. * s上传图片
  52. * */
  53. public function upload()
  54. {
  55. $res = Upload::instance()->setUploadPath('article')->image('file');
  56. if (!is_array($res)) return Json::fail($res);
  57. SystemAttachment::attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 5, $res['image_type'], $res['time']);
  58. return Json::successful('图片上传成功!', ['name' => $res['name'], 'url' => path_to_url($res['thumb_path'])]);
  59. }
  60. /**
  61. * 保存类型管理
  62. * */
  63. public function save()
  64. {
  65. $data = Util::postMore([
  66. 'title',
  67. 'pid',
  68. 'intr',
  69. ['new_id', []],
  70. ['image', []],
  71. ['sort', 0],
  72. ['is_type', 0],
  73. 'status',]);
  74. if (!$data['title']) return Json::fail('请输入类型名称');
  75. if (count($data['image']) != 1) return Json::fail('请选择类型图片,并且只能上传一张');
  76. if ($data['sort'] < 0) return Json::fail('排序不能是负数');
  77. $data['add_time'] = time();
  78. $data['image'] = $data['image'][0];
  79. $new_id = $data['new_id'];
  80. unset($data['new_id']);
  81. $res = ArticleCategoryModel::create($data);
  82. return Json::successful('添加类型成功!');
  83. }
  84. /**
  85. * 修改类型
  86. * */
  87. public function edit($id)
  88. {
  89. if (!$id) return $this->failed('参数错误');
  90. $article = ArticleCategoryModel::get($id)->getData();
  91. if (!$article) return Json::fail('数据不存在!');
  92. $f = array();
  93. $f[] = Form::select('pid', '父级id', (string)$article['pid'])->setOptions(function () {
  94. $list = ArticleCategoryModel::getTierList();
  95. $menus[] = ['value' => 0, 'label' => '顶级类型'];
  96. foreach ($list as $menu) {
  97. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
  98. }
  99. return $menus;
  100. })->filterable(1);
  101. $f[] = Form::input('title', '类型名称', $article['title']);
  102. $f[] = Form::input('intr', '类型简介', $article['intr'])->type('textarea');
  103. $f[] = Form::frameImageOne('image', '类型图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $article['image'])->icon('image')->width('100%')->height('500px');
  104. $f[] = Form::number('sort', '排序', 0);
  105. $f[] = Form::radio('is_type', '是否第三方', $article['is_type'])->options([['value' => 1, 'label' => '是'], ['value' => 0, 'label' => '否']]);
  106. $f[] = Form::radio('status', '状态', $article['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  107. $form = Form::make_post_form('编辑类型', $f, Url::buildUrl('update', array('id' => $id)));
  108. $this->assign(compact('form'));
  109. return $this->fetch('public/form-builder');
  110. }
  111. public function update($id)
  112. {
  113. $data = Util::postMore([
  114. 'pid',
  115. 'title',
  116. 'intr',
  117. ['image', []],
  118. ['sort', 0],
  119. ['is_type', 0],
  120. 'status',]);
  121. if (!$data['title']) return Json::fail('请输入类型名称');
  122. if (count($data['image']) != 1) return Json::fail('请选择类型图片,并且只能上传一张');
  123. if ($data['sort'] < 0) return Json::fail('排序不能是负数');
  124. $data['image'] = $data['image'][0];
  125. if (!ArticleCategoryModel::get($id)) return Json::fail('编辑的记录不存在!');
  126. ArticleCategoryModel::edit($data, $id);
  127. return Json::successful('修改成功!');
  128. }
  129. /**
  130. * 删除类型
  131. * */
  132. public function delete($id)
  133. {
  134. $res = ArticleCategoryModel::delArticleCategory($id);
  135. if (!$res)
  136. return Json::fail(ArticleCategoryModel::getErrorInfo('删除失败,请稍候再试!'));
  137. else
  138. return Json::successful('删除成功!');
  139. }
  140. }