test-1.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. uni.request({
  15. url: "https://qhome.usky.cn/USKYZHAF/sign.php",
  16. header: {
  17. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  18. },
  19. data: {
  20. "url": redirect_uri
  21. },
  22. method: 'GET',
  23. success: (res) => {
  24. let apiList = [ // 可能需要用到的能力 需要啥就写啥。多写也没有坏处
  25. 'openLocation',
  26. 'getLocation',
  27. 'scanQRCode'
  28. ];
  29. let info = {
  30. debug: true, // 调试,发布的时候改为false
  31. appId: res.data.appid,
  32. nonceStr: res.data.nonceStr,
  33. timestamp: parseInt(res.data.timestamp),
  34. signature: res.data.sha_str,
  35. jsApiList: apiList
  36. };
  37. this.wx_co(info)
  38. }
  39. });
  40. // #endif
  41. },
  42. methods: {
  43. wx_co: function(wx_co) {
  44. this.$wx.config({
  45. debug: false, // 开启调试模式
  46. appId: wx_co.appId, // 必填,公众号的唯一标识
  47. timestamp: wx_co.timestamp, // 必填,生成签名的时间戳
  48. nonceStr: wx_co.nonceStr, // 必填,生成签名的随机串
  49. signature: wx_co.signature, // 必填,签名,见附录1
  50. jsApiList: ['onMenuShareAppMessage', 'scanQRCode'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  51. });
  52. this.$wx.ready(function() { //需在用户可能点击分享按钮前就先调用
  53. this.$wx.checkJsApi({
  54. jsApiList: ['scanQRCode'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  55. success: function(res) {
  56. // 以键值对的形式返回,可用的api值true,不可用为false
  57. // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
  58. }
  59. });
  60. });
  61. this.$wx.error(function(res) {
  62. console.log(res, 'this.$wx.error') // config信息验证失败会执行error函数
  63. });
  64. },
  65. scancode: function() {
  66. alert(1);
  67. this.$wx.scanQRCode({
  68. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  69. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  70. success: function(res) {
  71. setTimeout(function(){
  72. /* 放200ms后执行的代码 */
  73. alert(2)
  74. },1000)
  75. var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
  76. }
  77. });
  78. }
  79. }
  80. }
  81. </script>
  82. <style lang="scss">
  83. </style>