index.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // pages/guicangDetails/index.js
  2. import drawQrcode from 'weapp-qrcode';
  3. import { getwxml, style } from './shareJson.js';
  4. Page({
  5. data: {
  6. id: null,
  7. itemDetail: null,
  8. h5Url: 'https://test.4dmuseum.cn/swkzModel/index.html',
  9. boxTop: 0,
  10. windowHeight: 0,
  11. isFullScreen: false,
  12. startY: 0,
  13. moveY: 0,
  14. showSharePopup: false,
  15. shareUrl: 'https://test.4dmuseum.cn/swkzModel/index.html',
  16. qrCodeUrl: '',
  17. shareImagePath: '',
  18. width: 0,
  19. height: 0,
  20. container: null,
  21. showAppBanner: true, // 控制下载APP提示栏显示
  22. },
  23. onLoad: function(options) {
  24. if (options.id) {
  25. this.setData({
  26. id: options.id
  27. });
  28. this.loadItemDetail(options.id);
  29. }
  30. // 获取屏幕高度
  31. const systemInfo = wx.getSystemInfoSync();
  32. const windowHeight = systemInfo.windowHeight;
  33. this.setData({
  34. windowHeight: windowHeight,
  35. boxTop: windowHeight * 0.8 // 初始位置在屏幕60%的位置
  36. });
  37. },
  38. // 初始化 wxml-to-canvas
  39. initCanvas: function() {
  40. const that = this;
  41. that.widget = this.selectComponent('.widget');
  42. this.generateShareImage();
  43. },
  44. // 加载详情数据
  45. loadItemDetail: function(id) {
  46. // 这里应该是从服务器获取数据
  47. // 目前使用模拟数据
  48. const mockDetail = {
  49. id: id,
  50. title: '唐代三彩骆驼骑俑',
  51. date: '唐代 (618-907)',
  52. source: '珍藏馆',
  53. description: '唐代三彩骆驼骑俑是唐代陶瓷艺术的代表作品之一,以其鲜艳的色彩和精湛的工艺著称。这件作品高约45厘米,宽约30厘米,由黄、绿、白三色釉彩装饰,形象生动地刻画了丝绸之路上的商旅场景。骆驼昂首挺胸,神态自若,背上载着商人和货物,展现了唐代中外文化交流的繁荣景象。\n\n三彩陶俑是唐代墓葬中的重要随葬品,反映了当时社会的生活风貌和审美情趣。这类作品不仅具有很高的艺术价值,也是研究唐代社会、经济、文化的重要实物资料。\n\n唐三彩的制作工艺复杂,需要经过选料、成型、素烧、施釉、再烧等多道工序。其釉色鲜艳夺目,釉面光亮透明,是中国古代陶瓷艺术的杰出代表。',
  54. dimensions: '高45厘米,宽30厘米',
  55. material: '陶瓷',
  56. imageUrl: '../../imgs/guicang/haiwaiwenwu.png'
  57. };
  58. this.setData({
  59. itemDetail: mockDetail
  60. });
  61. },
  62. // 打开H5页面
  63. openH5Page: function() {
  64. // 使用web-view内嵌打开H5页面
  65. wx.navigateTo({
  66. url: `/pages/webview/index?url=${encodeURIComponent(this.data.h5Url)}`
  67. });
  68. },
  69. // 触摸开始事件
  70. touchStart: function(e) {
  71. this.setData({
  72. startY: e.touches[0].clientY
  73. });
  74. },
  75. // 触摸移动事件
  76. touchMove: function(e) {
  77. const moveY = e.touches[0].clientY;
  78. const diff = moveY - this.data.startY;
  79. // 计算新的boxTop位置
  80. let newBoxTop = this.data.boxTop + diff;
  81. // 限制上下边界
  82. if (newBoxTop < this.data.windowHeight * 0.1) {
  83. newBoxTop = this.data.windowHeight * 0.1;
  84. } else if (newBoxTop > this.data.windowHeight * 0.7) {
  85. newBoxTop = this.data.windowHeight * 0.7;
  86. }
  87. this.setData({
  88. boxTop: newBoxTop,
  89. startY: moveY
  90. });
  91. },
  92. // 触摸结束事件
  93. touchEnd: function(e) {
  94. // 判断是否切换到全屏模式
  95. if (this.data.boxTop < this.data.windowHeight * 0.4) {
  96. this.setData({
  97. boxTop: this.data.windowHeight * 0.1,
  98. isFullScreen: true
  99. });
  100. } else {
  101. this.setData({
  102. boxTop: this.data.windowHeight * 0.8,
  103. isFullScreen: false
  104. });
  105. }
  106. },
  107. // 分享功能
  108. onShareAppMessage: function() {
  109. return {
  110. title: this.data.itemDetail ? this.data.itemDetail.title : '文物详情',
  111. path: '/pages/guicangDetails/index?id=' + this.data.id
  112. };
  113. },
  114. // 分享按钮点击事件
  115. shareItem: function() {
  116. this.setData({
  117. showSharePopup: true
  118. });
  119. this.showQRCode();
  120. setTimeout(() => {
  121. this.initCanvas();
  122. }, 1000)
  123. },
  124. // 关闭分享弹窗
  125. closeSharePopup: function() {
  126. this.setData({
  127. showSharePopup: false,
  128. shareImagePath: '',
  129. });
  130. },
  131. // 生成分享图片
  132. generateShareImage: function() {
  133. const that = this;
  134. if (!that.widget) {
  135. wx.showToast({
  136. title: 'wxml-to-canvas 组件未初始化',
  137. icon: 'none'
  138. });
  139. console.error('wxml-to-canvas 组件未初始化');
  140. return;
  141. }
  142. let wxml = getwxml(that.data.qrcodeUrl);
  143. console.log(wxml, 'wxml');
  144. // 设置要绘制的 WXML 节点
  145. that.widget.renderToCanvas({wxml, style}).then((res) => {
  146. // 将 canvas 转为图片
  147. that.container = res
  148. that.extraImage()
  149. }).catch((err) => {
  150. console.error('渲染 canvas 失败', err);
  151. wx.showToast({
  152. title: `${err}`,
  153. icon: 'none'
  154. });
  155. });
  156. },
  157. extraImage() {
  158. const p2 = this.widget.canvasToTempFilePath()
  159. p2.then(res => {
  160. this.setData({
  161. shareImagePath: res.tempFilePath,
  162. })
  163. })
  164. },
  165. // 生成二维码
  166. showQRCode() {
  167. let that = this;
  168. let url = 'https://3d-usdz.4dkankan.com/index.html?m=13719751007/captureModel/202503131501024131/data'
  169. drawQrcode({
  170. width: 72,
  171. height: 72,
  172. canvasId: 'myQrcode',
  173. text: url,
  174. correctLevel: 3
  175. })
  176. // 创建一个选择器查询对象
  177. const query = wx.createSelectorQuery();
  178. // 执行查询并获取 canvas 节点的实例
  179. query.select('#myQrcode').boundingClientRect()
  180. // 查询结束后执行回调函数
  181. query.exec((res) => {
  182. if (res[0]) {
  183. // res[0] 是 canvas 节点信息,确保节点存在
  184. wx.canvasToTempFilePath({
  185. canvasId: 'myQrcode',
  186. success(res) {
  187. const tempFilePath = res.tempFilePath;
  188. console.log(tempFilePath);
  189. that.setData({
  190. qrcodeUrl: tempFilePath
  191. })
  192. },
  193. fail(err) {
  194. console.error( err);
  195. }
  196. });
  197. } else {
  198. console.error('未能找到对应的 canvas 节点');
  199. }
  200. });
  201. },
  202. // 保存图片到本地
  203. saveToLocal: function() {
  204. const that = this;
  205. if (!that.data.shareImagePath) {
  206. wx.showToast({
  207. title: '图片生成中,请稍候',
  208. icon: 'none'
  209. });
  210. return;
  211. }
  212. // 获取保存图片到相册的权限
  213. wx.getSetting({
  214. success: function(res) {
  215. if (!res.authSetting['scope.writePhotosAlbum']) {
  216. wx.authorize({
  217. scope: 'scope.writePhotosAlbum',
  218. success: function() {
  219. that.saveImageToAlbum();
  220. },
  221. fail: function() {
  222. wx.showModal({
  223. title: '提示',
  224. content: '需要您授权保存图片到相册',
  225. showCancel: false,
  226. success: function() {
  227. wx.openSetting();
  228. }
  229. });
  230. }
  231. });
  232. } else {
  233. that.saveImageToAlbum();
  234. }
  235. }
  236. });
  237. },
  238. // 保存图片到相册
  239. saveImageToAlbum: function() {
  240. const that = this;
  241. wx.saveImageToPhotosAlbum({
  242. filePath: that.data.shareImagePath,
  243. success: function() {
  244. wx.showToast({
  245. title: '保存成功',
  246. icon: 'success'
  247. });
  248. },
  249. fail: function() {
  250. wx.showToast({
  251. title: '保存失败',
  252. icon: 'none'
  253. });
  254. }
  255. });
  256. },
  257. // 复制链接
  258. copyLink: function() {
  259. wx.setClipboardData({
  260. data: this.data.shareUrl,
  261. success: function() {
  262. wx.showToast({
  263. title: '链接已复制',
  264. icon: 'success'
  265. });
  266. }
  267. });
  268. },
  269. // 分享到微信好友
  270. shareToWechat: function() {
  271. if (!this.data.shareImagePath) {
  272. wx.showToast({
  273. title: '图片生成中,请稍候',
  274. icon: 'none'
  275. });
  276. return;
  277. }
  278. wx.showShareImageMenu({
  279. path: this.data.shareImagePath
  280. })
  281. },
  282. // 分享到朋友圈
  283. shareToTimeline: function() {
  284. if (!this.data.shareImagePath) {
  285. wx.showToast({
  286. title: '图片生成中,请稍候',
  287. icon: 'none'
  288. });
  289. return;
  290. }
  291. wx.showShareMenu({
  292. withShareTicket: true,
  293. menus: ['shareTimeline']
  294. });
  295. },
  296. // 分享给朋友(小程序自带分享功能)
  297. onShareAppMessage: function() {
  298. return {
  299. title: this.data.itemDetail ? this.data.itemDetail.title : '文物详情',
  300. path: '/pages/guicangDetails/index?id=' + this.data.id,
  301. imageUrl: this.data.shareImagePath || ''
  302. };
  303. },
  304. // 关闭下载APP提示栏
  305. closeAppBanner: function() {
  306. this.setData({
  307. showAppBanner: false
  308. });
  309. },
  310. // 下载APP - 跳转到苹果商店
  311. downloadApp: function() {
  312. // 苹果商店链接,需要替换为实际的App Store链接
  313. const appStoreUrl = 'https://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124';
  314. // 使用 wx.miniapp.openUrl 直接打开第三方App
  315. wx.miniapp.openUrl({
  316. url: appStoreUrl,
  317. success: () => {
  318. console.log('成功打开App Store');
  319. },
  320. fail: (err) => {
  321. console.log('打开App Store失败:', err);
  322. // 如果直接打开失败,则使用备用方案
  323. wx.showModal({
  324. title: '下载APP',
  325. content: '即将跳转到App Store下载归藏APP',
  326. confirmText: '前往下载',
  327. cancelText: '取消',
  328. success: (res) => {
  329. if (res.confirm) {
  330. wx.setClipboardData({
  331. data: appStoreUrl,
  332. success: () => {
  333. wx.showToast({
  334. title: '链接已复制,请在浏览器中打开',
  335. icon: 'none',
  336. duration: 3000
  337. });
  338. }
  339. });
  340. }
  341. }
  342. });
  343. }
  344. });
  345. }
  346. })