index.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <view
  3. class="codeDetail"
  4. :style="{
  5. background: colorList[idx].bgColor,
  6. boxShadow: colorList[idx].shadow,
  7. }"
  8. >
  9. <capsule :showBorder="true" bgColor="transparent">
  10. <template v-slot:top>
  11. <view class="shop-top">
  12. <view class="back-icon" @click="backClick">
  13. <tui-icon name="arrowleft" :size="36" color="#FFFFFF"></tui-icon>
  14. </view>
  15. <view class="shop-name">{{ title }}</view>
  16. </view>
  17. </template>
  18. </capsule>
  19. <view class="code-container">
  20. <view class="top-img">
  21. <image class="" :src="shopInfo.shopLogo" />
  22. </view>
  23. <view class="code-img">
  24. <image class="" :src="payCode" :show-menu-by-longpress="true" />
  25. <canvas canvas-id="code" id="code"></canvas>
  26. <!-- <template v-if="idx == 1">
  27. </template>
  28. <template v-else>
  29. </template> -->
  30. </view>
  31. <view class="code-text">长按保存二维码</view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. import drawQrcode from "@/static/js/qrcode.js";
  37. export default {
  38. created() {
  39. // 获取本地info
  40. this.shopInfo = uni.getStorageSync("shopInfo");
  41. },
  42. onLoad(option) {
  43. this.idx = Number(option.type);
  44. switch (this.idx) {
  45. case 0:
  46. this.title = "商家店铺码";
  47. this.setCode(uni.getStorageSync("code"), "code");
  48. break;
  49. case 1:
  50. this.title = "商家收款码";
  51. this.payCode = uni.getStorageSync("code");
  52. break;
  53. case 2:
  54. this.title = "兑换专区码";
  55. this.setCode(uni.getStorageSync("code"), "code");
  56. break;
  57. }
  58. },
  59. data() {
  60. return {
  61. idx: 0,
  62. title: "",
  63. shopInfo: {},
  64. colorList: [
  65. {
  66. bgColor: "linear-gradient(90deg, #24c4ef 0%, #46a8f7 100%)",
  67. shadow: "0px 0px 10px 0px rgba(68, 169, 246, 0.5)",
  68. },
  69. {
  70. bgColor: "linear-gradient(90deg, #FC8756 0%, #FE5427 100%)",
  71. shadow: "0px 0px 10px 0px rgba(68, 169, 246, 0.5)",
  72. },
  73. {
  74. bgColor: "linear-gradient(90deg, #7ADBCF 0%, #47CAA4 100%)",
  75. shadow: "0px 0px 10px 0px rgba(68, 169, 246, 0.5)",
  76. },
  77. ],
  78. payCode: "",
  79. };
  80. },
  81. methods: {
  82. // 生成二维码
  83. async setCode(url, id) {
  84. this.$loading.show("生成中...");
  85. await drawQrcode({
  86. width: 202,
  87. height: 202,
  88. canvasId: id,
  89. text: url,
  90. callback: (e) => {
  91. uni.canvasToTempFilePath({
  92. canvasId: "code",
  93. success: (res) => {
  94. console.log("成功", res);
  95. this.getImage(res.tempFilePath);
  96. },
  97. fail: (res) => {
  98. console.log("失败", res);
  99. },
  100. });
  101. },
  102. });
  103. },
  104. // 图片上传接口
  105. async getImage(file) {
  106. // 加载状态
  107. uni.uploadFile({
  108. // url: "https://nsbusinessapi.tuanfengkeji.cn/file/upload", //仅为示例,非真实的接口地址
  109. url: `${process.env.UNI_BASE_URL}/file/upload`, //仅为示例,非真实的接口地址
  110. filePath: file,
  111. header: {
  112. "Content-Type": "application/json; charset=UTF-8",
  113. "Authorization-Business": uni.getStorageSync("storage_key"),
  114. },
  115. name: "file",
  116. formData: {
  117. user: "code",
  118. },
  119. success: (uploadFileRes) => {
  120. let res = JSON.parse(uploadFileRes.data);
  121. const tokenerr = [20003, "20003", 20004, "20004", 20005, "20005"];
  122. if (tokenerr.includes(res.code)) {
  123. // 清除本地缓存所有的数据
  124. // uni.clearStorageSync();
  125. setTimeout(() => {
  126. // 跳转回登陆页面
  127. uni.redirectTo({
  128. url: "/pages/login/index"
  129. });
  130. }, 1500);
  131. this.$showToast("登陆超时,请重新登陆", "none", 1500);
  132. this.$loading.hide();
  133. return;
  134. }
  135. this.payCode = res.data.url;
  136. this.$loading.hide();
  137. },
  138. });
  139. },
  140. backClick() {
  141. uni.navigateBack();
  142. },
  143. },
  144. // 页面卸载清除缓存
  145. onUnload() {
  146. uni.removeStorageSync("code");
  147. },
  148. };
  149. </script>
  150. <style lang="scss" scoped>
  151. .codeDetail {
  152. width: 100vw;
  153. height: 100vh;
  154. position: relative;
  155. .shop-top {
  156. width: 100%;
  157. height: 100%;
  158. position: relative;
  159. z-index: 999;
  160. @include flex(center);
  161. .back-icon {
  162. position: absolute;
  163. left: 0;
  164. top: 50%;
  165. transform: translateY(-50%);
  166. }
  167. .shop-name {
  168. color: #ffffff;
  169. }
  170. }
  171. .code-container {
  172. margin: 0 auto;
  173. margin-top: 186rpx;
  174. border-radius: 32rpx;
  175. width: 606rpx;
  176. height: 724rpx;
  177. background-color: #ffffff;
  178. position: relative;
  179. .top-img {
  180. width: 144rpx;
  181. height: 144rpx;
  182. position: absolute;
  183. left: 50%;
  184. transform: translateX(-50%);
  185. top: -72rpx;
  186. image {
  187. width: 100%;
  188. height: 100%;
  189. border-radius: 50%;
  190. border: 10rpx solid #ffffff;
  191. }
  192. }
  193. .code-img {
  194. width: 404rpx;
  195. height: 404rpx;
  196. position: absolute;
  197. top: 128rpx;
  198. left: 52%;
  199. transform: translateX(-50%);
  200. image {
  201. width: 100%;
  202. height: 100%;
  203. display: block;
  204. position: relative;
  205. z-index: 1;
  206. }
  207. canvas {
  208. width: 100%;
  209. height: 100%;
  210. z-index: -2;
  211. position: fixed;
  212. top: 1000rpx;
  213. left: 1000rpx;
  214. }
  215. }
  216. .code-text {
  217. color: #666666;
  218. font-size: 28rpx;
  219. position: absolute;
  220. bottom: 32rpx;
  221. left: 50%;
  222. transform: translateX(-50%);
  223. }
  224. }
  225. }
  226. </style>