Industry.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace app\badminapi\controller\merchant;
  3. use app\badminapi\controller\AuthController;
  4. use app\models\article\ArticleCategory;
  5. use app\models\merchant\Industry as IndustryModel;
  6. use crmeb\services\FormBuilder as Form;
  7. use crmeb\basic\BaseModel;
  8. use crmeb\services\UtilService;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. use think\facade\Route as Url;
  13. use think\Exception;
  14. use think\Response;
  15. class Industry extends AuthController
  16. {
  17. public function initialize()
  18. {
  19. parent::initialize(); // TODO: Change the autogenerated stub
  20. }
  21. /**
  22. * 获取行业列表
  23. * @return mixed
  24. * @throws DbException
  25. * @throws DataNotFoundException
  26. * @throws ModelNotFoundException
  27. */
  28. public function get_list()
  29. {
  30. $search = UtilService::getMore([
  31. ['name', ''],
  32. ['pid', ''],
  33. ['is_show', ''],
  34. ['page', 1],
  35. ['limit', 10],
  36. ['type', 0]
  37. ], $this->request, false);
  38. if ($search['type']) {
  39. $list = IndustryModel::getList();
  40. $list['list'] = ArticleCategory::tidyTree($list['list']);
  41. } else {
  42. $list = IndustryModel::getList($search);
  43. $list['list'] = array_slice(ArticleCategory::tidyTree($list['list']), ((int)$search['page'] - 1) * $search['limit'], $search['limit']);
  44. }
  45. return $this->success($list);
  46. }
  47. /**
  48. * 树形列表
  49. * @return mixed
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. * @throws \think\exception\DbException
  53. */
  54. public function tree_list($type)
  55. {
  56. $list = IndustryModel::getTierList(null, $type);
  57. return $this->success($list);
  58. }
  59. /**
  60. * 修改状态
  61. * @param string $is_show
  62. * @param string $id
  63. * @return Response
  64. */
  65. public function set_show($id = '', $is_show = '')
  66. {
  67. ($is_show == '' || $id == '') && $this->fail('缺少参数');
  68. if (IndustryModel::setIndustryShow($id, (int)$is_show)) {
  69. return $this->success($is_show == 1 ? '显示成功' : '隐藏成功');
  70. } else {
  71. return $this->fail(IndustryModel::getErrorInfo($is_show == 1 ? '显示失败' : '隐藏失败'));
  72. }
  73. }
  74. /**
  75. * 显示创建资源表单页.
  76. *
  77. * @return \think\Response
  78. */
  79. public function create()
  80. {
  81. $field = [
  82. Form::select('pid', '父级')->setOptions(function () {
  83. $list = IndustryModel::getTierList(null, 0);
  84. $menus = [['value' => 0, 'label' => '顶级菜单']];
  85. foreach ($list as $menu) {
  86. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['name']];
  87. }
  88. return $menus;
  89. })->filterable(1),
  90. Form::input('name', '行业名称'),
  91. Form::textarea('info', '详细描述'),
  92. Form::number('sort', '排序')->value(0),
  93. Form::radio('is_show', '状态', 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])
  94. ];
  95. return $this->makePostForm('添加行业', $field, Url::buildUrl('/badminapi/industry/add/0'), 'POST');
  96. }
  97. /**
  98. * 显示编辑资源表单页.
  99. *
  100. * @param int $id
  101. * @return \think\Response
  102. */
  103. public function edit($id)
  104. {
  105. $c = IndustryModel::get($id);
  106. if (!$c) return $this->fail('数据不存在!');
  107. $field = [
  108. Form::select('pid', '父级', (string)$c->getData('pid'))->setOptions(function () use ($id) {
  109. $list = IndustryModel::getTierList(IndustryModel::where('id', '<>', $id), 0);
  110. $menus = [['value' => 0, 'label' => '顶级菜单']];
  111. foreach ($list as $menu) {
  112. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['name']];
  113. }
  114. return $menus;
  115. })->filterable(1),
  116. Form::input('name', '行业名称', $c->getData('name')),
  117. Form::textarea('info', '详细描述', $c->getData('info')),
  118. Form::number('sort', '排序', $c->getData('sort')),
  119. Form::radio('is_show', '状态', $c->getData('is_show'))->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])
  120. ];
  121. return $this->makePostForm('编辑行业', $field, Url::buildUrl('/badminapi/industry/add/' . $id), 'POST');
  122. }
  123. /**
  124. * 快速编辑
  125. * @param string $id
  126. * @return Response
  127. * @throws \Exception
  128. */
  129. public function set_industry($id)
  130. {
  131. $data = UtilService::postMore([
  132. ['field', 'name', '', '', 'empty_check', '缺少参数'],
  133. ['value', '', '', '', 'empty_check', '缺少参数']
  134. ]);
  135. if (!$id) return $this->fail('缺少参数');
  136. if (IndustryModel::where('id', $id)->update([$data['field'] => $data['value'], 'update' => time()]))
  137. return $this->success('保存成功');
  138. else
  139. return $this->fail('保存失败');
  140. }
  141. /**
  142. * 新增/修改行业
  143. * @param int $id
  144. * @return mixed
  145. * @throws DbException
  146. */
  147. public function add_industry($id = 0)
  148. {
  149. $data = UtilService::postMore([
  150. ['name', '', '', '', 'empty_check', '请输入行业名'],
  151. ['pid', 0],
  152. ['info', ''],
  153. ['is_show', 0],
  154. ['sort', 0]
  155. ]);
  156. if ($id) {
  157. if (!IndustryModel::vaildWhere()->where('id', $id)->find()) {
  158. return $this->fail('所选行业不存在');
  159. }
  160. }
  161. if ($data['pid'] != 0) {
  162. if (!IndustryModel::vaildWhere()->where('id', $data['pid'])->find()) {
  163. return $this->fail('上级行业不存在');
  164. }
  165. }
  166. BaseModel::beginTrans();
  167. try {
  168. $data['update'] = time();
  169. if ($id) {
  170. $res = IndustryModel::edit($data, $id);
  171. } else {
  172. $data['add_time'] = time();
  173. $res = IndustryModel::create($data);
  174. }
  175. if ($res) {
  176. BaseModel::commitTrans();
  177. return $this->success('操作成功');
  178. } else {
  179. BaseModel::rollbackTrans();
  180. return $this->fail('操作失败');
  181. }
  182. } catch (Exception $e) {
  183. BaseModel::rollbackTrans();
  184. return $this->fail($e->getMessage());
  185. } catch (DbException $e) {
  186. BaseModel::rollbackTrans();
  187. return $this->fail($e->getMessage());
  188. }
  189. }
  190. /**
  191. * 获取指定资源
  192. *
  193. * @param int $id
  194. * @return Response
  195. */
  196. public function get_industry($id)
  197. {
  198. return $this->success('ok', IndustryModel::get($id) ? IndustryModel::get($id)->toArray() : []);
  199. }
  200. /**
  201. * 删除指定资源
  202. *
  203. * @param int $id
  204. * @return Response
  205. */
  206. public function delete($id)
  207. {
  208. if (!IndustryModel::delIndustry($id))
  209. return $this->fail(IndustryModel::getErrorInfo('删除失败,请稍候再试!'));
  210. else
  211. return $this->success('删除成功!');
  212. }
  213. }