index.vue 5.5 KB

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