library.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. content: '',
  18. isResult: false,
  19. libraryList: [],
  20. isAll: 0,
  21. nodataArray: {
  22. text: "搜索无结果"
  23. }
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad: function (options) {
  29. if (!options.isAdd){
  30. options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`) || ''
  31. }
  32. if (options.isAdd==1){
  33. this.getLocation(res => {
  34. this.setData({ isLoaded: 1 })
  35. })
  36. }else{
  37. this.getData()
  38. }
  39. this.setData({ options })
  40. console.log(options)
  41. // console.log(options.isAdd)
  42. // this.getLocation(res => {
  43. // this.setData({ isLoaded: 1 })
  44. // })
  45. },
  46. /**
  47. * 生命周期函数--监听页面初次渲染完成
  48. */
  49. onReady: function () {
  50. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  51. for (const i in obj) {
  52. this[i] = obj[i]
  53. }
  54. },
  55. /**
  56. * 生命周期函数--监听页面显示
  57. */
  58. onShow: function () {
  59. let _this = this
  60. wx.getStorage({
  61. key: 'setStoragelibraryList',
  62. success: function (res) {
  63. const setStoragelibraryList = res.data
  64. _this.setData({
  65. setStoragelibraryList: setStoragelibraryList,
  66. })
  67. // console.log(setStoragelibraryList)
  68. }
  69. })
  70. },
  71. /**
  72. * 生命周期函数--监听页面隐藏
  73. */
  74. onHide: function () {
  75. },
  76. /**
  77. * 生命周期函数--监听页面卸载
  78. */
  79. onUnload: function () {
  80. },
  81. returnBack:function(){
  82. wx.navigateBack({
  83. })
  84. },
  85. /**
  86. * 输入搜索值
  87. */
  88. inputSearchCtl: function (e) {
  89. const { value } = e.detail
  90. const { content } = this.data
  91. if (content == value) {
  92. return
  93. }
  94. this.setData({ content: value, pageNo: 1 })
  95. this.setData({ isResult: true })
  96. this.getLibrary()
  97. },
  98. /**
  99. * 清除搜索值
  100. */
  101. clearSearchCtl: function () {
  102. this.setData({ content: '' })
  103. },
  104. /**
  105. * 页面相关事件处理函数--监听用户下拉动作
  106. */
  107. onPullDownRefresh: function () {
  108. const { libraryList}=this.data
  109. if (libraryList.length<11) return
  110. this.setData({ pageNo: 1 })
  111. this.getLibrary(res => {
  112. wx.stopPullDownRefresh()
  113. })
  114. },
  115. /**
  116. * 页面上拉触底事件的处理函数
  117. */
  118. onReachBottom: function () {
  119. const { isAll } = this.data
  120. if (isAll) return
  121. this.getLibrary()
  122. },
  123. /**
  124. * 用户点击右上角分享
  125. */
  126. onShareAppMessage: function () {
  127. if (isFn(sharePage)) return sharePage()
  128. },
  129. /**
  130. * 获取定位
  131. */
  132. getLocation: function (cb) {
  133. getWxLocationSetting(res => {
  134. this.setData({
  135. isShowPosition: false
  136. })
  137. wx.showLoading({
  138. title: '定位中'
  139. })
  140. wx.getLocation({
  141. type: 'wgs84',
  142. success: res => {
  143. const { latitude, longitude } = res
  144. this.setData({
  145. latitude,
  146. longitude
  147. })
  148. this.getLibrary(cb)
  149. },
  150. fail: res => {
  151. wx.showToast({
  152. title: '定位失败',
  153. icon: 'none'
  154. })
  155. },
  156. complete: res => {
  157. wx.hideLoading()
  158. }
  159. })
  160. })
  161. },
  162. /**
  163. * 获取图书馆
  164. */
  165. getData: function (cb) {
  166. const continuousFn = { fn: this.getData, param: { ...arguments } }
  167. GetMineInfo({ data: {}, continuousFn }).then(res => {
  168. const { vipList } = res.data
  169. const data = (list => {
  170. const arr = []
  171. for (const v of list) {
  172. const { lname = '', lib_id = '',type=''} = v
  173. arr.push({
  174. league_name: lname,
  175. id: lib_id,
  176. type:type
  177. })
  178. }
  179. return arr
  180. })(vipList)
  181. console.log(data)
  182. if (data.length>0){
  183. this.setData({ libraryList: data, isLoaded:1})
  184. }else{
  185. this.getLocation(res => {
  186. this.setData({ isLoaded: 1 })
  187. })
  188. }
  189. cb && cb(res)
  190. }).catch(res => {
  191. cb && cb(res)
  192. })
  193. },
  194. /**
  195. * 获取图书馆
  196. */
  197. getLibrary: function (cb) {
  198. const continuousFn = { fn: this.getLibrary, param: { ...arguments } }
  199. const { options, latitude, longitude, pageNo, pageSize, libraryList, content } = this.data
  200. const { vipId } = options
  201. GetLibrary({ data: { vipId, pageNo, pageSize, leagueLat: latitude, leagueLng: longitude, leagueName: content }, continuousFn }).then(res => {
  202. const { list } = res.data
  203. let listTemp = []
  204. if (pageNo == 1) {
  205. listTemp = [].concat(list)
  206. } else {
  207. listTemp = [].concat(libraryList, list)
  208. }
  209. this.setData({
  210. libraryList: listTemp,
  211. pageNo: list.length == pageSize ? pageNo + 1 : pageNo,
  212. isAll: list.length < pageSize
  213. })
  214. cb && cb(res)
  215. }).catch(res => {
  216. cb && cb(res)
  217. })
  218. },
  219. /**
  220. * 获取图书馆
  221. */
  222. selectLibraryCtl: function (e) {
  223. const { index } = e.currentTarget.dataset
  224. const { libraryList,options } = this.data
  225. const { id, league_name } = libraryList[index]
  226. const { vipId = '', sex = '', school = '', grade = '', childName = '',isAdd,phone='',parentRole='',parentName='',imgUrl='',zindex}=options
  227. wx.setStorageSync('setStoragelibraryList', libraryList[index])
  228. console.log(isAdd)
  229. if (id) {
  230. if (isAdd==1){
  231. redirectTo({ url: 'parents/pages/information/information', paras: { zindex: 3, vipId, sex, school, grade, childName, libId: id, league_name, isAdd,phone,parentRole,parentName,imgUrl,isSchool:1} })
  232. }else{
  233. getApp().globalData.library = libraryList[index]
  234. wx.setStorageSync('library', libraryList[index])
  235. redirectTo({ url: 'parents/pages/me/me', zindex: 3 })
  236. }
  237. } else {
  238. wx.showToast({
  239. title: '该图书馆无效',
  240. icon: 'none'
  241. })
  242. }
  243. }
  244. })