WechatNews.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\admin\controller\article;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\SystemAttachment;
  5. use app\admin\model\wechat\WechatNews as WechatNewsModel;
  6. use crmeb\services\{
  7. UtilService as Util, JsonService as Json
  8. };
  9. use app\admin\model\article\{
  10. Article as ArticleModel, ArticleCategory as ArticleCategoryModel
  11. };
  12. use crmeb\services\upload\Upload;
  13. /**
  14. * 图文管理
  15. * Class WechatNews
  16. * @package app\admin\controller\wechat
  17. */
  18. class WechatNews extends AuthController
  19. {
  20. /**
  21. * 显示后台管理员添加的图文
  22. * @return mixed
  23. */
  24. public function index($cid = 0)
  25. {
  26. $where = Util::getMore([
  27. ['title', '']
  28. ], $this->request);
  29. if ($cid)
  30. $where['cid'] = $cid;
  31. else
  32. $where['cid'] = '';
  33. $this->assign('where', $where);
  34. $where['merchant'] = 0;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  35. $this->assign('cid', $cid);
  36. $this->assign(ArticleModel::getAll($where));
  37. return $this->fetch();
  38. }
  39. /**
  40. * 展示页面 添加和删除
  41. * @return mixed
  42. */
  43. public function create()
  44. {
  45. $id = input('id');
  46. $cid = input('cid');
  47. $news = array();
  48. $news['id'] = '';
  49. $news['image_input'] = '';
  50. $news['title'] = '';
  51. $news['author'] = '';
  52. $news['content'] = '';
  53. $news['synopsis'] = '';
  54. $news['url'] = '';
  55. $news['cid'] = array();
  56. if ($id) {
  57. $news = WechatNewsModel::where('n.id', $id)->alias('n')->field('n.*,c.content')->join('wechat_news_content c', 'c.nid=n.id')->find();
  58. if (!$news) return $this->failedNotice('数据不存在!');
  59. $news['cid'] = explode(',', $news['cid']);
  60. }
  61. $all = array();
  62. $select = 0;
  63. if (!$cid)
  64. $cid = '';
  65. else {
  66. if ($id) {
  67. $all = ArticleCategoryModel::where('id', $cid)->where('hidden', '<>', 0)->column('title', 'id');
  68. $select = 1;
  69. } else {
  70. $all = ArticleCategoryModel::where('id', $cid)->column('title', 'id');
  71. $select = 1;
  72. }
  73. }
  74. if (empty($all)) {
  75. $all = ArticleCategoryModel::getField();//新闻分类
  76. $select = 0;
  77. }
  78. $this->assign('all', $all);
  79. $this->assign('news', $news);
  80. $this->assign('cid', $cid);
  81. $this->assign('select', $select);
  82. return $this->fetch();
  83. }
  84. /**
  85. * 上传图文图片
  86. * @return \think\response\Json
  87. */
  88. public function upload_image()
  89. {
  90. $uploadType = (int)sys_config('upload_type', 1);
  91. $upload = new Upload($uploadType, [
  92. 'accessKey' => sys_config('accessKey'),
  93. 'secretKey' => sys_config('secretKey'),
  94. 'uploadUrl' => sys_config('uploadUrl'),
  95. 'storageName' => sys_config('storage_name'),
  96. 'storageRegion' => sys_config('storage_region'),
  97. ]);
  98. $resInfo = $upload->to('wechat/image/' . date('Ymd'))->validate()->move($this->request->post('file'));
  99. if ($resInfo === false) {
  100. return Json::fail($upload->getError());
  101. }
  102. $res = $upload->getUploadInfo();
  103. $res['image_type'] = $uploadType;
  104. SystemAttachment::attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 5, $res['image_type'], $res['time']);
  105. return Json::successful('上传成功!', ['url' => $res['thumb_path']]);
  106. }
  107. /**
  108. * 添加和修改图文
  109. */
  110. public function add_new()
  111. {
  112. $data = Util::postMore([
  113. ['id', 0],
  114. ['cid', []],
  115. 'title',
  116. 'author',
  117. 'image_input',
  118. 'content',
  119. 'synopsis',
  120. 'share_title',
  121. 'share_synopsis',
  122. ['visit', 0],
  123. ['sort', 0],
  124. 'url',
  125. ['status', 1],]);
  126. $data['cid'] = implode(',', $data['cid']);
  127. $content = $data['content'];
  128. unset($data['content']);
  129. if ($data['id']) {
  130. $id = $data['id'];
  131. unset($data['id']);
  132. ArticleModel::beginTrans();
  133. $res1 = ArticleModel::edit($data, $id, 'id');
  134. $res2 = ArticleModel::setContent($id, $content);
  135. if ($res1 && $res2)
  136. $res = true;
  137. else
  138. $res = false;
  139. ArticleModel::checkTrans($res);
  140. if ($res)
  141. return Json::successful('修改图文成功!', $id);
  142. else
  143. return Json::fail('修改图文失败!', $id);
  144. } else {
  145. $data['add_time'] = time();
  146. $data['admin_id'] = $this->adminId;
  147. ArticleModel::beginTrans();
  148. $res1 = ArticleModel::create($data);
  149. $res2 = false;
  150. if ($res1)
  151. $res2 = ArticleModel::setContent($res1->id, $content);
  152. if ($res1 && $res2)
  153. $res = true;
  154. else
  155. $res = false;
  156. ArticleModel::checkTrans($res);
  157. if ($res)
  158. return Json::successful('添加图文成功!', $res1->id);
  159. else
  160. return Json::successful('添加图文失败!', $res1->id);
  161. }
  162. }
  163. /**
  164. * 删除图文
  165. * @param $id
  166. * @return \think\response\Json
  167. */
  168. public function delete($id)
  169. {
  170. $res = ArticleModel::del($id);
  171. if (!$res)
  172. return Json::fail('删除失败,请稍候再试!');
  173. else
  174. return Json::successful('删除成功!');
  175. }
  176. public function merchantIndex()
  177. {
  178. $where = Util::getMore([
  179. ['title', '']
  180. ], $this->request);
  181. $this->assign('where', $where);
  182. $where['cid'] = input('cid');
  183. $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1
  184. $this->assign(ArticleModel::getAll($where));
  185. return $this->fetch();
  186. }
  187. }