js获取手机信息

时间:2023-03-09 00:26:29
js获取手机信息
  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script src="js/jquery-2.1.4.min.js"></script>
  7. <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
  8. <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
  9. </head>
  10. <body>
  11. <script language="javascript" type="text/javascript" src="http://pv.sohu.com/cityjson?ie=utf-8">
  12. </script>
  13. <script>
  14. var map;
  15. var gpsPoint;
  16. var baiduPoint;
  17. var gpsAddress;
  18. var baiduAddress;
  19. //ip ip地址 字符串
  20. //time 时间 单位秒
  21. //os 操作系统版本
  22. //platform 平台 android or ios
  23. //city 城市
  24. //location = {lng:XX,lnt:XX} 格式
  25. //brand 手机品牌
  26. /* var ip="192.168.1.24";//测试数据
  27. var time="2015/12/15 下午13:11";
  28. var os="android 4.4.3";
  29. var platform="android";
  30. var city="成都";
  31. var brand="DOA";
  32. var lng="250";
  33. var lnt="140";*/
  34. var ip;
  35. var time;
  36. var os;
  37. var platform;
  38. var city;
  39. var brand;
  40. var lng;
  41. var lnt;
  42. function getLocation() {
  43. String.prototype.trim = function () {
  44. return this.replace(/(^\s*)|(\s*$)/g, '');
  45. }
  46. var test1 = navigator.userAgent.toLocaleString().split("(");
  47. var test2 = test1[1].split(")")[0].split(";");
  48. if (test2[0] == "iPhone") {
  49. platform = "ios";
  50. brand = test2[0].trim();
  51. os = test2[1].split(" ")[3] + " " + test2[1].split(" ")[4].trim();
  52. } else {
  53. if (test1.length > 2) {
  54. var test2 = test1[1].split(")")[0].split(";");
  55. if (test2.length == 5) {
  56. os = test2[2].trim();
  57. //alert(test2[4].split(" ").length)
  58. platform = test2[2].split(" ")[1];
  59. //if(test2[4].split(" ")[0]==" "){
  60. //test2[4].split(" ")[1];
  61. //}
  62. if (test2[4].split(" ").length == 4) {
  63. brand = (test2[4].split(" ")[1] + " " + test2[4].split(" ")[2]).trim();
  64. } else {
  65. brand = (test2[4].split(" ")[0] + " " + test2[4].split(" ")[1]).trim();
  66. }
  67. }
  68. }
  69. }
  70. //时间格式化的方法
  71. /* Date.prototype.Format = function (fmt) { //author: meizz
  72. var o = {
  73. "M+": this.getMonth() + 1, //月份
  74. "d+": this.getDate(), //日
  75. "h+": this.getHours(), //小时
  76. "m+": this.getMinutes(), //分
  77. "s+": this.getSeconds(), //秒
  78. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  79. "S": this.getMilliseconds() //毫秒
  80. };
  81. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  82. for (var k in o)
  83. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  84. return fmt;
  85. }
  86. time =  new Date().Format("yyyy-MM-dd hh:mm");*/
  87. var d = new Date();
  88. var sumS = d.getTime();
  89. time = sumS;
  90. //根据IP获取城市
  91. var myCity = new BMap.LocalCity();
  92. myCity.get(getCityByIP);
  93. /* alert("当前时间为" + myDate.toLocaleString());
  94. alert("操作系统版本" + navigator.platform)
  95. alert("设备其他信息:" + navigator.userAgent);*/
  96. }
  97. function showMap(value) {
  98. var longitude = value.coords.longitude;
  99. var latitude = value.coords.latitude;
  100. map = new BMap.Map("map");
  101. lng = longitude;
  102. lnt = latitude;
  103. gpsPoint = new BMap.Point(longitude, latitude);    // 创建点坐标
  104. map.centerAndZoom(gpsPoint, 15);
  105. //根据坐标逆解析地址
  106. var geoc = new BMap.Geocoder();
  107. geoc.getLocation(gpsPoint, getCityByCoordinate);
  108. BMap.Convertor.translate(gpsPoint, 0, translateCallback);
  109. /*  alert('ip' + ip);
  110. alert("时间" + time);
  111. alert('城市' + city);
  112. alert('操作系统' + os);
  113. alert('平台' + platform);
  114. alert("平台长度"+platform.length);
  115. alert('设备' + brand);
  116. alert('经度' + lnt)
  117. alert('纬度' + lng)*/
  118. $.ajax({
  119. type: "get",
  120. async: false,
  121. url: "http://192.168.1.34:27017" +
  122. "?ip="
  123. + encodeURIComponent(ip)
  124. + "&time=" + encodeURIComponent(time)
  125. + "&os=" + encodeURIComponent(os)
  126. + "&platform=" + encodeURIComponent(platform)
  127. + "&city=" + encodeURIComponent(city)
  128. + "&brand=" + encodeURIComponent(brand)
  129. + "&lng=" + encodeURIComponent(lng)
  130. + "&lnt=" + encodeURIComponent(lnt),
  131. dataType: "jsonp",
  132. jsonpCallback: "jsonpCallback",
  133. scriptCharset: 'UTF-8',
  134. success: function (data) {
  135. alert("测试成功")
  136. window.close();
  137. console.log(data);
  138. },
  139. error: function () {
  140. alert("测试成功")
  141. window.opener = null;
  142. window.open('', '_self');
  143. window.close();
  144. console.log('fail');
  145. }
  146. });
  147. function jsonpCallback(data) {
  148. alert("测试成功")
  149. window.close();
  150. console.log(data)
  151. }
  152. }
  153. translateCallback = function (point) {
  154. baiduPoint = point;
  155. var geoc = new BMap.Geocoder();
  156. geoc.getLocation(baiduPoint, getCityByBaiduCoordinate);
  157. }
  158. function getCityByCoordinate(rs) {
  159. gpsAddress = rs.addressComponents;
  160. var address = "GPS标注:" + gpsAddress.province + "," + gpsAddress.city + "," + gpsAddress.district + "," + gpsAddress.street + "," + gpsAddress.streetNumber;
  161. var marker = new BMap.Marker(gpsPoint);  // 创建标注
  162. /* map.addOverlay(marker);              // 将标注添加到地图中*/
  163. var labelgps = new BMap.Label(address, { offset: new BMap.Size(20, -10) });
  164. /* marker.setLabel(labelgps); //添加GPS标注*/
  165. }
  166. function getCityByBaiduCoordinate(rs) {
  167. baiduAddress = rs.addressComponents;
  168. var address = "百度标注:" + baiduAddress.province + "," + baiduAddress.city + "," + baiduAddress.district + "," + baiduAddress.street + "," + baiduAddress.streetNumber;
  169. /* alert("当前位置为" + address + "附近");*/
  170. var marker = new BMap.Marker(baiduPoint);  // 创建标注
  171. map.addOverlay(marker);              // 将标注添加到地图中
  172. var labelbaidu = new BMap.Label(address, { offset: new BMap.Size(20, -10) });
  173. marker.setLabel(labelbaidu); //添加百度标注
  174. }
  175. //根据IP获取城市
  176. function getCityByIP(rs) {
  177. var cityName = rs.name;
  178. ip = returnCitySN.cip;
  179. city = cityName;
  180. //获取GPS坐标
  181. if (navigator.geolocation) {
  182. navigator.geolocation.getCurrentPosition(showMap, handleError, { enableHighAccuracy: true, maximumAge: 1000 });
  183. } else {
  184. alert("您的浏览器不支持使用HTML 5来获取地理位置服务");
  185. window.close();
  186. }
  187. var options = {
  188. enableHighAccuracy: true,
  189. maximumAge: 10
  190. }
  191. }
  192. function handleError(value) {
  193. switch (value.code) {
  194. case 1:
  195. alert("位置服务被拒绝");
  196. break;
  197. case 2:
  198. alert("暂时获取不到位置信息");
  199. break;
  200. case 3:
  201. alert("获取信息超时");
  202. break;
  203. case 4:
  204. alert("未知错误");
  205. break;
  206. }
  207. window.close();
  208. }
  209. function init() {
  210. getLocation();
  211. }
  212. window.onload = init;
  213. </script>
  214. <div id="map"></div>
  215. </body>
  216. </html>