getDeviceType.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. export default {
  2. mounted() {
  3. this.getDeviceType()
  4. },
  5. data() {
  6. return {
  7. // 设备类型pc,ios,android
  8. deviceType: 'pc'
  9. }
  10. },
  11. methods: {
  12. /**
  13. * 设备判断
  14. */
  15. getDeviceType() {
  16. const strCookie = document.cookie;
  17. console.log('all cookie:' + strCookie)
  18. //将多cookie切割为多个名/值对
  19. const arrCookie = strCookie.split("; ");
  20. let xAPP = "";
  21. //遍历cookie数组,处理每个cookie对
  22. for(let i=0; i<arrCookie.length; i++){
  23. const arr = arrCookie[i].split("=");
  24. //找到名称为x_app的cookie,并返回它的值
  25. if ("x_app" === arr[0]){
  26. xAPP = arr[1];
  27. break;
  28. }
  29. }
  30. console.log('x_app:' +xAPP)
  31. if (xAPP.toLowerCase().indexOf('ios') !== -1) { //判断iPhone|iPad|iPod|iOS APP
  32. this.deviceType = 'ios';
  33. } else if (xAPP.toLowerCase().indexOf('android') !== -1) { //判断Android app
  34. this.deviceType = 'android';
  35. } else { // pc OR H5 todo 判断UA,精准识别是H5还是PC
  36. this.deviceType = 'pc';
  37. }
  38. },
  39. /**
  40. * get sign for iOS
  41. */
  42. getSign() {
  43. const sign = {
  44. // x_app: 'ios 4.6.0',
  45. x_nonce_str: '1558105441627',
  46. x_signature: 'h5'
  47. };
  48. if (document && document.cookie) {
  49. document.cookie.split(/; ?/).forEach(item => {
  50. if (item) {
  51. const it = item.split('=');
  52. if (it[0] === 'x_app' || it[0] === 'x_access_token') {
  53. sign[it[0]] = it[1];
  54. }
  55. }
  56. });
  57. }
  58. return sign;
  59. },
  60. /**
  61. * get cookie
  62. */
  63. getCookie() {
  64. const cookie = {};
  65. if (document && document.cookie) {
  66. document.cookie.split(/; ?/).forEach(item => {
  67. if (item) {
  68. const it = item.split('=');
  69. cookie[it[0]] = it[1];
  70. }
  71. });
  72. }
  73. return cookie;
  74. }
  75. }
  76. }