library.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // parents/pages/library/library.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal, getWxLocationSetting } from '../../../utils/util.js'
  3. import { GetLibrary,GetMineInfo } from '../../../utils/api.js'
  4. const { navigateTo, redirectTo, navigateBack } = routers()
  5. const { globalData } = getApp()
  6. const { baseImgUrl } = globalData
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. isLoaded: 0,
  13. latitude: '',
  14. longitude: '',
  15. pageNo: 1,
  16. pageSize: 10,
  17. libraryList: [],
  18. isAll: 0,
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. if (!options.isAdd){
  25. options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`) || ''
  26. }
  27. this.setData({ options })
  28. if (options.isAdd){
  29. this.getLocation(res => {
  30. this.setData({ isLoaded: 1 })
  31. })
  32. }else{
  33. this.getData()
  34. }
  35. // this.getLocation(res => {
  36. // this.setData({ isLoaded: 1 })
  37. // })
  38. },
  39. /**
  40. * 生命周期函数--监听页面初次渲染完成
  41. */
  42. onReady: function () {
  43. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  44. for (const i in obj) {
  45. this[i] = obj[i]
  46. }
  47. },
  48. /**
  49. * 生命周期函数--监听页面显示
  50. */
  51. onShow: function () {
  52. },
  53. /**
  54. * 生命周期函数--监听页面隐藏
  55. */
  56. onHide: function () {
  57. },
  58. /**
  59. * 生命周期函数--监听页面卸载
  60. */
  61. onUnload: function () {
  62. },
  63. /**
  64. * 页面相关事件处理函数--监听用户下拉动作
  65. */
  66. onPullDownRefresh: function () {
  67. const { libraryList}=this.data
  68. if (libraryList.length<11) return
  69. this.setData({ pageNo: 1 })
  70. this.getLibrary(res => {
  71. wx.stopPullDownRefresh()
  72. })
  73. },
  74. /**
  75. * 页面上拉触底事件的处理函数
  76. */
  77. onReachBottom: function () {
  78. const { isAll } = this.data
  79. if (isAll) return
  80. this.getLibrary()
  81. },
  82. /**
  83. * 用户点击右上角分享
  84. */
  85. onShareAppMessage: function () {
  86. if (isFn(sharePage)) return sharePage()
  87. },
  88. /**
  89. * 获取定位
  90. */
  91. getLocation: function (cb) {
  92. getWxLocationSetting(res => {
  93. this.setData({
  94. isShowPosition: false
  95. })
  96. wx.showLoading({
  97. title: '定位中'
  98. })
  99. wx.getLocation({
  100. type: 'wgs84',
  101. success: res => {
  102. const { latitude, longitude } = res
  103. this.setData({
  104. latitude,
  105. longitude
  106. })
  107. this.getLibrary(cb)
  108. },
  109. fail: res => {
  110. wx.showToast({
  111. title: '定位失败',
  112. icon: 'none'
  113. })
  114. },
  115. complete: res => {
  116. wx.hideLoading()
  117. }
  118. })
  119. })
  120. },
  121. /**
  122. * 获取图书馆
  123. */
  124. getData: function (cb) {
  125. const continuousFn = { fn: this.getData, param: { ...arguments } }
  126. GetMineInfo({ data: {}, continuousFn }).then(res => {
  127. const { vipList } = res.data
  128. const data = (list => {
  129. const arr = []
  130. for (const v of list) {
  131. const { lname = '', lib_id = ''} = v
  132. arr.push({
  133. league_name: lname ,
  134. id: lib_id
  135. })
  136. }
  137. return arr
  138. })(vipList)
  139. if (data.length>0){
  140. this.setData({ libraryList: data, isLoaded: 1 })
  141. }else{
  142. this.getLocation(res => {
  143. this.setData({ isLoaded: 1 })
  144. })
  145. }
  146. cb && cb(res)
  147. }).catch(res => {
  148. cb && cb(res)
  149. })
  150. },
  151. /**
  152. * 获取图书馆
  153. */
  154. getLibrary: function (cb) {
  155. const continuousFn = { fn: this.getLibrary, param: { ...arguments } }
  156. const { options, latitude, longitude, pageNo, pageSize, libraryList } = this.data
  157. const { vipId } = options
  158. GetLibrary({ data: { vipId, pageNo, pageSize, leagueLat: latitude, leagueLng: longitude }, continuousFn }).then(res => {
  159. const { list } = res.data
  160. let listTemp = []
  161. if (pageNo == 1) {
  162. listTemp = [].concat(list)
  163. } else {
  164. listTemp = [].concat(libraryList, list)
  165. }
  166. this.setData({
  167. libraryList: listTemp,
  168. pageNo: list.length == pageSize ? pageNo + 1 : pageNo,
  169. isAll: list.length < pageSize
  170. })
  171. cb && cb(res)
  172. }).catch(res => {
  173. cb && cb(res)
  174. })
  175. },
  176. /**
  177. * 获取图书馆
  178. */
  179. selectLibraryCtl: function (e) {
  180. const { index } = e.currentTarget.dataset
  181. const { libraryList,options } = this.data
  182. const { id, league_name } = libraryList[index]
  183. const { vipId = '', sex = '', school = '', grade = '', childName = ''}=options
  184. if (id) {
  185. if (options.isAdd){
  186. redirectTo({ url: 'parents/pages/information/information', paras: { zindex: 3, vipId, sex, school, grade, childName, libId: id, league_name} })
  187. }else{
  188. getApp().globalData.library = libraryList[index]
  189. wx.setStorageSync('library', libraryList[index])
  190. redirectTo({ url: 'parents/pages/me/me', zindex: 3 })
  191. }
  192. } else {
  193. wx.showToast({
  194. title: '该图书馆无效',
  195. icon: 'none'
  196. })
  197. }
  198. }
  199. })