Service.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace app\common\library;
  3. use addons\shop\library\aip\AipContentCensor;
  4. use addons\shop\library\aip\AipNlp;
  5. use addons\shop\library\SensitiveHelper;
  6. use addons\shop\library\VicWord;
  7. use think\View;
  8. class Service
  9. {
  10. /**
  11. * 检测内容是否合法
  12. * @param string $content 检测内容
  13. * @param string $type 类型
  14. * @return bool
  15. */
  16. public static function isContentLegal($content, $type = null)
  17. {
  18. $config = get_addon_config('shop');
  19. $type = is_null($type) ? $config['audittype'] : $type;
  20. if ($type == 'local') {
  21. // 敏感词过滤
  22. $handle = SensitiveHelper::init()->setTreeByFile(ADDON_PATH . 'shop/data/words.dic');
  23. //首先检测是否合法
  24. $isLegal = $handle->islegal($content);
  25. return $isLegal ? true : false;
  26. } elseif ($type == 'baiduyun') {
  27. $client = new AipContentCensor($config['aip_appid'], $config['aip_apikey'], $config['aip_secretkey']);
  28. $result = $client->textCensorUserDefined($content);
  29. if (!isset($result['conclusionType']) || $result['conclusionType'] > 1) {
  30. return false;
  31. }
  32. }
  33. return true;
  34. }
  35. /**
  36. * 获取标题的关键字
  37. * @param $title
  38. * @return array
  39. */
  40. public static function getContentTags($title)
  41. {
  42. $arr = [];
  43. $config = get_addon_config('shop');
  44. if ($config['nlptype'] == 'local') {
  45. !defined('_VIC_WORD_DICT_PATH_') && define('_VIC_WORD_DICT_PATH_', ADDON_PATH . 'shop/data/dict.json');
  46. $handle = new VicWord('json');
  47. $result = $handle->getAutoWord($title);
  48. foreach ($result as $index => $item) {
  49. $arr[] = $item[0];
  50. }
  51. } else {
  52. $client = new AipNlp($config['aip_appid'], $config['aip_apikey'], $config['aip_secretkey']);
  53. $result = $client->lexer($title);
  54. if (isset($result['items'])) {
  55. foreach ($result['items'] as $index => $item) {
  56. if (!in_array($item['pos'], ['v', 'vd', 'nd', 'a', 'ad', 'an', 'd', 'm', 'q', 'r', 'p', 'c', 'u', 'xc', 'w'])) {
  57. $arr[] = $item['item'];
  58. }
  59. }
  60. }
  61. }
  62. foreach ($arr as $index => $item) {
  63. if (mb_strlen($item) == 1) {
  64. unset($arr[$index]);
  65. }
  66. }
  67. return array_filter(array_unique($arr));
  68. }
  69. /**
  70. * 内容关键字自动加链接
  71. */
  72. public static function autolinks($value)
  73. {
  74. $links = [];
  75. $value = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', function ($match) use (&$links) {
  76. return '<' . array_push($links, $match[1]) . '>';
  77. }, $value);
  78. $config = get_addon_config('shop');
  79. $autolinks = $config['autolinks'];
  80. $value = preg_replace_callback('/(' . implode('|', array_keys($autolinks)) . ')/i', function ($match) use ($autolinks) {
  81. if (!isset($autolinks[$match[1]])) {
  82. return $match[0];
  83. } else {
  84. return '<a href="' . $autolinks[$match[1]] . '" target="_blank">' . $match[0] . '</a>';
  85. }
  86. }, $value);
  87. return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) {
  88. return $links[$match[1] - 1];
  89. }, $value);
  90. }
  91. /**
  92. * @ 获取商品模板
  93. * @return void
  94. */
  95. public static function getSourceTpl($tpl_id, $source_id)
  96. {
  97. if (!$tpl_id || !$source_id) {
  98. return '';
  99. }
  100. $row = \addons\shop\model\Card::where('status', 'normal')->where('id', $tpl_id)->find();
  101. if (empty($row)) {
  102. return '';
  103. }
  104. $source = null;
  105. switch ($row['type']) {
  106. case 1:
  107. $source = \addons\shop\model\Coupon::getCouponInfo($source_id);
  108. break;
  109. default:
  110. $source = \addons\shop\model\Goods::get($source_id);
  111. }
  112. if (empty($source)) {
  113. return '';
  114. }
  115. $view = new View();
  116. $html = $view->fetch($row->content, ['source' => $source, 'tpl' => $row], [], [], true);
  117. return $html;
  118. }
  119. /**
  120. * @ 替换多余的卡片信息
  121. * @param $value
  122. * @return void
  123. */
  124. public static function replaceSourceTpl($value)
  125. {
  126. if (!empty($value)) {
  127. return preg_replace('/(?<=data-id=\"shop\"\>).*?(?=\<div\s+data-id="end"\>(\s|[\r\n]|&nbsp;)*\<\/div\>)/is', '&nbsp;', $value);
  128. }
  129. return $value;
  130. }
  131. /**
  132. * @ 商品模板转化
  133. * @return void
  134. */
  135. public static function formatSourceTpl($value)
  136. {
  137. if (!empty($value)) {
  138. $value = preg_replace_callback('/\<div\s+data-tpl\=\"(\d+)"\s+data\-source=\"(\w+)\"[\s\S]*?\<div\s+data-id\=\"end\"\>[\s\S]*?\<\/div\>[\s\S]*?\<\/div\>/i', function ($match) {
  139. return \addons\shop\library\Service::getSourceTpl($match[1], $match[2]);
  140. }, $value);
  141. }
  142. return $value;
  143. }
  144. /**
  145. * @ uniapp商品模板转化
  146. * @return void
  147. */
  148. public static function formatTplToUniapp($value)
  149. {
  150. if (!empty($value)) {
  151. $value = preg_replace_callback("/href=\"(.*?)\"(\s|[\r\n]|&nbsp;)*data\-type=\"(goods|coupon)\"(\s|[\r\n]|&nbsp;)*data\-id=\"(\w+)\"/is", function ($matches) {
  152. return 'href="/pages/' . $matches[3] . '/detail?id=' . $matches[5] . '"';
  153. }, $value);
  154. }
  155. return $value;
  156. }
  157. /**
  158. * 获取缓存标签和时长
  159. * @param string $type
  160. * @param array $tag
  161. * @return array
  162. */
  163. public static function getCacheKeyExpire($type, $tag = [])
  164. {
  165. $config = get_addon_config('shop');
  166. $cache = !isset($tag['cache']) ? $config['cachelifetime'] : $tag['cache'];
  167. $cache = in_array($cache, ['true', 'false', true, false], true) ? (in_array($cache, ['true', true], true) ? 0 : -1) : (int)$cache;
  168. $cacheKey = $cache > -1 ? "shop-taglib-{$type}-" . md5(serialize($tag)) : false;
  169. $cacheExpire = $cache > -1 ? $cache : null;
  170. return [$cacheKey, $cacheExpire];
  171. }
  172. }