WaterCate.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\controller\water;
  8. use app\admin\controller\AuthController;
  9. use crmeb\services\{ExpressService,
  10. JsonService,
  11. JsonService as Json,
  12. MiniProgramService,
  13. WechatService,
  14. FormBuilder as Form,
  15. CacheService,
  16. UtilService as Util};
  17. use think\facade\Route as Url;
  18. use think\facade\Validate;
  19. Use app\admin\model\water\WaterCate as model;
  20. /**
  21. * 订单管理控制器 同一个订单表放在一个控制器
  22. * Class StoreOrder
  23. * @package app\admin\controller\store
  24. */
  25. class WaterCate extends AuthController
  26. {
  27. /**
  28. * @return mixed
  29. */
  30. public function index()
  31. {
  32. return $this->fetch();
  33. }
  34. public function list()
  35. {
  36. $where = Util::getMore([
  37. ['page', 1],
  38. ['limit', 20],
  39. ['name', ''],
  40. ['role', '']
  41. ]);
  42. return Json::successlayui(model::list($where));
  43. }
  44. /**
  45. * 显示创建资源表单页.
  46. *
  47. * @return \think\Response
  48. */
  49. public function create($id = 0)
  50. {
  51. $f = [];
  52. $f[] = Form::input('name', '分类名称');
  53. $f[] = Form::select('type', '选择分类', '')->options([
  54. ['value' => 0, 'label' => '请选择分类'],
  55. ['value' => 1, 'label' => '顶板'],
  56. ['value' => 2, 'label' => '侧板'],
  57. ['value' => 3, 'label' => '拉筋'],
  58. ['value' => 4, 'label' => '底板'],
  59. ['value' => 5, 'label' => '立柱'],
  60. ['value' => 6, 'label' => '辅拉'],
  61. ['value' => 7, 'label' => '保温'],
  62. ['value' => 8, 'label' => '槽钢'],
  63. ['value' => 9, 'label' => '扶梯管'],
  64. ])->filterable(true);
  65. $form = Form::make_post_form('添加', $f, Url::buildUrl('save'));
  66. $this->assign(compact('form'));
  67. return $this->fetch('public/form-builder');
  68. }
  69. public function save()
  70. {
  71. $model = new model;
  72. $data = Util::postMore([
  73. 'name',
  74. 'type',
  75. ]);
  76. $validate = Validate::rule([
  77. 'name' => 'require',
  78. 'type' => 'require',
  79. ]);
  80. $validate->message([
  81. 'name.require' => '名称不能为空',
  82. 'type.require' => '分类不能为空',
  83. ]);
  84. if (!$validate->check($data)) {
  85. return Json::fail($validate->getError());
  86. }
  87. $res = $model->save($data);
  88. if ($res) return Json::successful('添加成功');
  89. return Json::fail('添加失败');
  90. }
  91. /**
  92. * 显示创建资源表单页.
  93. *
  94. * @return \think\Response
  95. */
  96. public function edit($id = 0)
  97. {
  98. $data = model::find($id);
  99. $f = [];
  100. $f[] = Form::input('name', '分类名称', $data['name']);
  101. $f[] = Form::select('type', '选择分类', (string)$data['type'])->options([
  102. ['value' => 0, 'label' => '请选择分类'],
  103. ['value' => 1, 'label' => '顶板'],
  104. ['value' => 2, 'label' => '侧板'],
  105. ['value' => 3, 'label' => '拉筋'],
  106. ['value' => 4, 'label' => '底板'],
  107. ['value' => 5, 'label' => '立柱'],
  108. ['value' => 6, 'label' => '辅拉'],
  109. ['value' => 7, 'label' => '保温'],
  110. ['value' => 8, 'label' => '槽钢'],
  111. ['value' => 9, 'label' => '扶梯管'],
  112. ])->filterable(true);
  113. $f[] = Form::hidden('id', $id);
  114. $form = Form::make_post_form('修改', $f, Url::buildUrl('update'));
  115. $this->assign(compact('form'));
  116. return $this->fetch('public/form-builder');
  117. }
  118. /**
  119. * 修改
  120. * @return void
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function update()
  126. {
  127. $model = new model;
  128. $data = Util::postMore([
  129. 'name',
  130. 'type',
  131. 'id'
  132. ]);
  133. $validate = Validate::rule([
  134. 'title' => 'require',
  135. 'price' => 'require',
  136. ]);
  137. $validate->message([
  138. 'name.require' => '名称不能为空',
  139. 'type.require' => '分类不能为空',
  140. ]);
  141. $details = $model->find($data['id']);
  142. $details['name'] = $data['name'];
  143. $details['type'] = $data['type'];
  144. $res = $details->save();
  145. if ($res) return Json::successful('修改成功');
  146. return Json::fail('修改失败');
  147. }
  148. /**
  149. * 删除
  150. * @param $id
  151. * @return void
  152. * @throws \Exception
  153. */
  154. public function delete($id)
  155. {
  156. if (!$id) Json::fail('删除失败');
  157. $model = new model;
  158. $res = model::destroy($id);
  159. if ($res){
  160. return Json::success('删除成功!');
  161. }else{
  162. return Json::fail($model->getErrorInfo());
  163. }
  164. }
  165. }