phone.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. class Phone {
  2. //设备是否为android
  3. isAndroid() {
  4. console.log("成功进入isAndroid这个方法")
  5. var u = navigator.userAgent;
  6. if (u.indexOf('Android') > -1 || u.indexOf('Adr') > -1) {
  7. return true;
  8. }
  9. return false;
  10. }
  11. // 判断设备为 ios
  12. isIos() {
  13. console.log("成功进入isIos这个方法")
  14. var u = navigator.userAgent;
  15. if (u.indexOf("iPhone") > -1 || u.indexOf("iOS") > -1) {
  16. return true;
  17. }
  18. return false;
  19. }
  20. //获取设备的类型
  21. getSysType(): string {
  22. if (this.isAndroid()) {
  23. return "android"
  24. }
  25. if (this.isIos()) {
  26. return "ios"
  27. }
  28. return "web"
  29. }
  30. displayRow() {
  31. if (this.isIos()) {
  32. try {
  33. //@ts-ignore
  34. window.webkit.messageHandlers.displayRow.postMessage('displayRow')
  35. } catch (error) {
  36. console.log(error)
  37. }
  38. } else if (this.isAndroid()) {
  39. //@ts-ignore
  40. window.android && window.android.displayRow()
  41. }
  42. }
  43. displayCol() {
  44. if (this.isIos()) {
  45. try {
  46. //@ts-ignore
  47. window.webkit.messageHandlers.displayRow.postMessage('displayCol')
  48. } catch (error) {
  49. console.log(error)
  50. }
  51. } else if (this.isAndroid()) {
  52. //@ts-ignore
  53. window.android && window.android.displayCol()
  54. }
  55. }
  56. backView() {
  57. if (this.isIos()) {
  58. try {
  59. //@ts-ignore
  60. window.webkit.messageHandlers.backView.postMessage('backView')
  61. } catch (error) {
  62. console.log(error)
  63. }
  64. } else if (this.isAndroid()) {
  65. try {
  66. // alert('运行前')
  67. // console.log('运行前')
  68. // console.log(typeof window.android.backView)
  69. //@ts-ignore
  70. window.WebViewJavascriptBridge && window.WebViewJavascriptBridge.callHandler(
  71. 'backView'
  72. , { 'param': '返回的方法' }
  73. , function () {
  74. }
  75. );
  76. //@ts-ignore
  77. window.android && window.android.backView()
  78. // alert('运行后')
  79. } catch (error) {
  80. console.log(error)
  81. }
  82. }
  83. }
  84. //ios 内购的方法
  85. ipyCallgo() {
  86. if (this.isIos()) {
  87. try {
  88. //@ts-ignore eslint-disable-next-line
  89. window.webkit.messageHandlers.ipyCallgo.postMessage('ipyCallgo')
  90. //@ts-ignore
  91. document.getElementById('msg').textContent = '成功调用ipyCallgo'
  92. } catch (error) {
  93. if(document.getElementById('msg')){
  94. //@ts-ignore
  95. document.getElementById('msg').textContent = JSON.stringify(error)
  96. }
  97. console.error(error);
  98. }
  99. }
  100. }
  101. //微信购买的方法
  102. spyCallgo(universalLink: string) {
  103. if (this.isIos()) {
  104. try {
  105. //@ts-ignore
  106. window.webkit.messageHandlers.spyCallgo.postMessage(universalLink)
  107. } catch (error) {
  108. console.log(error)
  109. }
  110. }
  111. }
  112. //下单的接口错误
  113. errCallgo(code: string) {
  114. if (this.isIos()) {
  115. try {
  116. //@ts-ignore
  117. window.webkit.messageHandlers.errCallgo.postMessage(code)
  118. } catch (error) {
  119. console.log(error)
  120. }
  121. }
  122. }
  123. ladCallgo(){
  124. if (this.isIos()) {
  125. try {
  126. //@ts-ignore
  127. window.webkit.messageHandlers.ladCallgo.postMessage('n://wap/pay?')
  128. } catch (error) {
  129. console.log(error)
  130. }
  131. }
  132. }
  133. }
  134. export default Phone