wish.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // parents/pages/wish/wish.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
  3. import { MyWishBook, OthersWishBook, WishHelp } from '../../../utils/api.js'
  4. const { navigateTo, redirectTo, navigateBack } = routers()
  5. const { globalData, hasLibraryService, hasVipService } = getApp()
  6. const { baseImgUrl, qrcodeInvalidToastText } = globalData
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. baseImgUrl,
  13. isLoaded: 0,
  14. isShowWishModal: 0,
  15. navIndex: 0,
  16. navList: [
  17. {
  18. text: '我的心愿',
  19. num: 0,
  20. pageNo: 1,
  21. pageSize: 10,
  22. isAll: 0,
  23. list: [],
  24. nodataArray: {
  25. text: '暂没有心愿书单',
  26. image: '../../../assets/nodata_4.png',
  27. hasNodataBtn: 1,
  28. btnText: '去书库看看'
  29. }
  30. },
  31. {
  32. text: '他们的心愿',
  33. num: 0,
  34. pageNo: 1,
  35. pageSize: 10,
  36. isAll: 0,
  37. list: [],
  38. nodataArray: {
  39. text: '暂没有心愿书单',
  40. image: '../../../assets/nodata_5.png',
  41. hasNodataBtn: 1,
  42. btnText: '去书库看看'
  43. }
  44. }
  45. ],
  46. hotBooks: [
  47. // {
  48. // imgUrl: '',
  49. // bookName: '绝地反击是绝地反击是绝地反击是绝地反击是绝地反击是',
  50. // hotNum: 756656565656568
  51. // },
  52. // {
  53. // imgUrl: '',
  54. // bookName: '绝地反击是',
  55. // hotNum: 78
  56. // },
  57. // {
  58. // imgUrl: '',
  59. // bookName: '绝地反击是',
  60. // hotNum: 78
  61. // },
  62. // {
  63. // imgUrl: '',
  64. // bookName: '绝地反击是',
  65. // hotNum: 78
  66. // },
  67. // {
  68. // imgUrl: '',
  69. // bookName: '绝地反击是',
  70. // hotNum: 78565656565656568
  71. // },
  72. // {
  73. // imgUrl: '',
  74. // bookName: '绝地反击是',
  75. // hotNum: 78
  76. // },
  77. // {
  78. // imgUrl: '',
  79. // bookName: '绝地反击是',
  80. // hotNum: 78
  81. // }
  82. ]
  83. },
  84. /**
  85. * 生命周期函数--监听页面加载
  86. */
  87. onLoad: function (options) {
  88. options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`) || ''
  89. options.libId = getGlobalVal('library').id || ''
  90. this.setData({ options })
  91. this.getData(res => {
  92. this.setData({ isLoaded: 1 })
  93. })
  94. const isShowWishModal = getGlobalVal('wishModal')
  95. if (!isShowWishModal) {
  96. this.setData({ isShowWishModal: 1 })
  97. }
  98. },
  99. /**
  100. * 生命周期函数--监听页面初次渲染完成
  101. */
  102. onReady: function () {
  103. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  104. for (const i in obj) {
  105. this[i] = obj[i]
  106. }
  107. },
  108. /**
  109. * 生命周期函数--监听页面显示
  110. */
  111. onShow: function () {
  112. },
  113. /**
  114. * 生命周期函数--监听页面隐藏
  115. */
  116. onHide: function () {
  117. },
  118. /**
  119. * 生命周期函数--监听页面卸载
  120. */
  121. onUnload: function () {
  122. },
  123. /**
  124. * 页面相关事件处理函数--监听用户下拉动作
  125. */
  126. onPullDownRefresh: function () {
  127. const { navList, navIndex } = this.data
  128. navList[navIndex]['pageNo'] = 1
  129. this.getData(res => {
  130. wx.stopPullDownRefresh()
  131. })
  132. },
  133. /**
  134. * 页面上拉触底事件的处理函数
  135. */
  136. onReachBottom: function () {
  137. const { navList, navIndex } = this.data
  138. const { isAll } = navList[navIndex]
  139. if (isAll) {
  140. return
  141. }
  142. this.getData()
  143. },
  144. /**
  145. * 用户点击右上角分享
  146. */
  147. onShareAppMessage: function () {
  148. const { index } = e.target.dataset
  149. const { navList, navIndex, options } = this.data
  150. const { vipId, libId } = options
  151. const { isbn13 } = navList[navIndex]['list'][index]
  152. const { child_name = '' } = getGlobalVal('vip', `vip_${getGlobalVal('userId')}`)
  153. return {
  154. title: `${child_name}的心愿书单详情`,
  155. path: `parents/pages/wish_detail/wish_detail?type=10&vipId=${vipId}&isbn13=${isbn13}&libId=${libId}`,
  156. success: res => {
  157. wx.showToast({
  158. title: '转发成功',
  159. icon: 'success'
  160. })
  161. }
  162. }
  163. },
  164. /**
  165. * 获取数据
  166. */
  167. getData: function (cb) {
  168. const continuousFn = { fn: this.getData, param: { ...arguments } }
  169. const { navList, navIndex, options } = this.data
  170. const { pageNo, pageSize, list } = navList[navIndex]
  171. const { vipId, libId } = options
  172. const Fn = [MyWishBook, OthersWishBook]
  173. const data = [{ vipId, libId, pageNo, pageSize }, { libId, pageNo, pageSize }]
  174. if (navIndex == 0 && !hasVipService()) {
  175. cb && cb()
  176. return
  177. }
  178. if (!hasLibraryService()) {
  179. cb && cb()
  180. return
  181. }
  182. Fn[navIndex]({ data: data[navIndex], continuousFn }).then(res => {
  183. const newList = res.data.list
  184. let listTemp = []
  185. if (pageNo == 1) {
  186. listTemp = [].concat(newList)
  187. } else {
  188. listTemp = [].concat(list, newList)
  189. }
  190. navList[navIndex]['list'] = listTemp
  191. navList[navIndex]['pageNo'] = newList.length == pageSize ? pageNo + 1 : pageNo
  192. navList[navIndex]['isAll'] = newList.length < pageSize
  193. this.setData({ navList })
  194. cb && cb(res)
  195. }).catch(res => {
  196. cb && cb(res)
  197. })
  198. },
  199. /**
  200. * 选择导航栏
  201. */
  202. selectNavCtl: function (e) {
  203. const { index } = e.currentTarget.dataset
  204. const { navIndex, navList } = this.data
  205. if (navIndex == index) return
  206. this.setData({ navIndex: index })
  207. const { list } = navList[index]
  208. if (list.length == 0) {
  209. this.getData()
  210. }
  211. },
  212. /**
  213. * 切换心愿弹框
  214. */
  215. toggleWishModal: function () {
  216. this.setData({ isShowWishModal: 0 })
  217. wx.setStorageSync('wishModal', 1)
  218. },
  219. /**
  220. * 助力
  221. */
  222. helpWishCtl: function (e) {
  223. const continuousFn = { fn: this.helpWishCtl, param: { ...arguments } }
  224. if (!hasVipService() || !hasLibraryService()) return
  225. const { index } = e.currentTarget.dataset
  226. const { navIndex, navList, options } = this.data
  227. const { vipId, libId } = options
  228. const { list } = navList[navIndex]
  229. const { isbn13 } = list[index]
  230. WishHelp({ data: { vipId, isbn13 }, continuousFn }).then(res => {
  231. wx.showToast({
  232. title: '助力成功',
  233. icon: 'none'
  234. })
  235. })
  236. },
  237. /**
  238. * 没有数据模板按钮触发事件
  239. */
  240. nodataBtnCtl: function () {
  241. redirectTo({ url: 'parents/pages/home/home', zindex: 3 })
  242. }
  243. })