getDeviceType.js 1.8 KB

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