common.php 38 KB

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