article.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import {
  2. get,
  3. post
  4. } from '../../utils/http';
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. jsData: {
  10. columnsHeight: [0, 0],
  11. isListLoading: false
  12. },
  13. data: {
  14. current: 0,
  15. value: '',
  16. classify: [],
  17. list: [],
  18. total: 0,
  19. page: 1,
  20. spinning: true,
  21. columns: [
  22. [],
  23. []
  24. ],
  25. tempPics: [], //美丽秘籍内容列表
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad: function (options) {
  31. },
  32. /**
  33. * 生命周期函数--监听页面初次渲染完成
  34. */
  35. onReady: function () {
  36. },
  37. /**
  38. * 生命周期函数--监听页面显示
  39. */
  40. onShow: function () {
  41. this.getClassify()
  42. },
  43. /**
  44. * 生命周期函数--监听页面隐藏
  45. */
  46. onHide: function () {
  47. },
  48. /**
  49. * 生命周期函数--监听页面卸载
  50. */
  51. onUnload: function () {
  52. },
  53. /**
  54. * 页面相关事件处理函数--监听用户下拉动作
  55. */
  56. onPullDownRefresh: function () {
  57. },
  58. /**
  59. * 页面上拉触底事件的处理函数
  60. */
  61. onReachBottom: function () {
  62. if (this.data.page * 10 < this.data.total) {
  63. this.getArticleList(++this.data.page)
  64. }
  65. },
  66. /**
  67. * 用户点击右上角分享
  68. */
  69. onShareAppMessage: function () {
  70. },
  71. /**
  72. * 监听键盘输入
  73. */
  74. onChange(e) {
  75. this.setData({
  76. value: e.detail.value
  77. })
  78. },
  79. /**
  80. * 搜索店铺
  81. */
  82. onConfirm() {
  83. this.getArticleList(1)
  84. },
  85. /**
  86. * 切换状态
  87. */
  88. onTabsChange(e) {
  89. let current = e.currentTarget.dataset.id
  90. if (current == this.data.current) {
  91. return;
  92. }
  93. this.setData({
  94. current,
  95. page: 1,
  96. }, () => {
  97. this.getArticleList(1)
  98. })
  99. },
  100. /**
  101. * 获取美丽秘籍分类
  102. * /api/info/classify
  103. */
  104. getClassify() {
  105. get('api/info/classify', {}, (res) => {
  106. this.setData({
  107. classify: res.data.list
  108. }, () => {
  109. this.getArticleList()
  110. })
  111. })
  112. },
  113. /**
  114. * 获取美丽秘籍列表
  115. * /api/info
  116. */
  117. getArticleList(_page) {
  118. this.setData({
  119. spinning: true
  120. })
  121. let {
  122. tempPics,
  123. classify,
  124. page,
  125. value,
  126. current
  127. } = this.data;
  128. get('api/info', {
  129. page: _page || page,
  130. limit: 10,
  131. keyword: value,
  132. classify_id: classify[current].id,
  133. }, (res) => {
  134. res.data.list.map(item => {
  135. item.create_time = item.create_time.substring(0, 10)
  136. })
  137. if (_page == 1 || page == 1) {
  138. tempPics = []
  139. this.data.page = 1
  140. }
  141. tempPics.push(...res.data.list)
  142. this.setData({
  143. tempPics,
  144. total: res.data.total,
  145. spinning: false
  146. })
  147. this.jsData.isListLoading = true
  148. })
  149. },
  150. //获取图片尺寸数据
  151. loadPic: function (e) {
  152. var that = this,
  153. data = that.data,
  154. tempPics = data.tempPics,
  155. index = e.currentTarget.dataset.index
  156. if (tempPics[index]) {
  157. //以750为宽度算出相对应的高度
  158. tempPics[index].height = e.detail.height * 750 / e.detail.width
  159. tempPics[index].isLoad = true
  160. }
  161. that.setData({
  162. tempPics: tempPics
  163. }, function () {
  164. that.finLoadPic()
  165. })
  166. },
  167. //图片加载错误处理
  168. loadPicError: function (e) {
  169. var that = this,
  170. data = that.data,
  171. tempPics = data.tempPics,
  172. index = e.currentTarget.dataset.index
  173. if (tempPics[index]) {
  174. //图片加载错误时高度固定750,展示为正方形
  175. tempPics[index].height = 750
  176. tempPics[index].isLoad = true
  177. }
  178. that.setData({
  179. tempPics: tempPics
  180. }, function () {
  181. that.finLoadPic()
  182. })
  183. },
  184. //判断图片是否加载完成
  185. finLoadPic: function () {
  186. var that = this,
  187. data = that.data,
  188. tempPics = data.tempPics,
  189. length = tempPics.length,
  190. fin = true
  191. for (var i = 0; i < length; i++) {
  192. if (!tempPics[i].isLoad) {
  193. fin = false
  194. break
  195. }
  196. }
  197. if (fin) {
  198. if (that.jsData.isListLoading) {
  199. that.jsData.isListLoading = false
  200. that.renderPage()
  201. }
  202. }
  203. },
  204. //渲染到瀑布流
  205. renderPage: function () {
  206. var that = this,
  207. data = that.data,
  208. columns = data.columns,
  209. tempPics = data.tempPics,
  210. length = tempPics.length,
  211. columnsHeight = that.jsData.columnsHeight,
  212. index = 0
  213. if (this.data.page == 1) {
  214. columns = [
  215. [],
  216. []
  217. ]
  218. }
  219. for (var i = 0; i < length; i++) {
  220. index = columnsHeight[1] < columnsHeight[0] ? 1 : 0
  221. columns[index].push(tempPics[i])
  222. columnsHeight[index] += tempPics[i].height
  223. }
  224. that.setData({
  225. columns: columns,
  226. tempPics: []
  227. })
  228. that.jsData.columnsHeight = columnsHeight
  229. },
  230. onClear() {
  231. this.setData({
  232. value: ''
  233. })
  234. },
  235. // 点击跳转美丽秘籍
  236. goArticleDetail(e) {
  237. let id = e.currentTarget.dataset.id
  238. wx.navigateTo({
  239. url: `/pages/articleDetail/articleDetail?id=${id}`,
  240. })
  241. },
  242. })