common.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. function page_result($code = 0, $msg = '', $data = [])
  13. {
  14. $res = ['code' => $code, 'msg' => $msg, 'data' => $data];
  15. $response = \think\Response::create($res, 'json');
  16. throw new \think\exception\HttpResponseException($response);
  17. }
  18. /**
  19. * 时间
  20. */
  21. function tranTime($utime)
  22. {
  23. $time = time() - $utime;
  24. if ($time < 60) {
  25. $str = '刚刚';
  26. } elseif ($time < 60 * 60) {
  27. $min = floor($time / 60);
  28. $str = $min . '分钟前';
  29. } elseif ($time < 60 * 60 * 24) {
  30. $h = floor($time / (60 * 60));
  31. $str = $h . '小时前';
  32. } elseif ($time < 60 * 60 * 24 * 3) {
  33. $d = floor($time / (60 * 60 * 24));
  34. if ($d == 1) {
  35. $str = '昨天';
  36. } else {
  37. $str = '前天';
  38. }
  39. } else {
  40. $str = date("m-d", $utime);
  41. }
  42. return $str;
  43. }
  44. /**
  45. * api 接口正确输出
  46. * @param string $data 返回数据
  47. * @param string $message 提示信息
  48. */
  49. function json_success($data = '', $message = 'success')
  50. {
  51. header('Content-Type:application/json; charset=utf-8');
  52. $result['status'] = 1;
  53. $result['message'] = $message;
  54. $result['data'] = empty($data) ? [] : $data;
  55. exit(json_encode($result));
  56. }
  57. /**
  58. * api 接口错误输出
  59. * @param int $status 状态码: -1参数错误(开发提示) -2用户提示(用户输入错误、商品不存在等) -9token过期
  60. * @param string $message 提示信息
  61. */
  62. function json_error($message = 'error', $status = -1)
  63. {
  64. header('Content-Type:application/json; charset=utf-8');
  65. $result['status'] = $status;
  66. $result['message'] = $message;
  67. exit(json_encode($result));
  68. }
  69. /**
  70. * 获取客户端IP地址
  71. * @param int $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  72. * @return mixed
  73. */
  74. function get_client_ip($type = 0)
  75. {
  76. $type = $type ? 1 : 0;
  77. static $ip = null;
  78. if ($ip !== null) {
  79. return $ip[$type];
  80. }
  81. if (isset($_SERVER['HTTP_X_REAL_IP'])) {
  82. //nginx 代理模式下,获取客户端真实IP
  83. $ip = $_SERVER['HTTP_X_REAL_IP'];
  84. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  85. //客户端的ip
  86. $ip = $_SERVER['HTTP_CLIENT_IP'];
  87. } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  88. //浏览当前页面的用户计算机的网关
  89. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  90. $pos = array_search('unknown', $arr);
  91. if (false !== $pos) {
  92. unset($arr[$pos]);
  93. }
  94. $ip = trim($arr[0]);
  95. } else {
  96. //浏览当前页面的用户计算机的ip地址
  97. $ip = $_SERVER['REMOTE_ADDR'];
  98. }
  99. //IP地址合法验证
  100. $long = sprintf("%u", ip2long($ip));
  101. $ip = $long ? [$ip, $long] : ['0.0.0.0', 0];
  102. return $ip[$type];
  103. }
  104. /**
  105. * 字符转码
  106. * @param $data
  107. * @return array|string
  108. */
  109. function gbk2utf8($data)
  110. {
  111. if (is_array($data)) {
  112. return array_map('gbk2utf8', $data);
  113. }
  114. return iconv('gbk', 'utf-8//IGNORE', $data); //IGNORE 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。
  115. }
  116. /**
  117. * 根据身份证号码获取性别
  118. * author:xiaochuan
  119. * @param string $idcard 身份证号码
  120. * @return int $sex 性别 1男 2女 0未知
  121. */
  122. function get_sex($idcard)
  123. {
  124. if (empty($idcard)) {
  125. return null;
  126. }
  127. $sexint = (int)substr($idcard, 16, 1);
  128. return $sexint % 2 === 0 ? 2 : 1;
  129. }
  130. /**
  131. * 根据身份证号码获取生日
  132. * author:xiaochuan
  133. * @param string $idcard 身份证号码
  134. * @return $birthday
  135. */
  136. function get_birthday($idcard)
  137. {
  138. if (empty($idcard)) {
  139. return null;
  140. }
  141. $bir = substr($idcard, 6, 8);
  142. $year = (int)substr($bir, 0, 4);
  143. $month = (int)substr($bir, 4, 2);
  144. $day = (int)substr($bir, 6, 2);
  145. return $year . "-" . $month . "-" . $day;
  146. }
  147. /**
  148. * 根据身份证号码计算年龄
  149. * author:xiaochuan
  150. * @param string $idcard 身份证号码
  151. * @return int $age
  152. */
  153. function get_age($idcard)
  154. {
  155. if (empty($idcard)) {
  156. return null;
  157. }
  158. # 获得出生年月日的时间戳
  159. $date = strtotime(substr($idcard, 6, 8));
  160. # 获得今日的时间戳
  161. $today = strtotime('today');
  162. # 得到两个日期相差的大体年数
  163. $diff = floor(($today - $date) / 86400 / 365);
  164. # strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
  165. $age = strtotime(substr($idcard, 6, 8) . ' +' . $diff . 'years') > $today ? ($diff + 1) : $diff;
  166. return $age;
  167. }
  168. /**
  169. * 根据身份证号码获取出身地址
  170. * author:xiaochuan
  171. * @param string $idcard 身份证号码
  172. * @return string $address
  173. */
  174. // function get_address($idcard, $type=1){
  175. // if(empty($idcard)) return null;
  176. // $address = include('./address.php');
  177. // switch ($type) {
  178. // case 1:
  179. // # 截取前六位数(获取基体到县区的地址)
  180. // $key = substr($idcard,0,6);
  181. // if(!empty($address[$key])) return $address[$key];
  182. // # 截取前两位数(没有基体到县区的地址就获取省份)
  183. // $key = substr($idcard,0,2);
  184. // if(!empty($address[$key])) return $address[$key];
  185. // # 都没有
  186. // return '未知地址';
  187. // break;
  188. // case 2:
  189. // # 截取前两位数(只获取省份)
  190. // $key = substr($idcard,0,2);
  191. // if(!empty($address[$key])) return $address[$key];
  192. // break;
  193. // default:
  194. // return null;
  195. // break;
  196. // }
  197. // }
  198. /**
  199. * 判断字符串是否是身份证号
  200. * author:xiaochuan
  201. * @param string $idcard 身份证号码
  202. */
  203. function isIdCard($idcard)
  204. {
  205. # 转化为大写,如出现x
  206. $idcard = strtoupper($idcard);
  207. # 加权因子
  208. $wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  209. $ai = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
  210. # 按顺序循环处理前17位
  211. $sigma = 0;
  212. # 提取前17位的其中一位,并将变量类型转为实数
  213. for ($i = 0; $i < 17; $i++) {
  214. $b = (int)$idcard[$i];
  215. # 提取相应的加权因子
  216. $w = $wi[$i];
  217. # 把从身份证号码中提取的一位数字和加权因子相乘,并累加
  218. $sigma += $b * $w;
  219. }
  220. # 计算序号
  221. $sidcard = $sigma % 11;
  222. # 按照序号从校验码串中提取相应的字符。
  223. $check_idcard = $ai[$sidcard];
  224. if ($idcard[17] == $check_idcard) {
  225. return true;
  226. } else {
  227. return false;
  228. }
  229. }
  230. /**
  231. * 根据身份证号,返回对应的生肖
  232. * author:xiaochuan
  233. * @param string $idcard 身份证号码
  234. */
  235. function get_zodiac($idcard)
  236. { //
  237. if (empty($idcard)) {
  238. return null;
  239. }
  240. $start = 1901;
  241. $end = (int)substr($idcard, 6, 4);
  242. $x = ($start - $end) % 12;
  243. $val = '';
  244. if ($x == 1 || $x == -11) {
  245. $val = '鼠';
  246. }
  247. if ($x == 0) {
  248. $val = '牛';
  249. }
  250. if ($x == 11 || $x == -1) {
  251. $val = '虎';
  252. }
  253. if ($x == 10 || $x == -2) {
  254. $val = '兔';
  255. }
  256. if ($x == 9 || $x == -3) {
  257. $val = '龙';
  258. }
  259. if ($x == 8 || $x == -4) {
  260. $val = '蛇';
  261. }
  262. if ($x == 7 || $x == -5) {
  263. $val = '马';
  264. }
  265. if ($x == 6 || $x == -6) {
  266. $val = '羊';
  267. }
  268. if ($x == 5 || $x == -7) {
  269. $val = '猴';
  270. }
  271. if ($x == 4 || $x == -8) {
  272. $val = '鸡';
  273. }
  274. if ($x == 3 || $x == -9) {
  275. $val = '狗';
  276. }
  277. if ($x == 2 || $x == -10) {
  278. $val = '猪';
  279. }
  280. return $val;
  281. }
  282. /**
  283. * 根据身份证号,返回对应的星座
  284. * author:xiaochuan
  285. * @param string $idcard 身份证号码
  286. */
  287. function get_starsign($idcard)
  288. {
  289. if (empty($idcard)) {
  290. return null;
  291. }
  292. $b = substr($idcard, 10, 4);
  293. $m = (int)substr($b, 0, 2);
  294. $d = (int)substr($b, 2);
  295. $val = '';
  296. if (($m == 1 && $d <= 21) || ($m == 2 && $d <= 19)) {
  297. $val = "水瓶座";
  298. } elseif (($m == 2 && $d > 20) || ($m == 3 && $d <= 20)) {
  299. $val = "双鱼座";
  300. } elseif (($m == 3 && $d > 20) || ($m == 4 && $d <= 20)) {
  301. $val = "白羊座";
  302. } elseif (($m == 4 && $d > 20) || ($m == 5 && $d <= 21)) {
  303. $val = "金牛座";
  304. } elseif (($m == 5 && $d > 21) || ($m == 6 && $d <= 21)) {
  305. $val = "双子座";
  306. } elseif (($m == 6 && $d > 21) || ($m == 7 && $d <= 22)) {
  307. $val = "巨蟹座";
  308. } elseif (($m == 7 && $d > 22) || ($m == 8 && $d <= 23)) {
  309. $val = "狮子座";
  310. } elseif (($m == 8 && $d > 23) || ($m == 9 && $d <= 23)) {
  311. $val = "处女座";
  312. } elseif (($m == 9 && $d > 23) || ($m == 10 && $d <= 23)) {
  313. $val = "天秤座";
  314. } elseif (($m == 10 && $d > 23) || ($m == 11 && $d <= 22)) {
  315. $val = "天蝎座";
  316. } elseif (($m == 11 && $d > 22) || ($m == 12 && $d <= 21)) {
  317. $val = "射手座";
  318. } elseif (($m == 12 && $d > 21) || ($m == 1 && $d <= 20)) {
  319. $val = "魔羯座";
  320. }
  321. return $val;
  322. }