Article.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace app\admin\controller\article;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\SystemAttachment;
  5. use crmeb\services\{
  6. UtilService as Util, JsonService as Json
  7. };
  8. use app\admin\model\article\{
  9. ArticleCategory as ArticleCategoryModel, Article as ArticleModel
  10. };
  11. /**
  12. * 图文管理
  13. * Class WechatNews
  14. * @package app\admin\controller\wechat
  15. */
  16. class Article extends AuthController
  17. {
  18. /**
  19. * TODO 显示后台管理员添加的图文
  20. * @return mixed
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. */
  25. public function index()
  26. {
  27. $where = Util::getMore([
  28. ['title', ''],
  29. ['cid', $this->request->param('pid', '')],
  30. ], $this->request);
  31. $this->assign('where', $where);
  32. $where['merchant'] = 0;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  33. $tree = sort_list_tier(ArticleCategoryModel::getArticleCategoryList());
  34. $this->assign(compact('tree'));
  35. $this->assign(ArticleModel::getAll($where));
  36. return $this->fetch();
  37. }
  38. /**
  39. * TODO 文件添加和修改
  40. * @return mixed
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @throws \think\exception\DbException
  44. */
  45. public function create()
  46. {
  47. $id = $this->request->param('id');
  48. $cid = $this->request->param('cid');
  49. $news = [];
  50. $all = [];
  51. $news['id'] = '';
  52. $news['image_input'] = '';
  53. $news['title'] = '';
  54. $news['author'] = '';
  55. $news['is_banner'] = '';
  56. $news['is_hot'] = '';
  57. $news['content'] = '';
  58. $news['synopsis'] = '';
  59. $news['url'] = '';
  60. $news['cid'] = [];
  61. $select = 0;
  62. if ($id) {
  63. $news = ArticleModel::where('n.id', $id)->alias('n')->field('n.*,c.content')->join('ArticleContent c', 'c.nid=n.id', 'left')->find();
  64. if (!$news) return $this->failed('数据不存在!');
  65. $news['cid'] = explode(',', $news['cid']);
  66. @file_put_contents('quanju.txt', $news['content'] . "-一开始\r\n", 8);
  67. $news['content'] = htmlspecialchars_decode($news['content']);
  68. @file_put_contents('quanju.txt', $news['content'] . "-转码后\r\n", 8);
  69. }
  70. if ($cid && in_array($cid, ArticleCategoryModel::getArticleCategoryInfo(0, 'id'))) {
  71. $all = ArticleCategoryModel::getArticleCategoryInfo($cid);
  72. $select = 1;
  73. }
  74. if (!$select) {
  75. $list = ArticleCategoryModel::getTierList();
  76. foreach ($list as $menu) {
  77. $all[$menu['id']] = $menu['html'] . $menu['title'];
  78. }
  79. }
  80. $this->assign('all', $all);
  81. $this->assign('news', $news);
  82. $this->assign('cid', $cid);
  83. $this->assign('select', $select);
  84. return $this->fetch();
  85. }
  86. /**
  87. * 上传图文图片
  88. * @return \think\response\Json
  89. */
  90. public function upload_image()
  91. {
  92. $res = Upload::instance()->setUploadPath('wechat/image/' . date('Ymd'))->image($_POST['file']);
  93. if (!is_array($res)) return Json::fail($res);
  94. SystemAttachment::attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 5, $res['image_type'], $res['time']);
  95. return Json::successful('上传成功!', ['url' => $res['dir']]);
  96. }
  97. /**
  98. * 添加和修改图文
  99. */
  100. public function add_new()
  101. {
  102. $data = Util::postMore([
  103. ['id', 0],
  104. ['cid', []],
  105. 'title',
  106. 'author',
  107. 'image_input',
  108. 'content',
  109. 'synopsis',
  110. 'share_title',
  111. 'share_synopsis',
  112. ['visit', 0],
  113. ['sort', 0],
  114. 'url',
  115. ['is_banner', 0],
  116. ['is_hot', 0],
  117. ['video_link', ''],
  118. ['status', 1],]);
  119. if ($data['image_input']!=''&&$data['video_link']!=''){
  120. return Json::fail('只能上传图片或视频,不能同时上传!');
  121. }
  122. $data['cid'] = implode(',', $data['cid']);
  123. $content = $data['content'];
  124. unset($data['content']);
  125. if ($data['id']) {
  126. $id = $data['id'];
  127. unset($data['id']);
  128. $res = false;
  129. ArticleModel::beginTrans();
  130. $res1 = ArticleModel::edit($data, $id, 'id');
  131. $res2 = ArticleModel::setContent($id, $content);
  132. if ($res1 && $res2) {
  133. $res = true;
  134. }
  135. @file_put_contents('quanju.txt', $content . "-123\r\n", 8);
  136. ArticleModel::checkTrans($res);
  137. if ($res)
  138. return Json::successful('修改图文成功!', $id);
  139. else
  140. return Json::fail('修改图文失败,您并没有修改什么!', $id);
  141. } else {
  142. $data['add_time'] = time();
  143. $data['admin_id'] = $this->adminId;
  144. $res = false;
  145. ArticleModel::beginTrans();
  146. $res1 = ArticleModel::create($data);
  147. $res2 = false;
  148. if ($res1)
  149. $res2 = ArticleModel::setContent($res1->id, $content);
  150. if ($res1 && $res2) {
  151. $res = true;
  152. }
  153. @file_put_contents('quanju.txt', $content . "-456\r\n", 8);
  154. ArticleModel::checkTrans($res);
  155. if ($res)
  156. return Json::successful('添加图文成功!', $res1->id);
  157. else
  158. return Json::successful('添加图文失败!', $res1->id);
  159. }
  160. }
  161. /**
  162. * 删除图文
  163. * @param $id
  164. * @return \think\response\Json
  165. */
  166. public function delete($id)
  167. {
  168. $res = ArticleModel::del($id);
  169. if (!$res)
  170. return Json::fail('删除失败,请稍候再试!');
  171. else
  172. return Json::successful('删除成功!');
  173. }
  174. public function merchantIndex()
  175. {
  176. $where = Util::getMore([
  177. ['title', '']
  178. ], $this->request);
  179. $this->assign('where', $where);
  180. $where['cid'] = input('cid');
  181. $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  182. $this->assign(ArticleModel::getAll($where));
  183. return $this->fetch();
  184. }
  185. /**
  186. * 关联文章 id
  187. * @param int $id
  188. */
  189. public function relation($id = 0)
  190. {
  191. $this->assign('id', $id);
  192. return $this->fetch();
  193. }
  194. /**
  195. * 保存选择的产品
  196. * @param int $id
  197. */
  198. public function edit_article($id = 0)
  199. {
  200. if (!$id) return Json::fail('缺少参数');
  201. list($product_id) = Util::postMore([
  202. ['product_id', 0]
  203. ], $this->request, true);
  204. if (ArticleModel::edit(['product_id' => $product_id], ['id' => $id]))
  205. return Json::successful('保存成功');
  206. else
  207. return Json::fail('保存失败');
  208. }
  209. /**
  210. * 取消绑定的产品id
  211. * @param int $id
  212. */
  213. public function unrelation($id = 0)
  214. {
  215. if (!$id) return Json::fail('缺少参数');
  216. if (ArticleModel::edit(['product_id' => 0], $id))
  217. return Json::successful('取消关联成功!');
  218. else
  219. return Json::fail('取消失败');
  220. }
  221. }