index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <template>
  2. <div class="storeCode">
  3. <div class="paymentCode-left">
  4. <div class="paymentCode-title">店铺兑换商品二维码</div>
  5. <template v-if="canvasImg == ''">
  6. <!-- v-if="canvasImg == ''" -->
  7. <div ref="paymentCodeCanvas" id="paymentCodeCanvas">
  8. <div class="paymentCode-img" ref="businessCardCanvas" id="businessCardCanvas">
  9. <div class="paymentCode-img-live">兑换专区</div>
  10. <div class="paymentCode-img-title">扫码兑换商品</div>
  11. <div class="img-box">
  12. <img :src="codeInfo.shopCode" alt="">
  13. </div>
  14. <div class="img-button">{{ codeInfo.shopName }}</div>
  15. </div>
  16. </div>
  17. </template>
  18. <template v-else>
  19. <div class="paymentCode-img-box">
  20. <el-image style="width: 463px; height: 595px" :src="canvasImg" :preview-src-list="srcList">
  21. </el-image>
  22. </div>
  23. </template>
  24. <div class="paymentCode-open">
  25. <div class="open-icon">
  26. <img src="@/assets/images/scan.png" alt="">
  27. </div>
  28. <div class="open-text">打开微信扫一扫即可进入兑换专区</div>
  29. </div>
  30. <div class="paymentCode-btn" @click="downloadCode">下载兑换二维码</div>
  31. </div>
  32. </div>
  33. </template>
  34. <script>
  35. import { shopSysGetById } from "@/api/shopSys"
  36. import QRCode from 'qrcode';
  37. // 引入 cookie 的库
  38. import Cookies from 'js-cookie'
  39. import html2canvas from 'html2canvas'
  40. export default {
  41. created() {
  42. this.generateCode()
  43. },
  44. data() {
  45. return {
  46. codeInfo: {
  47. shopName: '',
  48. shopCode: ''
  49. },
  50. canvasImg: '',
  51. srcList: [],
  52. // 支持支付方式数据
  53. payList: [
  54. {
  55. url: require('@/assets/images/mode-img/1.png'),
  56. text: '团蜂佣金'
  57. },
  58. {
  59. url: require('@/assets/images/mode-img/2.png'),
  60. text: '团蜂代金券'
  61. },
  62. {
  63. url: require('@/assets/images/mode-img/3.png'),
  64. text: '建行支付'
  65. },
  66. {
  67. url: require('@/assets/images/mode-img/4.png'),
  68. text: '团蜂消费金'
  69. },
  70. {
  71. url: require('@/assets/images/mode-img/5.png'),
  72. text: '微信支付'
  73. },
  74. {
  75. url: require('@/assets/images/mode-img/6.png'),
  76. text: '支付宝'
  77. },
  78. ],
  79. // 没有 logo 的二维码图片
  80. qCodeImg: '',
  81. shopLogo: ''
  82. }
  83. },
  84. // updated() {
  85. // },
  86. methods: {
  87. // 生成二维码
  88. async setQRcode() {
  89. // https://www.tuanfengkeji.cn/TFShop_Uni_H5/#/another-tf/another-user/shop/shop-detail?shopId=494
  90. // 获取本地存储的商家 id
  91. let shopId = Cookies.get('shopID');
  92. // let urlCode = `https://www.tuanfengkeji.cn/TFShop_Uni_H5/#/another-tf/another-user/exchange/index?shopId=${shopId}&shopName=${this.codeInfo.shopName}`
  93. // let urlCode = `http://192.168.0.151:8988/TFShop_Uni_H5/#/another-tf/another-user/exchange/index?shopId=${shopId}&shopName=${this.codeInfo.shopName}`
  94. let urlCode = ""
  95. if(process.env.NODE_ENV == "production"){
  96. urlCode = `https://www.tuanfengkeji.cn/TFShop_Uni_H5/#/another-tf/another-user/exchange/index?shopId=${shopId}&shopName=${this.codeInfo.shopName}`
  97. // urlCode = `https://www.tuanfengkeji.cn/TFShop_Uni_H5/#/another-tf/another-user/exchange/index?shopId=491&shopName=测试小店`
  98. }else{
  99. urlCode = `https://test.tuanfengkeji.cn/TFShop_Uni_H5/#/another-tf/another-user/exchange/index?shopId=${shopId}&shopName=${this.codeInfo.shopName}`
  100. }
  101. // let urlCode = `https://test.tuanfengkeji.cn/TFShop_Uni_H5/#/another-tf/another-user/exchange/index?shopId=${shopId}&shopName=${this.codeInfo.shopName}`
  102. // 根据 qcode 生成二维码链接
  103. // let imgCode = await QRCode.toDataURL(`https://www.tuanfengkeji.cn/TFShop_Uni_H5/#/another-tf/another-user/shop/shop-detail?shopId=${shopId}`)
  104. // console.log(imgCode);
  105. const qrCodeCanvas = document.createElement('canvas');
  106. qrCodeCanvas.width = 300; // 根据需要调整二维码尺寸
  107. qrCodeCanvas.height = 300; // 根据需要调整二维码尺寸
  108. await QRCode.toCanvas(qrCodeCanvas, urlCode);
  109. const ctx = qrCodeCanvas.getContext('2d');
  110. const logo = new Image();
  111. // 本地头像
  112. logo.src = this.shopLogo || 'https://jufeng-shop-1317254189.cos.ap-guangzhou.myqcloud.com/1722073118502-微信图片_20230511154105.jpg';
  113. // 解决跨域 重要
  114. logo.setAttribute('crossOrigin', 'anonymous');
  115. console.log(logo);
  116. logo.onload = () => {
  117. console.log("进来2");
  118. const logoSize = qrCodeCanvas.width * 0.26; // 根据需要调整logo尺寸
  119. const logoX = (qrCodeCanvas.width - logoSize) / 2;
  120. const logoY = (qrCodeCanvas.height - logoSize) / 2;
  121. // 绘制二维码
  122. ctx.drawImage(logo, logoX, logoY, logoSize, logoSize);
  123. // 将生成的二维码插入到页面中
  124. this.codeInfo.shopCode = qrCodeCanvas.toDataURL();
  125. this.$nextTick(() => {
  126. this.generateImage()
  127. })
  128. };
  129. },
  130. async generateCode() {
  131. const res = await shopSysGetById({})
  132. this.codeInfo.shopName = res.data.shopName
  133. this.shopLogo = res.data.shopLogo
  134. // 生成 qCode 二维码
  135. this.setQRcode()
  136. },
  137. generateImage() {
  138. html2canvas(this.$refs.paymentCodeCanvas, {
  139. dpi: 300,
  140. useCORS: false,
  141. allowTaint: true,
  142. logging: false,
  143. scale: 1,
  144. windowHeight: this.$refs.businessCardCanvas?.offsetHeight,
  145. width: this.$refs.businessCardCanvas?.offsetWidth,
  146. height: this.$refs.businessCardCanvas?.offsetHeight
  147. }).then((canvas) => {
  148. console.log("执行到这里了9999");
  149. this.canvasImg = canvas.toDataURL('image/png');
  150. // console.log(this.canvasImg)
  151. this.srcList.push(this.canvasImg)
  152. }).catch(() => {
  153. console.log("生成图片错误");
  154. });
  155. },
  156. downloadCode() {
  157. this.$confirm('您确定需要下载商家二维码吗?', '提示', {
  158. confirmButtonText: '确定',
  159. cancelButtonText: '取消',
  160. type: 'warning'
  161. }).then(() => {
  162. this.setImgSize(463, 595)
  163. }).catch(() => {
  164. this.$message({
  165. type: 'info',
  166. message: '取消下载成功'
  167. });
  168. });
  169. },
  170. // 设置图片大小
  171. setImgSize(width, height) {
  172. const canvas = document.createElement('canvas')
  173. const ctx = canvas.getContext('2d')
  174. // 设置canvas尺寸
  175. canvas.width = width
  176. canvas.height = height
  177. // 绘制图片到canvas上
  178. const img = new Image()
  179. img.src = this.canvasImg
  180. img.onload = () => {
  181. ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
  182. // 将canvas内容转换为base64格式的图片数据
  183. const imageData = canvas.toDataURL('image/png')
  184. // 创建下载链接并设置文件名
  185. const filename = `${this.codeInfo.shopName}的店铺二维码.png`
  186. const link = document.createElement('a')
  187. link.href = imageData
  188. link.download = filename
  189. // 将下载链接添加到页面并点击下载
  190. document.body.appendChild(link)
  191. link.click()
  192. document.body.removeChild(link)
  193. }
  194. },
  195. }
  196. }
  197. </script>
  198. <style lang="scss" scoped>
  199. * {
  200. margin: 0;
  201. padding: 0;
  202. }
  203. img {
  204. margin: 0;
  205. padding: 0;
  206. }
  207. .storeCode {
  208. display: flex;
  209. justify-content: center;
  210. gap: 117px;
  211. padding-top: 50px;
  212. .paymentCode-left {
  213. display: flex;
  214. align-items: center;
  215. justify-content: center;
  216. flex-direction: column;
  217. .paymentCode-title {
  218. font-size: 28px;
  219. color: #000;
  220. font-weight: 500;
  221. margin-bottom: 20px;
  222. }
  223. .paymentCode-img {
  224. width: 463px;
  225. height: 595px;
  226. display: flex;
  227. flex-direction: column;
  228. align-items: center;
  229. background-color: #EF530E;
  230. border-radius: 10px;
  231. position: relative;
  232. .paymentCode-img-live {
  233. font-size: 28px;
  234. color: #fff;
  235. font-weight: 700;
  236. font-style: italic;
  237. margin-top: 15px;
  238. }
  239. .paymentCode-img-title {
  240. display: block !important;
  241. width: 100%;
  242. height: 56px !important;
  243. color: #fff !important;
  244. font-size: 50px !important;
  245. margin: 20px 0 24px !important;
  246. font-weight: 700 !important;
  247. font-style: italic !important;
  248. box-sizing: border-box;
  249. text-align: center;
  250. line-height: 56px;
  251. }
  252. .img-box {
  253. width: 300px;
  254. height: 300px;
  255. img {
  256. width: 100% !important;
  257. height: 100% !important;
  258. }
  259. }
  260. .img-button {
  261. width: 100% !important;
  262. height: 88px !important;
  263. background-color: #fff;
  264. position: absolute;
  265. left: 0;
  266. bottom: 0;
  267. border-radius: 0 0 9px 9px !important;
  268. text-align: center !important;
  269. line-height: 88px !important;
  270. font-size: 24px !important;
  271. }
  272. }
  273. .paymentCode-img-box {
  274. width: 463px;
  275. height: 595px;
  276. img {
  277. width: 100% !important;
  278. height: 100% !important;
  279. }
  280. }
  281. .paymentCode-open {
  282. display: flex;
  283. align-items: center;
  284. gap: 20px;
  285. margin-top: 30px;
  286. .open-icon {
  287. width: 50px;
  288. height: 50px;
  289. img {
  290. width: 100% !important;
  291. height: 100% !important;
  292. }
  293. }
  294. .open-text {
  295. font-size: 28px;
  296. color: #3D3D3D;
  297. }
  298. }
  299. .paymentCode-btn {
  300. width: 315px;
  301. height: 67px;
  302. background-color: #EF530E;
  303. color: #fff;
  304. border-radius: 10px;
  305. text-align: center;
  306. line-height: 67px;
  307. margin-top: 32px;
  308. font-size: 28px;
  309. cursor: pointer;
  310. }
  311. }
  312. }
  313. </style>