index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <route lang="json">
  2. { "style": { "navigationBarTitleText": "个人码", "navigationBarBackgroundColor": "#fff" } }
  3. </route>
  4. <script setup lang="ts">
  5. import UQRCode from 'uqrcodejs'
  6. import { getPointsOrder, getDesignerInfo,storeAndPunchIn } from '../../../../core/libs/requests'
  7. import { qrCodeString2Object, toQrCodeString } from '../../../../core/utils/common'
  8. import { QrCodeBusinessType } from '../../../../core/libs/enums'
  9. import { useUserStore } from '../../../../store'
  10. import { storeToRefs } from 'pinia'
  11. import { useRouter } from '@/core/utils/router'
  12. const id = ref()
  13. const userStore = useUserStore()
  14. const { userInfo } = storeToRefs(userStore)
  15. const router = useRouter()
  16. const qrCodeCanvas = ref()
  17. const { data, run: setData } = useRequest(() => getPointsOrder(id.value), { initialData: {} })
  18. const { data: designerInfo, run: setDesignerInfo } = useRequest(() => getDesignerInfo(id.value), {
  19. initialData: {},
  20. })
  21. const a = async (canvasContext: UniApp.CanvasContext) => {
  22. const qr = new UQRCode()
  23. // 设置二维码内容
  24. // qr.data = data.value.orderNo
  25. // qr.data = toQrCodeString('', {
  26. // no: '',
  27. // })
  28. // qr.data = '12324'
  29. qr.data = toQrCodeString(QrCodeBusinessType.PagePath, {
  30. path: `/pages-sub/mine/homepage/index?id=${userInfo.value?.userId}`,
  31. })
  32. // 设置二维码大小,必须与canvas设置的宽高一致
  33. qr.size = 200
  34. qr.foregroundImageSrc = userInfo.value?.avatar
  35. // console.log(userInfo.value?.avatar)
  36. // 调用制作二维码方法
  37. qr.make()
  38. qr.canvasContext = canvasContext
  39. // 调用绘制方法将二维码图案绘制到canvas上
  40. await qr.drawCanvas()
  41. }
  42. const generateCode = async () => {
  43. const currentInstance = getCurrentInstance()
  44. await nextTick(async () => {
  45. const canvasContext = uni.createCanvasContext('qrcode', currentInstance) // 如果是组件,this必须传入
  46. a(canvasContext)
  47. })
  48. }
  49. const generateCodeMp = async () => {
  50. const currentInstance = getCurrentInstance()
  51. await nextTick(() => {
  52. uni
  53. .createSelectorQuery()
  54. .in(currentInstance)
  55. .select('#qrcode')
  56. .fields({ node: true, size: true }, () => {})
  57. .exec((res) => {
  58. const canvas = res[0].node
  59. const dpr = wx.getSystemInfoSync().pixelRatio
  60. canvas.width = res[0].width * dpr
  61. canvas.height = res[0].height * dpr
  62. const ctx = canvas.getContext('2d')
  63. ctx.scale(dpr, dpr)
  64. UQRCode.prototype.loadImage = (src: string) =>
  65. new Promise((resolve, reject) => {
  66. const img = canvas.createImage()
  67. img.src = src
  68. img.onload = () => {
  69. resolve(img)
  70. }
  71. img.onerror = (err) => {
  72. // reject返回错误信息
  73. reject(err)
  74. }
  75. })
  76. a(ctx)
  77. qrCodeCanvas.value = canvas
  78. })
  79. })
  80. }
  81. const handleClickScan = async () => {
  82. const { result } = await uni.scanCode({})
  83. console.log(result)
  84. // const a = qrCodeString2Object('WIFI:S:KM;T:WPA;P:km666888;H:false;;')
  85. // console.log(a)
  86. // console.log(toQrCodeString('到店', { a: 1, orderId: 2222 }))
  87. // console.log(qrCodeString2Object(toQrCodeString('到店', { a: 1, orderId: 2222 })))
  88. const { type, options } = qrCodeString2Object(result)
  89. // if (type === QrCodeBusinessType.InStoreClockIn) {
  90. // if (!features.value.checkInAtStoreTask) return router.push('/pages-sub/mine/authentication/index')
  91. // try {
  92. // await storeAndPunchIn({ id: options.id })
  93. // // await storeAndPunchIn({ id: 24 })
  94. // router.push(`/pages-sub/mine/scan/result/index?result=${result}`)
  95. // } catch (e) {
  96. // if (e.code === 1000) {
  97. // router.push(
  98. // `/pages-sub/mine/scan/result/index?result=${toQrCodeString(type, { name: options.name, desc: e.msg })}`,
  99. // )
  100. // } else {
  101. // uni.showToast({ title: e.msg, icon: 'none' })
  102. // }
  103. // }
  104. // return
  105. // }
  106. if (type === QrCodeBusinessType.PagePath) {
  107. await router.push(options.path)
  108. return
  109. }
  110. await router.push(`/pages-sub/mine/scan/result/index?result=${result}`)
  111. }
  112. const saveQrcode = async () => {
  113. try {
  114. console.log(qrCodeCanvas.value)
  115. // const option = {
  116. // // #ifdef H5
  117. // canvasId: 'qrcode',
  118. // // #endif
  119. // // #ifdef MP-WEIXIN
  120. // canvas: toRaw(qrCodeCanvas.value),
  121. // // #endif
  122. // }
  123. // console.log(option)
  124. // const { tempFilePath } = await wx.canvasToTempFilePath(option)
  125. // console.log(tempFilePath)
  126. uni.downloadFile({
  127. url:designerInfo.value.homeUrl,
  128. success: (res) => {
  129. uni.saveImageToPhotosAlbum({
  130. // filePath: res.tempFilePath,
  131. filePath: res.tempFilePath,
  132. })
  133. }
  134. })
  135. await uni.showToast({ title: '保存成功' })
  136. } catch (e) {
  137. await uni.showToast({ title: '保存失败', icon: 'none' })
  138. }
  139. }
  140. onLoad(async (query: { id: string }) => {
  141. id.value = query.id
  142. // #ifdef MP-WEIXIN
  143. // await generateCodeMp()
  144. await setDesignerInfo()
  145. // #endif
  146. // #ifdef H5
  147. await generateCode()
  148. // #endif
  149. })
  150. onReady(() => {})
  151. </script>
  152. <template>
  153. <div class="flex-grow flex flex-col items-center bg-white">
  154. <div class="flex-grow flex flex-col justify-center">
  155. <div>
  156. <!-- #ifdef MP-WEIXIN -->
  157. <wd-img :width="204" :height="204" :src="designerInfo.homeUrl"></wd-img>
  158. <!-- <canvas
  159. type="2d"
  160. id="qrcode"
  161. canvas-id="qrcode"
  162. style="width: 200px; height: 200px"
  163. ></canvas> -->
  164. <!-- #endif -->
  165. <!-- #ifdef H5 -->
  166. <canvas id="qrcode" canvas-id="qrcode" style="width: 200px; height: 200px"></canvas>
  167. <!-- #endif-->
  168. </div>
  169. <div
  170. class="text-center text-black/40 text-xs font-normal font-['PingFang_SC'] leading-normal my-3.5"
  171. >
  172. 扫一扫上面的二维码,可查看我的主页
  173. </div>
  174. </div>
  175. <div class="w-full flex items-center justify-around my-10">
  176. <div
  177. @click="handleClickScan"
  178. class="text-center text-[#3f588f] text-base font-normal font-['PingFang_SC'] leading-normal"
  179. >
  180. 扫一扫
  181. </div>
  182. <div
  183. class="text-center text-black/40 text-xs font-normal font-['PingFang_SC'] leading-normal"
  184. >
  185. |
  186. </div>
  187. <div
  188. class="text-center text-[#3f588f] text-base font-normal font-['PingFang_SC'] leading-normal"
  189. @click="saveQrcode"
  190. >
  191. 保存图片
  192. </div>
  193. </div>
  194. </div>
  195. </template>