test.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <view>
  3. <button class="bg-blue" @click="scancode()">扫码</button>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {}
  10. },
  11. onLoad() {
  12. // #ifdef H5
  13. let redirect_uri = location.href.split('#')[0]
  14. this.$http.get('你的接口', {
  15. sing_url: redirect_uri
  16. }).then(res => {
  17. this.wx_co(res.data)
  18. }).catch(e => {
  19. console.log('错误信息', e);
  20. });
  21. // #endif
  22. },
  23. methods: {
  24. wx_co: function(wx_co) {
  25. this.$wx.config({
  26. debug: false, // 开启调试模式
  27. appId: wx_co.appId, // 必填,公众号的唯一标识
  28. timestamp: wx_co.timestamp, // 必填,生成签名的时间戳
  29. nonceStr: wx_co.nonceStr, // 必填,生成签名的随机串
  30. signature: wx_co.signature, // 必填,签名,见附录1
  31. jsApiList: ['onMenuShareAppMessage', 'scanQRCode'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  32. });
  33. this.$wx.ready(function() { //需在用户可能点击分享按钮前就先调用
  34. this.$wx.checkJsApi({
  35. jsApiList: ['scanQRCode'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  36. success: function(res) {
  37. // 以键值对的形式返回,可用的api值true,不可用为false
  38. // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
  39. }
  40. });
  41. });
  42. this.$wx.error(function(res) {
  43. console.log(res, 'this.$wx.error') // config信息验证失败会执行error函数
  44. });
  45. },
  46. scancode: function() {
  47. alert(1);
  48. this.$wx.scanQRCode({
  49. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  50. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  51. success: function(res) {
  52. alert(2);
  53. var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
  54. }
  55. });
  56. }
  57. }
  58. }
  59. </script>
  60. <style lang="scss">
  61. </style>