Content.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 会员投稿管理
  13. // +----------------------------------------------------------------------
  14. namespace app\index\controller\cms;
  15. use addons\cms\library\Service;
  16. use addons\cms\model\Category as CategoryModel;
  17. use addons\cms\model\CategoryPriv;
  18. use addons\cms\model\CmsContent as CmsContentModel;
  19. use app\admin\model\cms\Cms as CmsModel;
  20. use app\common\controller\Frontend;
  21. use think\exception\ValidateException;
  22. use think\facade\Db;
  23. class Content extends Frontend
  24. {
  25. protected $CmsModel = null;
  26. protected $noNeedLogin = [];
  27. protected $noNeedRight = ['*'];
  28. public function initialize()
  29. {
  30. parent::initialize();
  31. $this->CmsModel = new CmsModel;
  32. }
  33. public function publish()
  34. {
  35. $this->check_group_auth();
  36. $config = get_addon_config("cms");
  37. //没有认证用户不得投稿
  38. if ($config['contribute_verify'] && (!$this->auth->ischeck_email && !$this->auth->ischeck_mobile)) {
  39. $this->error("投稿必须激活邮箱或手机!");
  40. }
  41. //判断每日投稿数
  42. $todayCount = CmsContentModel::where('user_id', $this->auth->id)
  43. ->whereDay('create_time')
  44. ->count();
  45. if ($todayCount >= $config['daily_contribute_limit']) {
  46. $this->error('今日投稿数量已达上限');
  47. }
  48. if ($this->request->isPost()) {
  49. $param = $this->request->param('', '', 'trim,xss_clean');
  50. // 验证数据
  51. $rule = [
  52. 'modelField.title|标题' => 'require|length:3,100',
  53. 'modelField.catid|栏目' => 'require|integer',
  54. '__token__' => 'require|token',
  55. ];
  56. try {
  57. $this->validate($param, $rule);
  58. } catch (ValidateException $e) {
  59. // 验证失败 输出错误信息
  60. $this->error($e->getError(), null, ['token' => $this->request->buildToken()]);
  61. }
  62. $catid = intval($param['modelField']['catid']);
  63. if (empty($catid)) {
  64. $this->error("请指定栏目ID!", null, ['token' => $this->request->buildToken()]);
  65. }
  66. $catidPrv = CategoryPriv::where(["catid" => $catid, "roleid" => $this->auth->group_id, "is_admin" => 0, "action" => "add"])->find();
  67. if (empty($catidPrv)) {
  68. $this->error("您没有该栏目投稿权限!", null, ['token' => $this->request->buildToken()]);
  69. }
  70. $category = CategoryModel::find($catid);
  71. if (empty($category)) {
  72. $this->error('该栏目不存在!', null, ['token' => $this->request->buildToken()]);
  73. }
  74. $fields = Db::name('cms_field')->where('modelid', $category['modelid'])->where('isadd', 1)->column('ifsystem', 'name');
  75. $data = [];
  76. foreach ($fields as $k => $v) {
  77. if (1 == $v && isset($param['modelField'][$k])) {
  78. $data['modelField'][$k] = $param['modelField'][$k];
  79. } elseif (0 == $v && isset($param['modelFieldExt'][$k])) {
  80. $data['modelFieldExt'][$k] = $param['modelFieldExt'][$k];
  81. }
  82. }
  83. $data['modelField']['user_id'] = $this->auth->id;
  84. $data['modelField']['sysadd'] = 0;
  85. //是否需要审核
  86. if ($config['is_contribute_audit'] == 1) {
  87. $data['modelField']['status'] = 0;
  88. } else {
  89. $data['modelField']['status'] = 1;
  90. }
  91. $id = 0;
  92. if ($category['type'] == 2) {
  93. $data['modelFieldExt'] = isset($data['modelFieldExt']) ? $data['modelFieldExt'] : [];
  94. try {
  95. $id = $this->CmsModel->addModelData($data['modelField'], $data['modelFieldExt']);
  96. } catch (\Exception $ex) {
  97. $this->error($ex->getMessage(), null, ['token' => $this->request->buildToken()]);
  98. }
  99. }
  100. //添加投稿记录
  101. if ($id) {
  102. CmsContentModel::create([
  103. 'catid' => $catid,
  104. 'content_id' => $id,
  105. 'user_id' => $this->auth->id,
  106. 'username' => $this->auth->username,
  107. 'status' => $data['modelField']['status'],
  108. ]);
  109. }
  110. if ($data['modelField']['status'] == 1) {
  111. $this->success('操作成功,内容已通过审核!', url('published'));
  112. } else {
  113. $this->success('操作成功,等待管理员审核!', url('published'));
  114. }
  115. } else {
  116. $this->assign('title', '在线投稿');
  117. $step = $this->request->param('step/d', 1);
  118. if ($step == 1) {
  119. return $this->fetch('content_declaration');
  120. }
  121. $catid = $this->request->param('catid/d', 0);
  122. $tree = new \util\Tree();
  123. $str = "<option value=@catidurl @selected @disabled>@spacer @catname</option>";
  124. $array = CategoryModel::order('listorder DESC, id DESC')->column('*', 'id');
  125. foreach ($array as $k => $v) {
  126. if ($v['id'] == $catid) {
  127. $array[$k]['selected'] = "selected";
  128. }
  129. //含子栏目和单页不可以发表
  130. if ($v['child'] == 1 || $v['type'] == 1) {
  131. $array[$k]['disabled'] = "disabled";
  132. $array[$k]['catidurl'] = url('publish', ['step' => 2]);
  133. } else {
  134. $array[$k]['disabled'] = "";
  135. $array[$k]['catidurl'] = url('publish', ['step' => 2, 'catid' => $v['id']]);
  136. }
  137. }
  138. $tree->init($array);
  139. $categoryselect = $tree->getTree(0, $str);
  140. //如果有选择栏目的情况下
  141. if ($catid) {
  142. $category = CategoryModel::find($catid);
  143. if (empty($category)) {
  144. $this->error('该栏目不存在!');
  145. }
  146. if ($category['type'] == 2) {
  147. $modelid = $category['modelid'];
  148. $fieldList = Service::getFieldList($modelid);
  149. $this->assign([
  150. 'catid' => $catid,
  151. 'fieldList' => $fieldList,
  152. ]);
  153. }
  154. }
  155. $this->assign("categoryselect", $categoryselect);
  156. return $this->fetch('content_publish');
  157. }
  158. }
  159. /**
  160. * 编辑内容
  161. */
  162. public function edit()
  163. {
  164. $this->check_group_auth();
  165. $config = get_addon_config("cms");
  166. if ($this->request->isPost()) {
  167. $param = $this->request->param('', '', 'trim,xss_clean');
  168. // 验证数据
  169. $rule = [
  170. 'modelField.title|标题' => 'require|length:3,100',
  171. 'modelField.catid|栏目' => 'require|integer',
  172. '__token__' => 'require|token',
  173. ];
  174. try {
  175. $this->validate($param, $rule);
  176. } catch (ValidateException $e) {
  177. // 验证失败 输出错误信息
  178. $this->error($e->getError(), null, ['token' => $this->request->buildToken()]);
  179. }
  180. $id = intval($param['modelField']['id']);
  181. $catid = intval($param['modelField']['catid']);
  182. if (empty($id) || empty($catid)) {
  183. $this->error("请指定栏目ID!");
  184. }
  185. $category = CategoryModel::find($catid);
  186. if (empty($category)) {
  187. $this->error('该栏目不存在!');
  188. }
  189. $catidPrv = CategoryPriv::where(["catid" => $catid, "roleid" => $this->auth->group_id, "is_admin" => 0, "action" => "add"])->find();
  190. if (empty($catidPrv)) {
  191. $this->error("您没有该栏目投稿权限!");
  192. }
  193. $fields = Db::name('cms_field')->where('modelid', $category['modelid'])->where('isadd', 1)->column('ifsystem', 'name');
  194. $data = [];
  195. foreach ($fields as $k => $v) {
  196. if (1 == $v && isset($param['modelField'][$k])) {
  197. $data['modelField'][$k] = $param['modelField'][$k];
  198. } elseif (0 == $v && isset($param['modelFieldExt'][$k])) {
  199. $data['modelFieldExt'][$k] = $param['modelFieldExt'][$k];
  200. }
  201. }
  202. //判断会员组投稿是否需要审核
  203. if ($config['is_contribute_audit'] == 1) {
  204. $data['modelField']['status'] = 0;
  205. } else {
  206. $data['modelField']['status'] = 1;
  207. }
  208. if ($category['type'] == 2) {
  209. $data['modelFieldExt'] = isset($data['modelFieldExt']) ? $data['modelFieldExt'] : [];
  210. try {
  211. $this->CmsModel->editModelData($data['modelField'], $data['modelFieldExt']);
  212. } catch (\Exception $ex) {
  213. $this->error($ex->getMessage());
  214. }
  215. }
  216. if ($data['modelField']['status'] == 1) {
  217. CmsContentModel::where(['content_id' => $id, 'catid' => $catid])->update(['status' => 1]);
  218. $this->success('编辑成功,内容已通过审核!', url('published'));
  219. } else {
  220. CmsContentModel::where(['content_id' => $id, 'catid' => $catid])->update(['status' => 0]);
  221. $this->success('编辑成功,等待管理员审核!', url('published'));
  222. }
  223. } else {
  224. $id = $this->request->param('id/d', 0);
  225. $info = CmsContentModel::where(['user_id' => $this->auth->id, 'id' => $id])->find();
  226. if (empty($info)) {
  227. $this->error('稿件不存在!');
  228. }
  229. $catid = $info['catid'];
  230. //取得栏目数据
  231. $category = getCategory($catid);
  232. if (empty($category)) {
  233. $this->error('该栏目不存在!', url('publish', ['step' => 2]));
  234. }
  235. //模型ID
  236. $modelid = (int) $category['modelid'];
  237. //判断是否终极栏目
  238. if ($category['child'] || $category['type'] == 0) {
  239. $this->error("该栏目不允许投稿!", url('publish', ['step' => 2]));
  240. }
  241. $fieldList = Service::getFieldList($modelid, $info['content_id']);
  242. $this->assign([
  243. 'catid' => $catid,
  244. 'fieldList' => $fieldList,
  245. ]);
  246. $this->assign('title', '编辑投稿');
  247. return $this->fetch('content_edit');
  248. }
  249. }
  250. public function published()
  251. {
  252. if ($this->request->isAjax()) {
  253. $limit = $this->request->param('limit/d', 10);
  254. $page = $this->request->param('page/d', 10);
  255. $type = $this->request->param('type/s', "");
  256. $where = ['user_id' => $this->auth->id];
  257. if ('check' == $type) {
  258. $where['status'] = 1;
  259. }
  260. if ('checking' == $type) {
  261. $where['status'] = 0;
  262. }
  263. $total = CmsContentModel::where($where)->count();
  264. $list = CmsContentModel::where($where)->page($page, $limit)->order("id DESC")->select();
  265. foreach ($list as $k => $v) {
  266. $modelid = getCategory($v['catid'], 'modelid');
  267. $info = Db::name(getModel($modelid, 'tablename'))->where(["id" => $v['content_id'], "sysadd" => 0])->find();
  268. if ($info) {
  269. $list[$k]['url'] = buildContentUrl($v['catid'], $v['content_id'], $info['url']);
  270. $list[$k]['title'] = $info['title'];
  271. $list[$k]['catname'] = getCategory($v['catid'], 'catname');
  272. }
  273. }
  274. $result = ["code" => 0, "count" => $total, "data" => $list];
  275. return json($result);
  276. }
  277. $this->assign('title', '我的投稿');
  278. return $this->fetch('content_published');
  279. }
  280. //删除
  281. public function del()
  282. {
  283. $ids = $this->request->param('id/a', null);
  284. if (empty($ids)) {
  285. $this->error('请指定需要删除的信息!');
  286. }
  287. if (!is_array($ids)) {
  288. $ids = [0 => $ids];
  289. }
  290. foreach ($ids as $id) {
  291. //信息
  292. $row = CmsContentModel::where('id', $id)->find();
  293. //只能删除自己的 且 未通过审核的
  294. if ($row && $row['user_id'] == $this->auth->id && $row['status'] != 1) {
  295. try {
  296. $this->CmsModel->deleteModelData($row['catid'], $row['content_id']);
  297. $row->delete();
  298. } catch (\Exception $e) {
  299. $this->error($e->getMessage());
  300. }
  301. } else {
  302. $this->error('对不起,你无权删除!');
  303. }
  304. }
  305. $this->success('删除成功!');
  306. }
  307. //检查会员组权限
  308. private function check_group_auth()
  309. {
  310. $config = get_addon_config("cms");
  311. //判断当前会员,是否可投稿
  312. if ($config['denied_contribute_groups'] && in_array($this->auth->group_id, explode(',', $config['denied_contribute_groups']))) {
  313. $this->error('当前用户组无权投稿');
  314. }
  315. }
  316. }