common.php 12 KB

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