common.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. <?php
  2. use think\facade\Db;
  3. /* 引用全局定义 */
  4. require __DIR__ . '/common_global.php';
  5. /* 商品相关调用 */
  6. require __DIR__ . '/common_goods.php';
  7. /* 图片上传、生成缩略图、删除等操作调用 */
  8. require __DIR__ . '/common_upload.php';
  9. function ds_validate($name) {
  10. $name = preg_replace_callback('/([-_]+([a-z]{1}))/i', function($matches) {
  11. return strtoupper($matches[2]);
  12. }, $name);
  13. $class_name = '\app\common\validate\\' . ucfirst($name);
  14. return new $class_name;
  15. }
  16. function model($name, $layer = 'model') {
  17. $name = preg_replace_callback('/([-_]+([a-z]{1}))/i', function($matches) {
  18. return strtoupper($matches[2]);
  19. }, $name);
  20. $class_name = '\app\common\\' . $layer . '\\' . ucfirst($name);
  21. return new $class_name;
  22. }
  23. /*
  24. * 更换数组的键值 为了应对 ->key
  25. */
  26. function ds_change_arraykey($array, $key) {
  27. $data = array();
  28. foreach ($array as $value) {
  29. $data[$value[$key]] = $value;
  30. }
  31. return $data;
  32. }
  33. /**
  34. *
  35. * @param type $table 数据表
  36. * @param type $field 条件对应的字段
  37. * @param type $name 条件对应的值
  38. * @param type $value 数值
  39. * @return type
  40. */
  41. function ds_getvalue_byname($table, $field, $name, $value) {
  42. return Db::name($table)->where($field, $name)->value($value);
  43. }
  44. /*
  45. * 编辑器内容
  46. */
  47. function build_editor($params = array()) {
  48. $name = isset($params['name']) ? $params['name'] : null;
  49. $theme = isset($params['theme']) ? $params['theme'] : 'normal';
  50. $content = isset($params['content']) ? $params['content'] : null;
  51. //http://fex.baidu.com/ueditor/#start-toolbar
  52. /* 指定使用哪种主题 */
  53. $themes = array(
  54. 'normal' => "[
  55. 'fullscreen', 'source', '|', 'undo', 'redo', '|',
  56. 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
  57. 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
  58. 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
  59. 'directionalityltr', 'directionalityrtl', 'indent', '|',
  60. 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
  61. 'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
  62. 'emotion', 'map', 'gmap', 'insertcode', 'template', '|',
  63. 'horizontal', 'date', 'time', 'spechars', '|',
  64. 'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
  65. 'searchreplace', 'help', 'drafts', 'charts'
  66. ]", 'simple' => " ['fullscreen', 'source', 'undo', 'redo', 'bold']",
  67. );
  68. switch ($theme) {
  69. case 'simple':
  70. $theme_config = $themes['simple'];
  71. break;
  72. case 'normal':
  73. $theme_config = $themes['normal'];
  74. break;
  75. default:
  76. $theme_config = $themes['normal'];
  77. break;
  78. }
  79. /* 配置界面语言 */
  80. switch (config('lang.default_lang')) {
  81. case 'zh-cn':
  82. $lang = PLUGINS_SITE_ROOT . '/ueditor/lang/zh-cn/zh-cn.js';
  83. break;
  84. case 'en-us':
  85. $lang = PLUGINS_SITE_ROOT . '/ueditor/lang/en/en.js';
  86. break;
  87. default:
  88. $lang = PLUGINS_SITE_ROOT . '/ueditor/lang/zh-cn/zh-cn.js';
  89. break;
  90. }
  91. $include_js = '<script type="text/javascript" charset="utf-8" src="' . PLUGINS_SITE_ROOT . '/ueditor/ueditor.config.js"></script> <script type="text/javascript" charset="utf-8" src="' . PLUGINS_SITE_ROOT . '/ueditor/ueditor.all.min.js""> </script><script type="text/javascript" charset="utf-8" src="' . $lang . '"></script>';
  92. $content = json_encode($content);
  93. $str = <<<EOT
  94. $include_js
  95. <script type="text/javascript">
  96. var ue = UE.getEditor('{$name}',{
  97. toolbars:[{$theme_config}],
  98. });
  99. if($content){
  100. ue.ready(function() {
  101. this.setContent($content);
  102. })
  103. }
  104. </script>
  105. EOT;
  106. return $str;
  107. }
  108. /**
  109. *
  110. * @param type $code 100000表示为正确,其他为错误代码
  111. * @param type $message 提示消息
  112. * @param type $result 返回数据
  113. * @param type $$requestMethod 返回请求Method
  114. */
  115. function ds_json_encode($code, $message = '', $result = '', $requestMethod = '', $if_exit = true) {
  116. $data = array('code' => $code, 'message' => $message, 'result' => $result, 'requestMethod' => $requestMethod);
  117. if (!empty($_GET['callback'])) {
  118. echo $_GET['callback'] . '(' . json_encode($data) . ')';
  119. } else {
  120. echo json_encode($data);
  121. }
  122. if ($if_exit) {
  123. exit;
  124. }
  125. }
  126. /**
  127. * 规范数据返回函数
  128. * @param unknown $code
  129. * @param unknown $msg
  130. * @param unknown $data
  131. * @return multitype:unknown
  132. */
  133. function ds_callback($code, $msg = '', $data = array()) {
  134. return array('code' => $code, 'msg' => $msg, 'data' => $data);
  135. }
  136. /**
  137. * 格式化字节大小
  138. * @param number $size 字节数
  139. * @param string $delimiter 数字和单位分隔符
  140. * @return string 格式化后的带单位的大小
  141. */
  142. function format_bytes($size, $delimiter = '') {
  143. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  144. for ($i = 0; $size >= 1024 && $i < 5; $i++)
  145. $size /= 1024;
  146. return round($size, 2) . $delimiter . $units[$i];
  147. }
  148. /**
  149. * 消息提示,主要适用于普通页面AJAX提交的情况
  150. *
  151. * @param string $message 消息内容
  152. * @param string $url 提示完后的URL去向
  153. * @param stting $alert_type 提示类型 error/succ/notice 分别为错误/成功/警示
  154. * @param string $extrajs 扩展JS
  155. * @param int $time 停留时间
  156. */
  157. function ds_show_dialog($message = '', $url = '', $alert_type = 'error', $extrajs = '', $time = 2) {
  158. $message = str_replace("'", "\\'", strip_tags($message));
  159. $paramjs = null;
  160. if ($url == 'reload') {
  161. $paramjs = 'window.location.reload()';
  162. } elseif ($url != '') {
  163. $paramjs = 'window.location.href =\'' . $url . '\'';
  164. }
  165. if ($paramjs) {
  166. $paramjs = 'function (){' . $paramjs . '}';
  167. } else {
  168. $paramjs = 'null';
  169. }
  170. $modes = array('error' => 'alert', 'succ' => 'succ', 'notice' => 'notice', 'js' => 'js');
  171. $cover = $alert_type == 'error' ? 1 : 0;
  172. $extra = 'showDialog(\'' . $message . '\', \'' . $modes[$alert_type] . '\', null, ' . ($paramjs ? $paramjs : 'null') . ', ' . $cover . ', null, null, null, null, ' . (is_numeric($time) ? $time : 'null') . ', null);';
  173. $extra = '<script type="text/javascript" reload="1">' . $extra . '</script>';
  174. if ($extrajs != '' && substr(trim($extrajs), 0, 7) != '<script') {
  175. $extrajs = '<script type="text/javascript" reload="1">' . $extrajs . '</script>';
  176. }
  177. $extra .= $extrajs;
  178. ob_end_clean();
  179. @header("Expires: -1");
  180. @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
  181. @header("Pragma: no-cache");
  182. @header("Content-type: text/xml; charset=utf-8");
  183. $string = '<?xml version="1.0" encoding="utf-8"?>' . "\r\n";
  184. $string .= '<root><![CDATA[' . $message . $extra . ']]></root>';
  185. echo $string;
  186. exit;
  187. }
  188. /**
  189. * 取上一步来源地址
  190. *
  191. * @param
  192. * @return string 字符串类型的返回结果
  193. */
  194. function get_referer() {
  195. return empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER'];
  196. }
  197. /**
  198. * 加密函数
  199. *
  200. * @param string $txt 需要加密的字符串
  201. * @param string $key 密钥
  202. * @return string 返回加密结果
  203. */
  204. function ds_encrypt($txt, $key = '') {
  205. if (empty($txt))
  206. return $txt;
  207. if (empty($key))
  208. $key = md5(config('ds_config.setup_date'));
  209. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
  210. $ikey = "-x6g6ZWm2G9g_vr0Bo.pOq3kRIxsZ6rm";
  211. $nh1 = rand(0, 64);
  212. $nh2 = rand(0, 64);
  213. $nh3 = rand(0, 64);
  214. $ch1 = $chars{$nh1};
  215. $ch2 = $chars{$nh2};
  216. $ch3 = $chars{$nh3};
  217. $nhnum = $nh1 + $nh2 + $nh3;
  218. $knum = 0;
  219. $i = 0;
  220. while (isset($key{$i}))
  221. $knum += ord($key{$i++});
  222. $mdKey = substr(md5(md5(md5($key . $ch1) . $ch2 . $ikey) . $ch3), $nhnum % 8, $knum % 8 + 16);
  223. $txt = base64_encode(TIMESTAMP . '_' . $txt);
  224. $txt = str_replace(array('+', '/', '='), array('-', '_', '.'), $txt);
  225. $tmp = '';
  226. $j = 0;
  227. $k = 0;
  228. $tlen = strlen($txt);
  229. $klen = strlen($mdKey);
  230. for ($i = 0; $i < $tlen; $i++) {
  231. $k = $k == $klen ? 0 : $k;
  232. $j = ($nhnum + strpos($chars, $txt{$i}) + ord($mdKey{$k++})) % 64;
  233. $tmp .= $chars{$j};
  234. }
  235. $tmplen = strlen($tmp);
  236. $tmp = substr_replace($tmp, $ch3, $nh2 % ++$tmplen, 0);
  237. $tmp = substr_replace($tmp, $ch2, $nh1 % ++$tmplen, 0);
  238. $tmp = substr_replace($tmp, $ch1, $knum % ++$tmplen, 0);
  239. return $tmp;
  240. }
  241. /**
  242. * 解密函数
  243. *
  244. * @param string $txt 需要解密的字符串
  245. * @param string $key 密匙
  246. * @return string 字符串类型的返回结果
  247. */
  248. function ds_decrypt($txt, $key = '', $ttl = 0) {
  249. if (empty($txt))
  250. return $txt;
  251. if (empty($key))
  252. $key = md5(config('ds_config.setup_date'));
  253. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
  254. $ikey = "-x6g6ZWm2G9g_vr0Bo.pOq3kRIxsZ6rm";
  255. $knum = 0;
  256. $i = 0;
  257. $tlen = @strlen($txt);
  258. while (isset($key{$i}))
  259. $knum += ord($key{$i++});
  260. $ch1 = @$txt{$knum % $tlen};
  261. $nh1 = strpos($chars, $ch1);
  262. $txt = @substr_replace($txt, '', $knum % $tlen--, 1);
  263. $ch2 = @$txt{$nh1 % $tlen};
  264. $nh2 = @strpos($chars, $ch2);
  265. $txt = @substr_replace($txt, '', $nh1 % $tlen--, 1);
  266. $ch3 = @$txt{$nh2 % $tlen};
  267. $nh3 = @strpos($chars, $ch3);
  268. $txt = @substr_replace($txt, '', $nh2 % $tlen--, 1);
  269. $nhnum = $nh1 + $nh2 + $nh3;
  270. $mdKey = substr(md5(md5(md5($key . $ch1) . $ch2 . $ikey) . $ch3), $nhnum % 8, $knum % 8 + 16);
  271. $tmp = '';
  272. $j = 0;
  273. $k = 0;
  274. $tlen = @strlen($txt);
  275. $klen = @strlen($mdKey);
  276. for ($i = 0; $i < $tlen; $i++) {
  277. $k = $k == $klen ? 0 : $k;
  278. $j = strpos($chars, $txt{$i}) - $nhnum - ord($mdKey{$k++});
  279. while ($j < 0)
  280. $j += 64;
  281. $tmp .= $chars{$j};
  282. }
  283. $tmp = str_replace(array('-', '_', '.'), array('+', '/', '='), $tmp);
  284. $tmp = trim(base64_decode($tmp));
  285. if (preg_match("/\d{10}_/s", substr($tmp, 0, 11))) {
  286. if ($ttl > 0 && (TIMESTAMP - (int) substr($tmp, 0, 11) > $ttl)) {
  287. $tmp = null;
  288. } else {
  289. $tmp = substr($tmp, 11);
  290. }
  291. }
  292. return $tmp;
  293. }
  294. /**
  295. * 获取文件列表(所有子目录文件)
  296. *
  297. * @param string $path 目录
  298. * @param array $file_list 存放所有子文件的数组
  299. * @param array $ignore_dir 需要忽略的目录或文件
  300. * @return array 数据格式的返回结果
  301. */
  302. function read_file_list($path, &$file_list, $ignore_dir = array()) {
  303. $path = rtrim($path, '/');
  304. if (is_dir($path)) {
  305. $handle = @opendir($path);
  306. if ($handle) {
  307. while (false !== ($dir = readdir($handle))) {
  308. if ($dir != '.' && $dir != '..') {
  309. if (!in_array($dir, $ignore_dir)) {
  310. if (is_file($path . '/' . $dir)) {
  311. $file_list[] = $path . '/' . $dir;
  312. } elseif (is_dir($path . '/' . $dir)) {
  313. read_file_list($path . '/' . $dir, $file_list, $ignore_dir);
  314. }
  315. }
  316. }
  317. }
  318. @closedir($handle);
  319. } else {
  320. return false;
  321. }
  322. } else {
  323. return false;
  324. }
  325. }
  326. /**
  327. * 价格格式化
  328. *
  329. * @param int $price
  330. * @return string $price_format
  331. */
  332. function ds_price_format($price) {
  333. $price_format = number_format($price, 2, '.', '');
  334. return $price_format;
  335. }
  336. /**
  337. * 价格格式化
  338. *
  339. * @param int $price
  340. * @return string $price_format
  341. */
  342. function ds_price_format_forlist($price) {
  343. if ($price >= 10000) {
  344. return number_format(floor($price / 100) / 100, 2, '.', '') . lang('ten_thousand');
  345. } else {
  346. return lang('currency') . $price;
  347. }
  348. }
  349. /**
  350. * 通知邮件/通知消息 内容转换函数
  351. *
  352. * @param string $message 内容模板
  353. * @param array $param 内容参数数组
  354. * @return string 通知内容
  355. */
  356. function ds_replace_text($message, $param) {
  357. if (!is_array($param))
  358. return false;
  359. foreach ($param as $k => $v) {
  360. $message = str_replace('${' . $k . '}', $v, $message);
  361. }
  362. return $message;
  363. }
  364. /** @noinspection InconsistentLineSeparators */
  365. /**
  366. * 字符串切割函数,一个字母算一个位置,一个字算2个位置
  367. *
  368. * @param string $string 待切割的字符串
  369. * @param int $length 切割长度
  370. * @param string $dot 尾缀
  371. */
  372. function str_cut($string, $length, $dot = '') {
  373. $string = str_replace(array(
  374. '&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;',
  375. '&middot;', '&hellip;'
  376. ), array(' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
  377. $strlen = strlen($string);
  378. if ($strlen <= $length)
  379. return $string;
  380. $maxi = $length - strlen($dot);
  381. $strcut = '';
  382. $n = $tn = $noc = 0;
  383. while ($n < $strlen) {
  384. $t = ord($string[$n]);
  385. if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
  386. $tn = 1;
  387. $n++;
  388. $noc++;
  389. } elseif (194 <= $t && $t <= 223) {
  390. $tn = 2;
  391. $n += 2;
  392. $noc += 2;
  393. } elseif (224 <= $t && $t < 239) {
  394. $tn = 3;
  395. $n += 3;
  396. $noc += 2;
  397. } elseif (240 <= $t && $t <= 247) {
  398. $tn = 4;
  399. $n += 4;
  400. $noc += 2;
  401. } elseif (248 <= $t && $t <= 251) {
  402. $tn = 5;
  403. $n += 5;
  404. $noc += 2;
  405. } elseif ($t == 252 || $t == 253) {
  406. $tn = 6;
  407. $n += 6;
  408. $noc += 2;
  409. } else {
  410. $n++;
  411. }
  412. if ($noc >= $maxi)
  413. break;
  414. }
  415. if ($noc > $maxi)
  416. $n -= $tn;
  417. $strcut = substr($string, 0, $n);
  418. $strcut = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&#039;', '&lt;', '&gt;'), $strcut);
  419. return $strcut . $dot;
  420. }
  421. /*
  422. * 重写$_SERVER['REQUREST_URI']
  423. */
  424. function request_uri() {
  425. if (isset($_SERVER['REQUEST_URI'])) {
  426. $uri = $_SERVER['REQUEST_URI'];
  427. } else {
  428. if (isset($_SERVER['argv'])) {
  429. $uri = $_SERVER['PHP_SELF'] . '?' . $_SERVER['argv'][0];
  430. } else {
  431. $uri = $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
  432. }
  433. }
  434. return $uri;
  435. }
  436. function get_member_id_by_XDSKEY() {
  437. $key = request()->header('X-DS-KEY');
  438. if (!$key) {
  439. return;
  440. }
  441. $mbusertoken_model = model('mbusertoken');
  442. $mb_user_token_info = $mbusertoken_model->getMbusertokenInfoByToken($key);
  443. if (empty($mb_user_token_info)) {
  444. return;
  445. } else {
  446. return $mb_user_token_info['member_id'];
  447. }
  448. }
  449. function get_member_idcard_image($member_image) {
  450. if ($member_image) {
  451. return ds_get_pic(ATTACH_IDCARD_IMAGE, $member_image);
  452. }
  453. return '';
  454. }
  455. /**
  456. * 取得用户头像图片
  457. *
  458. * @param string $member_avatar
  459. * @return string
  460. */
  461. function get_member_avatar($member_avatar) {
  462. if (empty($member_avatar)) {
  463. return ds_get_pic(ATTACH_COMMON,config('ds_config.default_user_portrait'));
  464. } else {
  465. $url = ds_get_pic(ATTACH_AVATAR, $member_avatar);
  466. if ($url) {
  467. return $url;
  468. } else {
  469. return ds_get_pic(ATTACH_COMMON,config('ds_config.default_user_portrait'));
  470. }
  471. }
  472. }
  473. /**
  474. * 成员头像
  475. * @param string $member_id
  476. * @return string
  477. */
  478. function get_member_avatar_for_id($id) {
  479. $member_model=model('member');
  480. $member_info=$member_model->getMemberInfoByID($id);
  481. if($member_info){
  482. return get_member_avatar($member_info['member_avatar']);
  483. }
  484. }
  485. /**
  486. * 取得店铺标志
  487. *
  488. * @param string $img 图片名
  489. * @param string $type 查询类型 store_logo/store_avatar
  490. * @return string
  491. */
  492. function get_store_logo($img, $type = 'store_avatar') {
  493. $linfo = explode('_', $img);
  494. $store_id = $linfo['0'];
  495. if ($store_id == 'alioss') {
  496. $store_id = $linfo['1'];
  497. }
  498. if ($type == 'store_avatar') {
  499. if (empty($img)) {
  500. return ds_get_pic(ATTACH_COMMON, config('ds_config.default_store_avatar'));
  501. } else {
  502. $url = ds_get_pic(ATTACH_STORE . '/' . $store_id, $img);
  503. if (!$url) {
  504. return ds_get_pic(ATTACH_COMMON, config('ds_config.default_store_avatar'));
  505. } else {
  506. return $url;
  507. }
  508. }
  509. } elseif ($type == 'store_logo') {
  510. if (empty($img)) {
  511. return ds_get_pic(ATTACH_COMMON, config('ds_config.default_store_logo'));
  512. } else {
  513. return ds_get_pic(ATTACH_STORE . '/' . $store_id, $img);
  514. }
  515. } elseif ($type == 'store_banner') {
  516. if (!empty($img)) {
  517. return ds_get_pic(ATTACH_STORE . '/' . $store_id, $img);
  518. }
  519. }
  520. }
  521. function get_adv_code($adv_code) {
  522. $url = ds_get_pic(ATTACH_ADV, $adv_code);
  523. if (!$url) {
  524. return ds_get_pic(ATTACH_COMMON,config('ds_config.default_goods_image'));
  525. } else {
  526. return $url;
  527. }
  528. }
  529. function get_appadv_code($adv_code) {
  530. $url = ds_get_pic(ATTACH_APPADV, $adv_code);
  531. if (!$url) {
  532. return ds_get_pic(ATTACH_COMMON,config('ds_config.default_goods_image'));
  533. } else {
  534. return $url;
  535. }
  536. }
  537. /**
  538. * 获取用户相册图片
  539. * @param type $user_id
  540. * @param type $ap_cover
  541. * @return type
  542. */
  543. function get_snsalbumpic($user_id, $ap_cover) {
  544. $url = ds_get_pic(ATTACH_MALBUM . '/' . $user_id, $ap_cover);
  545. if (!$url) {
  546. return ds_get_pic(ATTACH_COMMON,config('ds_config.default_goods_image'));
  547. } else {
  548. return $url;
  549. }
  550. }
  551. /**
  552. * 获取开店申请图片
  553. */
  554. function get_store_joinin_imageurl($image_name = '') {
  555. return ds_get_pic(ATTACH_STORE_JOININ, $image_name);
  556. }
  557. /**
  558. * 获取提货点图片
  559. */
  560. function get_chain_imageurl($image_name = '') {
  561. return ds_get_pic(ATTACH_CHAIN, $image_name);
  562. }
  563. /**
  564. * 取得随机数
  565. *
  566. * @param int $length 生成随机数的长度
  567. * @param int $numeric 是否只产生数字随机数 1是0否
  568. * @return string
  569. */
  570. function random($length, $numeric = 0) {
  571. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  572. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  573. $hash = '';
  574. $max = strlen($seed) - 1;
  575. for ($i = 0; $i < $length; $i++) {
  576. $hash .= $seed{mt_rand(0, $max)};
  577. }
  578. return $hash;
  579. }
  580. /**
  581. * sns表情标示符替换为html
  582. */
  583. function parsesmiles($message,$type=0) {
  584. if ($type==1) {
  585. $chat_goods = $message;
  586. $message = '<div class="dstouch-chat-product"> <a href="' . HOME_SITE_URL . '/goods/index?goods_id=' . $chat_goods['goods_id'] . '" target="_blank"><div class="goods-pic"><img src="' . $chat_goods['goods_image_url'] . '" alt=""/></div><div class="goods-info"><div class="goods-name">' . $chat_goods['goods_name'] . '</div><div class="goods-price">¥' . $chat_goods['goods_price'] . "</div></div></a> </div>";
  587. } else {
  588. $file = root_path() . 'extend/smilies.php';
  589. if (file_exists($file)) {
  590. include $file;
  591. if (!empty($smilies_array) && is_array($smilies_array)) {
  592. $imagesurl = PLUGINS_SITE_ROOT . '/js' . '/smilies' . '/images' . '/';
  593. $replace_arr = array();
  594. foreach ($smilies_array['replacearray'] AS $key => $smiley) {
  595. $replace_arr[$key] = '<img src="' . $imagesurl . $smiley['imagename'] . '" title="' . $smiley['desc'] . '" border="0" alt="' . $imagesurl . $smiley['desc'] . '" />';
  596. }
  597. $message = preg_replace($smilies_array['searcharray'], $replace_arr, $message);
  598. }
  599. }
  600. }
  601. return $message;
  602. }
  603. /**
  604. * 延时加载分页功能,判断是否有更多连接和limitstart值和经过验证修改的$delay_eachnum值
  605. * @param int $delay_eachnum 延时分页每页显示的条数
  606. * @param int $delay_page 延时分页当前页数
  607. * @param int $count 总记录数
  608. * @param bool $ispage 是否在分页模式中实现延时分页(前台显示的两种不同效果)
  609. * @param int $page_nowpage 分页当前页数
  610. * @param int $page_eachnum 分页每页显示条数
  611. * @param int $page_limitstart 分页初始limit值
  612. * @return array array('hasmore'=>'是否显示更多连接','limitstart'=>'加载的limit开始值','delay_eachnum'=>'经过验证修改的$delay_eachnum值');
  613. */
  614. function lazypage($delay_eachnum, $delay_page, $count, $ispage = false, $page_nowpage = 1, $page_eachnum = 1, $page_limitstart = 1) {
  615. //是否有多余
  616. $hasmore = true;
  617. $limitstart = 0;
  618. if ($ispage == true) {
  619. if ($delay_eachnum < $page_eachnum) {//当延时加载每页条数小于分页的每页条数时候实现延时加载,否则按照普通分页程序流程处理
  620. $page_totlepage = ceil($count / $page_eachnum);
  621. //计算limit的开始值
  622. $limitstart = $page_limitstart + ($delay_page - 1) * $delay_eachnum;
  623. if ($page_totlepage > $page_nowpage) {//当前不为最后一页
  624. if ($delay_page >= $page_eachnum / $delay_eachnum) {
  625. $hasmore = false;
  626. }
  627. //判断如果分页的每页条数与延时加载每页的条数不能整除的处理
  628. if ($hasmore == false && $page_eachnum % $delay_eachnum > 0) {
  629. $delay_eachnum = $page_eachnum % $delay_eachnum;
  630. }
  631. } else {//当前最后一页
  632. $showcount = ($page_totlepage - 1) * $page_eachnum + $delay_eachnum * $delay_page; //已经显示的记录总数
  633. if ($count <= $showcount) {
  634. $hasmore = false;
  635. }
  636. }
  637. } else {
  638. $hasmore = false;
  639. }
  640. } else {
  641. if ($count <= $delay_page * $delay_eachnum) {
  642. $hasmore = false;
  643. }
  644. //计算limit的开始值
  645. $limitstart = ($delay_page - 1) * $delay_eachnum;
  646. }
  647. return array('hasmore' => $hasmore, 'limitstart' => $limitstart, 'delay_eachnum' => $delay_eachnum);
  648. }
  649. /**
  650. * 返回以原数组某个值为下标的新数据
  651. *
  652. * @param array $array
  653. * @param string $key
  654. * @param int $type 1一维数组2二维数组
  655. * @return array
  656. */
  657. function array_under_reset($array, $key, $type = 1) {
  658. if (is_array($array)) {
  659. $tmp = array();
  660. foreach ($array as $v) {
  661. if ($type === 1) {
  662. $tmp[$v[$key]] = $v;
  663. } elseif ($type === 2) {
  664. $tmp[$v[$key]][] = $v;
  665. }
  666. }
  667. return $tmp;
  668. } else {
  669. return $array;
  670. }
  671. }
  672. /**
  673. * KV缓存 读
  674. *
  675. * @param string $key 缓存名称
  676. * @param boolean $callback 缓存读取失败时是否使用回调 true代表使用cache.model中预定义的缓存项 默认不使用回调
  677. * @param callable $callback 传递非boolean值时 通过is_callable进行判断 失败抛出异常 成功则将$key作为参数进行回调
  678. * @return mixed
  679. */
  680. function rkcache($key, $callback = false) {
  681. $value = cache($key);
  682. if (empty($value) && $callback !== false) {
  683. if ($callback === true) {
  684. $callback = array(model('cache'), 'call');
  685. }
  686. if (!is_callable($callback)) {
  687. throw new \think\Exception('Invalid rkcache callback!', 10006);
  688. }
  689. $value = call_user_func($callback, $key);
  690. wkcache($key, $value);
  691. }
  692. return $value;
  693. }
  694. /**
  695. * KV缓存 写
  696. *
  697. * @param string $key 缓存名称
  698. * @param mixed $value 缓存数据 若设为否 则下次读取该缓存时会触发回调(如果有)
  699. * @param int $expire 缓存时间 单位秒 null代表不过期
  700. * @return boolean
  701. */
  702. function wkcache($key, $value, $expire = 7200) {
  703. return cache($key, $value, $expire);
  704. }
  705. /**
  706. * KV缓存 删
  707. *
  708. * @param string $key 缓存名称
  709. * @return boolean
  710. */
  711. function dkcache($key) {
  712. return cache($key, NULL);
  713. }
  714. /**
  715. * 读取缓存信息
  716. *
  717. * @param string $key 要取得缓存键
  718. * @param string $prefix 键值前缀
  719. * @return array/bool
  720. */
  721. function rcache($key = null, $prefix = '') {
  722. if ($key === null || !config('ds_config.cache_open'))
  723. return array();
  724. if (!empty($prefix)) {
  725. $name = $prefix . $key;
  726. } else {
  727. $name = $key;
  728. }
  729. $cache_info = cache($name);
  730. //如果name值不存在,则默认返回 false。
  731. return $cache_info;
  732. }
  733. /**
  734. * 写入缓存
  735. *
  736. * @param string $key 缓存键值
  737. * @param array $data 缓存数据
  738. * @param string $prefix 键值前缀
  739. * @param int $expire 缓存周期 单位分,0为永久缓存
  740. * @return bool 返回值
  741. */
  742. function wcache($key = null, $data = array(), $prefix = '', $expire = 3600) {
  743. if ($key === null || !config('ds_config.cache_open') || !is_array($data))
  744. return;
  745. if (!empty($prefix)) {
  746. $name = $prefix . $key;
  747. } else {
  748. $name = $key;
  749. }
  750. $cache_info = cache($name, $data, $expire);
  751. //如果设置成功返回true,否则返回false。
  752. return $cache_info;
  753. }
  754. /**
  755. * 删除缓存
  756. * @param string $key 缓存键值
  757. * @param string $prefix 键值前缀
  758. * @return boolean
  759. */
  760. function dcache($key = null, $prefix = '') {
  761. if ($key === null || !config('ds_config.cache_open'))
  762. return true;
  763. if (!empty($prefix)) {
  764. $name = $prefix . $key;
  765. } else {
  766. $name = $key;
  767. }
  768. return cache($name, NULL);
  769. }
  770. /**
  771. * 输出聊天信息
  772. *
  773. * @return string
  774. */
  775. function get_chat() {
  776. return Chat::getChatHtml();
  777. }
  778. /**
  779. * 验证是否为平台店铺
  780. *
  781. * @return boolean
  782. */
  783. function check_platform_store() {
  784. return session('is_platform_store');
  785. }
  786. /**
  787. * 验证是否为平台店铺 并且绑定了全部商品类目
  788. *
  789. * @return boolean
  790. */
  791. function check_platform_store_bindingall_goodsclass() {
  792. return check_platform_store() && session('bind_all_gc');
  793. }
  794. /**
  795. * 生成20位编号(时间+微秒+随机数+会员ID%1000),该值会传给第三方支付接口
  796. * 长度 =12位 + 3位 + 2位 + 3位 = 20位
  797. * 1000个会员同一微秒提订单,重复机率为1/100
  798. * @return string
  799. */
  800. function makePaySn($member_id) {
  801. return date('ymdHis', TIMESTAMP) . sprintf('%03d', (float) microtime() * 1000) . mt_rand(10, 99) . sprintf('%03d', intval($member_id) % 1000);
  802. }
  803. /**
  804. * 获得店铺状态样式名称
  805. * @param $param array $store_info
  806. * @return string
  807. */
  808. function get_store_state_classname($store_info) {
  809. $result = 'open';
  810. if (intval($store_info['store_state']) === 1) {
  811. $store_endtime = intval($store_info['store_endtime']);
  812. if ($store_endtime > 0) {
  813. if ($store_endtime < TIMESTAMP) {
  814. $result = 'expired';
  815. } elseif (($store_endtime - 864000) < TIMESTAMP) {
  816. //距离到期10天
  817. $result = 'expire';
  818. }
  819. }
  820. } else {
  821. $result = 'close';
  822. }
  823. return $result;
  824. }
  825. /**
  826. * 将字符部分加密并输出
  827. * @param unknown $str
  828. * @param unknown $start 从第几个位置开始加密(从1开始)
  829. * @param unknown $length 连续加密多少位
  830. * @return string
  831. */
  832. function encrypt_show($str, $start, $length) {
  833. $end = $start - 1 + $length;
  834. $array = str_split($str);
  835. foreach ($array as $k => $v) {
  836. if ($k >= $start - 1 && $k < $end) {
  837. $array[$k] = '*';
  838. }
  839. }
  840. return implode('', $array);
  841. }
  842. /**
  843. * CURL请求
  844. * @param $url 请求url地址
  845. * @param $method 请求方法 get post
  846. * @param null $postfields post数据数组
  847. * @param array $headers 请求header信息
  848. * @param bool|false $debug 调试开启 默认false
  849. * @return mixed
  850. */
  851. function http_request($url, $method = "GET", $postfields = null, $headers = array(), $debug = false) {
  852. $method = strtoupper($method);
  853. $ci = curl_init();
  854. /* Curl settings */
  855. curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  856. curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
  857. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */
  858. curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
  859. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  860. switch ($method) {
  861. case "POST":
  862. curl_setopt($ci, CURLOPT_POST, true);
  863. if (!empty($postfields)) {
  864. $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
  865. curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
  866. }
  867. break;
  868. default:
  869. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
  870. break;
  871. }
  872. $ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE;
  873. curl_setopt($ci, CURLOPT_URL, $url);
  874. if ($ssl) {
  875. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
  876. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
  877. }
  878. //curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
  879. curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
  880. curl_setopt($ci, CURLOPT_MAXREDIRS, 2); /* 指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的 */
  881. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  882. curl_setopt($ci, CURLINFO_HEADER_OUT, true);
  883. /* curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
  884. $response = curl_exec($ci);
  885. $requestinfo = curl_getinfo($ci);
  886. $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  887. if ($debug) {
  888. echo "=====post data======\r\n";
  889. var_dump($postfields);
  890. echo "=====info===== \r\n";
  891. print_r($requestinfo);
  892. echo "=====response=====\r\n";
  893. print_r($response);
  894. }
  895. curl_close($ci);
  896. return $response;
  897. }
  898. /**
  899. * Layer 提交成功返回函数
  900. * @param type $message
  901. */
  902. function dsLayerOpenSuccess($msg = '', $url = '') {
  903. // echo "<script>var index = parent.layer.getFrameIndex(window.name);parent.layer.close(index);parent.location.reload();</script>";
  904. $url_js = empty($url) ? "parent.location.reload();" : "parent.location.href='" . $url . "';";
  905. $str = "<script>";
  906. $str .= "parent.layer.alert('" . $msg . "',{yes:function(index, layero){" . $url_js . "},cancel:function(index, layero){" . $url_js . "}});";
  907. $str .= "</script>";
  908. echo $str;
  909. exit;
  910. }
  911. /**
  912. * 移除微信昵称中的emoji字符
  913. * @param type $nickname
  914. * @return type
  915. */
  916. function removeEmoji($nickname) {
  917. $clean_text = "";
  918. // Match Emoticons
  919. $regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';
  920. $clean_text = preg_replace($regexEmoticons, '', $nickname);
  921. // Match Miscellaneous Symbols and Pictographs
  922. $regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';
  923. $clean_text = preg_replace($regexSymbols, '', $clean_text);
  924. // Match Transport And Map Symbols
  925. $regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';
  926. $clean_text = preg_replace($regexTransport, '', $clean_text);
  927. // Match Miscellaneous Symbols
  928. $regexMisc = '/[\x{2600}-\x{26FF}]/u';
  929. $clean_text = preg_replace($regexMisc, '', $clean_text);
  930. // Match Dingbats
  931. $regexDingbats = '/[\x{2700}-\x{27BF}]/u';
  932. $clean_text = preg_replace($regexDingbats, '', $clean_text);
  933. //截取指定长度的昵称
  934. $clean_text = ds_substing($clean_text, 0, 20);
  935. return trim($clean_text);
  936. }
  937. /**
  938. * 截取指定长度的字符
  939. * @param type $string 内容
  940. * @param type $start 开始
  941. * @param type $length 长度
  942. * @return type
  943. */
  944. function ds_substing($string, $start = 0, $length = 80) {
  945. $string = strip_tags($string);
  946. $string = preg_replace('/\s/', '', $string);
  947. return mb_substr($string, $start, $length);
  948. }
  949. /**
  950. * 针对批量删除进行处理 '1,2,3' 转换为数组批量删除
  951. * @param type $ids
  952. * @return boolean
  953. */
  954. function ds_delete_param($ids) {
  955. //转换为数组
  956. $ids_array = explode(',', $ids);
  957. //数组值转为整数型
  958. $ids_array = array_map("intval", $ids_array);
  959. if (empty($ids_array) || in_array(0, $ids_array)) {
  960. return FALSE;
  961. } else {
  962. return $ids_array;
  963. }
  964. }
  965. function word_filter_access_token(){
  966. $appid=config('ds_config.word_filter_appid');
  967. $secret=config('ds_config.word_filter_secret');
  968. $access_token=config('ds_config.word_filter_access_token');
  969. $access_token_expire=config('ds_config.word_filter_access_token_expire');
  970. if(!$access_token || $access_token_expire<TIMESTAMP){
  971. $res=http_request('https://aip.baidubce.com/oauth/2.0/token','POST',array(
  972. 'grant_type'=>'client_credentials',
  973. 'client_id'=>$appid,
  974. 'client_secret'=>$secret,
  975. ));
  976. $res = json_decode($res, true);
  977. if(isset($res['error'])){
  978. return ds_callback(false, $res['error_description']);
  979. }
  980. $access_token=$res['access_token'];
  981. $expires_in=$res['expires_in'];
  982. $config_model = model('config');
  983. $update_array=array(
  984. 'word_filter_access_token'=>$access_token,
  985. 'word_filter_access_token_expire'=>TIMESTAMP+$expires_in
  986. );
  987. $config_model->editConfig($update_array);
  988. }
  989. return ds_callback(true,'',$access_token);
  990. }
  991. /**
  992. * 敏感词过滤
  993. * @param type $text
  994. * @return boolean
  995. */
  996. function word_filter($text) {
  997. $data=array();
  998. $data['text']=$text;
  999. $data['if_sensitive']=false;
  1000. if(config('ds_config.word_filter_open')!=1){
  1001. return ds_callback(true, '', $data);
  1002. }
  1003. $res=word_filter_access_token();
  1004. if(!$res['code']){
  1005. return $res;
  1006. }
  1007. $access_token=$res['data'];
  1008. $res=http_request('https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token='.$access_token,'POST',array(
  1009. 'text'=> $text
  1010. ));
  1011. $res = json_decode($res, true);
  1012. if(isset($res['error_code'])){
  1013. return ds_callback(false, $res['error_msg']);
  1014. }
  1015. if($res['conclusionType']==2){
  1016. $data['if_sensitive']=true;
  1017. $data['sensitive_msg']=array();
  1018. $data['sensitive_word']=array();
  1019. foreach($res['data'] as $val){
  1020. $data['sensitive_msg'][]=$val['msg'];
  1021. foreach($val['hits'] as $v){
  1022. $data['sensitive_word']=array_merge($data['sensitive_word'],$v['words']);
  1023. $data['text']=str_replace($v['words'],'**',$data['text']);
  1024. }
  1025. }
  1026. }
  1027. return ds_callback(true, '', $data);
  1028. }
  1029. /**
  1030. * 敏感图过滤
  1031. * @param type $text
  1032. * @return boolean
  1033. */
  1034. function image_filter($img_url) {
  1035. $data=array();
  1036. $data['if_sensitive']=false;
  1037. if(config('ds_config.word_filter_open')!=1){
  1038. return ds_callback(true, '', $data);
  1039. }
  1040. $res=word_filter_access_token();
  1041. if(!$res['code']){
  1042. return $res;
  1043. }
  1044. $access_token=$res['data'];
  1045. $image=imgToBase64($img_url);
  1046. if(empty($image)){
  1047. return ds_callback(false, 'image empty');
  1048. }
  1049. $res=http_request('https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token='.$access_token,'POST',array(
  1050. 'image'=> $image['content']
  1051. ),array(
  1052. 'Content-Type: application/x-www-form-urlencoded'
  1053. ));
  1054. $res = json_decode($res, true);
  1055. if(isset($res['error_code'])){
  1056. return ds_callback(false, $res['error_msg']);
  1057. }
  1058. if($res['conclusionType']==2){
  1059. $data['if_sensitive']=true;
  1060. $data['sensitive_msg']=array();
  1061. foreach($res['data'] as $val){
  1062. $data['sensitive_msg'][]=$val['msg'];
  1063. }
  1064. }
  1065. return ds_callback(true, '', $data);
  1066. }