Ajax.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace app\admin\controller\cms;
  3. use addons\cms\library\aip\AipContentCensor;
  4. use addons\cms\library\SensitiveHelper;
  5. use addons\cms\library\Service;
  6. use app\common\controller\Backend;
  7. use think\Config;
  8. use think\Db;
  9. /**
  10. * Ajax
  11. *
  12. * @icon fa fa-circle-o
  13. * @internal
  14. */
  15. class Ajax extends Backend
  16. {
  17. /**
  18. * 模型对象
  19. */
  20. protected $model = null;
  21. protected $noNeedRight = ['*'];
  22. /**
  23. * 获取模板列表
  24. * @internal
  25. */
  26. public function get_template_list()
  27. {
  28. $files = [];
  29. $keyValue = $this->request->request("keyValue");
  30. if (!$keyValue) {
  31. $type = $this->request->request("type");
  32. $name = $this->request->request("name");
  33. if ($name) {
  34. //$files[] = ['name' => $name . '.html'];
  35. }
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags']);
  38. $config = get_addon_config('cms');
  39. $themeDir = ADDON_PATH . 'cms' . DS . 'view' . DS . $config['theme'] . DS;
  40. $dh = opendir($themeDir);
  41. while (false !== ($filename = readdir($dh))) {
  42. if ($filename == '.' || $filename == '..') {
  43. continue;
  44. }
  45. if ($type) {
  46. $rule = $type == 'channel' ? '(channel|list)' : $type;
  47. if (!preg_match("/^{$rule}(.*)/i", $filename)) {
  48. continue;
  49. }
  50. }
  51. $files[] = ['name' => $filename];
  52. }
  53. } else {
  54. $files[] = ['name' => $keyValue];
  55. }
  56. return $result = ['total' => count($files), 'list' => $files];
  57. }
  58. /**
  59. * 检查内容是否包含违禁词
  60. * @throws \Exception
  61. */
  62. public function check_content_islegal()
  63. {
  64. $config = get_addon_config('cms');
  65. $content = $this->request->post('content');
  66. if (!$content) {
  67. $this->error(__('Please input your content'));
  68. }
  69. if ($config['audittype'] == 'local') {
  70. // 敏感词过滤
  71. $handle = SensitiveHelper::init()->setTreeByFile(ADDON_PATH . 'cms/data/words.dic');
  72. //首先检测是否合法
  73. $arr = $handle->getBadWord($content);
  74. if ($arr) {
  75. $this->error(__('The content is not legal'), null, $arr);
  76. } else {
  77. $this->success(__('The content is legal'));
  78. }
  79. } else {
  80. $client = new AipContentCensor($config['aip_appid'], $config['aip_apikey'], $config['aip_secretkey']);
  81. $result = $client->textCensorUserDefined($content);
  82. if (isset($result['conclusionType']) && $result['conclusionType'] > 1) {
  83. $msg = [];
  84. foreach ($result['data'] as $index => $datum) {
  85. $msg[] = $datum['msg'];
  86. }
  87. $this->error(implode("<br>", $msg), null, []);
  88. } else {
  89. $this->success(__('The content is legal'));
  90. }
  91. }
  92. }
  93. /**
  94. * 获取关键字
  95. * @throws \Exception
  96. */
  97. public function get_content_keywords()
  98. {
  99. $config = get_addon_config('cms');
  100. $title = $this->request->post('title');
  101. $tags = $this->request->post('tags', '');
  102. $content = $this->request->post('content');
  103. if (!$content) {
  104. $this->error(__('Please input your content'));
  105. }
  106. $keywords = Service::getContentTags($title);
  107. $keywords = in_array($title, $keywords) ? [] : $keywords;
  108. $keywords = array_filter(array_merge([$tags], $keywords));
  109. $description = mb_substr(strip_tags($content), 0, 200);
  110. $data = [
  111. "keywords" => implode(',', $keywords),
  112. "description" => $description
  113. ];
  114. $this->success("提取成功", null, $data);
  115. }
  116. /**
  117. * 获取标题拼音
  118. */
  119. public function get_title_pinyin()
  120. {
  121. $config = get_addon_config('cms');
  122. $title = $this->request->post("title");
  123. //分隔符
  124. $delimiter = $this->request->post("delimiter", "");
  125. $pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
  126. if ($title) {
  127. if ($config['autopinyin']) {
  128. $result = $pinyin->permalink($title, $delimiter);
  129. $this->success("", null, ['pinyin' => $result]);
  130. } else {
  131. $this->error();
  132. }
  133. } else {
  134. $this->error(__('Parameter %s can not be empty', 'name'));
  135. }
  136. }
  137. /**
  138. * 获取表字段列表
  139. * @internal
  140. */
  141. public function get_fields_list()
  142. {
  143. $table = $this->request->request('table');
  144. $fieldList = Service::getTableFields($table);
  145. $this->success("", null, ['fieldList' => $fieldList]);
  146. }
  147. /**
  148. * 获取自定义字段列表HTML
  149. * @internal
  150. */
  151. public function get_fields_html()
  152. {
  153. $this->view->engine->layout(false);
  154. $source = $this->request->post('source');
  155. $id = $this->request->post('id/d');
  156. if (in_array($source, ['channel', 'page', 'special'])) {
  157. $values = \think\Db::name("cms_{$source}")->where('id', $id)->find();
  158. $values = $values ? $values : [];
  159. $fields = \addons\cms\library\Service::getCustomFields($source, 0, $values);
  160. $this->view->assign('fields', $fields);
  161. $this->view->assign('values', $values);
  162. $this->success('', null, ['html' => $this->view->fetch('cms/common/fields')]);
  163. } else {
  164. $this->error(__('Please select type'));
  165. }
  166. $this->error(__('Parameter %s can not be empty', 'ids'));
  167. }
  168. public function get_page_list()
  169. {
  170. $pageList = [
  171. ['path' => 'https://www.baidu.com', 'name' => '外部链接'],
  172. ['path' => '/pages/index/index?model=1', 'name' => '主页(?model=1或channel=1)'],
  173. ['path' => '/pages/my/my', 'name' => '个人中心'],
  174. ['path' => '/pages/my/profile', 'name' => '个人资料'],
  175. ['path' => '/pages/my/agreement', 'name' => '用户协议'],
  176. ['path' => '/pages/my/comment', 'name' => '我发表的评论'],
  177. ['path' => '/pages/my/myorder', 'name' => '我的消费订单'],
  178. ['path' => '/pages/my/about', 'name' => '关于我们'],
  179. ['path' => '/pages/logs/money', 'name' => '余额日志'],
  180. ['path' => '/pages/logs/score', 'name' => '积分日志'],
  181. ['path' => '/pages/article/article', 'name' => '资讯(?model=1或channel=1)'],
  182. ['path' => '/pages/article/detail?id=1', 'name' => '资讯详情(咨询ID)'],
  183. ['path' => '/pages/product/product', 'name' => '产品(?model=1或channel=1)'],
  184. ['path' => '/pages/product/detail?id=1', 'name' => '产品详情(产品ID)'],
  185. ['path' => '/pages/publish/channel', 'name' => '选择栏目'],
  186. ['path' => '/pages/publish/publish', 'name' => '发布文章'],
  187. ['path' => '/pages/publish/myarticle', 'name' => '我发布的文章'],
  188. ['path' => '/pages/search/search', 'name' => '搜索'],
  189. ['path' => '/pages/diyform/diyform?diyname=diyname', 'name' => '自定义表单(可指定表的名称)'],
  190. ['path' => '/pages/diyform/lists?diyname=diyname', 'name' => '留言列表(可指定表的名称)'],
  191. ['path' => '/pages/diyform/detail?id=1&diyname=diyname', 'name' => '留言详情(可指定表的名称)'],
  192. ['path' => '/pages/tag/tag?name=tagName', 'name' => '标签'],
  193. ['path' => '/pages/user/user?user_id=1', 'name' => '用户主页'],
  194. ['path' => '/pages/signin/signin', 'name' => '签到'],
  195. ['path' => '/pages/signin/ranking', 'name' => '签到排行榜'],
  196. ['path' => '/pages/signin/logs', 'name' => '签到日志'],
  197. ['path' => '/pages/login/login', 'name' => '登录(账号密码)'],
  198. ['path' => '/pages/login/mobilelogin', 'name' => '登录(手机号)'],
  199. ['path' => '/pages/login/register', 'name' => '注册'],
  200. ['path' => '/pages/login/forgetpwd', 'name' => '忘记密码'],
  201. ['path' => '/pages/my/member', 'name' => 'VIP会员'],
  202. ['path' => '/pages/my/collection', 'name' => '我的收藏'],
  203. ];
  204. $this->view->assign('pageList', $pageList);
  205. return $this->view->fetch('cms/common/pages');
  206. }
  207. public function get_link_list()
  208. {
  209. if ($this->request->isAjax()) {
  210. $filter = $this->request->request("filter", '', 'trim');
  211. $filter = (array)json_decode($filter, true);
  212. $pageList = \app\admin\model\cms\Page::all();
  213. $specialList = \app\admin\model\cms\Special::all();
  214. $diyformList = \app\admin\model\cms\Diyform::all();
  215. $rows = [];
  216. if (!isset($filter['type']) || $filter['type'] == 'page') {
  217. foreach ($pageList as $index => $item) {
  218. $rows[] = ['type' => 'page', 'url' => $item['url'], 'name' => $item['title']];
  219. }
  220. }
  221. if (!isset($filter['type']) || $filter['type'] == 'special') {
  222. foreach ($specialList as $index => $item) {
  223. $rows[] = ['type' => 'special', 'url' => $item['url'], 'name' => $item['title']];
  224. }
  225. }
  226. if (!isset($filter['type']) || $filter['type'] == 'diyform') {
  227. foreach ($diyformList as $index => $item) {
  228. $rows[] = ['type' => 'diyform', 'url' => $item['url'], 'name' => $item['name'] . ' - 列表页'];
  229. $rows[] = ['type' => 'diyform', 'url' => $item['post_url'], 'name' => $item['name'] . " - 投稿页"];
  230. }
  231. }
  232. foreach ($rows as $index => $row) {
  233. if (isset($filter['url']) && stripos($row['url'], $filter['url']) === false) {
  234. unset($rows[$index]);
  235. continue;
  236. }
  237. if (isset($filter['name']) && stripos($row['name'], $filter['name']) === false) {
  238. unset($rows[$index]);
  239. continue;
  240. }
  241. }
  242. return [
  243. 'rows' => array_values($rows),
  244. 'rows' => $rows,
  245. 'total' => count($rows)
  246. ];
  247. }
  248. $typeList = [
  249. 'special' => '专题',
  250. 'page' => '单页',
  251. 'diyform' => '自定义表单',
  252. ];
  253. $this->view->assign('typeList', $typeList);
  254. return $this->view->fetch('cms/common/links');
  255. }
  256. }