index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <view class="body body-bg">
  3. <view class="main uni-column">
  4. <text class="shopname">{{shopInfo.shopName}}</text>
  5. <text class="button" @click="btnScanQR">扫码兑换</text>
  6. <text class="button" @click="btnExchgRecord">兑换记录</text>
  7. <!-- <text class="">{{qrCode}}</text> -->
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import tools from '/common/tools';
  13. import {
  14. apiVerfExchange,
  15. apiGetWxJsSdkConfig,
  16. checkResCode
  17. } from '../../common/api.js';
  18. import wx from 'weixin-js-sdk';
  19. export default {
  20. data() {
  21. return {
  22. token: "",
  23. wxcode: "",
  24. wxconfig: {},
  25. shopInfo: {
  26. shopName: ""
  27. },
  28. qrCode: ""
  29. }
  30. },
  31. onLoad() {
  32. this.token = tools.getToken();
  33. // console.log("token:", this.token);
  34. tools.checkToken(this.token);
  35. this.shopInfo = tools.getShopInfo();
  36. // console.log("shopInfo:", this.shopInfo);
  37. this.getWxJsSdkConfig();
  38. },
  39. methods: {
  40. // 获取微信JsConfig
  41. getWxJsSdkConfig() {
  42. let that = this;
  43. var url = location.href.split('#')[0]; //获取当前网页的url
  44. // console.log("url ", url);
  45. uni.request({
  46. url: apiGetWxJsSdkConfig,
  47. header: {
  48. "Content-Type": "application/x-www-form-urlencoded",
  49. },
  50. method: "POST",
  51. data: {
  52. url: url
  53. },
  54. success: (res) => {
  55. // console.log("getWxJsSdkConfig", res);
  56. that.wxconfig = res.data.data;
  57. wx.config({
  58. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  59. appId: that.wxconfig.appId, // 必填,公众号的唯一标识
  60. timestamp: that.wxconfig.timestamp, // 必填,生成签名的时间戳
  61. nonceStr: that.wxconfig.nonceStr, // 必填,生成签名的随机串
  62. signature: that.wxconfig.signature, // 必填,签名
  63. jsApiList: ['checkJsApi', 'scanQRCode'] // 必填,需要使用的JS接口列表
  64. });
  65. },
  66. fail: (err) => {
  67. console.log("getWxJsSdkConfig err", err);
  68. },
  69. });
  70. },
  71. btnScanQR() {
  72. try {
  73. let that = this;
  74. wx.ready(function() {
  75. wx.scanQRCode({
  76. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  77. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  78. success: function(res) {
  79. console.log("scanQRCode result: ", res);
  80. that.qrCode = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
  81. that.verfExchange();
  82. }
  83. });
  84. });
  85. wx.error(function(res) {
  86. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  87. console.log("wx.error:", res);
  88. });
  89. } catch (e) {
  90. console.log("btnScanQR err:", e.message);
  91. }
  92. },
  93. // 商家系统核销
  94. verfExchange() {
  95. uni.request({
  96. url: apiVerfExchange,
  97. header: {
  98. "Content-Type": "application/x-www-form-urlencoded",
  99. "token": this.token,
  100. },
  101. method: "POST",
  102. data: {
  103. qrCode: this.qrCode
  104. },
  105. success: (res) => {
  106. console.log("verfExchange", res);
  107. if (checkResCode(res)) {
  108. uni.showToast({
  109. icon: "success",
  110. title: "核销成功",
  111. duration: 2000
  112. });
  113. }
  114. },
  115. fail: (err) => {
  116. console.log("verfExchange err", err);
  117. uni.showToast({
  118. icon: "error",
  119. title: "核销失败",
  120. duration: 3000
  121. });
  122. },
  123. });
  124. },
  125. btnExchgRecord() {
  126. uni.navigateTo({
  127. url: "/pages/exchgRecord/exchgRecord"
  128. })
  129. }
  130. }
  131. }
  132. </script>
  133. <style scoped>
  134. .body-bg {
  135. width: 100vw;
  136. height: 100vh;
  137. background: url("/static/bg_index.png") no-repeat;
  138. background-size: 100% 100%;
  139. }
  140. .main {
  141. width: 90%;
  142. height: 60%;
  143. justify-content: space-evenly;
  144. }
  145. .shopname {
  146. font-weight: 500;
  147. color: #000000;
  148. font-size: 30px;
  149. text-align: center;
  150. }
  151. .button {
  152. width: 230px;
  153. height: 80px;
  154. background: linear-gradient(180deg, #ffffff 0%, #ebebeb 71.96%, #ffffff 100%);
  155. border: 1px solid;
  156. border-color: #d8d8d8;
  157. border-radius: 42px;
  158. box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16);
  159. text-align: center;
  160. line-height: 80px;
  161. font-weight: 500;
  162. color: #000000;
  163. font-size: 29px;
  164. }
  165. </style>