home.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // parents/pages/home/home.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal, getWxLocationSetting } from '../../../utils/util.js'
  3. import { GetLibrary, GetParentHome, GetBorrowBookNum, ScanningBooks } 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. isFirst: 0,
  14. latitude: '',
  15. longitude: '',
  16. isLoaded: 0,
  17. pageNo: 1,
  18. pageSize: 10,
  19. routers: [
  20. {
  21. icon: '../../../assets/wo_yao_jie_shu.png',
  22. text: '我要借书',
  23. url: 'parents/pages/borrow/borrow',
  24. zindex: 3,
  25. },
  26. {
  27. icon: '../../../assets/yu_yue_jie_shu.png',
  28. text: '预约借书',
  29. url: 'parents/pages/reservations/reservations',
  30. zindex: 3,
  31. },
  32. {
  33. icon: '../../../assets/cheng_zhang_ji_lu.png',
  34. text: '成长记录',
  35. url: 'parents/pages/growth/growth',
  36. zindex: 3,
  37. }
  38. ],
  39. topList: [],
  40. list: [],
  41. footerData: {
  42. actIndex: 0,
  43. list: [
  44. {
  45. text: '找好书',
  46. icon: '../../../assets/book_gray.png',
  47. actIcon: '../../../assets/book_blue.png',
  48. url: 'parents/pages/home/home',
  49. zindex: 3,
  50. },
  51. {
  52. text: '好书推荐',
  53. icon: '../../../assets/wish_gray.png',
  54. actIcon: '../../../assets/wish_blue.png',
  55. url: 'parents/pages/good_books/good_books',
  56. zindex: 3,
  57. },
  58. {
  59. text: '我的',
  60. icon: '../../../assets/me_gray.png',
  61. actIcon: '../../../assets/me_blue.png',
  62. url: 'parents/pages/me/me',
  63. zindex: 3,
  64. }
  65. ]
  66. }
  67. },
  68. /**
  69. * 生命周期函数--监听页面加载
  70. */
  71. onLoad: function (options) {
  72. const userId = getGlobalVal('userId')
  73. options.vipId = getGlobalVal('vipId', `vipId_${userId}`) || ''
  74. options.library = getGlobalVal('library')
  75. this.setData({ options, isFirst: 1 })
  76. if (options.library) {
  77. this.getList(res => {
  78. this.setData({ isLoaded: 1 })
  79. })
  80. } else {
  81. this.getLocation(res => {
  82. this.setData({ isLoaded: 1 })
  83. })
  84. }
  85. },
  86. /**
  87. * 生命周期函数--监听页面初次渲染完成
  88. */
  89. onReady: function () {
  90. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  91. for (const i in obj) {
  92. this[i] = obj[i]
  93. }
  94. },
  95. /**
  96. * 生命周期函数--监听页面显示
  97. */
  98. onShow: function () {
  99. const { options, isFirst } = this.data
  100. options.library = getGlobalVal('library')
  101. this.setData({ options })
  102. if (!options.library && !isFirst) {
  103. this.getLocation()
  104. }
  105. },
  106. /**
  107. * 生命周期函数--监听页面隐藏
  108. */
  109. onHide: function () {
  110. },
  111. /**
  112. * 生命周期函数--监听页面卸载
  113. */
  114. onUnload: function () {
  115. },
  116. /**
  117. * 页面相关事件处理函数--监听用户下拉动作
  118. */
  119. onPullDownRefresh: function () {
  120. this.getList(res => {
  121. wx.stopPullDownRefresh()
  122. })
  123. },
  124. /**
  125. * 页面上拉触底事件的处理函数
  126. */
  127. onReachBottom: function () {
  128. },
  129. /**
  130. * 用户点击右上角分享
  131. */
  132. onShareAppMessage: function () {
  133. if (isFn(sharePage)) return sharePage()
  134. },
  135. /**
  136. * 获取定位
  137. */
  138. getLocation: function (cb) {
  139. getWxLocationSetting(res => {
  140. this.setData({
  141. isShowPosition: false
  142. })
  143. wx.showLoading({
  144. title: '定位中'
  145. })
  146. wx.getLocation({
  147. type: 'wgs84',
  148. success: res => {
  149. const { latitude, longitude } = res
  150. this.setData({
  151. latitude,
  152. longitude
  153. })
  154. this.getLibrary(cb)
  155. },
  156. fail: res => {
  157. wx.showToast({
  158. title: '定位失败',
  159. icon: 'none'
  160. })
  161. },
  162. complete: res => {
  163. wx.hideLoading()
  164. }
  165. })
  166. })
  167. },
  168. /**
  169. * 获取图书馆
  170. */
  171. getLibrary: function (cb) {
  172. const continuousFn = { fn: this.getLibrary, param: { ...arguments } }
  173. const { options, latitude, longitude, pageNo, pageSize } = this.data
  174. const { vipId } = options
  175. GetLibrary({ data: { vipId, pageNo, pageSize, leagueLat: latitude, leagueLng: longitude }, continuousFn }).then(res => {
  176. const { list } = res.data
  177. if (list[0]['id']) {
  178. getApp().globalData.library = list[0]
  179. wx.setStorageSync('library', list[0])
  180. this.setData({ 'options.library': list[0] })
  181. this.getList(cb)
  182. }
  183. })
  184. },
  185. /**
  186. * 获取首页数据
  187. */
  188. getList: function (cb) {
  189. const continuousFn = { fn: this.getList, param: { ...arguments } }
  190. const libId = this.data.options.library.id || ''
  191. hasLibraryService()
  192. if (!libId) {
  193. wx.showToast({
  194. title: '请先定位图书馆',
  195. icon: 'none'
  196. })
  197. return
  198. }
  199. GetParentHome({ data: { libId }, continuousFn }).then(res => {
  200. const { topList, list } = res.data
  201. this.setData({ topList, list })
  202. cb && cb(res)
  203. }).catch(res => {
  204. cb && cb(res)
  205. })
  206. },
  207. /**
  208. * 选择跳转进入页
  209. */
  210. selectRouteCtl: function (e) {
  211. const { index } = e.currentTarget.dataset
  212. const { routers } = this.data
  213. const { url, zindex } = routers[index]
  214. if (!hasVipService()) return
  215. navigateTo({ url, zindex })
  216. },
  217. /**
  218. * 调用微信扫一扫之前判断可借书多少本
  219. */
  220. scanCtl: function () {
  221. const continuousFn = { fn: this.scanCtl, param: { ...arguments } }
  222. const { vipId, library } = this.data.options
  223. const libId = library.id || ''
  224. if (!hasVipService() || !hasLibraryService()) return
  225. GetBorrowBookNum({ data: { vipId, libId }, continuousFn }).then(res => {
  226. const { totalBook, lentBook } = res.data
  227. wx.showToast({
  228. title: `您还可以借${totalBook}本书`,
  229. icon: 'none'
  230. })
  231. if (totalBook > 0) {
  232. this.scanFn()
  233. }
  234. this.setData({ ...res.data })
  235. })
  236. },
  237. /**
  238. * 调用微信扫一扫
  239. */
  240. scanFn: function () {
  241. wx.scanCode({
  242. success: res => {
  243. const url = res.result
  244. const detailsId = url.split('_')[1].split('.')[0]
  245. if (!detailsId) {
  246. wx.showToast({
  247. title: qrcodeInvalidToastText,
  248. icon: 'none'
  249. })
  250. return
  251. }
  252. this.scanningBooksFn(detailsId)
  253. }
  254. })
  255. },
  256. /**
  257. * 扫一扫借书
  258. */
  259. scanningBooksFn: function (detailsId) {
  260. const continuousFn = { fn: this.scanningBooksFn, param: { ...arguments } }
  261. const { vipId } = this.data.options
  262. ScanningBooks({ data:{ detailsId, vipId }, continuousFn}).then(res => {
  263. navigateTo({ url: 'borrow', paras: { ...res.data } })
  264. }).catch(res => {
  265. wx.showToast({
  266. title: res.msg,
  267. icon: 'none'
  268. })
  269. })
  270. }
  271. })