share.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // pages/share/share.ts
  2. import { shareRoom } from '../../api/shareRoom'
  3. import { fetchRoom, fetchRoomMinCode, RoomDetailType } from '../../api/fetchRoom'
  4. Page({
  5. /**
  6. * 初始数据
  7. */
  8. data: {
  9. shared_img: "",
  10. shareMiniCode: '',
  11. userInfo: {} as GlobalUserInfo,
  12. roomDetail: {} as RoomDetailType,
  13. roomId: '',
  14. canvas: {} as WechatMiniprogram.Canvas,
  15. canvasWidth: NaN,
  16. canvasHeight: NaN,
  17. context: {} as any,
  18. save_url: ''
  19. },
  20. onLoad(option: any) {
  21. console.log('query', option);
  22. const { id } = option
  23. if (id) {
  24. this.setData({
  25. roomId: id
  26. })
  27. } else {
  28. wx.showToast({
  29. title: "分享出错了~"
  30. })
  31. }
  32. },
  33. onReady() {
  34. this.handleCanvasInit()
  35. },
  36. async onShow() {
  37. const app = getApp<IAppOption>();
  38. this.setData({
  39. userInfo: app.globalData.userInfo
  40. })
  41. const roomInfo: RoomDetailType = await fetchRoom(this.data.roomId);
  42. console.log('roomInfo', roomInfo)
  43. if (!roomInfo.roomHostCode) {
  44. const url = await fetchRoomMinCode(this.data.roomId);
  45. if (url) {
  46. roomInfo.roomHostCode = url
  47. }
  48. }
  49. this.setData({
  50. roomDetail: roomInfo,
  51. shareMiniCode: roomInfo.roomHostCode,
  52. shared_img: roomInfo.roomCoverUrl,
  53. })
  54. },
  55. async loadImage(url: string): Promise<HTMLImageElement> {
  56. let src = await this.downloadFile(url)
  57. return new Promise((resolve, reject) => {
  58. const image = this.data.canvas.createImage();
  59. image.onload = () => resolve(image);
  60. image.onerror = reject;
  61. image.src = src;
  62. })
  63. },
  64. async handleSaveToAblum() {
  65. await this.drawCover();
  66. await this.drawDown();
  67. wx.showLoading({
  68. title: '保存中...',
  69. });
  70. setTimeout(() => {
  71. this.handleSaveCanvasToTmp();
  72. }, 1000)
  73. },
  74. async drawDown(): Promise<boolean> {
  75. try {
  76. const image = await this.loadImage(this.data.shareMiniCode);
  77. const top = this.data.canvasHeight * 0.7 + 20
  78. const left = this.data.canvasWidth - 110
  79. console.log('logo', image, top, left)
  80. this.data.context.drawImage(image, left, top, 100, 100);
  81. this.data.context.drawImage(image, left, top, 100, 100);
  82. //title
  83. this.data.context.font = 'normal 800 15px sans-serif'
  84. this.data.context.fillStyle = "#000";
  85. let title = this.data.roomDetail.roomTitle || '四维带看'
  86. if (title.length > 10) {
  87. let fistTitle = String(title).substring(0,10)
  88. let secTitle = String(title).substring(10,title.length)
  89. this.data.context.fillText(fistTitle, 10, top + 20)
  90. this.data.context.fillText(secTitle, 10, top + 40)
  91. }else{
  92. this.data.context.fillText(title, 10, top + 20)
  93. }
  94. this.data.context.font = 'normal 400 12px sans-serif'
  95. this.data.context.fillStyle = "#000";
  96. this.data.context.fillText('语音云带看,邀您来参与', 10, top + 70)
  97. this.data.context.font = 'normal 200 10px sans-serif'
  98. this.data.context.fillStyle = "#666";
  99. this.data.context.fillText('长按识别二维码参与带看', 10, top + 90)
  100. this.data.context.restore()
  101. return Promise.resolve(true);
  102. } catch (error) {
  103. return Promise.resolve(false);
  104. }
  105. },
  106. async drawCover(): Promise<boolean> {
  107. console.log(' this.data.context', this.data.context)
  108. this.drawCard();
  109. const cover = await this.loadImage(this.data.shared_img);
  110. const img_width = this.data.canvasWidth * 0.8644,
  111. img_height = this.data.canvasHeight * 0.8644;
  112. // const left = (this.data.canvasWidth - img_width) / 2;
  113. const dpr = wx.getSystemInfoSync().pixelRatio
  114. console.log('cover', dpr, this.data.canvas, img_width, img_height);
  115. this.data.context.save()
  116. this.roundedImage(10, 10, this.data.canvasWidth - 20, this.data.canvasHeight * 0.7, 15);
  117. this.data.context.clip()
  118. this.data.context.drawImage(cover, 10, 10, this.data.canvasWidth - 20, this.data.canvasHeight * 0.7);
  119. this.data.context.restore()
  120. return Promise.resolve(true)
  121. },
  122. roundedImage(x: number, y: number, width: number, height: number, radius: number) {
  123. this.data.context.beginPath();
  124. this.data.context.moveTo(x + radius, y);
  125. this.data.context.lineTo(x + width - radius, y);
  126. this.data.context.quadraticCurveTo(x + width, y, x + width, y + radius);
  127. this.data.context.lineTo(x + width, y + height - radius);
  128. this.data.context.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
  129. this.data.context.lineTo(x + radius, y + height);
  130. this.data.context.quadraticCurveTo(x, y + height, x, y + height - radius);
  131. this.data.context.lineTo(x, y + radius);
  132. this.data.context.quadraticCurveTo(x, y, x + radius, y);
  133. this.data.context.closePath();
  134. },
  135. downloadFile(url: string): Promise<string> {
  136. return new Promise(resolve => {
  137. wx.downloadFile({
  138. url: url,
  139. success(res) {
  140. resolve(res.tempFilePath)
  141. },
  142. async fail(res) {
  143. console.log('下载失败,再触发下载一次', res)
  144. wx.downloadFile({
  145. url: url,
  146. success(res) {
  147. resolve(res.tempFilePath)
  148. },
  149. fail() {
  150. console.log('二次下载失败', res)
  151. resolve('')
  152. }
  153. })
  154. }
  155. })
  156. })
  157. },
  158. async handleCanvasInit() {
  159. const query = wx.createSelectorQuery()
  160. query.select('#content')
  161. .fields({ node: true, size: true })
  162. .exec((res) => {
  163. console.log('res', res)
  164. const canvas = res[0].node
  165. const ctx = canvas.getContext('2d')
  166. const dpr = wx.getSystemInfoSync().pixelRatio
  167. canvas.width = res[0].width * dpr
  168. canvas.height = res[0].height * dpr
  169. ctx.scale(dpr, dpr)
  170. this.setData({
  171. canvas: canvas,
  172. context: ctx,
  173. canvasWidth: res[0].width,
  174. canvasHeight: res[0].height
  175. })
  176. });
  177. },
  178. drawCard() {
  179. this.data.context.fillStyle = "#fff";
  180. this.data.context.fillRect(0, 0, this.data.canvas.width, this.data.canvas.height);
  181. },
  182. handleSaveCanvasToTmp() {
  183. wx.canvasToTempFilePath({
  184. x: 0,
  185. y: 0,
  186. width: this.data.canvasWidth,
  187. height: this.data.canvasHeight,
  188. canvas: this.data.canvas,
  189. success: (res) => {
  190. this.setData({
  191. save_url: res.tempFilePath
  192. }, () => {
  193. wx.saveImageToPhotosAlbum({
  194. filePath: this.data.save_url,
  195. success(res) {
  196. wx.showModal({
  197. title: '图片保存成功',
  198. content: '图片已保存到相册,快去分享吧~',
  199. showCancel: false,
  200. confirmText: '好的',
  201. confirmColor: '#72B9C3',
  202. success: (res) => {
  203. if (res.confirm) { }
  204. }
  205. })
  206. }
  207. })
  208. })
  209. wx.hideLoading();
  210. }
  211. })
  212. },
  213. onShareAppMessage: function (res) {
  214. const roomId = this.data.roomId
  215. const userId = this.data.userInfo.wxUserId
  216. const isTour = this.data.isTour
  217. const newPicUrl = this.data.roomDetail.roomCoverUrl || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
  218. const base = {
  219. imageUrl: newPicUrl,
  220. path: `/pages/room/room?roomId=${roomId}&role=customer&isTour=${isTour}`
  221. }
  222. if (roomId && userId) {
  223. shareRoom(roomId, userId);
  224. }
  225. console.error('share', base, roomId, userId);
  226. if (res.from === 'button') {
  227. this.setData({
  228. showShare: false
  229. })
  230. return {
  231. ...base,
  232. title: '【好友邀请】一起来带看吧!',
  233. }
  234. } else {
  235. return {
  236. ...base,
  237. title: '【好友邀请】一起来带看吧!',
  238. }
  239. }
  240. },
  241. })