common.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. use app\models\lala\LalaRatio;
  13. if (!function_exists('exception')) {
  14. /**
  15. * 抛出异常处理
  16. *
  17. * @param string $msg 异常消息
  18. * @param integer $code 异常代码 默认为0
  19. * @param string $exception 异常类
  20. *
  21. * @throws Exception
  22. */
  23. function exception($msg, $code = 0, $exception = '')
  24. {
  25. $e = $exception ?: '\think\Exception';
  26. throw new $e($msg, $code);
  27. }
  28. }
  29. if (!function_exists('sys_config')) {
  30. /**
  31. * 获取系统单个配置
  32. * @param string $name
  33. * @param string $default
  34. * @return string
  35. */
  36. function sys_config(string $name, $default = '', $catch = false)
  37. {
  38. if (empty($name))
  39. return $default;
  40. $config = trim(app('sysConfig')->get($name, $default, $catch));
  41. if ($config === '' || $config === false) {
  42. return $default;
  43. } else {
  44. return $config;
  45. }
  46. }
  47. }
  48. if (!function_exists('sys_data')) {
  49. /**
  50. * 获取系统单个配置
  51. * @param string $name
  52. * @return array
  53. */
  54. function sys_data(string $name, int $limit = 0)
  55. {
  56. return app('sysGroupData')->getData($name, $limit, true);
  57. }
  58. }
  59. if (!function_exists('filter_emoji')) {
  60. // 过滤掉emoji表情
  61. function filter_emoji($str)
  62. {
  63. $str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
  64. '/./u',
  65. function (array $match) {
  66. return strlen($match[0]) >= 4 ? '' : $match[0];
  67. },
  68. $str);
  69. return $str;
  70. }
  71. }
  72. if (!function_exists('str_middle_replace')) {
  73. /** TODO 系统未使用
  74. * @param string $string 需要替换的字符串
  75. * @param int $start 开始的保留几位
  76. * @param int $end 最后保留几位
  77. * @return string
  78. */
  79. function str_middle_replace($string, $start, $end)
  80. {
  81. $strlen = mb_strlen($string, 'UTF-8');//获取字符串长度
  82. $firstStr = mb_substr($string, 0, $start, 'UTF-8');//获取第一位
  83. $lastStr = mb_substr($string, -1, $end, 'UTF-8');//获取最后一位
  84. return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($string, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  85. }
  86. }
  87. if (!function_exists('sensitive_words_filter')) {
  88. /**
  89. * 敏感词过滤
  90. *
  91. * @param string
  92. * @return string
  93. */
  94. function sensitive_words_filter($str)
  95. {
  96. if (!$str) return '';
  97. $file = app()->getAppPath() . 'public/static/plug/censorwords/CensorWords';
  98. $words = file($file);
  99. foreach ($words as $word) {
  100. $word = str_replace(array("\r\n", "\r", "\n", "/", "<", ">", "=", " "), '', $word);
  101. if (!$word) continue;
  102. $ret = preg_match("/$word/", $str, $match);
  103. if ($ret) {
  104. return $match[0];
  105. }
  106. }
  107. return '';
  108. }
  109. }
  110. if (!function_exists('make_path')) {
  111. /**
  112. * 上传路径转化,默认路径
  113. * @param $path
  114. * @param int $type
  115. * @param bool $force
  116. * @return string
  117. */
  118. function make_path($path, int $type = 2, bool $force = false)
  119. {
  120. $path = DS . ltrim(rtrim($path));
  121. switch ($type) {
  122. case 1:
  123. $path .= DS . date('Y');
  124. break;
  125. case 2:
  126. $path .= DS . date('Y') . DS . date('m');
  127. break;
  128. case 3:
  129. $path .= DS . date('Y') . DS . date('m') . DS . date('d');
  130. break;
  131. }
  132. try {
  133. if (is_dir(app()->getRootPath() . 'public' . DS . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DS . 'uploads' . $path, 0777, true) == true) {
  134. return trim(str_replace(DS, '/', $path), '.');
  135. } else return '';
  136. } catch (\Exception $e) {
  137. if ($force)
  138. throw new \Exception($e->getMessage());
  139. return '无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS;
  140. }
  141. }
  142. }
  143. if (!function_exists('curl_file_exist')) {
  144. /**
  145. * CURL 检测远程文件是否在
  146. * @param $url
  147. * @return bool
  148. */
  149. function curl_file_exist($url)
  150. {
  151. $ch = curl_init();
  152. try {
  153. curl_setopt($ch, CURLOPT_URL, $url);
  154. curl_setopt($ch, CURLOPT_HEADER, 1);
  155. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  156. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  157. $contents = curl_exec($ch);
  158. if (preg_match("/404/", $contents)) return false;
  159. if (preg_match("/403/", $contents)) return false;
  160. return true;
  161. } catch (\Exception $e) {
  162. return false;
  163. }
  164. }
  165. }
  166. if (!function_exists('set_file_url')) {
  167. /**
  168. * 设置附加路径
  169. * @param $url
  170. * @return bool
  171. */
  172. function set_file_url($image, $siteUrl = '')
  173. {
  174. if (!strlen(trim($siteUrl))) $siteUrl = sys_config('site_url');
  175. $domainTop = substr($image, 0, 4);
  176. if ($domainTop == 'http') return $image;
  177. $image = str_replace('\\', '/', $image);
  178. return $siteUrl . $image;
  179. }
  180. }
  181. if (!function_exists('set_http_type')) {
  182. /**
  183. * 修改 https 和 http
  184. * @param $url $url 域名
  185. * @param int $type 0 返回https 1 返回 http
  186. * @return string
  187. */
  188. function set_http_type($url, $type = 0)
  189. {
  190. $domainTop = substr($url, 0, 5);
  191. if ($type) {
  192. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  193. } else {
  194. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  195. }
  196. return $url;
  197. }
  198. }
  199. if (!function_exists('check_card')) {
  200. /**
  201. * 身份证验证
  202. * @param $card
  203. * @return bool
  204. */
  205. function check_card($card)
  206. {
  207. $city = [11 => "北京", 12 => "天津", 13 => "河北", 14 => "山西", 15 => "内蒙古", 21 => "辽宁", 22 => "吉林", 23 => "黑龙江 ", 31 => "上海", 32 => "江苏", 33 => "浙江", 34 => "安徽", 35 => "福建", 36 => "江西", 37 => "山东", 41 => "河南", 42 => "湖北 ", 43 => "湖南", 44 => "广东", 45 => "广西", 46 => "海南", 50 => "重庆", 51 => "四川", 52 => "贵州", 53 => "云南", 54 => "西藏 ", 61 => "陕西", 62 => "甘肃", 63 => "青海", 64 => "宁夏", 65 => "新疆", 71 => "台湾", 81 => "香港", 82 => "澳门", 91 => "国外 "];
  208. $tip = "";
  209. $match = "/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/";
  210. $pass = true;
  211. if (!$card || !preg_match($match, $card)) {
  212. //身份证格式错误
  213. $pass = false;
  214. } else if (!$city[substr($card, 0, 2)]) {
  215. //地址错误
  216. $pass = false;
  217. } else {
  218. //18位身份证需要验证最后一位校验位
  219. if (strlen($card) == 18) {
  220. $card = str_split($card);
  221. //∑(ai×Wi)(mod 11)
  222. //加权因子
  223. $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  224. //校验位
  225. $parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
  226. $sum = 0;
  227. $ai = 0;
  228. $wi = 0;
  229. for ($i = 0; $i < 17; $i++) {
  230. $ai = $card[$i];
  231. $wi = $factor[$i];
  232. $sum += $ai * $wi;
  233. }
  234. $last = $parity[$sum % 11];
  235. if ($parity[$sum % 11] != $card[17]) {
  236. // $tip = "校验位错误";
  237. $pass = false;
  238. }
  239. } else {
  240. $pass = false;
  241. }
  242. }
  243. if (!$pass) return false;/* 身份证格式错误*/
  244. return true;/* 身份证格式正确*/
  245. }
  246. }
  247. if (!function_exists('anonymity')) {
  248. /**
  249. * 匿名处理处理用户昵称
  250. * @param $name
  251. * @return string
  252. */
  253. function anonymity($name)
  254. {
  255. $strLen = mb_strlen($name, 'UTF-8');
  256. $min = 3;
  257. if ($strLen <= 1)
  258. return '*';
  259. if ($strLen <= $min)
  260. return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $min - 1);
  261. else
  262. return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $strLen - 1) . mb_substr($name, -1, 1, 'UTF-8');
  263. }
  264. }
  265. if (!function_exists('sort_list_tier')) {
  266. /**
  267. * 分级排序
  268. * @param $data
  269. * @param int $pid
  270. * @param string $field
  271. * @param string $pk
  272. * @param string $html
  273. * @param int $level
  274. * @param bool $clear
  275. * @return array
  276. */
  277. function sort_list_tier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
  278. {
  279. static $list = [];
  280. if ($clear) $list = [];
  281. foreach ($data as $k => $res) {
  282. if ($res[$field] == $pid) {
  283. $res['html'] = str_repeat($html, $level);
  284. $list[] = $res;
  285. unset($data[$k]);
  286. sort_list_tier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
  287. }
  288. }
  289. return $list;
  290. }
  291. }
  292. if (!function_exists('time_tran')) {
  293. /**
  294. * 时间戳人性化转化
  295. * @param $time
  296. * @return string
  297. */
  298. function time_tran($time)
  299. {
  300. $t = time() - $time;
  301. $f = array(
  302. '31536000' => '年',
  303. '2592000' => '个月',
  304. '604800' => '星期',
  305. '86400' => '天',
  306. '3600' => '小时',
  307. '60' => '分钟',
  308. '1' => '秒'
  309. );
  310. foreach ($f as $k => $v) {
  311. if (0 != $c = floor($t / (int)$k)) {
  312. return $c . $v . '前';
  313. }
  314. }
  315. }
  316. }
  317. if (!function_exists('url_to_path')) {
  318. /**
  319. * url转换路径
  320. * @param $url
  321. * @return string
  322. */
  323. function url_to_path($url)
  324. {
  325. $path = trim(str_replace('/', DS, $url), DS);
  326. if (0 !== strripos($path, 'public'))
  327. $path = 'public' . DS . $path;
  328. return app()->getRootPath() . $path;
  329. }
  330. }
  331. if (!function_exists('path_to_url')) {
  332. /**
  333. * 路径转url路径
  334. * @param $path
  335. * @return string
  336. */
  337. function path_to_url($path)
  338. {
  339. return trim(str_replace(DS, '/', $path), '.');
  340. }
  341. }
  342. if (!function_exists('image_to_base64')) {
  343. /**
  344. * 获取图片转为base64
  345. * @param string $avatar
  346. * @return bool|string
  347. */
  348. function image_to_base64($avatar = '', $timeout = 9)
  349. {
  350. try {
  351. $url = parse_url($avatar);
  352. $url = $url['host'];
  353. $header = [
  354. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
  355. 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
  356. 'Accept-Encoding: gzip, deflate, br',
  357. 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
  358. 'Host:' . $url
  359. ];
  360. $dir = pathinfo($url);
  361. $host = $dir['dirname'];
  362. $refer = $host . '/';
  363. $curl = curl_init();
  364. curl_setopt($curl, CURLOPT_REFERER, $refer);
  365. curl_setopt($curl, CURLOPT_URL, $avatar);
  366. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  367. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  368. curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
  369. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
  370. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  371. $data = curl_exec($curl);
  372. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  373. curl_close($curl);
  374. if ($code == 200) {
  375. return "data:image/jpeg;base64," . base64_encode($data);
  376. } else {
  377. return false;
  378. }
  379. } catch (\Exception $e) {
  380. return false;
  381. }
  382. }
  383. }
  384. if (!function_exists('put_image')) {
  385. /**
  386. * 获取图片转为base64
  387. * @param string $avatar
  388. * @return bool|string
  389. */
  390. function put_image($url, $filename = '')
  391. {
  392. if ($url == '') {
  393. return false;
  394. }
  395. try {
  396. if ($filename == '') {
  397. $ext = pathinfo($url);
  398. if ($ext['extension'] != "jpg" && $ext['extension'] != "png" && $ext['extension'] != "jpeg") {
  399. return false;
  400. }
  401. $filename = time() . "." . $ext['extension'];
  402. }
  403. //文件保存路径
  404. ob_start();
  405. readfile($url);
  406. $img = ob_get_contents();
  407. ob_end_clean();
  408. $path = 'uploads/qrcode';
  409. $fp2 = fopen($path . '/' . $filename, 'a');
  410. fwrite($fp2, $img);
  411. fclose($fp2);
  412. return $path . '/' . $filename;
  413. } catch (\Exception $e) {
  414. return false;
  415. }
  416. }
  417. }
  418. if (!function_exists('debug_file')) {
  419. /**
  420. * 文件调试
  421. * @param $content
  422. */
  423. function debug_file($content, string $fileName = 'error', string $ext = 'txt')
  424. {
  425. $msg = '[' . date('Y-m-d H:i:s', time()) . '] [ DEBUG ] ';
  426. $pach = app()->getRuntimePath();
  427. file_put_contents($pach . $fileName . '.' . $ext, $msg . print_r($content, true) . "\r\n", FILE_APPEND);
  428. }
  429. }
  430. if (!function_exists('sql_filter')) {
  431. /**
  432. * sql 参数过滤
  433. * @param string $str
  434. * @return mixed
  435. */
  436. function sql_filter(string $str)
  437. {
  438. $filter = ['select ', 'insert ', 'update ', 'delete ', 'drop', 'truncate ', 'declare', 'xp_cmdshell', '/add', ' or ', 'exec', 'create', 'chr', 'mid', ' and ', 'execute'];
  439. $toupper = array_map(function ($str) {
  440. return strtoupper($str);
  441. }, $filter);
  442. return str_replace(array_merge($filter, $toupper, ['%20']), '', $str);
  443. }
  444. }
  445. if (!function_exists('is_brokerage_statu')) {
  446. /**
  447. * 是否能成为推广人
  448. * @param float $price
  449. * @return bool
  450. */
  451. function is_brokerage_statu(float $price)
  452. {
  453. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  454. if ($storeBrokerageStatus == 1) {
  455. return false;
  456. } else {
  457. $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
  458. return $price >= $storeBrokeragePrice;
  459. }
  460. }
  461. }
  462. if (!function_exists('array_unique_fb')) {
  463. /**
  464. * 二维数组去掉重复值
  465. * @param $array
  466. * @return array
  467. */
  468. function array_unique_fb($array)
  469. {
  470. $out = array();
  471. foreach ($array as $key => $value) {
  472. if (!in_array($value, $out)) {
  473. $out[$key] = $value;
  474. }
  475. }
  476. $out = array_values($out);
  477. return $out;
  478. }
  479. }
  480. if (!function_exists('not_empty_check')) {
  481. /**
  482. * 非空验证
  483. * @param $param
  484. * @return bool
  485. */
  486. function not_empty_check($param)
  487. {
  488. if (is_array($param)) {
  489. return !(count($param) <= 0);
  490. } else {
  491. if ($param == '') {
  492. return false;
  493. }
  494. if ($param == null) {
  495. return false;
  496. }
  497. return true;
  498. }
  499. }
  500. }
  501. if (!function_exists('mobile_check')) {
  502. /**
  503. * 电话验证
  504. * @param $param
  505. * @return bool
  506. */
  507. function mobile_check($param)
  508. {
  509. if (!preg_match("/^1[3456789]\d{9}$/", $param)) {
  510. return false;
  511. } else {
  512. return true;
  513. }
  514. }
  515. }
  516. if (!function_exists('do_request')) {
  517. /**
  518. * CURL 请求接口
  519. * @param string $url 请求地址
  520. * @param array $data 请求参数
  521. * @param array $header 请求头
  522. * @param bool $post true:post请求 false:get请求
  523. * @param bool $json post请求时 请求数据打包方式是否为json
  524. * @param int $format 数据打包为json格式时打包的格式值 来自json_encode
  525. * @param bool $form post请求时 请求数据是否为表单 同时为false时为http提交 优先级高于json
  526. * @return bool|false|string
  527. */
  528. function do_request($url, $data, $header = null, $post = true, $json = false, $format = 0, $form = false)
  529. {
  530. $curl = curl_init();
  531. if (!$post && $data) {
  532. $url = $url . '?' . http_build_query($data);
  533. }
  534. curl_setopt($curl, CURLOPT_URL, $url);
  535. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  536. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  537. if ($post) {
  538. curl_setopt($curl, CURLOPT_POST, 1);
  539. if (!$json && !$form) {
  540. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  541. } else if ($json && !$form) {
  542. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, $format));
  543. } else {
  544. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  545. }
  546. }
  547. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  548. if ($header) {
  549. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  550. curl_setopt($curl, CURLOPT_HEADER, 0);
  551. }
  552. $result = curl_exec($curl);
  553. if (curl_errno($curl)) {
  554. return json_encode(['status' => curl_errno($curl), 'msg' => '请求失败']);
  555. }
  556. curl_close($curl);
  557. return $result;
  558. }
  559. }
  560. if (!function_exists('time_format')) {
  561. /**
  562. * 电话验证
  563. * @param $param
  564. * @return bool
  565. */
  566. function time_format($param)
  567. {
  568. return $param > 0 ? date('Y-m-d H:i:s', $param) : '--';
  569. }
  570. }
  571. if (!function_exists('init_money_type')) {
  572. /**
  573. * @return array
  574. */
  575. function init_money_type(): array
  576. {
  577. $money_type = sys_data('money_type');
  578. // var_dump($money_type);
  579. $new = [];
  580. foreach ($money_type as $v) {
  581. $new[$v['code']] = $v['name'];
  582. }
  583. return $new;
  584. }
  585. }
  586. if (!function_exists('get_money_name')) {
  587. /**
  588. * @return string
  589. */
  590. function get_money_name($code): string
  591. {
  592. if ($code == 'now_money') return '货款';
  593. if ($code == 'brokerage_price' || $code == 'brokerage') return '佣金';
  594. if ($code == 'integral') return '积分';
  595. $money_type = sys_data('money_type');
  596. foreach ($money_type as $v) {
  597. if ($v['code'] == $code)
  598. return $v['name'];
  599. }
  600. return '未知';
  601. }
  602. }
  603. if (!function_exists('us_price')) {
  604. /**
  605. * @return float
  606. */
  607. function us_price(): float
  608. {
  609. return 6.5;
  610. $rate = \crmeb\services\CacheService::get('rate', 0);
  611. if (!$rate) {
  612. $url = 'https://www.mycurrency.net/US.json';
  613. $res = do_request($url, [], null, false);
  614. $res = json_decode($res, true);
  615. foreach ($res['rates'] ?: [] as $v) {
  616. if ($v['currency_code'] == 'CNY') {
  617. \crmeb\services\CacheService::set('rate', $v['rate'], 3600 * 6);
  618. return $v['rate'];
  619. }
  620. }
  621. return 0;
  622. } else {
  623. return $rate;
  624. }
  625. }
  626. }
  627. if (!function_exists('get_huobi_price')) {
  628. /**
  629. * @return float
  630. */
  631. function get_huobi_price($type)
  632. {
  633. return 0;
  634. if (\think\facade\Env::get('app_debug')) return 0;
  635. $type = strtolower($type);
  636. $rate = \crmeb\services\CacheService::get('usdt_' . $type, 0);
  637. // var_dump($rate);
  638. if (!$rate) {
  639. $url = "https://api.huobi.de.com/market/detail/merged?symbol={$type}usdt";
  640. $res = do_request($url, [], null, false, true);
  641. $res = json_decode($res, true);
  642. if ($res['status'] == 'error') {
  643. return 0;
  644. } else {
  645. \crmeb\services\CacheService::set('usdt_' . $type, $res['tick']['close'], 600);
  646. return $res['tick']['close'];
  647. }
  648. } else {
  649. return $rate;
  650. }
  651. }
  652. }
  653. if (!function_exists('arraySort')) {
  654. /**
  655. * @param $array
  656. * @param $keys
  657. * @param int $sort
  658. * @return array
  659. */
  660. function arraySort($array, $keys, int $sort = SORT_DESC): array
  661. {
  662. $keysValue = [];
  663. foreach ($array as $k => $v) {
  664. $keysValue[$k] = $v[$keys];
  665. }
  666. array_multisort($keysValue, $sort, $array);
  667. return $array;
  668. }
  669. }
  670. if (!function_exists('open_times')) {
  671. /**
  672. * @return array
  673. */
  674. function open_times(): array
  675. {
  676. $times = sys_config('open_times');
  677. $times = explode(',', $times);
  678. $open_times = [];
  679. foreach ($times as $v) {
  680. $open_time = date('Y-m-d') . ' ' . $v . ':00';
  681. $open_time_value = strtotime(date('Y-m-d') . ' ' . $v . ':00');
  682. $open_times[] = ['name' => $open_time, 'value' => $open_time_value];
  683. }
  684. if (count($open_times)) {
  685. $open_time_value = strtotime('+1day', $open_times[0]['value']);
  686. $open_time = time_format($open_time_value);
  687. $open_times[] = ['name' => $open_time, 'value' => $open_time_value];
  688. $open_time_value = strtotime('+1day', $open_times[1]['value']);
  689. $open_time = time_format($open_time_value);
  690. $open_times[] = ['name' => $open_time, 'value' => $open_time_value];
  691. }
  692. return arraySort($open_times, 'value', SORT_ASC);
  693. }
  694. }
  695. if (!function_exists('get_hpool_price')) {
  696. /**
  697. * @param $unit
  698. * @return float
  699. */
  700. function get_hpool_price($unit): float
  701. {
  702. $res = do_request('https://www.ihpool.com/api/home/list?type=eco', [], null, false);
  703. $res = json_decode($res, true);
  704. $price = 0;
  705. if (!isset($res['code']) || $res['code'] != 200) $price = 0;
  706. foreach ($res['data'] as $v) {
  707. if ($v['unit'] == $unit) {
  708. $price = bcdiv($v['income'], 1024, 8);
  709. break;
  710. }
  711. }
  712. return $price;
  713. }
  714. }
  715. if (!function_exists('get_fil_price')) {
  716. /**
  717. * @param $unit
  718. * @return float
  719. */
  720. function get_fil_price(): float
  721. {
  722. $res = do_request('https://filfox.info/api/v1/overview', [], null, false);
  723. $res = json_decode($res, true);
  724. $price = 0;
  725. if (!isset($res['code']) || $res['code'] != 200) $price = 0;
  726. // foreach ($res['data'] as $v) {
  727. // if ($v['unit'] == $unit) {
  728. // $price = bcdiv($v['income'], 1024, 8);
  729. // break;
  730. // }
  731. // }
  732. $coins = $res['dailyCoinsMined'];
  733. $power = $res['totalQualityAdjPower'];
  734. $coins = bcdiv($coins, '1000000000000000000', 8);
  735. $power = bcdiv($power, bcpow(1024, 5), 8);
  736. $price = bcdiv($coins, $power, 8);
  737. $price = bcdiv($price, 1024, 8);
  738. return $price;
  739. }
  740. }
  741. if (!function_exists('get_lala_ratio')) {
  742. /**
  743. * @return string
  744. */
  745. function get_lala_ratio($date = null): string
  746. {
  747. $money_types = sys_data('money_type');
  748. $origin_ratio = 0;
  749. foreach ($money_types as $v) {
  750. if ($v['code'] == 'LALA') {
  751. $origin_ratio = $v['usdt_price'];
  752. $lala_ratio = LalaRatio::initRatio($date ?: date('Y-m-d'));
  753. $origin_ratio = bcmul($origin_ratio, bcdiv(bcadd(100, $lala_ratio), 100, 2), 8);
  754. // $origin_ratio = bcdiv(1, $origin_ratio, 8);
  755. }
  756. }
  757. return $origin_ratio;
  758. }
  759. }