artInfo.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. Page({
  2. data: {
  3. graceSkeleton: true,
  4. //文章对象格式
  5. //{
  6. // title : "标题",
  7. // authorFcae : "作者头像",
  8. // authorName : "作者姓名",
  9. // viewNumber : "浏览次数",
  10. // date : "日期",
  11. // contents : [
  12. // {type : "text", content : "文本内容"},
  13. // {type : "img", content : "图片路径"},
  14. // //.....
  15. // ]
  16. //}
  17. article: {}
  18. },
  19. onLoad: function () {
  20. // 加载文章详情
  21. wx.showLoading({});
  22. wx.request({
  23. url: 'http://grace.hcoder.net/api/news/info',
  24. method: 'GET',
  25. data: {},
  26. success: (res) => {
  27. console.log(res)
  28. var news = res.data.data;
  29. wx.setNavigationBarTitle({ title: news.title });
  30. // 此处先规划骨架
  31. var art = { contents: [] };
  32. for (var i = 0; i < news.contents.length; i++) {
  33. art.contents.push({ 'type': news.contents[i].type });
  34. }
  35. this.setData({ article: art });
  36. // 骨架屏规划后延长 500 毫秒进行数据替换
  37. setTimeout(() => {
  38. this.setData({ article: news, graceSkeleton: false });
  39. }, 500);
  40. },
  41. fail: () => { },
  42. complete: () => {
  43. wx.hideLoading();
  44. }
  45. });
  46. },
  47. prevImg: function (e) {
  48. var imgs = [];
  49. var currentUrl = e.currentTarget.dataset.imgurl;
  50. for (let i = 0; i < this.data.article.contents.length; i++) {
  51. if (this.data.article.contents[i].type == 'img') {
  52. imgs.push(this.data.article.contents[i].content);
  53. }
  54. }
  55. wx.previewImage({
  56. urls: imgs,
  57. current: currentUrl
  58. })
  59. }
  60. })