test-1.vue 2.8 KB

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