鸿宇研学生前端代码
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.

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