You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

67 lines
2.0 KiB

  1. <template>
  2. <view></view>
  3. </template>
  4. <script>
  5. import wx from '@/common/js/weixin-js-sdk.js';
  6. export default {
  7. name:"scanCode",
  8. props:{},
  9. data() {
  10. return {
  11. scanCodeReady:false,
  12. };
  13. },
  14. mounted() {
  15. this.setConfig()
  16. },
  17. methods: {
  18. setConfig() {
  19. const promise = new Promise((resolve, reject) => {
  20. this.HTTP_GET("weixinapi/getweixinwebaccess_token?url=" + encodeURIComponent(window.location.href)).then((success)=>{
  21. if(!success){
  22. resolve(false)
  23. return
  24. }
  25. wx.config({
  26. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  27. appId: success.appid, // 必填,公众号的唯一标识
  28. timestamp: success.timestamp, // 必填,生成签名的时间戳
  29. nonceStr: success.noncestr, // 必填,生成签名的随机串
  30. signature: success.certificate, // 必填,签名
  31. jsApiList: ["scanQRCode"] // 必填,需要使用的JS接口列表
  32. });
  33. wx.ready(() => {
  34. this.scanCodeReady = true
  35. })
  36. })
  37. })
  38. return promise
  39. },
  40. scanCode(){
  41. return new Promise((resolve)=>{
  42. if(!this.scanCodeReady){
  43. resolve(false)
  44. }
  45. wx.scanQRCode({
  46. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  47. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  48. success: function (res) {
  49. var result = res.resultStr; // 当 needResult 为 1 时,扫码返回的结果
  50. var resultArr = result.split(','); // 扫描结果以逗号分割数组
  51. var codeContent = resultArr[resultArr.length - 1]; // 获取数组最后一个元素,也就是最终的内容
  52. resolve({result:codeContent})
  53. },
  54. fail: function (res) {
  55. this.TOAST("调用扫码失败")
  56. resolve(false)
  57. }
  58. });
  59. })
  60. }
  61. },
  62. }
  63. </script>
  64. <style>
  65. </style>