common.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. <?php
  2. // 应用公共文件
  3. use SingKa\Sms\SkSms;
  4. use GatewayClient\Gateway;
  5. use \utils\Str;
  6. use think\facade\Queue;
  7. /**
  8. * 框架内部默认ajax返回
  9. * @param string $msg 提示信息
  10. * @param string $redirect 重定向类型 current|parent|''
  11. * @param string $alert 父层弹框信息
  12. * @param bool $close 是否关闭当前层
  13. * @param string $url 重定向地址
  14. * @param string $data 附加数据
  15. * @param int $code 错误码
  16. * @param array $extend 扩展数据
  17. * @param int $count 总数
  18. */
  19. function success($msg, $data = '', $count = 0, $page = 1, $code = 0)
  20. {
  21. return ret($code, $msg, $data, $count, $page);
  22. }
  23. /**
  24. * 返回警告json信息
  25. */
  26. function warning($msg, $data = '', $count = 0, $page = 1 , $code = 400)
  27. {
  28. return success($msg ? : lang('system.fail'), $data, $count, $page, $code);
  29. }
  30. /**
  31. * 返回错误json信息
  32. */
  33. function error($msg, $code = 502)
  34. {
  35. return ret($code, lang('system.error').':'.$msg ? : lang('system.fail'));
  36. }
  37. /**
  38. * 提前终止信息
  39. */
  40. function shutdown($msg, $code = 401)
  41. {
  42. exit(json_encode(['code' => $code, 'msg' => $msg?:lang('system.forbidden'), 'data' => []]));
  43. }
  44. /**
  45. * ajax数据返回,规范格式
  46. * @param array $data 返回的数据,默认空数组
  47. * @param string $msg 信息
  48. * @param int $code 错误码,0-未出现错误|其他出现错误
  49. * @param array $extend 扩展数据
  50. */
  51. function ret($code, $msg = "",$data = [],$count=0, $page=0)
  52. {
  53. $ret = ["code" =>$code, "msg" => $msg,'count'=>$count, "data" => $data,'page'=>$page];
  54. return json($ret);
  55. }
  56. /**
  57. * 将秒数转换为 HH:MM:SS 格式的时间字符串
  58. * @param int $seconds 要转换的秒数(支持正数/0,负数会转为0)
  59. * @param bool $padZero 是否补零(默认true,确保小时/分/秒都是两位)
  60. * @return string 格式化后的时间,如 01:23:20、00:00:00
  61. */
  62. function formatSecondsToTime(int $seconds, bool $padZero = true): string
  63. {
  64. // 处理负数:转为0(避免出现负时间)
  65. $seconds = max(0, $seconds);
  66. // 计算小时、分钟、秒
  67. $hours = floor($seconds / 3600); // 总小时数(1小时=3600秒)
  68. $remainingSeconds = $seconds % 3600; // 计算小时后剩余的秒数
  69. $minutes = floor($remainingSeconds / 60); // 剩余秒数转分钟
  70. $secs = $remainingSeconds % 60; // 最终剩余的秒数
  71. // 补零处理:确保每位都是两位(如 1小时→01,5分钟→05)
  72. if ($padZero) {
  73. $hours = str_pad($hours, 2, '0', STR_PAD_LEFT);
  74. $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT);
  75. $secs = str_pad($secs, 2, '0', STR_PAD_LEFT);
  76. }
  77. // 拼接成 HH:MM:SS 格式
  78. return "{$hours}:{$minutes}:{$secs}";
  79. }
  80. // 执行绑定
  81. function doBindUid($user_id,$client_id,$cid='',$isMobile = false){
  82. // 如果当前ID在线,将其他地方登陆挤兑下线
  83. if(Gateway::isUidOnline($user_id)){
  84. wsSendMsg($user_id,'offline',['id'=>$user_id,'client_id'=>$client_id,'isMobile'=>$isMobile]);
  85. }
  86. Gateway::bindUid($client_id, $user_id);
  87. // 查询团队,如果有团队则加入团队
  88. $group=Group::getMyGroup(['gu.user_id'=>$user_id,'gu.status'=>1]);
  89. if($group){
  90. $group=$group->toArray();
  91. $group_ids=arrayToString($group,'group_id',false);
  92. foreach($group_ids as $v){
  93. Gateway::joinGroup($client_id, $v);
  94. }
  95. }
  96. if($cid){
  97. bindCid($user_id,$cid);
  98. }
  99. wsSendMsg(0,'isOnline',['id'=>$user_id,'is_online'=>1]);
  100. }
  101. function ensureUrl($url)
  102. {
  103. if (!filter_var($url, FILTER_VALIDATE_URL)) {
  104. if (empty($url)) return $url;
  105. $newUrl = config('app.url') . $url;
  106. return filter_var($newUrl, FILTER_VALIDATE_URL) ? $newUrl : $url;
  107. }
  108. return $url;
  109. }
  110. function replacePartInUrl($url)
  111. {
  112. if (filter_var($url, FILTER_VALIDATE_URL)) {
  113. $target = config('app.url');
  114. return str_replace($target, '', $url);
  115. }
  116. return $url;
  117. }
  118. function getSelectData($data, $key_name = 'value', $value_name = 'label')
  119. {
  120. $selectData = [];
  121. foreach ($data as $key => $value) {
  122. $selectData[] = [
  123. $value_name => $value,
  124. $key_name => $key,
  125. ];
  126. }
  127. return $selectData;
  128. }
  129. if (!function_exists('linear_to_tree')) {
  130. function linear_to_tree($data, $sub_key_name = 'children', $id_name = 'id', $parent_id_name = 'parent_id', $parent_id = 0)
  131. {
  132. $tree = [];
  133. foreach ($data as $row) {
  134. if ($row[$parent_id_name] == $parent_id) {
  135. $temp = $row;
  136. $child = linear_to_tree($data, $sub_key_name, $id_name, $parent_id_name, $row[$id_name]);
  137. if ($child) {
  138. $temp[$sub_key_name] = $child;
  139. }
  140. $tree[] = $temp;
  141. }
  142. }
  143. return $tree;
  144. }
  145. }
  146. if (!function_exists('is_valid_date')) {
  147. function is_valid_date($date)
  148. {
  149. // 使用正则表达式匹配 yyyy-mm-dd 格式的日期
  150. return preg_match('/^\d{4}-\d{2}-\d{2}$/', $date) === 1;
  151. }
  152. }
  153. if (!function_exists('getUuid')) {
  154. function getUuid()
  155. {
  156. // Generate 16 bytes (128 bits) of random data or use the data passed into the function.
  157. $data = random_bytes(16);
  158. // Set version to 0100
  159. $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
  160. // Set bits 6-7 to 10
  161. $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
  162. // Output the 36 character UUID.
  163. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  164. }
  165. }
  166. if (!function_exists('generate_random_string')) {
  167. function generate_random_string(int $length = 6): string
  168. {
  169. $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
  170. $randomString = '';
  171. for ($i = 0; $i < $length; $i++) {
  172. $randomString .= $characters[rand(0, strlen($characters) - 1)];
  173. }
  174. return $randomString;
  175. }
  176. }
  177. /**
  178. * 辅助函数:十进制转十六进制(支持大数)
  179. */
  180. function bcdechex($dec)
  181. {
  182. $hex = '';
  183. while (bccomp($dec, 0) > 0) {
  184. $last = bcmod($dec, 16);
  185. $hex = dechex($last) . $hex;
  186. $dec = bcdiv($dec, 16, 0);
  187. }
  188. return $hex ?: '0';
  189. }
  190. function removeZero($str)
  191. {
  192. if (empty($str)) {
  193. return 0;
  194. }
  195. if (!is_numeric($str)) {
  196. return $str;
  197. }
  198. $number = number_format($str, 10, '.', '');
  199. // 使用 rtrim 移除末尾的零和小数点
  200. return rtrim(rtrim($number, '0'), '.');
  201. }
  202. /**
  203. * 唯一订单号
  204. * @return string
  205. */
  206. function createOrderNo(): string
  207. {
  208. $str = microtime(true);
  209. $arr = explode('.', $str);
  210. $decimal = $arr[1];
  211. return date('YmdHis') . $decimal;
  212. }
  213. /**
  214. * 获取精度
  215. * @param $number
  216. * @return int
  217. */
  218. function getScale($number): int
  219. {
  220. if (!is_numeric($number)) {
  221. return 1;
  222. }
  223. $sub = strrchr($number, ".");
  224. if (empty($sub)) {
  225. return 1;
  226. }
  227. $scale = strlen(substr($sub, 1));
  228. if ($scale == 0) {
  229. return 1;
  230. }
  231. return $scale;
  232. }
  233. /**
  234. * 将用户名中间部分字符替换为*号
  235. * @param string $username 原始用户名
  236. * @param int $keepLength 前后保留的字符数量(默认1)
  237. * @return string 处理后的用户名
  238. */
  239. function maskUsername($username, $keepLength = 1) {
  240. // 获取用户名长度
  241. $length = mb_strlen($username, 'UTF-8');
  242. // 短于等于2个字符的不处理
  243. if ($length <= $keepLength) {
  244. return $username;
  245. }
  246. if ($length > 8) {
  247. $keepLength = 3;
  248. }
  249. // 计算需要替换的字符数量
  250. $replaceLength = $length - 2 * $keepLength;
  251. // 获取前后保留的字符
  252. $prefix = mb_substr($username, 0, $keepLength, 'UTF-8');
  253. $suffix = mb_substr($username, -$keepLength, $keepLength, 'UTF-8');
  254. // 生成替换的*号
  255. $stars = str_repeat('*', 3);
  256. // 组合结果
  257. return $prefix . $stars . $suffix;
  258. }
  259. /* @param string $string 原文或者密文
  260. * @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE
  261. * @param string $key 密钥
  262. * @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效
  263. * @return string 处理后的 原文或者 经过 base64_encode 处理后的密文
  264. *
  265. * @example
  266. *
  267. * $a = authcode('abc', 'ENCODE', 'key');
  268. * $b = authcode($a, 'DECODE', 'key'); // $b(abc)
  269. *
  270. * $a = authcode('abc', 'ENCODE', 'key', 3600);
  271. * $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为空
  272. */
  273. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 3600) {
  274. $ckey_length = 4;
  275. // 随机密钥长度 取值 0-32;
  276. // 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
  277. // 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
  278. // 当此值为 0 时,则不产生随机密钥
  279. $key = md5($key ? $key : 'default_key'); //这里可以填写默认key值
  280. $keya = md5(substr($key, 0, 16));
  281. $keyb = md5(substr($key, 16, 16));
  282. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  283. $cryptkey = $keya.md5($keya.$keyc);
  284. $key_length = strlen($cryptkey);
  285. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  286. $string_length = strlen($string);
  287. $result = '';
  288. $box = range(0, 255);
  289. $rndkey = array();
  290. for($i = 0; $i <= 255; $i++) {
  291. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  292. }
  293. for($j = $i = 0; $i < 256; $i++) {
  294. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  295. $tmp = $box[$i];
  296. $box[$i] = $box[$j];
  297. $box[$j] = $tmp;
  298. }
  299. for($a = $j = $i = 0; $i < $string_length; $i++) {
  300. $a = ($a + 1) % 256;
  301. $j = ($j + $box[$a]) % 256;
  302. $tmp = $box[$a];
  303. $box[$a] = $box[$j];
  304. $box[$j] = $tmp;
  305. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  306. }
  307. if($operation == 'DECODE') {
  308. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  309. return substr($result, 26);
  310. } else {
  311. return '';
  312. }
  313. } else {
  314. return $keyc.str_replace('=', '', base64_encode($result));
  315. }
  316. }
  317. function ssoTokenEncode($str,$key='lvzhesso',$expire=0){
  318. $ids=encryptIds($str);
  319. return authcode($ids,"ENCODE",$key,$expire);
  320. }
  321. function ssoTokenDecode($str,$key='lvzhesso'){
  322. $ids=authcode($str,"DECODE",$key);
  323. try{
  324. return decryptIds($ids);
  325. }catch(\Exception $e){
  326. return '';
  327. }
  328. }
  329. //id加密
  330. function encryptIds($str)
  331. {
  332. $hash = config('hashids');
  333. return \Hashids\Hashids::instance($hash['length'], $hash['salt'])->encode($str);
  334. }
  335. //id解密
  336. function decryptIds($str)
  337. {
  338. $hash = config('hashids');
  339. return \Hashids\Hashids::instance($hash['length'], $hash['salt'])->decode($str);
  340. }
  341. /**
  342. * 短信发送示例
  343. *
  344. * @mobile 短信发送对象手机号码
  345. * @action 短信发送场景,会自动传入短信模板
  346. * @parme 短信内容数组
  347. */
  348. function sendSms($mobile, $action, $parme)
  349. {
  350. $config = config('sms');
  351. //$this->SmsDefaultDriver是从数据库中读取的短信默认驱动
  352. $driver = $config['driver'] ?: 'aliyun';
  353. $conf=$config[$driver];
  354. $sms = new SkSms($driver, $conf);//传入短信驱动和配置信息
  355. //判断短信发送驱动,非阿里云和七牛云,需将内容数组主键序号化
  356. if ($driver == 'aliyun') {
  357. $result = $sms->$action($mobile, $parme);
  358. } elseif ($driver == 'qiniu') {
  359. $result = $sms->$action([$mobile], $parme);
  360. } elseif ($driver == 'upyun') {
  361. $result = $sms->$action($mobile, implode('|', restoreArray($parme)));
  362. } else {
  363. $result = $sms->$action($mobile, restoreArray($parme));
  364. }
  365. if ($result['code'] == 200) {
  366. $data['code'] = 200;
  367. $data['msg'] = lang('system.sendOK');
  368. } else {
  369. $data['code'] = $result['code'];
  370. $data['msg'] = $result['msg'];
  371. }
  372. return $data;
  373. }
  374. /**
  375. * 数组主键序号化
  376. *
  377. * @arr 需要转换的数组
  378. */
  379. function restoreArray($arr)
  380. {
  381. if (!is_array($arr)){
  382. return $arr;
  383. }
  384. $c = 0;
  385. $new = [];
  386. foreach ($arr as $key => $value) {
  387. $new[$c] = $value;
  388. $c++;
  389. }
  390. return $new;
  391. }
  392. //密码生成规则
  393. function password_hash_tp($password,$salt)
  394. {
  395. return md5($salt.$password.$salt);
  396. }
  397. // 获取主域名
  398. function getMainHost(){
  399. $host=config('app.app_host','');
  400. if($host){
  401. return $host;
  402. }
  403. $port=request()->port();
  404. $domain=request()->domain();
  405. // halt($domain);
  406. // 判断url是否有端口
  407. if(!hasPort($domain)){
  408. if($port!=80 && $port !=443){
  409. return request()->domain().":".$port;
  410. }
  411. }
  412. return $domain;
  413. }
  414. function hasPort($domainOrIp) {
  415. // 查找冒号的位置
  416. $colonPos = strrpos($domainOrIp, ':');
  417. if ($colonPos!== false) {
  418. // 获取冒号后面的字符串
  419. $portPart = substr($domainOrIp, $colonPos + 1);
  420. // 判断冒号后面的字符串是否为纯数字
  421. return ctype_digit($portPart);
  422. }
  423. return false;
  424. }
  425. // 获取url中的主机名
  426. function getHost($url){
  427. if(!preg_match('/http[s]:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is',$url)){
  428. return '';
  429. }
  430. $search = '~^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?~i';
  431. $url = trim($url);
  432. preg_match_all($search, $url ,$rr);
  433. return $rr[4][0];
  434. }
  435. //根据姓名画头像
  436. function circleAvatar($str,$s,$uid=0,$is_save=0,$save_path=''){
  437. //定义输出为图像类型
  438. header("content-type:image/png");
  439. $str =$str?:"A";
  440. $uid =$uid?:rand(0,10);
  441. $text=\utils\Str::getLastName($str,2);
  442. $width = $height = $s?:80;
  443. if($width<40 or $width>120){
  444. $width = $height =80;
  445. }
  446. $colors=['#F56C6C','#E6A23C','#fbbd08','#67C23A','#39b54a','#1cbbb4','#409EFF','#6739b6','#e239ff','#e03997'];
  447. $color=hex2rgb($colors[(int)$uid%10]);
  448. $size=$width/4;
  449. $textLeft=($height/2)-$size-$width/10;
  450. if($width<=80){
  451. $text=\utils\Str::getLastName($str,1);
  452. $size=$width/2;
  453. $textLeft=$size/3;
  454. }
  455. //新建图象
  456. $pic=imagecreate($width,$height);
  457. //定义黑白颜色
  458. $background=imagecolorallocate($pic,$color['r'],$color['g'],$color['b']);
  459. $textColor=imagecolorallocate($pic,255,255,255);
  460. imagefill($pic,0,0,$background);//填充背景色
  461. //定义字体
  462. $font=root_path()."/public/static/fonts/PingFangHeavy.ttf";
  463. //写 TTF 文字到图中
  464. imagettftext($pic,$size,0,$textLeft,($height/2)+$size/2,$textColor,$font,$text);
  465. if($is_save){
  466. $path=$save_path."/".$uid.".png";
  467. $dir = pathinfo($path,PATHINFO_DIRNAME);
  468. if(!is_dir($dir)){
  469. $file_create_res = mkdir($dir,0777,true);
  470. if(!$file_create_res){
  471. imagedestroy($pic);
  472. return false;//没有创建成功
  473. }
  474. }
  475. imagepng($pic,$path);
  476. imagedestroy($pic);
  477. return $path;
  478. }else{
  479. //输出图象
  480. imagepng($pic);
  481. //结束图形,释放内存空间
  482. imagedestroy($pic);
  483. return $pic;
  484. }
  485. }
  486. //头像拼接
  487. function avatarUrl($path, $str = "雨",$uid=0,$s=80,$is_group=0)
  488. {
  489. if ($path) {
  490. // 判断头像路径中是否有http
  491. if (strpos($path, 'http') !== false) {
  492. $url = $path;
  493. } else {
  494. $url = getDiskUrl() .'/'. ltrim($path,'/') ;
  495. }
  496. }else {
  497. if($is_group){
  498. $url=getMainHost()."/static/img/group.png";
  499. }else{
  500. $url=getMainHost()."/static/img/avatar.png";;
  501. }
  502. }
  503. return $url;
  504. // $str = Str::strFilter($str);
  505. // preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str, $matches);
  506. // $str=implode('', $matches[0]);
  507. // if($str==''){
  508. // $str="无";
  509. // }
  510. // if ($path) {
  511. // // 判断头像路径中是否有http
  512. // if (strpos($path, 'http') !== false) {
  513. // $url = $path;
  514. // } else {
  515. // $url = getDiskUrl() .'/'. ltrim($path,'/') ;
  516. // }
  517. // }else {
  518. // if($str){
  519. // $url=getMainHost()."/avatar/".$str.'/'.$s.'/'.$uid;
  520. // }else{
  521. // $url='';
  522. // }
  523. // }
  524. // return $url;
  525. }
  526. // 获取文件的地址
  527. function getFileUrl($path){
  528. if (strpos($path, 'http') !== false) {
  529. return $path;
  530. }
  531. return getDiskUrl() .'/'. ltrim($path,'/') ;
  532. }
  533. /**
  534. * 十六进制 转 RGB
  535. */
  536. function hex2rgb($hexColor)
  537. {
  538. $color = str_replace('#', '', $hexColor);
  539. if (strlen($color) > 3) {
  540. $rgb = array(
  541. 'r' => hexdec(substr($color, 0, 2)),
  542. 'g' => hexdec(substr($color, 2, 2)),
  543. 'b' => hexdec(substr($color, 4, 2))
  544. );
  545. } else {
  546. $color = $hexColor;
  547. $r = substr($color, 0, 1) . substr($color, 0, 1);
  548. $g = substr($color, 1, 1) . substr($color, 1, 1);
  549. $b = substr($color, 2, 1) . substr($color, 2, 1);
  550. $rgb = array(
  551. 'r' => hexdec($r),
  552. 'g' => hexdec($g),
  553. 'b' => hexdec($b)
  554. );
  555. }
  556. return $rgb;
  557. }
  558. /**
  559. * 将数组按字母A-Z排序
  560. * @return [type] [description]
  561. */
  562. function chartSort($array, $field,$isGroup=true,$chart='chart')
  563. {
  564. $newArray = [];
  565. foreach ($array as $k => &$v) {
  566. $v[$chart] = getFirstChart($v[$field]);
  567. $newArray[] = $v;
  568. }
  569. $data = [];
  570. if($isGroup){
  571. foreach ($newArray as $k => $v) {
  572. if (array_key_exists($v[$chart], $data)) {
  573. $data[$v[$chart]][] = $v;
  574. } else {
  575. $data[$v[$chart]] = [];
  576. $data[$v[$chart]][] = $v;
  577. }
  578. }
  579. ksort($data);
  580. }else{
  581. return $newArray;
  582. }
  583. return $data;
  584. }
  585. /**
  586. * 返回取汉字的第一个字的首字母
  587. * @param [type] $str [string]
  588. * @return [type] [strind]
  589. */
  590. function getFirstChart($str)
  591. {
  592. $str = str_replace(' ', '', $str);
  593. // 过滤特殊符号
  594. $str = preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9]/u', '', $str);
  595. if (empty($str) || is_numeric($str)) {
  596. return '#';
  597. }
  598. $char = ord($str[0]);
  599. if ($char >= ord('A') && $char <= ord('z')) {
  600. return strtoupper($str[0]);
  601. }
  602. $s1 = iconv('UTF-8', 'gb2312//IGNORE', $str);
  603. $s2 = iconv('gb2312', 'UTF-8//IGNORE', $s1);
  604. $s = $s2 == $str ? $s1 : $str;
  605. $asc = ord($s[0]) * 256 + ord($s[1]) - 65536;
  606. if ($asc >= -20319 && $asc <= -20284) return 'A';
  607. if ($asc >= -20283 && $asc <= -19776) return 'B';
  608. if ($asc >= -19775 && $asc <= -19219) return 'C';
  609. if ($asc >= -19218 && $asc <= -18711) return 'D';
  610. if ($asc >= -18710 && $asc <= -18527) return 'E';
  611. if ($asc >= -18526 && $asc <= -18240) return 'F';
  612. if ($asc >= -18239 && $asc <= -17923) return 'G';
  613. if ($asc >= -17922 && $asc <= -17418) return 'H';
  614. if ($asc >= -17417 && $asc <= -16475) return 'J';
  615. if ($asc >= -16474 && $asc <= -16213) return 'K';
  616. if ($asc >= -16212 && $asc <= -15641) return 'L';
  617. if ($asc >= -15640 && $asc <= -15166) return 'M';
  618. if ($asc >= -15165 && $asc <= -14923) return 'N';
  619. if ($asc >= -14922 && $asc <= -14915) return 'O';
  620. if ($asc >= -14914 && $asc <= -14631) return 'P';
  621. if ($asc >= -14630 && $asc <= -14150) return 'Q';
  622. if ($asc >= -14149 && $asc <= -14091) return 'R';
  623. if ($asc >= -14090 && $asc <= -13319) return 'S';
  624. if ($asc >= -13318 && $asc <= -12839) return 'T';
  625. if ($asc >= -12838 && $asc <= -12557) return 'W';
  626. if ($asc >= -12556 && $asc <= -11848) return 'X';
  627. if ($asc >= -11847 && $asc <= -11056) return 'Y';
  628. if ($asc >= -11055 && $asc <= -10247) return 'Z';
  629. return "#";
  630. }
  631. // 拼接聊天对象
  632. function chat_identify($from_user,$to_user){
  633. $identify=[$from_user,$to_user];
  634. sort($identify);
  635. return implode('-',$identify);
  636. }
  637. //数组中获取ID字符串
  638. function arrayToString($array,$field,$isStr=true){
  639. $idArr = [];
  640. foreach ($array as $k => $v) {
  641. if(is_array($field)){
  642. foreach($field as $val){
  643. $idArr[]=$v[$val];
  644. }
  645. }else{
  646. $idArr[] = $v[$field];
  647. }
  648. }
  649. if ($isStr) {
  650. $idStr = implode(',', $idArr);
  651. return $idStr;
  652. } else {
  653. return $idArr;
  654. }
  655. }
  656. // 根据文件后缀进行分类
  657. function getFileType($ext,$rst=false){
  658. $ext=strtolower($ext);
  659. $image=['jpg','jpeg','png','bmp','gif','webp','ico'];
  660. $radio=['mp3','wav','wmv','amr'];
  661. $video=['mp4','3gp','avi','m2v','mkv','mov'];
  662. $doc=['ppt','pptx','doc','docx','xls','xlsx','pdf','txt','md'];
  663. $msgType='file';
  664. if(in_array($ext,$doc)){
  665. $fileType=1;
  666. }elseif(in_array($ext,$image)){
  667. $fileType=2;
  668. $msgType='image';
  669. }elseif(in_array($ext,$radio)){
  670. $fileType=3;
  671. $msgType='voice';
  672. }elseif(in_array($ext,$video)){
  673. $fileType=4;
  674. $msgType='video';
  675. }else{
  676. $fileType=9;
  677. }
  678. if($rst){
  679. return $msgType;
  680. }else{
  681. return $fileType;
  682. }
  683. }
  684. /**
  685. * 二位数组排序
  686. * $array 需要排序的数组
  687. * $sort_key 需要排序的字段
  688. * $sort_order 正序还是倒序
  689. * $sort_type 排序的类型:数字,字母
  690. */
  691. function sortArray($arrays, $sort_key, $sort_order = SORT_ASC, $sort_type = SORT_NUMERIC)
  692. {
  693. if (is_array($arrays)) {
  694. foreach ($arrays as $array) {
  695. if (is_array($array)) {
  696. $key_arrays[] = $array[$sort_key];
  697. } else {
  698. return false;
  699. }
  700. }
  701. } else {
  702. return false;
  703. }
  704. array_multisort($key_arrays, $sort_order, $sort_type, $arrays);
  705. return $arrays;
  706. }
  707. //gateway向web页面推送消息
  708. function wsSendMsg($user, $type, $data, $isGroup=0)
  709. {
  710. $message = json_encode([
  711. 'type' => $type,
  712. 'time' => time(),
  713. 'data' => $data
  714. ]);
  715. try{
  716. Gateway::$registerAddress = config('gateway.registerAddress');
  717. if (!$user) {
  718. Gateway::sendToAll($message);
  719. } else {
  720. if (!$isGroup) {
  721. $send = 'sendToUid';
  722. // 如果是单聊和语音通话需要使用unipush推送
  723. $event=$data['extends']['event'] ?? '';
  724. if(in_array($type,['simple']) || ($event=='calling' && $type=='webrtc')){
  725. unipush($user,$data);
  726. }
  727. } else {
  728. $send = "sendToGroup";
  729. }
  730. Gateway::$send($user, $message);
  731. }
  732. if ($type == 'isOnline' && isset($data['is_online'])) {
  733. \app\enterprise\model\User::setOnline($data['id'], $data['is_online']);
  734. }
  735. }catch(\Exception $e){
  736. //忽略错误
  737. }
  738. }
  739. // 绑定unipush的cid
  740. function bindCid($uid,$cid){
  741. $url=env('unipush.url','');
  742. if(!$url){
  743. return false;
  744. }
  745. $data=[
  746. 'type'=>'bindCid',
  747. 'alias'=>[[
  748. 'cid'=>$cid,
  749. 'alias'=>$uid
  750. ]]
  751. ];
  752. try{
  753. $data=json_encode($data);
  754. utils\Curl::curl_post($url,$data,true,["Content-Type: application/json"]);
  755. }catch(\Exception $e){
  756. //忽略错误
  757. }
  758. }
  759. // unipush推送
  760. function unipush($toUser,$data){
  761. $url=env('unipush.url','');
  762. if(!$url){
  763. return false;
  764. }
  765. $content='';
  766. if($data['type']=='text'){
  767. $content=Str::subStr($data['content'],0,50);
  768. }else{
  769. $content=getMsgType($data['type'],$data['extends']['type'] ?? 0);
  770. }
  771. // 这个推送不需要发给发送人
  772. $fromUser=$data['fromUser']['id'] ?? '';
  773. if(is_array($toUser)){
  774. $toUser=array_diff($toUser,[$fromUser]);
  775. }
  776. $is_force=env('unipush.is_force',false);
  777. $data=[
  778. 'type'=>'push',
  779. 'toUser'=>$toUser,
  780. 'title'=>$data['fromUser']['displayName'],
  781. 'content'=>$content,
  782. 'force_notification'=>$is_force,
  783. 'payload'=>$data
  784. ];
  785. try{
  786. $data=json_encode($data);
  787. utils\Curl::curl_post($url,$data,true,["Content-Type: application/json"]);
  788. }catch(\Exception $e){
  789. //忽略错误
  790. }
  791. }
  792. // 预览文件
  793. function previewUrl($url){
  794. $previewUrl=env('preview.own','');
  795. // $preview='';
  796. // $suffix=explode('.',$url);
  797. // $ext=$suffix[count($suffix)-1];
  798. // $media=['jpg','jpeg','png','bmp','gif','pdf','mp3','wav','wmv','amr','mp4','3gp','avi','m2v','mkv','mov','webp'];
  799. // $doc=['ppt','pptx','doc','docx','xls','xlsx','pdf'];
  800. // if(in_array($ext,$media) && $previewConf['own']){
  801. // $preview=$previewConf['own']."view.html?src=".$url;
  802. // }elseif(in_array($ext,$doc) && $previewConf['yzdcs']){
  803. // $preview=$previewConf['yzdcs'].'?k='.$previewConf['keycode'].'&url='.$url;
  804. // }else{
  805. // }
  806. if($previewUrl){
  807. $preview=$previewUrl.$url;
  808. }else{
  809. $preview=getMainHost()."/view.html?src=".$url;
  810. }
  811. return $preview;
  812. }
  813. /**
  814. * 解析sql语句
  815. * @param string $content sql内容
  816. * @param int $limit 如果为1,则只返回一条sql语句,默认返回所有
  817. * @param array $prefix 替换表前缀
  818. * @return array|string 除去注释之后的sql语句数组或一条语句
  819. */
  820. function parse_sql($sql = '', $limit = 0, $prefix = []) {
  821. // 被替换的前缀
  822. $from = '';
  823. // 要替换的前缀
  824. $to = '';
  825. // 替换表前缀
  826. if (!empty($prefix)) {
  827. $to = current($prefix);
  828. $from = current(array_flip($prefix));
  829. }
  830. if ($sql != '') {
  831. // 纯sql内容
  832. $pure_sql = [];
  833. // 多行注释标记
  834. $comment = false;
  835. // 按行分割,兼容多个平台
  836. $sql = str_replace(["\r\n", "\r"], "\n", $sql);
  837. $sql = explode("\n", trim($sql));
  838. // 循环处理每一行
  839. foreach ($sql as $key => $line) {
  840. // 跳过空行
  841. if ($line == '') {
  842. continue;
  843. }
  844. // 跳过以#或者--开头的单行注释
  845. if (preg_match("/^(#|--)/", $line)) {
  846. continue;
  847. }
  848. // 跳过以/**/包裹起来的单行注释
  849. if (preg_match("/^\/\*(.*?)\*\//", $line)) {
  850. continue;
  851. }
  852. // 多行注释开始
  853. if (substr($line, 0, 2) == '/*') {
  854. $comment = true;
  855. continue;
  856. }
  857. // 多行注释结束
  858. if (substr($line, -2) == '*/') {
  859. $comment = false;
  860. continue;
  861. }
  862. // 多行注释没有结束,继续跳过
  863. if ($comment) {
  864. continue;
  865. }
  866. // 替换表前缀
  867. if ($from != '') {
  868. $line = str_replace('`'.$from, '`'.$to, $line);
  869. }
  870. if ($line == 'BEGIN;' || $line =='COMMIT;') {
  871. continue;
  872. }
  873. // sql语句
  874. array_push($pure_sql, $line);
  875. }
  876. // 只返回一条语句
  877. if ($limit == 1) {
  878. return implode("",$pure_sql);
  879. }
  880. // 以数组形式返回sql语句
  881. $pure_sql = implode("\n",$pure_sql);
  882. $pure_sql = explode(";\n", $pure_sql);
  883. return $pure_sql;
  884. } else {
  885. return $limit == 1 ? '' : [];
  886. }
  887. }
  888. /**
  889. * 更新或添加环境变量
  890. *
  891. * @param string $key 环境变量的键
  892. * @param string $value 环境变量的值
  893. * @return bool 成功返回 true,失败返回 false
  894. */
  895. function updateEnv($key, $value)
  896. {
  897. $envFile = app()->getRootPath() . '.env';
  898. if (!file_exists($envFile) || !is_writable($envFile)){
  899. return false;
  900. }
  901. // 读取 .env 文件内容
  902. $envContent = file_get_contents($envFile);
  903. $keyPattern = preg_quote($key, '/');
  904. $pattern = "/^{$keyPattern}=(.*)\$/m";
  905. if (preg_match($pattern, $envContent)) {
  906. // 如果找到了键值对,替换其值
  907. $replacement = "{$key}={$value}";
  908. $newEnvContent = preg_replace($pattern, $replacement, $envContent);
  909. } else {
  910. // 如果没有找到键值对,添加新的键值对
  911. $newEnvContent = $envContent . PHP_EOL . "{$key}={$value}";
  912. }
  913. // 保存更新后的 .env 文件内容
  914. return file_put_contents($envFile, $newEnvContent) !== false;
  915. }
  916. // 获取文件的域名
  917. function getDiskUrl(){
  918. $disk=env('filesystem.driver','local');
  919. $url=getMainHost();
  920. if($disk=='aliyun'){
  921. $url=env('filesystem.aliyun_url','');
  922. }elseif($disk=='qiniu'){
  923. $url=env('filesystem.qiniu_url','');
  924. }elseif($disk=='qcloud'){
  925. $url=env('filesystem.qcloud_cdn','');
  926. }
  927. $url=rtrim($url,'/');
  928. return $url;
  929. }
  930. /**
  931. * 合成图片
  932. * @param array $pic_list [图片列表数组]
  933. * @param boolean $is_save [是否保存,true保存,false输出到浏览器]
  934. * @param string $save_path [保存路径]
  935. * @return boolean|string
  936. */
  937. function getGroupAvatar($pic_list=array(),$is_save=false,$save_path=''){
  938. //验证参数
  939. if(empty($pic_list) || empty($save_path)){
  940. return false;
  941. }
  942. if($is_save){
  943. //如果需要保存,需要传保存地址
  944. if(empty($save_path)){
  945. return false;
  946. }
  947. }
  948. // 只操作前9个图片
  949. $pic_list = array_slice($pic_list, 0, 9);
  950. //设置背景图片宽高
  951. $bg_w = 150; // 背景图片宽度
  952. $bg_h = 150; // 背景图片高度
  953. //新建一个真彩色图像作为背景
  954. $background = imagecreatetruecolor($bg_w,$bg_h);
  955. //为真彩色画布创建白灰色背景,再设置为透明
  956. $color = imagecolorallocate($background, 202, 201, 201);
  957. imagefill($background, 0, 0, $color);
  958. imageColorTransparent($background, $color);
  959. //根据图片个数设置图片位置
  960. $pic_count = count($pic_list);
  961. $lineArr = array();//需要换行的位置
  962. $space_x = 3;
  963. $space_y = 3;
  964. $line_x = 0;
  965. switch($pic_count) {
  966. case 1: // 正中间
  967. $start_x = intval($bg_w/4); // 开始位置X
  968. $start_y = intval($bg_h/4); // 开始位置Y
  969. $pic_w = intval($bg_w/2); // 宽度
  970. $pic_h = intval($bg_h/2); // 高度
  971. break;
  972. case 2: // 中间位置并排
  973. $start_x = 2;
  974. $start_y = intval($bg_h/4) + 3;
  975. $pic_w = intval($bg_w/2) - 5;
  976. $pic_h = intval($bg_h/2) - 5;
  977. $space_x = 5;
  978. break;
  979. case 3:
  980. $start_x = 40; // 开始位置X
  981. $start_y = 5; // 开始位置Y
  982. $pic_w = intval($bg_w/2) - 5; // 宽度
  983. $pic_h = intval($bg_h/2) - 5; // 高度
  984. $lineArr = array(2);
  985. $line_x = 4;
  986. break;
  987. case 4:
  988. $start_x = 4; // 开始位置X
  989. $start_y = 5; // 开始位置Y
  990. $pic_w = intval($bg_w/2) - 5; // 宽度
  991. $pic_h = intval($bg_h/2) - 5; // 高度
  992. $lineArr = array(3);
  993. $line_x = 4;
  994. break;
  995. case 5:
  996. $start_x = 30; // 开始位置X
  997. $start_y = 30; // 开始位置Y
  998. $pic_w = intval($bg_w/3) - 5; // 宽度
  999. $pic_h = intval($bg_h/3) - 5; // 高度
  1000. $lineArr = array(3);
  1001. $line_x = 5;
  1002. break;
  1003. case 6:
  1004. $start_x = 5; // 开始位置X
  1005. $start_y = 30; // 开始位置Y
  1006. $pic_w = intval($bg_w/3) - 5; // 宽度
  1007. $pic_h = intval($bg_h/3) - 5; // 高度
  1008. $lineArr = array(4);
  1009. $line_x = 5;
  1010. break;
  1011. case 7:
  1012. $start_x = 53; // 开始位置X
  1013. $start_y = 5; // 开始位置Y
  1014. $pic_w = intval($bg_w/3) - 5; // 宽度
  1015. $pic_h = intval($bg_h/3) - 5; // 高度
  1016. $lineArr = array(2,5);
  1017. $line_x = 5;
  1018. break;
  1019. case 8:
  1020. $start_x = 30; // 开始位置X
  1021. $start_y = 5; // 开始位置Y
  1022. $pic_w = intval($bg_w/3) - 5; // 宽度
  1023. $pic_h = intval($bg_h/3) - 5; // 高度
  1024. $lineArr = array(3,6);
  1025. $line_x = 5;
  1026. break;
  1027. case 9:
  1028. $start_x = 5; // 开始位置X
  1029. $start_y = 5; // 开始位置Y
  1030. $pic_w = intval($bg_w/3) - 5; // 宽度
  1031. $pic_h = intval($bg_h/3) - 5; // 高度
  1032. $lineArr = array(4,7);
  1033. $line_x = 5;
  1034. break;
  1035. }
  1036. foreach( $pic_list as $k=>$pic_path ) {
  1037. $kk = $k + 1;
  1038. if ( in_array($kk, $lineArr) ) {
  1039. $start_x = $line_x;
  1040. $start_y = $start_y + $pic_h + $space_y;
  1041. }
  1042. //获取图片文件扩展类型和mime类型,判断是否是正常图片文件
  1043. //非正常图片文件,相应位置空着,跳过处理
  1044. $image_mime_info = @getimagesize($pic_path);
  1045. if($image_mime_info && !empty($image_mime_info['mime'])){
  1046. $mime_arr = explode('/',$image_mime_info['mime']);
  1047. if(is_array($mime_arr) && $mime_arr[0] == 'image' && !empty($mime_arr[1])){
  1048. switch($mime_arr[1]) {
  1049. case 'jpg':
  1050. case 'jpeg':
  1051. $imagecreatefromjpeg = 'imagecreatefromjpeg';
  1052. break;
  1053. case 'png':
  1054. $imagecreatefromjpeg = 'imagecreatefrompng';
  1055. break;
  1056. case 'gif':
  1057. default:
  1058. $imagecreatefromjpeg = 'imagecreatefromstring';
  1059. $pic_path = file_get_contents($pic_path);
  1060. break;
  1061. }
  1062. //创建一个新图像
  1063. $resource = $imagecreatefromjpeg($pic_path);
  1064. //将图像中的一块矩形区域拷贝到另一个背景图像中
  1065. // $start_x,$start_y 放置在背景中的起始位置
  1066. // 0,0 裁剪的源头像的起点位置
  1067. // $pic_w,$pic_h copy后的高度和宽度
  1068. imagecopyresized($background,$resource,$start_x,$start_y,0,0,$pic_w,$pic_h,imagesx($resource),imagesy($resource));
  1069. }
  1070. }
  1071. // 最后两个参数为原始图片宽度和高度,倒数两个参数为copy时的图片宽度和高度
  1072. $start_x = $start_x + $pic_w + $space_x;
  1073. }
  1074. if($is_save){
  1075. $dir = pathinfo($save_path,PATHINFO_DIRNAME);
  1076. if(!is_dir($dir)){
  1077. $file_create_res = mkdir($dir,0777,true);
  1078. if(!$file_create_res){
  1079. return false;//没有创建成功
  1080. }
  1081. }
  1082. $res = imagejpeg($background,$save_path);
  1083. imagedestroy($background);
  1084. if($res){
  1085. return true;
  1086. }else{
  1087. return false;
  1088. }
  1089. }else{
  1090. //直接输出
  1091. header("Content-type: image/jpg");
  1092. imagejpeg($background);
  1093. imagedestroy($background);
  1094. }
  1095. }
  1096. /**
  1097. * 获取一个唯一token
  1098. * @return string
  1099. */
  1100. function getOnlyToken()
  1101. {
  1102. return md5(uniqid(md5(microtime(true)), true));
  1103. }
  1104. // 设置排序规则
  1105. function orderBy($field, $type, $prefix = '', $default = 'update_time')
  1106. {
  1107. $type=is_numeric($type)?($type==1?'asc':'desc'):$type;
  1108. if ($field) {
  1109. $order = $prefix . $field . ' ' . $type;
  1110. } else {
  1111. $order = $prefix . $default . ' desc';
  1112. }
  1113. return $order;
  1114. }
  1115. // 获取文件后缀图片
  1116. function getExtUrl($path){
  1117. $ext=explode('.',$path);
  1118. $ext=end($ext);
  1119. // 如果是图片文件,就直接返回图片地址
  1120. $image=['jpg','jpeg','png','bmp','gif','webp'];
  1121. if(in_array($ext,$image)){
  1122. return getFileUrl($path);
  1123. }
  1124. $extUrl='/static/img/ext/'.strtoupper($ext).'.png';
  1125. // 判断文件是否存在
  1126. if(!file_exists(public_path().$extUrl)){
  1127. $extUrl='/static/img/ext/folder.png';
  1128. }
  1129. return getMainHost().$extUrl;
  1130. }
  1131. // 字符串内容加解密函数
  1132. function str_encipher($str,$encode=true,$key=''){
  1133. if($key==''){
  1134. $key=config('app.aes_chat_key');
  1135. }
  1136. if($key=='' || $str==''){
  1137. return $str;
  1138. }
  1139. if($encode){
  1140. $s=\utils\Aes::encrypt($str,$key);
  1141. }else{
  1142. $s=\utils\Aes::decrypt($str,$key) ?:'';
  1143. }
  1144. return $s;
  1145. }
  1146. // 推送时获取消息的类型
  1147. function getMsgType($type,$callVideo=false){
  1148. $msgName=lang('messageType.other');
  1149. switch($type){
  1150. case 'image':
  1151. $msgName=lang('messageType.image');
  1152. break;
  1153. case 'voice':
  1154. $msgName=lang('messageType.voice');
  1155. break;
  1156. case 'emoji':
  1157. $msgName=lang('messageType.emoji');
  1158. break;
  1159. case 'video':
  1160. $msgName=lang('messageType.video');
  1161. break;
  1162. case 'file':
  1163. $msgName=lang('messageType.file');
  1164. break;
  1165. case 'webrtc':
  1166. if($callVideo){
  1167. $msgName=lang('messageType.webrtcAudio');
  1168. }else{
  1169. $msgName=lang('messageType.webrtcVideo');
  1170. }
  1171. break;
  1172. }
  1173. return $msgName;
  1174. }
  1175. // 获取app的下载链接
  1176. function getAppDowmUrl($platform='android'){
  1177. $config=config('version.'.$platform);
  1178. $name=config('version.app_name');
  1179. if($platform=='windows'){
  1180. $packageName=$name."_Setup_".$config['version'].".exe";
  1181. $path="/downloadApp/windows";
  1182. }elseif($platform=='mac'){
  1183. $packageName=$name."_Setup_".$config['version'].".dmg";
  1184. $path="/downloadApp/mac";
  1185. }else{
  1186. $packageName=$name."_Setup_".$config['version'].".apk";
  1187. $path="/downloadApp/android";
  1188. }
  1189. if(is_file(PACKAGE_PATH . $packageName)){
  1190. return getMainHost().$path;
  1191. }else{
  1192. return '';
  1193. }
  1194. }
  1195. // php匹配文本中的所有url
  1196. function getAllUrl($text){
  1197. // 使用正则表达式匹配带有或不带有协议头的URL
  1198. $pattern = '/\b(?:https?:\/\/)?[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/i';
  1199. // 使用preg_replace()函数将URL转换为<a>标签
  1200. $replaced_text = preg_replace_callback($pattern, function($matches) {
  1201. $url = $matches[0];
  1202. if(utils\Regular::is_url($url)){
  1203. $newUrl=$url;
  1204. if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
  1205. $newUrl = "http://" . $url;
  1206. }
  1207. return '<a href="' . $newUrl . '">' . $url . '</a>';
  1208. }else{
  1209. return $url;
  1210. }
  1211. }, $text);
  1212. return $replaced_text;
  1213. }
  1214. // 将链接转成可点击的标签
  1215. function preg_link($text){
  1216. // 判断文本中是否有img标签
  1217. if(preg_match('/<img[^>]+>/i', $text)){
  1218. return $text;
  1219. }
  1220. // 匹配更广泛的 URL 的正则表达式
  1221. $pattern ='/\b(?:https?:\/\/|ftp:\/\/)?([a-z0-9-+&@#\/%?=~_|!:,.;]*\.[a-z]{2,}(?:\/[a-z0-9-+&@#\/%?=~_|!:,.;]*)*)\b/i';
  1222. // 使用preg_replace()函数将URL转换为<a>标签
  1223. $replaced_text = preg_replace_callback($pattern, function($matches) {
  1224. $url = $matches[0];
  1225. $isUrl=preg_match('/\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]|[a-z0-9-+&@#\/%?=~_|!:,.;]*\.[a-z]{2,}\b/i',$url) ? true : false;
  1226. if($isUrl){
  1227. $newUrl=$url;
  1228. if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
  1229. $newUrl = "https://" . $url;
  1230. }
  1231. return '<a href="' . $newUrl . '" target="_blank">' . $url . '</a>';
  1232. }else{
  1233. return $url;
  1234. }
  1235. }, $text);
  1236. return $replaced_text;
  1237. }
  1238. //消息队列think-queue
  1239. function queuePush($data, $delay = 0, $job = "Work", $queue = "im")
  1240. {
  1241. $data['job'] = $data['job'] ?? $job;
  1242. $data['queue'] = $data['queue'] ?? $queue;
  1243. $data['data'] = $data['data'] ?? [];
  1244. try {
  1245. if ($data) {
  1246. if ($delay == 0) {
  1247. Queue::push($job, $data, $queue);
  1248. } else {
  1249. Queue::later($delay, $job, $data, $queue);
  1250. }
  1251. }
  1252. return true;
  1253. } catch (Exception $e) {
  1254. return false;
  1255. }
  1256. }