StoreCategory.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\store
  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 = parent::getDataModification('store_cate', $id, '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. $res = parent::getDataModification('store_cate', $id, $field, $value);
  78. if ($res)
  79. return Json::successful('保存成功');
  80. else
  81. return Json::fail('保存失败');
  82. }
  83. /**
  84. * 显示创建资源表单页.
  85. *
  86. * @return \think\Response
  87. */
  88. public function create($id = 0)
  89. {
  90. $cate = [];
  91. if ($id) {
  92. $cate = CategoryModel::get($id);
  93. }
  94. $this->assign(['cate' => json_encode($cate), 'id' => $id]);
  95. return $this->fetch();
  96. }
  97. /**
  98. * 一级分类
  99. */
  100. public function get_cate_list()
  101. {
  102. $list = CategoryModel::where('pid', 0)->where('is_show', 1)->select();
  103. return Json::successful($list);
  104. }
  105. /**
  106. * 上传图片
  107. * @return \think\response\Json
  108. */
  109. public function upload()
  110. {
  111. $res = Upload::image('file', 'store/category' . date('Ymd'));
  112. $thumbPath = Upload::thumb($res->dir);
  113. //产品图片上传记录
  114. $fileInfo = $res->fileInfo->getinfo();
  115. SystemAttachment::attachmentAdd($res->fileInfo->getSaveName(), $fileInfo['size'], $fileInfo['type'], $res->dir, $thumbPath, 1);
  116. if ($res->status == 200)
  117. return Json::successful('图片上传成功!', ['name' => $res->fileInfo->getSaveName(), 'url' => Upload::pathToUrl($thumbPath)]);
  118. else
  119. return Json::fail($res->error);
  120. }
  121. /**
  122. * 保存新建的资源
  123. *
  124. * @param \think\Request $request
  125. * @return \think\Response
  126. */
  127. public function save(Request $request, $id = 0)
  128. {
  129. $data = parent::postMore([
  130. 'pid',
  131. 'cate_name',
  132. ['pic', []],
  133. 'sort',
  134. ['is_show', 0]
  135. ], $request);
  136. if ($data['pid'] == '') return Json::fail('请选择父类');
  137. if (!$data['cate_name']) return Json::fail('请输入分类名称');
  138. if ($data['pid'] > 0) {
  139. if (count($data['pic']) < 1) return Json::fail('请上传分类图标');
  140. }
  141. if ($data['sort'] < 0) $data['sort'] = 0;
  142. $data['pic'] = $data['pic'][0];
  143. if ($id) {
  144. if (CategoryModel::where(['cate_name' => $data['cate_name']])->where('id', '<>', $id)->count() >= 1) return Json::fail('分类名称已存在');
  145. CategoryModel::edit($data, $id);
  146. return Json::successful('修改成功!');
  147. } else {
  148. $data['add_time'] = time();
  149. if (CategoryModel::be(['cate_name' => $data['cate_name']])) {
  150. return Json::fail('分类名称已存在!');
  151. }
  152. CategoryModel::set($data);
  153. return Json::successful('添加分类成功!');
  154. }
  155. }
  156. /**
  157. * 删除指定资源
  158. *
  159. * @param int $id
  160. * @return \think\Response
  161. */
  162. public function delete($id)
  163. {
  164. if (!CategoryModel::delCategory($id))
  165. return Json::fail(CategoryModel::getErrorInfo('删除失败,请删除字分类后再试!'));
  166. else
  167. return Json::successful('删除成功!');
  168. }
  169. }