main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // parents/pages/home/home.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal, getWxLocationSetting } from '../../../utils/util.js'
  3. import { GetLibrary, GetParentHome, ScanningBooksTwo, GoodIndex, IndexBarList, InitBorrowCar, GetMineInfo} from '../../../utils/api.js'
  4. const { navigateTo, redirectTo, navigateBack } = routers()
  5. const { globalData, hasLibraryService, hasVipService } = getApp()
  6. const { baseImgUrl, qrcodeInvalidToastText, isIphoneX, noVipListToastText } = globalData
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. baseImgUrl,
  13. isIphoneX,
  14. isFirst: 0,
  15. latitude: '',
  16. longitude: '',
  17. isLoaded: 0,
  18. pageNo: 1,
  19. pageSize: 10,
  20. carouselList: ['../../../assets/main_banner1.png', '../../../assets/banner1.png'],
  21. routers: [
  22. {
  23. icon: '../../../assets/scond_banner1.png',
  24. text: '我要借书',
  25. url: 'parents/pages/borrow_car/borrow_car',
  26. zindex: 3,
  27. },
  28. {
  29. icon: '../../../assets/scond_banner2.png',
  30. text: '借阅记录',
  31. url: 'parents/pages/borrow_record/borrow_record',
  32. zindex: 3,
  33. },
  34. {
  35. icon: '../../../assets/scond_banner3.png',
  36. text: '图书分类',
  37. url: 'parents/pages/book_classification/book_classification',
  38. zindex: 3,
  39. },
  40. {
  41. icon: '../../../assets/scond_banner4.png',
  42. text: '申请新书',
  43. url: 'parents/pages/apply_book/apply_book',
  44. zindex: 3,
  45. }
  46. ],
  47. topList: [],
  48. list: [],
  49. nodataArray: {
  50. text: "暂无数据"
  51. },
  52. footerData: {
  53. isIphoneX,
  54. actIndex: 0,
  55. list: [
  56. {
  57. text: '找好书',
  58. icon: '../../../assets/book_gray.png',
  59. actIcon: '../../../assets/book_blue.png',
  60. url: 'parents/pages/main/main',
  61. zindex: 3,
  62. },
  63. {
  64. text: '成长轨迹',
  65. icon: '../../../assets/wish_gray.png',
  66. actIcon: '../../../assets/wish_blue.png',
  67. url: 'parents/pages/growth_track/growth_track',
  68. zindex: 3,
  69. },
  70. {
  71. text: '我的',
  72. icon: '../../../assets/me_gray.png',
  73. actIcon: '../../../assets/me_blue.png',
  74. url: 'parents/pages/me/me',
  75. zindex: 3,
  76. }
  77. ]
  78. }
  79. },
  80. /**
  81. * 生命周期函数--监听页面加载
  82. */
  83. onLoad: function (options) {
  84. const userId = getGlobalVal('userId')
  85. options.vipId = getGlobalVal('vipId', `vipId_${userId}`) || ''
  86. // options.vipId ='306330bd-5c8c-41aa-8a00-0f0c296ebb29',
  87. options.libId = getGlobalVal('library').id || ''
  88. options.library = getGlobalVal('library')
  89. this.setData({ options, isFirst: 1})
  90. if (options.isDirector==1){
  91. this.getVipList()
  92. }
  93. if (options.library) {
  94. this.getIndexBarList()
  95. this.getList(res => {
  96. this.setData({ isLoaded: 1 })
  97. })
  98. } else {
  99. this.getLocation(res => {
  100. this.setData({ isLoaded: 1 })
  101. })
  102. }
  103. },
  104. /**
  105. * 生命周期函数--监听页面初次渲染完成
  106. */
  107. onReady: function () {
  108. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  109. for (const i in obj) {
  110. this[i] = obj[i]
  111. }
  112. },
  113. /**
  114. * 生命周期函数--监听页面显示
  115. */
  116. onShow: function () {
  117. const { options, isFirst } = this.data
  118. options.library = getGlobalVal('library')
  119. const userId = getGlobalVal('userId')
  120. options.vipId = getGlobalVal('vipId', `vipId_${userId}`) || ''
  121. options.libId = getGlobalVal('library').id || ''
  122. const bookList = getGlobalVal(`borrowBook_${userId}_${options.libId}_${options.vipId}`) || []
  123. // console.log(bookList)
  124. this.setData({ options, bookList })
  125. if (!options.library && !isFirst) {
  126. this.getLocation()
  127. }
  128. },
  129. /**
  130. * 生命周期函数--监听页面隐藏
  131. */
  132. onHide: function () {
  133. },
  134. /**
  135. * 生命周期函数--监听页面卸载
  136. */
  137. onUnload: function () {
  138. },
  139. /**
  140. * 页面相关事件处理函数--监听用户下拉动作
  141. */
  142. onPullDownRefresh: function () {
  143. // this.getList(res => {
  144. // wx.stopPullDownRefresh()
  145. // })
  146. },
  147. /**
  148. * 页面上拉触底事件的处理函数
  149. */
  150. onReachBottom: function () {
  151. },
  152. /**
  153. * 用户点击右上角分享
  154. */
  155. onShareAppMessage: function () {
  156. if (isFn(sharePage)) return sharePage()
  157. },
  158. /**
  159. * 获取会员卡列表
  160. */
  161. getVipList: function () {
  162. const continuousFn = { fn: this.getVipList, param: { ...arguments } }
  163. const userId = getGlobalVal('userId')
  164. const { options } = this.data
  165. const { vipId}=this.data.options
  166. const fn = (i, v) => {
  167. globalData['vip'] = v
  168. wx.setStorageSync(`vip_${userId}`, v)
  169. globalData['vipIndex'] = i
  170. wx.setStorageSync(`vipIndex_${userId}`, i)
  171. globalData['vipId'] = v['vip_id']
  172. options.vipId  =  v['vip_id']  ||  ''
  173. this.setData({  options })
  174. wx.setStorageSync(`vipId_${userId}`, v['vip_id'])
  175. }
  176. GetMineInfo({ continuousFn }).then(res => {
  177. const { vipList = null } = res.data
  178. if (vipList.length > 0) {
  179. let isSame = false
  180. for (let i in vipList) {
  181. if (vipList[i]['vip_id'] == vipId) {
  182. isSame = true
  183. fn(i, vipList[i])
  184. break
  185. }
  186. }
  187. if (!isSame) {
  188. fn(0, vipList[0])
  189. }
  190. globalData['vipList'] = vipList
  191. wx.setStorageSync(`vipList_${userId}`, vipList)
  192. } else {
  193. wx.showToast({
  194. title: noVipListToastText,
  195. icon: 'none'
  196. })
  197. }
  198. })
  199. },
  200. /**
  201. * 获取首页数据
  202. */
  203. getList: function (cb) {
  204. const continuousFn = { fn: this.getList, param: { ...arguments } }
  205. const libId = this.data.options.library.id || ''
  206. if (!libId) {
  207. wx.showToast({
  208. title: '请先定位图书馆',
  209. icon: 'none'
  210. })
  211. return
  212. }
  213. GoodIndex({ data: { libId }, continuousFn }).then(res => {
  214. const { list } = res.data
  215. const listTemp = (res => {
  216. for (let i in res) {
  217. if (res[i].summary.length>30){
  218. res[i].summary = res[i].summary.slice(0, 30) + '...'
  219. }else{
  220. res[i].summary = res[i].summary
  221. }
  222. }
  223. return res
  224. })(list)
  225. // console.log(listTemp,list)
  226. this.setData({ list: listTemp })
  227. cb && cb(res)
  228. }).catch(res => {
  229. cb && cb(res)
  230. })
  231. },
  232. /**
  233. * 获取定位
  234. */
  235. getLocation: function (cb) {
  236. getWxLocationSetting(res => {
  237. this.setData({
  238. isShowPosition: false
  239. })
  240. wx.showLoading({
  241. title: '定位中'
  242. })
  243. wx.getLocation({
  244. type: 'wgs84',
  245. success: res => {
  246. const { latitude, longitude } = res
  247. this.setData({
  248. latitude,
  249. longitude
  250. })
  251. this.getLibrary(cb)
  252. },
  253. fail: res => {
  254. wx.showToast({
  255. title: '定位失败',
  256. icon: 'none'
  257. })
  258. },
  259. complete: res => {
  260. wx.hideLoading()
  261. }
  262. })
  263. })
  264. },
  265. /**
  266. * 获取图书馆
  267. */
  268. getLibrary: function (cb) {
  269. const continuousFn = { fn: this.getLibrary, param: { ...arguments } }
  270. const { options, latitude, longitude, pageNo, pageSize } = this.data
  271. const { vipId } = options
  272. GetLibrary({ data: { vipId, pageNo, pageSize, leagueLat: latitude, leagueLng: longitude }, continuousFn }).then(res => {
  273. const { list } = res.data
  274. if (list[0]['id']) {
  275. getApp().globalData.library = list[0]
  276. wx.setStorageSync('library', list[0])
  277. this.setData({ 'options.library': list[0] })
  278. this.getList(cb)
  279. }
  280. })
  281. },
  282. /**
  283. * 获取首页图片
  284. */
  285. getIndexBarList: function (cb) {
  286. const continuousFn = { fn: this.getIndexBarList, param: { ...arguments } }
  287. const { options, latitude, longitude, pageNo, pageSize } = this.data
  288. const { vipId } = options
  289. IndexBarList({ data: {}, continuousFn }).then(res => {
  290. const { imgList } = res.data
  291. this.setData({ imgList})
  292. })
  293. },
  294. /**
  295. * 选择跳转进入页
  296. */
  297. selectRouteCtl: function (e) {
  298. const { index } = e.currentTarget.dataset
  299. const { routers } = this.data
  300. const { url, zindex } = routers[index]
  301. if(index==0){
  302. this.scanCtl()
  303. } else if (index == 2){
  304. navigateTo({ url, zindex })
  305. }else{
  306. if (!hasVipService()) return
  307. navigateTo({ url, zindex })
  308. }
  309. },
  310. /**
  311. * 调用微信扫一扫之前判断可借书多少本
  312. */
  313. scanCtl: function () {
  314. const continuousFn = { fn: this.scanCtl, param: { ...arguments } }
  315. const { library, vipId, userId } = this.data.options
  316. const libId = library.id || ''
  317. if (!hasVipService()) return
  318. InitBorrowCar({ data: { vipId, libId }, continuousFn }).then(res => {
  319. const { canBorrowNum, borrowNumTotal } = res.data
  320. wx.showToast({
  321. title: `您还可以借${canBorrowNum}本书`,
  322. icon: 'none'
  323. })
  324. if (this.data.bookList.length>0){
  325. navigateTo({ url: 'parents/pages/borrow_car/borrow_car', paras: { canBorrowNum } })
  326. return
  327. }
  328. if (canBorrowNum - this.data.bookList.length > 0) {
  329. this.scanFn()
  330. }
  331. this.setData({ ...res.data })
  332. })
  333. },
  334. /**
  335. * 调用微信扫一扫
  336. */
  337. scanFn: function () {
  338. wx.scanCode({
  339. success: res => {
  340. const url = res.result
  341. const detailsId = url.split('_')[1].split('.')[0]
  342. if (!detailsId) {
  343. wx.showToast({
  344. title: qrcodeInvalidToastText,
  345. icon: 'none'
  346. })
  347. return
  348. }
  349. this.scanningBooksFn(detailsId)
  350. }
  351. })
  352. },
  353. /**
  354. * 扫一扫借书
  355. */
  356. scanningBooksFn: function (detailsId) {
  357. const continuousFn = { fn: this.scanningBooksFn, param: { ...arguments } }
  358. const { vipId } = this.data.options
  359. const { canBorrowNum, bookList } = this.data
  360. ScanningBooksTwo({ data: { detailsId, vipId }, continuousFn }).then(res => {
  361. const { detailsId}=res.data;
  362. for(var i in bookList){
  363. if (detailsId == bookList[i].detailsId){
  364. wx.showToast({
  365. title: '此二维码已经扫过',
  366. icon: 'none'
  367. })
  368. }
  369. }
  370. if (detailsId){
  371. navigateTo({ url: 'parents/pages/borrow_car/borrow_car', paras: { ...res.data, canBorrowNum } })
  372. }
  373. }).catch(res => {
  374. wx.showToast({
  375. title: res.msg,
  376. icon: 'none'
  377. })
  378. })
  379. }
  380. })