index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. filePath: file,
  110. header: {
  111. "Content-Type": "application/json; charset=UTF-8",
  112. "Authorization-Business": uni.getStorageSync("storage_key"),
  113. },
  114. name: "file",
  115. formData: {
  116. user: "code",
  117. },
  118. success: (uploadFileRes) => {
  119. this.payCode = JSON.parse(uploadFileRes.data).data.url;
  120. this.$loading.hide();
  121. },
  122. });
  123. },
  124. backClick() {
  125. uni.navigateBack();
  126. },
  127. },
  128. // 页面卸载清除缓存
  129. onUnload() {
  130. uni.removeStorageSync("code");
  131. },
  132. };
  133. </script>
  134. <style lang="scss" scoped>
  135. .codeDetail {
  136. width: 100vw;
  137. height: 100vh;
  138. position: relative;
  139. .shop-top {
  140. width: 100%;
  141. height: 100%;
  142. position: relative;
  143. z-index: 999;
  144. @include flex(center);
  145. .back-icon {
  146. position: absolute;
  147. left: 0;
  148. top: 50%;
  149. transform: translateY(-50%);
  150. }
  151. .shop-name {
  152. color: #ffffff;
  153. }
  154. }
  155. .code-container {
  156. margin: 0 auto;
  157. margin-top: 186rpx;
  158. border-radius: 32rpx;
  159. width: 606rpx;
  160. height: 724rpx;
  161. background-color: #ffffff;
  162. position: relative;
  163. .top-img {
  164. width: 144rpx;
  165. height: 144rpx;
  166. position: absolute;
  167. left: 50%;
  168. transform: translateX(-50%);
  169. top: -72rpx;
  170. image {
  171. width: 100%;
  172. height: 100%;
  173. border-radius: 50%;
  174. border: 10rpx solid #ffffff;
  175. }
  176. }
  177. .code-img {
  178. width: 404rpx;
  179. height: 404rpx;
  180. position: absolute;
  181. top: 128rpx;
  182. left: 52%;
  183. transform: translateX(-50%);
  184. image {
  185. width: 100%;
  186. height: 100%;
  187. display: block;
  188. position: relative;
  189. z-index: 1;
  190. }
  191. canvas {
  192. width: 100%;
  193. height: 100%;
  194. z-index: -2;
  195. position: fixed;
  196. top: 1000rpx;
  197. left: 1000rpx;
  198. }
  199. }
  200. .code-text {
  201. color: #666666;
  202. font-size: 28rpx;
  203. position: absolute;
  204. bottom: 32rpx;
  205. left: 50%;
  206. transform: translateX(-50%);
  207. }
  208. }
  209. }
  210. </style>