Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

128 Zeilen
3.6 KiB

  1. import wx from '@/common/js/weixin-js-sdk.js';
  2. // 参考文档 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
  3. export default {
  4. methods: {
  5. data(){
  6. return {
  7. wxInit:false
  8. }
  9. },
  10. // 初始化wx
  11. initWx(arr = ["getLocation"]) {
  12. const promise = new Promise((resolve, reject) => {
  13. // let url = window.location.href
  14. let url = /(Android)/i.test(navigator.userAgent) ? location.href.split('#')[0] : window
  15. .localStorage
  16. .getItem('scanUrl')
  17. this.HTTP_GET("weixinapi/getweixinwebaccess_token?url=" + encodeURIComponent(url)).then((
  18. success) => {
  19. if (!success) {
  20. resolve(false)
  21. return
  22. }
  23. wx.config({
  24. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  25. appId: success.appid, // 必填,公众号的唯一标识
  26. timestamp: success.timestamp, // 必填,生成签名的时间戳
  27. nonceStr: success.noncestr, // 必填,生成签名的随机串
  28. signature: success.certificate, // 必填,签名
  29. jsApiList: arr // 必填,需要使用的JS接口列表
  30. });
  31. wx.ready(() => {
  32. this.wxInit = true
  33. // this.TOAST("wx初始化成功")
  34. resolve(true)
  35. })
  36. wx.error(() => {
  37. this.TOAST("wx初始化失败")
  38. resolve(false)
  39. })
  40. })
  41. })
  42. return promise
  43. },
  44. // 获取定位
  45. async getLocation() {
  46. return new Promise(async (resolve) => {
  47. if (!this.wxInit) {
  48. let res = await this.initWx(["getLocation"])
  49. if(!res){
  50. resolve(false)
  51. return
  52. }
  53. }
  54. if(!wx.getLocation){
  55. this.TOAST("获取定位失败")
  56. resolve(false)
  57. }
  58. wx.getLocation({
  59. type: "gcj02", // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  60. success: function(res) {
  61. var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
  62. var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
  63. var speed = res.speed; // 速度,以米/每秒计
  64. var accuracy = res.accuracy; // 位置精度
  65. resolve({
  66. lat:latitude,
  67. lng:longitude
  68. })
  69. },
  70. fail: function(error) {
  71. this.TOAST("获取定位失败!")
  72. resolve(false)
  73. }
  74. });
  75. })
  76. },
  77. // 拍照
  78. async chooseImage() {
  79. return new Promise(async (resolve) => {
  80. if (!this.wxInit) {
  81. let res = await this.initWx(["chooseImage"])
  82. if(!res){
  83. resolve(false)
  84. return
  85. }
  86. }
  87. if(!wx.chooseImage){
  88. this.TOAST("无效方法")
  89. resolve(false)
  90. }
  91. wx.chooseImage({
  92. count: 1, // 默认9
  93. sizeType: ['original'], // 可以指定是原图还是压缩图,默认二者都有
  94. sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有
  95. success: function(res) {
  96. var localIds = res.localIds;// 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
  97. resolve(localIds)
  98. },
  99. fail: function(error) {
  100. this.TOAST("定位失败")
  101. resolve(false)
  102. }
  103. });
  104. })
  105. },
  106. // 预览图片
  107. async previewImage(url,urlArr) {
  108. return new Promise(async (resolve) => {
  109. if (!this.wxInit) {
  110. let res = await this.initWx(["previewImage"])
  111. if(!res){
  112. resolve(false)
  113. return
  114. }
  115. }
  116. if(!wx.previewImage){
  117. this.TOAST("无效方法")
  118. resolve(false)
  119. }
  120. wx.previewImage({
  121. current: url, // 当前显示图片的http链接
  122. urls: urlArr // 需要预览的图片http链接列表
  123. });
  124. })
  125. },
  126. }
  127. }