Ajax.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use addons\shop\library\aip\AipContentCensor;
  4. use addons\shop\library\SensitiveHelper;
  5. use addons\shop\library\Service;
  6. use app\common\controller\Backend;
  7. use think\Cache;
  8. /**
  9. * Ajax
  10. *
  11. * @icon fa fa-circle-o
  12. * @internal
  13. */
  14. class Ajax extends Backend
  15. {
  16. /**
  17. * 模型对象
  18. */
  19. protected $model = null;
  20. protected $noNeedRight = ['*'];
  21. /**
  22. * 获取模板列表
  23. * @internal
  24. */
  25. public function get_template_list()
  26. {
  27. $files = [];
  28. $keyValue = $this->request->request("keyValue");
  29. if (!$keyValue) {
  30. $type = $this->request->request("type");
  31. $name = $this->request->request("name");
  32. if ($name) {
  33. //$files[] = ['name' => $name . '.html'];
  34. }
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags']);
  37. $config = get_addon_config('shop');
  38. $themeDir = ADDON_PATH . 'shop' . DS . 'view' . DS . $config['theme'] . DS;
  39. $dh = opendir($themeDir);
  40. while (false !== ($filename = readdir($dh))) {
  41. if ($filename == '.' || $filename == '..') {
  42. continue;
  43. }
  44. if ($type) {
  45. $rule = $type == 'channel' ? '(channel|list)' : $type;
  46. if (!preg_match("/^{$rule}(.*)/i", $filename)) {
  47. continue;
  48. }
  49. }
  50. $files[] = ['name' => $filename];
  51. }
  52. } else {
  53. $files[] = ['name' => $keyValue];
  54. }
  55. return $result = ['total' => count($files), 'list' => $files];
  56. }
  57. /**
  58. * 检查内容是否包含违禁词
  59. * @throws \Exception
  60. */
  61. public function check_content_islegal()
  62. {
  63. $config = get_addon_config('shop');
  64. $content = $this->request->post('content');
  65. if (!$content) {
  66. $this->error(__('Please input your content'));
  67. }
  68. if ($config['audittype'] == 'local') {
  69. // 敏感词过滤
  70. $handle = SensitiveHelper::init()->setTreeByFile(ADDON_PATH . 'shop/data/words.dic');
  71. //首先检测是否合法
  72. $arr = $handle->getBadWord($content);
  73. if ($arr) {
  74. $this->error(__('The content is not legal'), null, $arr);
  75. } else {
  76. $this->success(__('The content is legal'));
  77. }
  78. } else {
  79. $client = new AipContentCensor($config['aip_appid'], $config['aip_apikey'], $config['aip_secretkey']);
  80. $result = $client->textCensorUserDefined($content);
  81. if (isset($result['conclusionType']) && $result['conclusionType'] > 1) {
  82. $msg = [];
  83. foreach ($result['data'] as $index => $datum) {
  84. $msg[] = $datum['msg'];
  85. }
  86. $this->error(implode("<br>", $msg), null, []);
  87. } else {
  88. $this->success(__('The content is legal'));
  89. }
  90. }
  91. }
  92. /**
  93. * 获取关键字
  94. * @throws \Exception
  95. */
  96. public function get_content_keywords()
  97. {
  98. $config = get_addon_config('shop');
  99. $title = $this->request->post('title');
  100. $tags = $this->request->post('tags', '');
  101. $content = $this->request->post('content');
  102. if (!$content) {
  103. $this->error(__('Please input your content'));
  104. }
  105. $keywords = Service::getContentTags($title);
  106. $keywords = in_array($title, $keywords) ? [] : $keywords;
  107. $keywords = array_filter(array_merge([$tags], $keywords));
  108. $description = mb_substr(strip_tags($content), 0, 200);
  109. $data = [
  110. "keywords" => implode(',', $keywords),
  111. "description" => $description
  112. ];
  113. $this->success("提取成功", null, $data);
  114. }
  115. /**
  116. * 获取标题拼音
  117. */
  118. public function get_title_pinyin()
  119. {
  120. $title = $this->request->post("title");
  121. //分隔符
  122. $delimiter = $this->request->post("delimiter", "");
  123. $pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
  124. if ($title) {
  125. $result = $pinyin->permalink($title, $delimiter);
  126. $this->success("", null, ['pinyin' => $result]);
  127. } else {
  128. $this->error(__('Parameter %s can not be empty', 'name'));
  129. }
  130. }
  131. /**
  132. * @ 获取模板内容
  133. * @return void
  134. */
  135. public function get_tpl()
  136. {
  137. $tpl_id = $this->request->param('tpl_id');
  138. $source_id = $this->request->param('source_id');
  139. if (empty($tpl_id)) {
  140. $this->error('缺少模板参数');
  141. }
  142. if (empty($source_id)) {
  143. $this->error('缺少卡片来源');
  144. }
  145. $html = \addons\shop\library\Service::getSourceTpl($tpl_id, $source_id);
  146. $this->success('', '', $html);
  147. }
  148. public function get_page_list()
  149. {
  150. $pageList = [
  151. ['path' => 'https://www.baidu.com', 'name' => '外部链接'],
  152. ['path' => '/pages/index/index', 'name' => '首页'],
  153. ['path' => '/pages/category/index', 'name' => '分类'],
  154. ['path' => '/pages/cart/cart', 'name' => '购物车'],
  155. ['path' => '/pages/my/my', 'name' => '我的', 'flag' => 'my'],
  156. ['path' => '/pages/order/list', 'name' => '我的订单', 'flag' => 'my'],
  157. ['path' => '/pages/signin/signin', 'name' => '每日一签', 'flag' => 'my'],
  158. ['path' => '/pages/my/collect', 'name' => '我的收藏', 'flag' => 'my'],
  159. ['path' => '/pages/address/address', 'name' => '我的地址', 'flag' => 'my'],
  160. ['path' => '/pages/coupon/user', 'name' => '我的优惠券', 'flag' => 'my'],
  161. ['path' => '/pages/score/order', 'name' => '我的积分兑换', 'flag' => 'my'],
  162. ['path' => '/pages/remark/comment', 'name' => '我的评论', 'flag' => 'my'],
  163. ['path' => '/pages/my/profile', 'name' => '个人资料', 'flag' => 'my'],
  164. ['path' => '/pages/help/index', 'name' => '帮助中心', 'flag' => 'my'],
  165. ['path' => '/pages/page/page?diyname=aboutus', 'name' => '关于我们', 'flag' => 'my'],
  166. ['path' => '/pages/my/agreement', 'name' => '用户协议', 'flag' => 'my'],
  167. ['path' => '/pages/signin/ranking', 'name' => '签到排行榜'],
  168. ['path' => '/pages/signin/logs', 'name' => '签到日志'],
  169. ['path' => '/pages/login/login', 'name' => '登录(账号密码)'],
  170. ['path' => '/pages/login/mobilelogin', 'name' => '登录(手机号)'],
  171. ['path' => '/pages/login/register', 'name' => '注册'],
  172. ['path' => '/pages/login/forgetpwd', 'name' => '忘记密码'],
  173. ['path' => '/pages/goods/goods?category_id=1', 'name' => '商品列表(category_id=分类ID)'],
  174. ['path' => '/pages/goods/detail?id=1', 'name' => '商品详情(id=数据ID)'],
  175. ['path' => '/pages/address/addedit', 'name' => '地址编辑(添加)'],
  176. ['path' => '/pages/page/page?id=1&diyname=diyname', 'name' => '单页详情(id=数据ID和diyname=单页自定义URL名称)'],
  177. ];
  178. if ($this->request->isAjax()) {
  179. $filter = $this->request->get('filter');
  180. $filterArr = (array)json_decode($filter, true);
  181. $list = \app\admin\model\shop\Category::where('status', '<>', 'hidden')->field('*')->select();
  182. foreach ($list as $index => $item) {
  183. $pageList[] = ['path' => '/pages/goods/goods?category_id=' . $item['id'], 'name' => '分类:' . $item['name'], 'flag' => 'category'];
  184. }
  185. $list = \app\admin\model\shop\Goods::where('status', '<>', 'hidden')->field('*')->select();
  186. foreach ($list as $index => $item) {
  187. $pageList[] = ['path' => '/pages/goods/detail?id=' . $item['id'], 'name' => '商品:' . $item['title'], 'flag' => 'goods'];
  188. }
  189. $list = \app\admin\model\shop\Page::where('status', '<>', 'hidden')->field('*')->select();
  190. foreach ($list as $index => $item) {
  191. $pageList[] = ['path' => '/pages/page/page?id=' . $item['id'], 'name' => '单页:' . $item['title'], 'flag' => 'page'];
  192. }
  193. if ($filterArr) {
  194. if (isset($filterArr['flag']) && $filterArr['flag'] && $filterArr['flag'] != 'all') {
  195. foreach ($pageList as $index => $item) {
  196. if (!isset($item['flag']) || $item['flag'] !== $filterArr['flag']) {
  197. unset($pageList[$index]);
  198. }
  199. }
  200. }
  201. $pageList = array_values($pageList);
  202. if (isset($filterArr['path']) && $filterArr['path']) {
  203. foreach ($pageList as $index => $item) {
  204. if (stripos($item['path'], $filterArr['path']) === false) {
  205. unset($pageList[$index]);
  206. }
  207. }
  208. }
  209. $pageList = array_values($pageList);
  210. if (isset($filterArr['name']) && $filterArr['name']) {
  211. foreach ($pageList as $index => $item) {
  212. if (stripos($item['name'], $filterArr['name']) === false) {
  213. unset($pageList[$index]);
  214. }
  215. }
  216. }
  217. }
  218. $search = $this->request->get('search', '');
  219. $offset = $this->request->get('offset', 0);
  220. $limit = $this->request->get('limit', 10);
  221. if ($search) {
  222. foreach ($pageList as $index => $item) {
  223. if (stripos($item['path'], $search) === false && stripos($item['name'], $search) === false) {
  224. unset($pageList[$index]);
  225. }
  226. }
  227. $pageList = array_values($pageList);
  228. }
  229. $result = array("total" => count($pageList), "rows" => array_slice($pageList, $offset, $limit));
  230. return json($result);
  231. }
  232. $this->view->assign('pageList', $pageList);
  233. return $this->view->fetch('shop/common/pages');
  234. }
  235. public function config()
  236. {
  237. $name = $this->request->get('name');
  238. if ($name == 'sms') {
  239. $config = config('addons.hooks');
  240. if (isset($config['sms_send']) && $config['sms_send']) {
  241. $name = reset($config['sms_send']);
  242. } else {
  243. $this->error("请在插件管理中安装一款短信验证插件", "");
  244. }
  245. } elseif ($name == 'oss') {
  246. $config = config('addons.hooks');
  247. if (isset($config['upload_config_init']) && $config['upload_config_init']) {
  248. $availableArr = array_intersect($config['upload_config_init'], ['alioss', 'bos', 'cos', 'upyun', 'ucloud', 'hwobs', 'qiniu']);
  249. if ($availableArr) {
  250. $name = reset($availableArr);
  251. }
  252. }
  253. if (!$name || $name == 'oss') {
  254. $this->error("请在插件管理中安装一款云存储插件", "");
  255. }
  256. } else {
  257. $info = get_addon_info($name);
  258. $addonArr = [
  259. 'third' => '第三方登录',
  260. 'signin' => '会员签到',
  261. 'epay' => '微信支付宝整合',
  262. ];
  263. if (!$info) {
  264. $this->error('请检查对应插件' . (isset($addonArr[$name]) ? "《{$addonArr[$name]}》" : "") . '是否安装且启用', "");
  265. }
  266. }
  267. $this->redirect('addon/config?name=' . $name . '&dialog=1');
  268. }
  269. /**
  270. * 清除缓存
  271. */
  272. public function clearcache()
  273. {
  274. Cache::clear("shop");
  275. $config = get_addon_config('shop');
  276. @rmdirs(TEMP_PATH, false);
  277. $this->success("");
  278. }
  279. }