心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SingleController.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2017年12月15日
  7. * 单页内容控制器
  8. */
  9. namespace app\admin\controller\content;
  10. use core\basic\Controller;
  11. use app\admin\model\content\SingleModel;
  12. class SingleController extends Controller
  13. {
  14. private $model;
  15. private $blank;
  16. public function __construct()
  17. {
  18. $this->model = new SingleModel();
  19. }
  20. // 单页内容列表
  21. public function index()
  22. {
  23. if ((! ! $id = get('id', 'int')) && $result = $this->model->getSingle($id)) {
  24. $this->assign('more', true);
  25. $this->assign('content', $result);
  26. } else {
  27. $this->assign('list', true);
  28. if (! $mcode = get('mcode', 'var')) {
  29. error('传递的模型编码参数有误,请核对后重试!');
  30. }
  31. if (! ! ($field = get('field', 'var')) && ! ! ($keyword = get('keyword', 'vars'))) {
  32. $result = $this->model->findSingle($mcode, $field, $keyword);
  33. } else {
  34. $result = $this->model->getList($mcode);
  35. }
  36. $this->assign('baidu_zz_token', $this->config('baidu_zz_token'));
  37. $this->assign('baidu_ks_token', $this->config('baidu_ks_token'));
  38. // 模型名称
  39. $this->assign('model_name', model('admin.content.Model')->getName($mcode));
  40. // 前端地址连接符判断
  41. $url_break_char = $this->config('url_break_char') ?: '_';
  42. $this->assign('url_break_char', $url_break_char);
  43. $this->assign('contents', $result);
  44. }
  45. $this->display('content/single.html');
  46. }
  47. // 单页内容删除
  48. public function del()
  49. {
  50. if (! $id = get('id', 'int')) {
  51. error('传递的参数值错误!', - 1);
  52. }
  53. if ($this->model->delSingle($id)) {
  54. $this->log('删除单页内容' . $id . '成功!');
  55. success('删除成功!', - 1);
  56. } else {
  57. $this->log('删除单页内容' . $id . '失败!');
  58. error('删除失败!', - 1);
  59. }
  60. }
  61. // 单页内容修改
  62. public function mod()
  63. {
  64. // 前端地址连接符判断
  65. if (get('baiduzz') || get('baiduxzh')) {
  66. $url_break_char = $this->config('url_break_char') ?: '_';
  67. $url_rule_sort_suffix = $this->config('url_rule_sort_suffix') ? true : false;
  68. }
  69. // 站长普通推送
  70. if (! ! $id = get('baiduzz')) {
  71. $domain = get_http_url();
  72. if (! $token = $this->config('baidu_zz_token')) {
  73. alert_back('请先到系统配置中填写百度普通收录推送token值!');
  74. }
  75. $api = "http://data.zz.baidu.com/urls?site=$domain&token=$token";
  76. $data = $this->model->getSingle($id);
  77. $data->urlname = $data->urlname ?: 'about';
  78. if ($data->outlink) {
  79. alert_back('链接类型不允许推送!');
  80. }
  81. if ($data->filename) {
  82. $url = $domain . homeurl('/home/Index/' . $data->filename, $url_rule_sort_suffix);
  83. $url = str_replace(".html","",$url);
  84. $urls[] = $url;
  85. } else {
  86. $url = $domain . homeurl('/home/Index/' . $data->urlname . $url_break_char . $data->scode, $url_rule_sort_suffix);
  87. $url = str_replace(".html","",$url);
  88. $urls[] = $url;
  89. }
  90. $result = post_baidu($api, $urls);
  91. if (isset($result->error)) {
  92. $this->log('百度普通收录推送失败:' . $urls[0]);
  93. alert_back('推送发生错误:' . $result->message);
  94. } elseif (isset($result->success)) {
  95. $this->log('百度普通收录推送成功:' . $urls[0]);
  96. alert_back('成功推送' . $result->success . '条,今天剩余可推送' . $result->remain . '条数!');
  97. } else {
  98. alert_back('发生未知错误!');
  99. }
  100. }
  101. // 站长快速推送
  102. if (! ! $id = get('baiduks')) {
  103. $domain = get_http_url();
  104. if (! $token = $this->config('baidu_ks_token')) {
  105. alert_back('请先到系统配置中填写百度快速收录推送token值!');
  106. }
  107. $api = "http://data.zz.baidu.com/urls?site=$domain&token=$token&type=daily";
  108. $data = $this->model->getSingle($id);
  109. $data->urlname = $data->urlname ?: 'about';
  110. if ($data->outlink) {
  111. alert_back('链接类型不允许推送!');
  112. }
  113. if ($data->filename) {
  114. $urls[] = $domain . homeurl('/home/Index/' . $data->filename, $url_rule_sort_suffix);
  115. } else {
  116. $urls[] = $domain . homeurl('/home/Index/' . $data->urlname . $url_break_char . $data->scode, $url_rule_sort_suffix);
  117. }
  118. $result = post_baidu($api, $urls);
  119. if (isset($result->error)) {
  120. $this->log('百度快速收录推送失败:' . $urls[0]);
  121. alert_back('推送发生错误:' . $result->message);
  122. } elseif (isset($result->success_daily)) {
  123. $this->log('百度快速收录推送成功:' . $urls[0]);
  124. alert_back('成功推送' . $result->success_daily . '条,今天剩余可推送' . $result->remain_daily . '条数!');
  125. } else {
  126. alert_back('发生未知错误!');
  127. }
  128. }
  129. if (! $id = get('id', 'int')) {
  130. error('传递的参数值错误!', - 1);
  131. }
  132. // 单独修改状态
  133. if (($field = get('field', 'var')) && ! is_null($value = get('value', 'var'))) {
  134. if ($this->model->modSingle($id, "$field='$value',update_user='" . session('username') . "'")) {
  135. location(- 1);
  136. } else {
  137. alert_back('修改失败!');
  138. }
  139. }
  140. // 修改操作
  141. if ($_POST) {
  142. // 获取数据
  143. $title = post('title');
  144. $author = post('author');
  145. $source = post('source');
  146. $ico = post('ico');
  147. $pics = post('pics');
  148. $content = post('content');
  149. $tags = str_replace(',', ',', post('tags'));
  150. $titlecolor = post('titlecolor');
  151. $subtitle = post('subtitle');
  152. $outlink = post('outlink');
  153. $date = post('date');
  154. $enclosure = post('enclosure');
  155. $keywords = post('keywords');
  156. $description = post('description');
  157. $status = post('status', 'int');
  158. // 获取多图标题
  159. $picstitle = post('picstitle');
  160. if ($picstitle) {
  161. $picstitle = implode(',', $picstitle);
  162. }
  163. if (! $title) {
  164. alert_back('单页内容标题不能为空!');
  165. }
  166. // 自动提起前一百个字符为描述
  167. if (! $description && isset($_POST['content'])) {
  168. $description = escape_string(clear_html_blank(substr_both(strip_tags($_POST['content']), 0, 150)));
  169. }
  170. // 无缩略图时,自动提取文章第一张图为缩略图
  171. if (! $ico && preg_match('/<img\s+.*?src=\s?[\'|\"](.*?(\.gif|\.jpg|\.png|\.jpeg))[\'|\"].*?[\/]?>/i', decode_string($content), $srcs) && isset($srcs[1])) {
  172. $ico = $srcs[1];
  173. }
  174. // 缩放缩略图
  175. if ($ico) {
  176. resize_img(ROOT_PATH . $ico, '', $this->config('ico.max_width'), $this->config('ico.max_height'));
  177. }
  178. // 构建数据
  179. $data = array(
  180. 'title' => $title,
  181. 'content' => $content,
  182. 'tags' => $tags,
  183. 'author' => $author,
  184. 'source' => $source,
  185. 'ico' => $ico,
  186. 'pics' => $pics,
  187. 'picstitle' => $picstitle,
  188. 'titlecolor' => $titlecolor,
  189. 'subtitle' => $subtitle,
  190. 'outlink' => $outlink,
  191. 'date' => $date,
  192. 'enclosure' => $enclosure,
  193. 'keywords' => $keywords,
  194. 'description' => clear_html_blank($description),
  195. 'status' => $status,
  196. 'update_user' => session('username')
  197. );
  198. // 执行添加
  199. if ($this->model->modSingle($id, $data)) {
  200. // 扩展内容修改
  201. foreach ($_POST as $key => $value) {
  202. if (preg_match('/^ext_[\w\-]+$/', $key)) {
  203. $temp = post($key);
  204. if (is_array($temp)) {
  205. $data2[$key] = implode(',', $temp);
  206. } else {
  207. $data2[$key] = str_replace("\r\n", '<br>', $temp);
  208. }
  209. }
  210. }
  211. if (isset($data2)) {
  212. if ($this->model->findContentExt($id)) {
  213. $this->model->modContentExt($id, $data2);
  214. } else {
  215. $data2['contentid'] = $id;
  216. $this->model->addContentExt($data2);
  217. }
  218. }
  219. $this->log('修改单页内容' . $id . '成功!');
  220. if (! ! $backurl = get('backurl')) {
  221. success('修改成功!', base64_decode($backurl));
  222. } else {
  223. success('修改成功!', url('/admin/Single/index/mcode/1'));
  224. }
  225. } else {
  226. location(- 1);
  227. }
  228. } else {
  229. // 调取修改内容
  230. $this->assign('mod', true);
  231. if (! $result = $this->model->getSingle($id)) {
  232. error('编辑的内容已经不存在!', - 1);
  233. }
  234. $this->assign('content', $result);
  235. // 扩展字段
  236. if (! $mcode = get('mcode', 'var')) {
  237. error('传递的模型编码参数有误,请核对后重试!');
  238. }
  239. $this->assign('extfield', model('admin.content.ExtField')->getModelField($mcode));
  240. $this->display('content/single.html');
  241. }
  242. }
  243. }