Ajax.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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=模型ID或channel=栏目ID)'],
  173. ['path' => '/pages/index/my', 'name' => '个人中心'],
  174. ['path' => '/pages/my/profile', 'name' => '个人资料'],
  175. ['path' => '/pages/my/agreement', 'name' => '用户协议'],
  176. ['path' => '/pages/my/article', 'name' => '我发布的文章'],
  177. ['path' => '/pages/my/comment', 'name' => '我发表的评论'],
  178. ['path' => '/pages/my/order', 'name' => '我的消费订单'],
  179. ['path' => '/pages/my/aboutus', 'name' => '关于我们'],
  180. ['path' => '/pages/my/member', 'name' => 'VIP会员'],
  181. ['path' => '/pages/my/collection', 'name' => '我的收藏'],
  182. ['path' => '/pages/my/moneylog', 'name' => '余额日志'],
  183. ['path' => '/pages/my/scorelog', 'name' => '积分日志'],
  184. ['path' => '/pages/article/article', 'name' => '文章列表(?model=模型ID或channel=栏目ID)'],
  185. ['path' => '/pages/article/detail?id=1', 'name' => '文章详情(id=资讯ID)'],
  186. ['path' => '/pages/product/product', 'name' => '产品列表(?model=模型ID或channel=栏目ID)'],
  187. ['path' => '/pages/product/detail?id=1', 'name' => '产品详情(id=产品ID)'],
  188. ['path' => '/pages/publish/channel', 'name' => '发布文章'],
  189. ['path' => '/pages/search/search', 'name' => '搜索'],
  190. ['path' => '/pages/diyform/lists?diyname=diyname', 'name' => '自定义表单列表(diyname=表单自定义URL名称)'],
  191. ['path' => '/pages/diyform/detail?id=1&diyname=diyname', 'name' => '自定义表单详情(id=数据ID和diyname=表单自定义URL名称)'],
  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/special/special', 'name' => '专题列表'],
  198. ['path' => '/pages/special/detail?diyname=name', 'name' => '专题详情(diyname=专题自定义URL名称)'],
  199. ['path' => '/pages/page/detail?diyname=name', 'name' => '单页详情(diyname=单页自定义URL名称)'],
  200. ];
  201. $this->view->assign('pageList', $pageList);
  202. return $this->view->fetch('cms/common/pages');
  203. }
  204. public function get_link_list()
  205. {
  206. if ($this->request->isAjax()) {
  207. $filter = $this->request->request("filter", '', 'trim');
  208. $filter = (array)json_decode($filter, true);
  209. $pageList = \app\admin\model\cms\Page::all();
  210. $specialList = \app\admin\model\cms\Special::all();
  211. $diyformList = \app\admin\model\cms\Diyform::all();
  212. $rows = [];
  213. if (!isset($filter['type']) || $filter['type'] == 'page') {
  214. foreach ($pageList as $index => $item) {
  215. $rows[] = ['type' => 'page', 'url' => $item['url'], 'title' => $item['title'], 'source_id' => $item['id']];
  216. }
  217. }
  218. if (!isset($filter['type']) || $filter['type'] == 'special') {
  219. foreach ($specialList as $index => $item) {
  220. $rows[] = ['type' => 'special', 'url' => $item['url'], 'title' => $item['title'], 'source_id' => $item['id']];
  221. }
  222. }
  223. if (!isset($filter['type']) || $filter['type'] == 'diyform') {
  224. foreach ($diyformList as $index => $item) {
  225. $rows[] = ['type' => 'diyform', 'url' => $item['url'], 'title' => $item['name'] . ' - 列表页', 'source_id' => $item['id']];
  226. $rows[] = ['type' => 'diyform', 'url' => $item['post_url'], 'title' => $item['name'] . " - 投稿页", 'source_id' => $item['id']];
  227. }
  228. }
  229. foreach ($rows as $index => $row) {
  230. if (isset($filter['url']) && stripos($row['url'], $filter['url']) === false) {
  231. unset($rows[$index]);
  232. continue;
  233. }
  234. if (isset($filter['title']) && stripos($row['title'], $filter['title']) === false) {
  235. unset($rows[$index]);
  236. continue;
  237. }
  238. }
  239. return [
  240. 'rows' => array_values($rows),
  241. 'total' => count($rows)
  242. ];
  243. }
  244. $typeList = [
  245. 'special' => '专题',
  246. 'page' => '单页',
  247. 'diyform' => '自定义表单',
  248. ];
  249. $this->view->assign('typeList', $typeList);
  250. return $this->view->fetch('cms/common/links');
  251. }
  252. public function config()
  253. {
  254. $name = $this->request->get('name');
  255. if ($name == 'sms') {
  256. $config = config('addons.hooks');
  257. if (isset($config['sms_send']) && $config['sms_send']) {
  258. $name = reset($config['sms_send']);
  259. } else {
  260. $this->error("请在插件管理中安装一款短信验证插件", "");
  261. }
  262. } elseif ($name == 'oss') {
  263. $config = config('addons.hooks');
  264. if (isset($config['upload_config_init']) && $config['upload_config_init']) {
  265. $availableArr = array_intersect($config['upload_config_init'], ['alioss', 'bos', 'cos', 'upyun', 'ucloud', 'hwobs', 'qiniu']);
  266. if ($availableArr) {
  267. $name = reset($availableArr);
  268. }
  269. }
  270. if (!$name || $name == 'oss') {
  271. $this->error("请在插件管理中安装一款云存储插件", "");
  272. }
  273. } else {
  274. $info = get_addon_info($name);
  275. $addonArr = [
  276. 'third' => '第三方登录',
  277. 'signin' => '会员签到',
  278. 'epay' => '微信支付宝整合',
  279. ];
  280. if (!$info) {
  281. $this->error('请检查对应插件' . (isset($addonArr[$name]) ? "《{$addonArr[$name]}》" : "") . '是否安装且启动', "");
  282. }
  283. }
  284. $this->redirect('addon/config?name=' . $name . '&dialog=1');
  285. }
  286. /**
  287. * 检测元素是否可用
  288. * @internal
  289. */
  290. public function check_config_available()
  291. {
  292. $name = $this->request->request('name');
  293. $value = $this->request->request('value');
  294. $name = substr($name, 4, -1);
  295. if (!$name) {
  296. $this->error(__('Parameter %s can not be empty', 'name'));
  297. }
  298. if ($name == 'theme') {
  299. if (!preg_match("/^([a-zA-Z0-9\-_]+)$/i", $value)) {
  300. $this->error("只支持字母数字下划线");
  301. }
  302. if (!is_dir(ADDON_PATH . 'cms' . DS . 'view' . DS . $value . DS)) {
  303. $this->error("皮肤目录不存在");
  304. }
  305. } elseif ($name == 'archivesratio') {
  306. $valueArr = explode(':', $value);
  307. if (!isset($valueArr[1])) {
  308. $this->error('格式不正确');
  309. }
  310. if (bcadd($valueArr[0], $valueArr[1], 2) != 1) {
  311. $this->error('分成占比相加必须等于1');
  312. }
  313. } elseif ($name == 'cachelifetime') {
  314. if (!is_numeric($value) && !in_array($value, ['true', 'false'])) {
  315. $this->error("格式不正确,只支持 数字/true");
  316. }
  317. }
  318. $this->success();
  319. }
  320. }