index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // pages/guicangDetails/index.js
  2. import drawQrcode from 'weapp-qrcode';
  3. import { getwxml, style } from './shareJson.js';
  4. const { newRequest, cosBaseUrl, fileBaseURL } = require('../../utils/newServices');
  5. Page({
  6. data: {
  7. id: null,
  8. itemDetail: null,
  9. h5Url: 'https://test.4dmuseum.cn/swkzModel/index.html',
  10. boxTop: 0,
  11. windowHeight: 0,
  12. isFullScreen: false,
  13. startY: 0,
  14. moveY: 0,
  15. showSharePopup: false,
  16. shareUrl: 'https://test.4dmuseum.cn/swkzModel/index.html',
  17. qrCodeUrl: '',
  18. shareImagePath: '',
  19. width: 0,
  20. height: 0,
  21. container: null,
  22. showAppBanner: true, // 控制下载APP提示栏显示
  23. },
  24. onLoad: function(options) {
  25. if (options.id) {
  26. this.setData({
  27. id: options.id
  28. });
  29. this.loadItemDetail(options.id);
  30. }
  31. // 获取屏幕高度
  32. const systemInfo = wx.getSystemInfoSync();
  33. const windowHeight = systemInfo.windowHeight;
  34. this.setData({
  35. windowHeight: windowHeight,
  36. boxTop: windowHeight * 0.8 // 初始位置在屏幕60%的位置
  37. });
  38. },
  39. // 初始化 wxml-to-canvas
  40. initCanvas: function() {
  41. const that = this;
  42. that.widget = this.selectComponent('.widget');
  43. this.generateShareImage();
  44. },
  45. // 加载详情数据
  46. loadItemDetail: function(id) {
  47. if (!id) return;
  48. // 显示加载状态
  49. wx.showLoading({
  50. title: '加载中...'
  51. });
  52. // 调用详情接口
  53. newRequest.getAntiqueDetail(
  54. {},
  55. 'GET',
  56. (res) => {
  57. wx.hideLoading();
  58. console.log('详情数据加载成功:', res);
  59. if (res.data && res.data.data) {
  60. const detail = res.data.data;
  61. const itemDetail = {
  62. id: detail.id,
  63. title: detail.name,
  64. date: detail.createTime ? detail.createTime.split(' ')[0] : '',
  65. source: detail.source || '归藏',
  66. description: detail.description || '',
  67. dimensions: detail.dimensions || detail.size,
  68. material: detail.material,
  69. imageUrl: detail.coverImgUrl.includes('http') ? detail.coverImgUrl : cosBaseUrl + detail.coverImgUrl,
  70. // file: cosBaseUrl + detail.fileList.find(file => file.name.includes('usdz')).url,
  71. h5Url: `${fileBaseURL}?m=${cosBaseUrl + detail.fileList.find(file => file.name.includes('usdz')).url}`,
  72. shareUrl: `${fileBaseURL}?m=${cosBaseUrl + detail.fileList.find(file => file.name.includes('usdz')).url}`,
  73. };
  74. console.log(itemDetail);
  75. this.setData({
  76. itemDetail: itemDetail
  77. });
  78. }
  79. },
  80. (err) => {
  81. wx.hideLoading();
  82. console.error('详情数据加载失败:', err);
  83. }
  84. );
  85. },
  86. // 打开H5页面
  87. openH5Page: function() {
  88. // 使用web-view内嵌打开H5页面
  89. wx.navigateTo({
  90. url: `/pages/webview/index?url=${encodeURIComponent(this.data.itemDetail.h5Url)}`
  91. });
  92. },
  93. // 触摸开始事件
  94. touchStart: function(e) {
  95. this.setData({
  96. startY: e.touches[0].clientY
  97. });
  98. },
  99. // 触摸移动事件
  100. touchMove: function(e) {
  101. const moveY = e.touches[0].clientY;
  102. const diff = moveY - this.data.startY;
  103. // 计算新的boxTop位置
  104. let newBoxTop = this.data.boxTop + diff;
  105. // 限制上下边界
  106. if (newBoxTop < this.data.windowHeight * 0.1) {
  107. newBoxTop = this.data.windowHeight * 0.1;
  108. } else if (newBoxTop > this.data.windowHeight * 0.7) {
  109. newBoxTop = this.data.windowHeight * 0.7;
  110. }
  111. this.setData({
  112. boxTop: newBoxTop,
  113. startY: moveY
  114. });
  115. },
  116. // 触摸结束事件
  117. touchEnd: function(e) {
  118. // 判断是否切换到全屏模式
  119. if (this.data.boxTop < this.data.windowHeight * 0.4) {
  120. this.setData({
  121. boxTop: this.data.windowHeight * 0.1,
  122. isFullScreen: true
  123. });
  124. } else {
  125. this.setData({
  126. boxTop: this.data.windowHeight * 0.8,
  127. isFullScreen: false
  128. });
  129. }
  130. },
  131. // 分享功能
  132. onShareAppMessage: function() {
  133. return {
  134. title: this.data.itemDetail ? this.data.itemDetail.title : '文物详情',
  135. path: '/pages/guicangDetails/index?id=' + this.data.id
  136. };
  137. },
  138. // 分享按钮点击事件
  139. shareItem: function() {
  140. this.setData({
  141. showSharePopup: true
  142. });
  143. this.showQRCode();
  144. setTimeout(() => {
  145. this.initCanvas();
  146. }, 1000)
  147. },
  148. // 关闭分享弹窗
  149. closeSharePopup: function() {
  150. this.setData({
  151. showSharePopup: false,
  152. shareImagePath: '',
  153. });
  154. },
  155. // 生成分享图片
  156. generateShareImage: function() {
  157. const that = this;
  158. if (!that.widget) {
  159. wx.showToast({
  160. title: 'wxml-to-canvas 组件未初始化',
  161. icon: 'none'
  162. });
  163. console.error('wxml-to-canvas 组件未初始化');
  164. return;
  165. }
  166. let wxml = getwxml(that.data.qrcodeUrl, this.data.itemDetail.title, this.data.itemDetail.imageUrl);
  167. console.log(wxml, 'wxml');
  168. // 设置要绘制的 WXML 节点
  169. that.widget.renderToCanvas({wxml, style}).then((res) => {
  170. // 将 canvas 转为图片
  171. that.container = res
  172. that.extraImage()
  173. }).catch((err) => {
  174. console.error('渲染 canvas 失败', err);
  175. wx.showToast({
  176. title: `${err}`,
  177. icon: 'none'
  178. });
  179. });
  180. },
  181. extraImage() {
  182. const p2 = this.widget.canvasToTempFilePath()
  183. p2.then(res => {
  184. wx.hideToast();
  185. wx.showShareImageMenu({
  186. path: res.tempFilePath
  187. })
  188. this.setData({
  189. shareImagePath: res.tempFilePath,
  190. })
  191. })
  192. },
  193. // 生成二维码
  194. showQRCode() {
  195. let that = this;
  196. let url = that.data.itemDetail.h5Url
  197. drawQrcode({
  198. width: 72,
  199. height: 72,
  200. canvasId: 'myQrcode',
  201. text: url,
  202. correctLevel: 2
  203. })
  204. // 创建一个选择器查询对象
  205. const query = wx.createSelectorQuery();
  206. // 执行查询并获取 canvas 节点的实例
  207. query.select('#myQrcode').boundingClientRect()
  208. // 查询结束后执行回调函数
  209. query.exec((res) => {
  210. if (res[0]) {
  211. // res[0] 是 canvas 节点信息,确保节点存在
  212. wx.canvasToTempFilePath({
  213. canvasId: 'myQrcode',
  214. success(res) {
  215. const tempFilePath = res.tempFilePath;
  216. console.log(tempFilePath);
  217. that.setData({
  218. qrcodeUrl: tempFilePath
  219. })
  220. },
  221. fail(err) {
  222. console.error( err);
  223. }
  224. });
  225. } else {
  226. console.error('未能找到对应的 canvas 节点');
  227. }
  228. });
  229. },
  230. // 复制链接
  231. copyLink: function() {
  232. wx.setClipboardData({
  233. data: this.data.itemDetail.shareUrl,
  234. success: function() {
  235. wx.showToast({
  236. title: '链接已复制',
  237. icon: 'success'
  238. });
  239. }
  240. });
  241. },
  242. // 分享到微信好友
  243. shareToWechat: function() {
  244. if (!this.data.shareImagePath) {
  245. wx.showToast({
  246. title: '正在生成分享图片,请稍候',
  247. icon: 'none',
  248. duration: 10000,
  249. mask: true,
  250. });
  251. this.showQRCode();
  252. setTimeout(() => {
  253. this.initCanvas();
  254. }, 1000)
  255. } else {
  256. wx.showShareImageMenu({
  257. path: this.data.shareImagePath
  258. })
  259. }
  260. },
  261. // 分享给朋友(小程序自带分享功能)
  262. onShareAppMessage: function() {
  263. return {
  264. title: this.data.itemDetail ? this.data.itemDetail.title : '文物详情',
  265. path: '/pages/guicangDetails/index?id=' + this.data.id,
  266. imageUrl: this.data.shareImagePath || ''
  267. };
  268. },
  269. // 关闭下载APP提示栏
  270. closeAppBanner: function() {
  271. this.setData({
  272. showAppBanner: false
  273. });
  274. },
  275. // 下载APP - 跳转到webview页面
  276. downloadApp: function() {
  277. const downloadUrl = 'https://test.4dmuseum.cn/admin/swkzMobile.html';
  278. wx.navigateTo({
  279. url: `/pages/webview/index?url=${encodeURIComponent(downloadUrl)}`
  280. });
  281. }
  282. })