common.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. // 应用公共文件
  3. use app\model\api\City as CityModel;
  4. function is_moblie($mobile){
  5. if (!is_numeric($mobile)) {
  6. return false;
  7. }
  8. return preg_match('/^1[3456789]{1}\d{9}$/', $mobile) ? true : false;
  9. }
  10. /**
  11. * 图片数组
  12. * @param type $imgs
  13. * @param type $sep
  14. * @param type $limit
  15. * @return type
  16. */
  17. function getImageAr($imgs,$limit=-1,$sep = ","){
  18. if(empty($imgs)){
  19. return [];
  20. }
  21. //返回全部
  22. if($limit==-1){
  23. return explode($sep,$imgs);
  24. }
  25. if($limit==0){
  26. return explode($sep,$imgs,1)[0];
  27. }
  28. return explode($sep,$imgs,$limit);
  29. }
  30. /**
  31. * 获取区域信息
  32. * @param type $id
  33. */
  34. function getAreaItemAr($id){
  35. $data = $CityModel->field("id,name,level")->where("id",$id)->find();
  36. if(empty($data)){
  37. return [];
  38. }
  39. if($data["level"]==0){
  40. return [$data];
  41. }
  42. $parentData = $CityModel->field("id,name,level")->where("city_id",$data["parent_id"])->find();
  43. if($data["level"]==1){
  44. return [$parentData,$data];
  45. }
  46. $firstData = $CityModel->field("id,name,level")->where("city_id",$parentData["parent_id"])->find();
  47. return [$firstData,$parentData,$data];
  48. }
  49. /**
  50. * 验证手机号是否正确
  51. * @param number $mobile
  52. * @author honfei
  53. */
  54. function isMobile($mobile)
  55. {
  56. if (!is_numeric($mobile)) {
  57. return false;
  58. }
  59. return preg_match('/^1[3456789]{1}\d{9}$/', $mobile) ? true : false;
  60. }
  61. /**
  62. * 中间加密 用正则
  63. */
  64. function encryptTel($tel)
  65. {
  66. $new_tel = preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $tel);
  67. return $new_tel;
  68. }
  69. /**
  70. * 清除html
  71. * @param $str
  72. * @return mixed|string
  73. */
  74. function clearHtml($str)
  75. {
  76. if (!empty($str)) {
  77. $str = html_entity_decode($str);
  78. $str = strip_tags($str);
  79. $str = str_replace("&nbsp;", "", $str);
  80. }
  81. return $str;
  82. }
  83. /**
  84. * 字符串截取,支持中文和其他编码
  85. * @static
  86. * @access public
  87. * @param string $str 需要转换的字符串
  88. * @param string $start 开始位置
  89. * @param string $length 截取长度
  90. * @param string $charset 编码格式
  91. * @param string $suffix 截断显示字符
  92. * @return string
  93. */
  94. function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
  95. {
  96. $_count = hanzi_length($str);
  97. if ($length > $_count && $start == 0) {
  98. $r = $str;
  99. } else {
  100. if (function_exists("mb_substr"))
  101. $slice = mb_substr($str, $start, $length, $charset);
  102. elseif (function_exists('iconv_substr')) {
  103. $slice = iconv_substr($str, $start, $length, $charset);
  104. } else {
  105. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  106. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  107. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  108. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  109. preg_match_all($re[$charset], $str, $match);
  110. $slice = join("", array_slice($match[0], $start, $length));
  111. }
  112. $r = $suffix ? $slice . '...' : $slice;
  113. }
  114. return $r;
  115. }
  116. /**
  117. * 计算字符串长度
  118. * @param type $str
  119. * @return int
  120. */
  121. function hanzi_length($str)
  122. {
  123. $len = strlen($str);
  124. $i = 0;
  125. $n = 0;
  126. while ($i < $len) {
  127. if (preg_match("/^[" . chr(0xa1) . "-" . chr(0xff) . "]+$/", $str[$i])) {
  128. $i += 4;
  129. } else {
  130. $i += 1;
  131. }
  132. $n += 1;
  133. }
  134. return $n;
  135. }
  136. /**
  137. * 随机码
  138. * @param type $length
  139. * @return string
  140. */
  141. function randString($length, $c = false)
  142. {
  143. if ($c) {
  144. $_codeSet = '123456789';//不要加0
  145. } else {
  146. $_codeSet = '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
  147. }
  148. $code = '';
  149. for ($i = 0; $i < $length; $i++) {
  150. $code .= $_codeSet[mt_rand(0, strlen($_codeSet) - 1)];
  151. }
  152. return $code;
  153. }
  154. /**
  155. * 验证用户信息返回用户信息
  156. *
  157. */
  158. function is_mobile($user)
  159. {
  160. $r = false;
  161. //验证是否手机号码
  162. $is_mobel = '/^0?(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[[0-9])[0-9]{8}/is';
  163. if (preg_match($is_mobel, $user)) {
  164. $r = true;
  165. }
  166. return $r;
  167. }
  168. function encodePassword($password)
  169. {
  170. return empty($password) ? '' : md5('$^95@Kdf' . md5('Pincll@yiyun' . $password));
  171. }
  172. /**
  173. * 生成用户唯一ip
  174. * @param type $uid
  175. */
  176. function makeUuip($uid){
  177. $codeSert = '123456789ABCDEFGHJKLMNPQRTUVWXY';
  178. $code = '';
  179. $length = 10-strlen($uid)-2;
  180. for ($i = 0; $i < $length; $i++) {
  181. $code .= $codeSert[mt_rand(0, strlen($codeSert) - 1)];
  182. }
  183. return $code."IP".$uid;
  184. }
  185. function makeOrderId($uid,$per="H"){
  186. $codeSert = '123456789ABCDEFGHJKLMNPQRTUVWXY';
  187. $code = '';
  188. $length = 4;
  189. for ($i = 0; $i < $length; $i++) {
  190. $code .= $codeSert[mt_rand(0, strlen($codeSert) - 1)];
  191. }
  192. $time = microtime(true)*10000;
  193. return $per . $time . $code . "OU" .$uid;
  194. }
  195. /**
  196. * 获取唯一token
  197. * @param type $lower 小写
  198. * @return string
  199. */
  200. function getPartToken($lower=false,$sep=true){
  201. $key = "";
  202. if (function_exists ( 'com_create_guid' )) {
  203. $key = com_create_guid();
  204. } else {
  205. //mt_srand((double) microtime() * 10000); //optional for php 4.2.0 and up.
  206. $charid = strtoupper(md5(uniqid(rand(),true))); //根据当前时间(微秒计)生成唯一id.
  207. $hyphen = chr(45); // "-"
  208. $uuid = '' . //chr(123)// "{"
  209. substr($charid,0,8 ).$hyphen.substr($charid,8,4).$hyphen.substr($charid,12,4).$hyphen.substr($charid,16,4).$hyphen.substr($charid,20,12);
  210. //.chr(125);// "}"
  211. $key = $uuid;
  212. }
  213. //小写
  214. if($lower){
  215. $key = strtolower($key);
  216. }
  217. //无分隔符
  218. if(!$sep){
  219. $key = str_replace(chr(45), "", $key);
  220. }
  221. return $key;
  222. }
  223. /**
  224. * 生成UUID
  225. * @param $uid
  226. * @return string
  227. */
  228. function Uuid($uid)
  229. {
  230. $uuid = randString(8,true);
  231. $uuid = substr($uuid,0, - (strlen($uid)+1)) ."0". $uid;
  232. return $uuid;
  233. }
  234. /**
  235. * 生成客服UUID
  236. * @param $uid
  237. * @return string
  238. */
  239. function Kuuid($admin_id)
  240. {
  241. $uuid = randString(8,true);
  242. $uuid = "kf".substr($uuid,0, - (strlen($admin_id)+1)) ."0". $admin_id;
  243. return $uuid;
  244. }
  245. /**
  246. * 生成TUUID
  247. * @param $tuid
  248. * @return string
  249. */
  250. function Tuuid($tuid)
  251. {
  252. $tuuid = randString(8,true);
  253. $tuuid = substr($tuuid,0, - (strlen($tuid)+1))."0".$tuid;
  254. $str = "ABCDEFGHIJKLMNOPQRSTUVWSYZ";
  255. $wordStart = substr($str,mt_rand(0,25),1);
  256. return $wordStart.$tuuid;
  257. }
  258. /**
  259. * 验证身份证
  260. * @param $num
  261. * @return bool
  262. */
  263. function IdentityCard($num) {
  264. return \library\utils\IdentityCard::isValid($num);
  265. }
  266. /**
  267. * 获取客户端IP地址
  268. * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  269. * @return mixed
  270. */
  271. function get_client_ip($type = 0) {
  272. $type = $type ? 1 : 0;
  273. static $ip = NULL;
  274. if ($ip !== NULL) return $ip[$type];
  275. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  276. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  277. $pos = array_search('unknown',$arr);
  278. if(false !== $pos) unset($arr[$pos]);
  279. $ip = trim($arr[0]);
  280. }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  281. $ip = $_SERVER['HTTP_CLIENT_IP'];
  282. }elseif (isset($_SERVER['REMOTE_ADDR'])) {
  283. $ip = $_SERVER['REMOTE_ADDR'];
  284. }
  285. // IP地址合法验证
  286. $long = sprintf("%u",ip2long($ip));
  287. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  288. return $ip[$type];
  289. }
  290. /**
  291. * 获取客户端信息
  292. */
  293. function getClientInfo(){
  294. $agent = $_SERVER['HTTP_USER_AGENT'];
  295. if(empty($agent)){
  296. return false;
  297. }
  298. $platform="";//平台
  299. $browser="";//内置浏览器版本
  300. //ios系统
  301. if(strpos($agent,'iPad') !== false || strpos($agent,'iPhone') !== false){
  302. $platform = "ios";
  303. }
  304. //android系统
  305. if(strpos($agent,'Android') !== false){
  306. $platform = "android";
  307. }
  308. //微信内置浏览器
  309. if (strpos($agent,'MicroMessenger') !== false ) {
  310. $browser = "weixin";
  311. }
  312. //QQ内置浏览器
  313. if (strpos($agent,'QQ') !== false && strpos($agent,'_SQ_') !== false) {
  314. $browser = "qq";
  315. }
  316. return ["platform"=>$platform,"browser"=>$browser];
  317. }
  318. /**
  319. * 获取随机键值
  320. * @param type $count
  321. * @return type
  322. */
  323. function getRandKeyVal(){
  324. $words = "abcdefghijklmnopqrstuvwxyz";
  325. $key = substr($words,mt_rand(0,25),1).substr($words,mt_rand(0,25),1).mt_rand(0,9);
  326. $val = mt_rand(100,999);
  327. return ["key"=>$key,"val"=>$val];
  328. }
  329. /**
  330. * 不四舍五入保留小数
  331. * @param type $num
  332. * @param type $digit
  333. * @return type
  334. */
  335. function num_min_format($num,$digit=2){
  336. $num = strval(floatval($num));
  337. if(strpos($num,'.')===false){
  338. return floatval($num);
  339. }else{
  340. return floatval(explode(".",$num)[0].".".substr(explode(".",$num)[1],0,$digit));
  341. }
  342. }
  343. /*********************************经纬度计算函数*******************************/
  344. /**
  345. * 获取定位信息
  346. * @param type $lats
  347. * @param type $lngs
  348. * @param type $gps
  349. * @param type $google
  350. * @return type
  351. */
  352. function getgps($lats,$lngs, $gps=false, $google=false)
  353. {
  354. $lat=$lats;
  355. $lng=$lngs;
  356. if($gps)
  357. $c=file_get_contents("http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=$lng&y=$lat");
  358. else if($google)
  359. $c=file_get_contents("http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=$lng&y=$lat");
  360. else
  361. return array($lat,$lng);
  362. $arr=(array)json_decode($c);
  363. if(!$arr['error'])
  364. {
  365. $lat=base64_decode($arr['y']);
  366. $lng=base64_decode($arr['x']);
  367. }
  368. return array($lat,$lng);
  369. }
  370. /**
  371. * @desc 根据两点间的经纬度计算距离
  372. * @param float $lat 纬度值
  373. * @param float $lng 经度值
  374. */
  375. function getDistance($lat1, $lng1, $lat2, $lng2) {
  376. $earthRadius = 6367000; //approximate radius of earth in meters
  377. $lat1 = ($lat1 * pi() ) / 180;
  378. $lng1 = ($lng1 * pi() ) / 180;
  379. $lat2 = ($lat2 * pi() ) / 180;
  380. $lng2 = ($lng2 * pi() ) / 180;
  381. $calcLongitude = $lng2 - $lng1;
  382. $calcLatitude = $lat2 - $lat1;
  383. $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
  384. $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
  385. $calculatedDistance = $earthRadius * $stepTwo;
  386. return round($calculatedDistance);
  387. }
  388. /**
  389. *获取距离
  390. **/
  391. function getDicAres($size){
  392. $v = $size / 1000;
  393. return number_format($v, 2, '.', '');
  394. }
  395. /**
  396. * 百度转化qq
  397. * @param type $lng
  398. * @param type $lat
  399. */
  400. function bMapTransQQMap($lng, $lat){
  401. $x_pi=3.14159265358979324 * 3000.0/180.0;
  402. $x = $lng - 0.0065;
  403. $y = $lat - 0.006;
  404. $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
  405. $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
  406. $lngs = $z * cos($theta);
  407. $lats = $z * sin($theta);
  408. return array($lngs,$lats);
  409. }
  410. /**
  411. * 获取图片信息
  412. * @param type $url
  413. * @return type
  414. */
  415. function getImageUrlInfo($url){
  416. //图片信息
  417. $imgInfo = @getimagesize($url);
  418. if(!$imgInfo){
  419. return false;
  420. }
  421. $urlAr = explode("/",$url);
  422. $fileName = $urlAr[count($urlAr)-1];
  423. $info=[];
  424. $info["size"] = remote_filesize($url);
  425. $info["name"] = explode(".",$fileName)[0];
  426. // $info["url"] = str_replace("/{$fileName}", "", $url);
  427. $info["url"] = $url;
  428. $info["w"] = $imgInfo[0];
  429. $info["h"] = $imgInfo[1];
  430. $info["ext"] = explode("/",$imgInfo["mime"])[1];
  431. $info["md5"] = md5_file($url);
  432. return $info;
  433. }
  434. // 获取远程文件大小函数
  435. function remote_filesize($url, $user = "", $pw = "")
  436. {
  437. ob_start();
  438. $ch = curl_init($url);
  439. curl_setopt($ch, CURLOPT_HEADER, 1);
  440. curl_setopt($ch, CURLOPT_NOBODY, 1);
  441. if(!empty($user) && !empty($pw))
  442. {
  443. $headers = array('Authorization: Basic ' . base64_encode("$user:$pw"));
  444. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  445. }
  446. $ok = curl_exec($ch);
  447. curl_close($ch);
  448. $head = ob_get_contents();
  449. ob_end_clean();
  450. $regex = '/Content-Length:\s([0-9].+?)\s/';
  451. $count = preg_match($regex, $head, $matches);
  452. return isset($matches[1]) ? intval($matches[1]) : 0;
  453. }