鸿宇研学生前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

428 lines
12 KiB

  1. <template>
  2. <uv-popup
  3. ref="popup"
  4. :overlayOpacity="0.6"
  5. mode="center"
  6. bgColor="none"
  7. :zIndex="1000000"
  8. @change="onPopupChange"
  9. >
  10. <view class="popup__view">
  11. <view class="canvas" style="width: 566rpx; height: 1060rpx; overflow: hidden;">
  12. <canvas id="myCanvas" canvas-id="firstCanvas1" type="2d" style="width: 100%; height: 100%;"></canvas>
  13. </view>
  14. <button class="btn" @click="saveImg">
  15. <view class="content">保存到相册</view>
  16. </button>
  17. </view>
  18. </uv-popup>
  19. </template>
  20. <script>
  21. export default {
  22. data() {
  23. return {
  24. wxCodeImage: '',
  25. baseUrl: 'https://image.hhlm1688.com/',
  26. canvas: {},
  27. retry: 10,
  28. }
  29. },
  30. async onLoad() {
  31. },
  32. methods: {
  33. open() {
  34. this.retry = 10
  35. this.$refs.popup.open();
  36. },
  37. close() {
  38. this.$refs.popup.close();
  39. },
  40. async fetchQrCode(path) {
  41. // todo: delete
  42. this.wxCodeImage = 'https://uploadfile.bizhizu.cn/up/e3/64/e0/e364e0f7f6af11f11abdafc22d17b15c.jpg'
  43. return
  44. try {
  45. this.wxCodeImage = (await this.$fetch('getInviteCode', { path }))?.url
  46. } catch (err) {
  47. }
  48. },
  49. // 生成有圆角的矩形
  50. drawBg(ctx, x, y, width, height, radius) {
  51. ctx.beginPath();
  52. ctx.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 3 / 2);
  53. ctx.lineTo(width - radius + x, y);
  54. ctx.arc(width - radius + x, radius + y, radius, Math.PI * 3 / 2, Math.PI * 2);
  55. ctx.lineTo(width + x, height + y - radius);
  56. ctx.arc(width - radius + x, height - radius + y, radius, 0, Math.PI * 1 / 2);
  57. ctx.lineTo(radius + x, height + y);
  58. ctx.arc(radius + x, height - radius + y, radius, Math.PI * 1 / 2, Math.PI);
  59. ctx.closePath();
  60. ctx.fillStyle = '#fff'
  61. ctx.fill()
  62. },
  63. drawCoverImg(canvas, ctx, x, y, width, height, radius, lineWidth) {
  64. return new Promise(resolve => {
  65. // 海报图片
  66. const paperImage = canvas.createImage()
  67. console.log('paperImage', paperImage)
  68. // todo: fetch
  69. paperImage.src = 'https://i1.hdslb.com/bfs/archive/c0101b4ce06e6bdda803728408e79c8f8b8d0725.jpg'
  70. paperImage.onload = () => {
  71. console.log('paperImage onload')
  72. ctx.beginPath();
  73. ctx.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 3 / 2);
  74. ctx.lineTo(width - radius + x, y);
  75. ctx.arc(width - radius + x, radius + y, radius, Math.PI * 3 / 2, Math.PI * 2);
  76. ctx.lineTo(width + x, height + y);
  77. ctx.lineTo(x, height + y);
  78. ctx.closePath();
  79. ctx.lineWidth = lineWidth;
  80. ctx.strokeStyle = '#F8F8F8';
  81. ctx.stroke();
  82. ctx.clip();
  83. ctx.drawImage(paperImage, x, y, width, height)
  84. resolve()
  85. }
  86. })
  87. },
  88. drawContentBg(ctx, x, y, width, height, radius) {
  89. ctx.beginPath();
  90. // ctx.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 3 / 2);
  91. ctx.moveTo(x, y);
  92. ctx.lineTo(width + x, y);
  93. ctx.lineTo(width + x, height + y - radius);
  94. ctx.arc(width - radius + x, height - radius + y, radius, 0, Math.PI * 1 / 2);
  95. ctx.lineTo(radius + x, height + y);
  96. ctx.arc(radius + x, height - radius + y, radius, Math.PI * 1 / 2, Math.PI);
  97. ctx.closePath();
  98. let gradient = ctx.createLinearGradient(x, y, x, y + height);
  99. gradient.addColorStop('0', '#DAF3FF');
  100. gradient.addColorStop('0.2', '#FBFEFF');
  101. gradient.addColorStop('1.0', '#FBFEFF');
  102. ctx.fillStyle = gradient
  103. ctx.fill()
  104. },
  105. drawAvatar(canvas, ctx, x, y, r) {
  106. console.log('drawAvatar', 'x', x, 'y', y, 'r', r)
  107. return new Promise(resolve => {
  108. // 头像图片
  109. const avatarImage = canvas.createImage()
  110. // todo: fetch
  111. avatarImage.src = 'https://i1.hdslb.com/bfs/archive/c0101b4ce06e6bdda803728408e79c8f8b8d0725.jpg'
  112. avatarImage.onload = () => {
  113. console.log('avatarImage onload')
  114. ctx.beginPath();
  115. console.log('arc', 'x', x + r, 'y', y + r, 'r', r)
  116. ctx.arc(x + r, y + r, r, 0, 2 * Math.PI);
  117. ctx.clip();
  118. const size = r*2
  119. console.log('size', size)
  120. ctx.drawImage(avatarImage, x, y, size, size)
  121. resolve()
  122. }
  123. })
  124. },
  125. drawMultilineText(ctx, text, x, y, maxWidth, lineHeight) {
  126. console.log('drawMultilineText', 'x', x, 'y', y)
  127. let line = '';
  128. for (let n = 0; n < text.length; n++) {
  129. const testLine = line + text[n];
  130. const metrics = ctx.measureText(testLine);
  131. const testWidth = metrics.width;
  132. if (testWidth > maxWidth && n > 0) {
  133. ctx.fillText(line, x, y);
  134. console.log(n, 'line', line, 'x', x, 'y', y)
  135. line = text[n];
  136. y += lineHeight;
  137. } else {
  138. line = testLine;
  139. }
  140. }
  141. console.log('line', line, 'x', x, 'y', y)
  142. ctx.fillText(line, x, y);
  143. },
  144. drawQrCodeImg(canvas, ctx, x, y, size) {
  145. return new Promise(resolve => {
  146. //二维码图片
  147. const coderImage = canvas.createImage()
  148. coderImage.src = this.wxCodeImage
  149. coderImage.onload = () => {
  150. console.log('coderImage onload')
  151. ctx.drawImage(coderImage, x, y, size, size)
  152. resolve()
  153. }
  154. })
  155. },
  156. draw() {
  157. wx.createSelectorQuery().in(this)
  158. .select('#myCanvas') // 绘制的canvas的id
  159. .fields({
  160. node: true,
  161. size: true
  162. })
  163. .exec(async (res) => {
  164. console.log('res', res)
  165. if (!res?.[0]?.node) {
  166. if (!this.retry) {
  167. console.log('retry fail')
  168. return
  169. }
  170. console.log('retry')
  171. this.retry -= 1
  172. setTimeout(() => {
  173. this.draw()
  174. }, 200)
  175. return
  176. }
  177. const canvas = res[0].node
  178. // 渲染上下文
  179. const ctx = canvas.getContext('2d')
  180. // Canvas 画布的实际绘制宽高
  181. const width = res[0].width
  182. const height = res[0].height
  183. // 初始化画布大小
  184. const dpr = wx.getWindowInfo().pixelRatio
  185. //根据dpr调整
  186. // dpr 2 4
  187. // 3 6
  188. console.log("--dpr", dpr)
  189. canvas.width = width * dpr
  190. canvas.height = height * dpr
  191. let Ratio = canvas.width / 566
  192. this.canvas = canvas
  193. ctx.scale(dpr, dpr)
  194. ctx.clearRect(0, 0, width, height)
  195. ctx.fillStyle = 'transparent'
  196. ctx.fillRect(0, 0, canvas.width, canvas.height)
  197. ctx.save()
  198. let radius = 48 * Ratio / dpr
  199. let w = 566 * Ratio / dpr
  200. let h = 1060 * Ratio / dpr
  201. this.drawBg(ctx, 0, 0, w, h, radius)
  202. ctx.restore();
  203. ctx.save()
  204. let lineWidth = 2 * Ratio / dpr
  205. let x = lineWidth
  206. let y = lineWidth
  207. w = 566 * Ratio / dpr - lineWidth * 2
  208. h = 400 * Ratio / dpr - lineWidth * 2
  209. await this.drawCoverImg(canvas, ctx, x, y, w, h, radius, lineWidth)
  210. ctx.restore();
  211. ctx.save()
  212. x = lineWidth
  213. y = 400 * Ratio / dpr + lineWidth
  214. h = 660 * Ratio / dpr - lineWidth * 2
  215. this.drawContentBg(ctx, x, y, w, h, radius, lineWidth)
  216. ctx.restore();
  217. ctx.save()
  218. radius = Math.floor(27 * Ratio / dpr)
  219. x = Math.floor(40 * Ratio / dpr)
  220. y = Math.floor(440 * Ratio / dpr)
  221. await this.drawAvatar(canvas, ctx, x, y, radius)
  222. ctx.restore();
  223. ctx.save()
  224. let text = '战斗世界'
  225. let maxWidth = 220 * Ratio / dpr
  226. let lineHeight = 17 * Ratio / dpr
  227. x = 100 * Ratio / dpr
  228. y = 474 * Ratio / dpr
  229. ctx.font = "normal normal normal 11px normal";
  230. ctx.fillStyle = "#7D7D7D";
  231. this.drawMultilineText(ctx, text, x, y, maxWidth, lineHeight)
  232. ctx.restore();
  233. ctx.save()
  234. maxWidth = 560 * Ratio / dpr
  235. lineHeight = 60 * Ratio / dpr
  236. ctx.font = "normal normal 600 16px normal";
  237. ctx.fillStyle = "#181818";
  238. text = '邀请您'
  239. x = 40 * Ratio / dpr
  240. y = 564 * Ratio / dpr
  241. this.drawMultilineText(ctx, text, x, y, maxWidth, lineHeight)
  242. y += lineHeight
  243. text = '探索新世界,开启研学之旅!'
  244. this.drawMultilineText(ctx, text, x, y, maxWidth, lineHeight)
  245. ctx.restore();
  246. ctx.save()
  247. text = '是否渴望一场充满知识与乐趣的冒险?现在,我们诚挚邀请你加入我们的研学小程序,开启一场别开生面的学习之旅!'
  248. maxWidth = 486 * Ratio / dpr
  249. lineHeight = 30 * Ratio / dpr
  250. ctx.font = "normal normal normal 10px normal";
  251. ctx.fillStyle = "#7D7D7D";
  252. x = 40 * Ratio / dpr
  253. y = 690 * Ratio / dpr
  254. this.drawMultilineText(ctx, text, x, y, maxWidth, lineHeight)
  255. ctx.restore();
  256. ctx.save()
  257. // 设置虚线样式
  258. ctx.setLineDash([4, 4]); // 第一个参数是实线长度, 第二个参数是间隔长度
  259. ctx.lineDashOffset = 2; // 设置虚线的起始偏移量
  260. ctx.lineWidth = lineWidth;
  261. ctx.strokeStyle = '#DADADA';
  262. // 开始绘制
  263. ctx.beginPath();
  264. x = 40 * Ratio / dpr
  265. y = 778 * Ratio / dpr
  266. ctx.moveTo(x, y);
  267. x += 486 * Ratio / dpr
  268. ctx.lineTo(x, y);
  269. ctx.stroke();
  270. ctx.restore();
  271. ctx.save()
  272. maxWidth = 250 * Ratio / dpr
  273. lineHeight = 44 * Ratio / dpr
  274. ctx.font = "normal normal 600 13px normal";
  275. ctx.fillStyle = "#181818";
  276. text = '立即加入我们,'
  277. x = 40 * Ratio / dpr
  278. y = 879 * Ratio / dpr
  279. this.drawMultilineText(ctx, text, x, y, maxWidth, lineHeight)
  280. text = '开启你的研学之旅!'
  281. y += lineHeight
  282. this.drawMultilineText(ctx, text, x, y, maxWidth, lineHeight)
  283. ctx.restore();
  284. ctx.save()
  285. x = 316 * Ratio / dpr
  286. y = 810 * Ratio / dpr
  287. let size = 210 * Ratio / dpr
  288. await this.drawQrCodeImg(canvas, ctx, x, y, size)
  289. uni.hideLoading()
  290. })
  291. },
  292. async init() {
  293. uni.showLoading({
  294. title: '加载中...'
  295. });
  296. await this.fetchQrCode()
  297. uni.hideLoading();
  298. uni.showLoading({
  299. title: "拼命绘画中..."
  300. })
  301. this.draw()
  302. },
  303. saveImg() {
  304. this.$authorize('scope.writePhotosAlbum').then((res) => {
  305. this.imgApi()
  306. })
  307. },
  308. imgApi() {
  309. uni.showLoading({
  310. title: '保存中...'
  311. });
  312. wx.canvasToTempFilePath({
  313. x: 0,
  314. y: 0,
  315. width: this.canvas.width,
  316. height: this.canvas.height,
  317. canvas: this.canvas,
  318. success: (res) => {
  319. let tempFilePath = res.tempFilePath;
  320. this.saveImgToPhone(tempFilePath)
  321. },
  322. fail: (err) => {
  323. console.log('--canvasToTempFilePath--fail', err)
  324. uni.hideLoading();
  325. }
  326. }, this);
  327. },
  328. saveImgToPhone(image) {
  329. /* 获取图片的信息 */
  330. uni.getImageInfo({
  331. src: image,
  332. success: function(image) {
  333. /* 保存图片到手机相册 */
  334. uni.saveImageToPhotosAlbum({
  335. filePath: image.path,
  336. success: function() {
  337. uni.showModal({
  338. title: '保存成功',
  339. content: '图片已成功保存到相册',
  340. showCancel: false
  341. });
  342. },
  343. complete(res) {
  344. console.log(res);
  345. uni.hideLoading();
  346. }
  347. });
  348. }
  349. });
  350. },
  351. onPopupChange(e) {
  352. if (!e.show) {
  353. return
  354. }
  355. this.init()
  356. },
  357. },
  358. }
  359. </script>
  360. <style scoped lang="scss">
  361. .canvas {
  362. border-radius: 48rpx;
  363. }
  364. .btn {
  365. margin-top: 32rpx;
  366. width: 100%;
  367. padding: 22rpx 0;
  368. box-sizing: border-box;
  369. font-family: PingFang SC;
  370. font-weight: 500;
  371. font-size: 36rpx;
  372. line-height: 1.4;
  373. color: #FFFFFF;
  374. background: linear-gradient(to right, #21FEEC, #019AF9);
  375. border: 2rpx solid #00A9FF;
  376. border-radius: 41rpx;
  377. }
  378. </style>