Article.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace app\adminapi\controller\v1\cms;
  3. use app\adminapi\controller\AuthController;
  4. use crmeb\services\{UtilService as Util};
  5. use app\models\article\{ArticleCategory as ArticleCategoryModel, Article as ArticleModel};
  6. use app\models\system\SystemAttachment;
  7. use think\Request;
  8. /**
  9. * 文章管理
  10. * Class Article
  11. * @package app\adminapi\controller\v1\cms
  12. */
  13. class Article extends AuthController
  14. {
  15. /**
  16. * 显示资源列表
  17. *
  18. * @return \think\Response
  19. */
  20. public function index()
  21. {
  22. $where = Util::getMore([
  23. ['title', ''],
  24. ['pid', 0],
  25. ['page', 1],
  26. ['limit', 10]
  27. ], $this->request);
  28. $where['cid'] = '';
  29. $pid = $where['pid'];
  30. $where['merchant'] = 0;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  31. $where['mer_id'] = $this->merId ?: '';
  32. $cateList = ArticleCategoryModel::getArticleCategoryList($where['mer_id']);
  33. //获取分类列表
  34. if (count($cateList)) {
  35. $tree = sort_list_tier($cateList);
  36. if ($pid) {
  37. $pids = Util::getChildrenPid($tree, $pid);
  38. $where['cid'] = ltrim($pid . $pids);
  39. }
  40. }
  41. $list = ArticleModel::getAll($where);
  42. return $this->success($list);
  43. }
  44. /**
  45. *
  46. * 显示创建资源表单页.
  47. *
  48. * @return \think\Response
  49. */
  50. public function create()
  51. {
  52. //
  53. }
  54. /**
  55. * 保存新建的资源
  56. *
  57. * @param \think\Request $request
  58. * @return \think\Response
  59. */
  60. public function save(Request $request)
  61. {
  62. $data = Util::postMore([
  63. ['id', 0],
  64. ['cid',''],
  65. 'title',
  66. 'author',
  67. 'image_input',
  68. 'content',
  69. 'synopsis',
  70. 'share_title',
  71. 'share_synopsis',
  72. ['visit', 0],
  73. ['sort', 0],
  74. 'url',
  75. ['is_banner', 0],
  76. ['is_hot', 0],
  77. ['status', 1],]);
  78. if (!$data['title']) return $this->fail('缺少参数');
  79. // $data['cid'] = implode(',', $data['cid']);
  80. $content = $data['content'];
  81. unset($data['content']);
  82. $data['mer_id'] = $this->merId ?: '';
  83. if ($data['id']) {
  84. $id = $data['id'];
  85. unset($data['id']);
  86. $res = false;
  87. ArticleModel::beginTrans();
  88. $res1 = ArticleModel::edit($data, $id, 'id');
  89. $res2 = ArticleModel::setContent($id, $content);
  90. if ($res1 && $res2) {
  91. $res = true;
  92. }
  93. ArticleModel::checkTrans($res);
  94. if ($res)
  95. return $this->success('修改成功!', ['id' => $id]);
  96. else
  97. return $this->fail('修改失败,您并没有修改什么!', ['id' => $id]);
  98. } else {
  99. $data['add_time'] = time();
  100. $data['admin_id'] = $this->adminId;
  101. // $data['admin_id'] = 1;
  102. $res = false;
  103. ArticleModel::beginTrans();
  104. $res1 = ArticleModel::create($data);
  105. $res2 = false;
  106. if ($res1)
  107. $res2 = ArticleModel::setContent($res1->id, $content);
  108. if ($res1 && $res2) {
  109. $res = true;
  110. }
  111. ArticleModel::checkTrans($res);
  112. if ($res)
  113. return $this->success('添加成功!', ['id' => $res1->id]);
  114. else
  115. return $this->success('添加失败!', ['id' => $res1->id]);
  116. }
  117. }
  118. /**
  119. * 显示指定的资源
  120. *
  121. * @param int $id
  122. * @return \think\Response
  123. */
  124. public function read($id)
  125. {
  126. if ($id) {
  127. $info = ArticleModel::where('n.id', $id)->alias('n')->field('n.*,c.content')->join('ArticleContent c', 'c.nid=n.id', 'left')->find();
  128. if (!$info) return $this->fail('数据不存在!');
  129. $info['cid'] = intval($info['cid']);
  130. }
  131. return $this->success(compact('info'));
  132. }
  133. /**
  134. * 显示编辑资源表单页.
  135. *
  136. * @param int $id
  137. * @return \think\Response
  138. */
  139. public function edit($id)
  140. {
  141. //
  142. }
  143. /**
  144. * 保存更新的资源
  145. *
  146. * @param \think\Request $request
  147. * @param int $id
  148. * @return \think\Response
  149. */
  150. public function update(Request $request, $id)
  151. {
  152. //
  153. }
  154. /**
  155. * 删除指定资源
  156. *
  157. * @param int $id
  158. * @return \think\Response
  159. */
  160. public function delete($id)
  161. {
  162. $res = ArticleModel::del($id);
  163. if (!$res)
  164. return $this->fail('删除失败,请稍候再试!');
  165. else
  166. return $this->success('删除成功!');
  167. }
  168. //TODO 暂留
  169. /**
  170. * 分类显示列表
  171. * $param int $id 分类id
  172. * @return mixed
  173. */
  174. public function merchantIndex($id = 0)
  175. {
  176. $where = Util::getMore([
  177. ['title', '']
  178. ], $this->request);
  179. if ($id) $where['cid'] = $id;
  180. $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  181. $where['mer_id'] = $this->merId ?: '';
  182. $list = ArticleModel::getAll($where);
  183. return $this->success(compact('list'));
  184. }
  185. /**
  186. * 关联商品
  187. * @param int $id 文章id
  188. */
  189. public function relation($id = 0)
  190. {
  191. if (!$id) return $this->fail('缺少参数');
  192. list($product_id) = Util::postMore([
  193. ['product_id', 0]
  194. ], $this->request, true);
  195. if (ArticleModel::edit(['product_id' => $product_id], ['id' => $id]))
  196. return $this->success('保存成功');
  197. else
  198. return $this->fail('保存失败');
  199. }
  200. /**
  201. * 取消绑定的商品id
  202. * @param int $id
  203. */
  204. public function unrelation($id = 0)
  205. {
  206. if (!$id) return $this->fail('缺少参数');
  207. if (ArticleModel::edit(['product_id' => 0], $id))
  208. return $this->success('取消关联成功!');
  209. else
  210. return $this->fail('取消失败');
  211. }
  212. }