article.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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({ value: e.detail.value })
  76. },
  77. /**
  78. * 搜索店铺
  79. */
  80. onConfirm() {
  81. this.getArticleList(1)
  82. },
  83. /**
  84. * 切换状态
  85. */
  86. onTabsChange(e) {
  87. let current = e.currentTarget.dataset.id
  88. if (current == this.data.current) {
  89. return;
  90. }
  91. this.setData({
  92. current,
  93. page: 1,
  94. }, () => {
  95. this.getArticleList(1)
  96. })
  97. },
  98. /**
  99. * 获取美丽秘籍分类
  100. * /api/info/classify
  101. */
  102. getClassify() {
  103. get('api/info/classify',{},(res) => {
  104. this.setData({
  105. classify: res.data.list
  106. },() => {
  107. this.getArticleList()
  108. })
  109. })
  110. },
  111. /**
  112. * 获取美丽秘籍列表
  113. * /api/info
  114. */
  115. getArticleList(_page) {
  116. this.setData({
  117. spinning: true
  118. })
  119. let {
  120. tempPics,
  121. classify,
  122. page,
  123. value,
  124. current
  125. } = this.data;
  126. get('api/info', {
  127. page: _page || page,
  128. limit: 10,
  129. keyword: value,
  130. classify_id: classify[current].id,
  131. }, (res) => {
  132. res.data.list.map(item => {
  133. item.create_time = item.create_time.substring(0, 10)
  134. })
  135. if (_page == 1 || page == 1) {
  136. tempPics = []
  137. this.data.page = 1
  138. }
  139. tempPics.push(...res.data.list)
  140. this.setData({
  141. tempPics,
  142. total: res.data.total,
  143. spinning: false
  144. })
  145. this.jsData.isListLoading = true
  146. })
  147. },
  148. //获取图片尺寸数据
  149. loadPic: function (e) {
  150. var that = this,
  151. data = that.data,
  152. tempPics = data.tempPics,
  153. index = e.currentTarget.dataset.index
  154. if (tempPics[index]) {
  155. //以750为宽度算出相对应的高度
  156. tempPics[index].height = e.detail.height * 750 / e.detail.width
  157. tempPics[index].isLoad = true
  158. }
  159. that.setData({
  160. tempPics: tempPics
  161. }, function () {
  162. that.finLoadPic()
  163. })
  164. },
  165. //图片加载错误处理
  166. loadPicError: function (e) {
  167. var that = this,
  168. data = that.data,
  169. tempPics = data.tempPics,
  170. index = e.currentTarget.dataset.index
  171. if (tempPics[index]) {
  172. //图片加载错误时高度固定750,展示为正方形
  173. tempPics[index].height = 750
  174. tempPics[index].isLoad = true
  175. }
  176. that.setData({
  177. tempPics: tempPics
  178. }, function () {
  179. that.finLoadPic()
  180. })
  181. },
  182. //判断图片是否加载完成
  183. finLoadPic: function () {
  184. var that = this,
  185. data = that.data,
  186. tempPics = data.tempPics,
  187. length = tempPics.length,
  188. fin = true
  189. for (var i = 0; i < length; i++) {
  190. if (!tempPics[i].isLoad) {
  191. fin = false
  192. break
  193. }
  194. }
  195. if (fin) {
  196. if (that.jsData.isListLoading) {
  197. that.jsData.isListLoading = false
  198. that.renderPage()
  199. }
  200. }
  201. },
  202. //渲染到瀑布流
  203. renderPage: function () {
  204. var that = this,
  205. data = that.data,
  206. columns = data.columns,
  207. tempPics = data.tempPics,
  208. length = tempPics.length,
  209. columnsHeight = that.jsData.columnsHeight,
  210. index = 0
  211. if (this.data.page == 1) {
  212. columns = [
  213. [],
  214. []
  215. ]
  216. }
  217. for (var i = 0; i < length; i++) {
  218. index = columnsHeight[1] < columnsHeight[0] ? 1 : 0
  219. columns[index].push(tempPics[i])
  220. columnsHeight[index] += tempPics[i].height
  221. }
  222. that.setData({
  223. columns: columns,
  224. tempPics: []
  225. })
  226. that.jsData.columnsHeight = columnsHeight
  227. },
  228. onClear() {
  229. this.setData({
  230. value: ''
  231. })
  232. },
  233. // 点击跳转美丽秘籍
  234. goArticleDetail(e) {
  235. let id = e.currentTarget.dataset.id
  236. wx.navigateTo({
  237. url: `/pages/articleDetail/articleDetail?id=${id}`,
  238. })
  239. },
  240. })