index.js 7.9 KB

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