StoreCategory.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\store;
  12. use app\admin\controller\AuthController;
  13. use service\FormBuilder as Form;
  14. use service\JsonService as Json;
  15. use service\UploadService as Upload;
  16. use think\Request;
  17. use app\admin\model\store\StoreCategory as CategoryModel;
  18. use think\Url;
  19. use app\admin\model\system\SystemAttachment;
  20. /**
  21. * 产品分类控制器
  22. * Class StoreCategory
  23. * @package app\admin\controller\system
  24. */
  25. class StoreCategory extends AuthController
  26. {
  27. /**
  28. * 显示资源列表
  29. *
  30. * @return \think\Response
  31. */
  32. public function index()
  33. {
  34. $this->assign('pid', $this->request->get('pid', 0));
  35. $this->assign('cate', CategoryModel::getTierList());
  36. return $this->fetch();
  37. }
  38. /**
  39. * 异步获取分类列表
  40. * @return json
  41. */
  42. public function category_list()
  43. {
  44. $where = parent::getMore([
  45. ['is_show', ''],
  46. ['pid', $this->request->param('pid', '')],
  47. ['cate_name', ''],
  48. ['page', 1],
  49. ['limit', 20],
  50. ['order', '']
  51. ]);
  52. return Json::successlayui(CategoryModel::CategoryList($where));
  53. }
  54. /**
  55. * 设置单个产品上架|下架
  56. *
  57. * @return json
  58. */
  59. public function set_show($is_show = '', $id = '')
  60. {
  61. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  62. $res = CategoryModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  63. if ($res) {
  64. return Json::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  65. } else {
  66. return Json::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  67. }
  68. }
  69. /**
  70. * 快速编辑
  71. *
  72. * @return json
  73. */
  74. public function set_category($field = '', $id = '', $value = '')
  75. {
  76. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  77. if (CategoryModel::where(['id' => $id])->update([$field => $value]))
  78. return Json::successful('保存成功');
  79. else
  80. return Json::fail('保存失败');
  81. }
  82. /**
  83. * 显示创建资源表单页.
  84. *
  85. * @return \think\Response
  86. */
  87. public function create($id = 0)
  88. {
  89. $cate=[];
  90. if ($id){
  91. $cate = CategoryModel::get($id);
  92. }
  93. $this->assign(['cate'=>json_encode($cate),'id'=>$id]);
  94. return $this->fetch();
  95. }
  96. /**
  97. * 一级分类
  98. */
  99. public function get_cate_list()
  100. {
  101. $list=CategoryModel::where('pid',0)->where('is_show',1)->select();
  102. return Json::successful($list);
  103. }
  104. /**
  105. * 上传图片
  106. * @return \think\response\Json
  107. */
  108. public function upload()
  109. {
  110. $res = Upload::image('file', 'store/category' . date('Ymd'));
  111. $thumbPath = Upload::thumb($res->dir);
  112. //产品图片上传记录
  113. $fileInfo = $res->fileInfo->getinfo();
  114. SystemAttachment::attachmentAdd($res->fileInfo->getSaveName(), $fileInfo['size'], $fileInfo['type'], $res->dir, $thumbPath, 1);
  115. if ($res->status == 200)
  116. return Json::successful('图片上传成功!', ['name' => $res->fileInfo->getSaveName(), 'url' => Upload::pathToUrl($thumbPath)]);
  117. else
  118. return Json::fail($res->error);
  119. }
  120. /**
  121. * 保存新建的资源
  122. *
  123. * @param \think\Request $request
  124. * @return \think\Response
  125. */
  126. public function save(Request $request,$id=0)
  127. {
  128. $data = parent::postMore([
  129. 'pid',
  130. 'cate_name',
  131. ['pic', []],
  132. 'sort',
  133. ['is_show', 0]
  134. ], $request);
  135. if ($data['pid'] == '') return Json::fail('请选择父类');
  136. if (!$data['cate_name']) return Json::fail('请输入分类名称');
  137. if($data['pid'] >0){
  138. if (count($data['pic']) < 1) return Json::fail('请上传分类图标');
  139. }
  140. if ($data['sort'] < 0) $data['sort'] = 0;
  141. $data['pic'] = $data['pic'][0];
  142. if($id){
  143. CategoryModel::edit($data, $id);
  144. return Json::successful('修改成功!');
  145. }else{
  146. $data['add_time'] = time();
  147. if(CategoryModel::be(['cate_name'=>$data['cate_name']])){
  148. return Json::fail('分类名称已存在!');
  149. }
  150. CategoryModel::set($data);
  151. return Json::successful('添加分类成功!');
  152. }
  153. }
  154. /**
  155. * 删除指定资源
  156. *
  157. * @param int $id
  158. * @return \think\Response
  159. */
  160. public function delete($id)
  161. {
  162. if (!CategoryModel::delCategory($id))
  163. return Json::fail(CategoryModel::getErrorInfo('删除失败,请删除字分类后再试!'));
  164. else
  165. return Json::successful('删除成功!');
  166. }
  167. }