good_books.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // parents/pages/good_books/good_books.js
  2. import F2 from '../../../utils/f2-canvas/lib/f2';
  3. let chart;
  4. import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
  5. import { GetBookRecommend } from '../../../utils/api.js'
  6. const { navigateTo, redirectTo, navigateBack } = routers()
  7. const { globalData, hasLibraryService, hasVipService } = getApp()
  8. const { baseImgUrl, qrcodeInvalidToastText } = globalData
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. baseImgUrl,
  15. isLoaded: false,
  16. opts: {
  17. lazyLoad: true // 延迟加载组件
  18. },
  19. borrowInfoList: [
  20. // {
  21. // "child_theme": "child5",
  22. // "book_num": 5,
  23. // "aliases": "地域江湖",
  24. // "theme_name": "地域江湖",
  25. // "bookList": [
  26. // {
  27. // "summary": "好",
  28. // "local_image": "/2017/11/20/ccf1b904a7d730734600c707111aa5484a2b070936566f6bdbfae788d90b75ee.jpeg",
  29. // "isbn13": "9787111520450",
  30. // "title": "用户力测试"
  31. // },
  32. // {
  33. // "summary": "好",
  34. // "local_image": "/2017/11/20/ccf1b904a7d730734600c707111aa5484a2b070936566f6bdbfae788d90b75ee.jpeg",
  35. // "isbn13": "9787111520450",
  36. // "title": "用户力测试"
  37. // }
  38. // ]
  39. // },
  40. // {
  41. // "child_theme": "child6",
  42. // "book_num":15,
  43. // "aliases": "江湖",
  44. // "theme_name": "江湖",
  45. // "bookList": [
  46. // {
  47. // "summary": "好",
  48. // "local_image": "/2017/11/20/ccf1b904a7d730734600c707111aa5484a2b070936566f6bdbfae788d90b75ee.jpeg",
  49. // "isbn13": "9787111520450",
  50. // "title": "用户力测试"
  51. // }
  52. // ]
  53. // },
  54. ],
  55. headInfoList: [
  56. // {
  57. // "name": "共享地球",
  58. // "percent": 1,
  59. // "book_count": 190,
  60. // "proportion": "100%",
  61. // "child_count": 5,
  62. // "bnum": 0
  63. // },
  64. // {
  65. // "name": "自我成长",
  66. // "percent": 1,
  67. // "book_count": 22,
  68. // "proportion": "0%",
  69. // "child_count": 6,
  70. // "bnum": 0
  71. // },
  72. // {
  73. // "name": "学科素养",
  74. // "percent": 1,
  75. // "book_count": 19,
  76. // "proportion": "0%",
  77. // "child_count": 8,
  78. // "bnum": 0
  79. // },
  80. // {
  81. // "name": "多彩世界",
  82. // "percent": 1,
  83. // "book_count": 90,
  84. // "proportion": "0%",
  85. // "child_count": 6,
  86. // "bnum": 0
  87. // }
  88. ],
  89. footerData: {
  90. actIndex: 1,
  91. list: [
  92. {
  93. text: '找好书',
  94. icon: '../../../assets/book_gray.png',
  95. actIcon: '../../../assets/book_blue.png',
  96. url: 'parents/pages/home/home',
  97. zindex: 3,
  98. },
  99. {
  100. text: '好书推荐',
  101. icon: '../../../assets/wish_gray.png',
  102. actIcon: '../../../assets/wish_blue.png',
  103. url: 'parents/pages/good_books/good_books',
  104. zindex: 3,
  105. },
  106. {
  107. text: '我的',
  108. icon: '../../../assets/me_gray.png',
  109. actIcon: '../../../assets/me_blue.png',
  110. url: 'parents/pages/me/me',
  111. zindex: 3,
  112. }
  113. ]
  114. },
  115. nodataArray: {
  116. text: "暂无阅读计划",
  117. margin: '140rpx auto'
  118. }
  119. },
  120. /**
  121. * 生命周期函数--监听页面加载
  122. */
  123. onLoad: function (options) {
  124. options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`)
  125. options.libId = getGlobalVal('library').id || ''
  126. this.setData({ options })
  127. this.getData()
  128. },
  129. /**
  130. * 生命周期函数--监听页面初次渲染完成
  131. */
  132. onReady: function () {
  133. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  134. for (const i in obj) {
  135. this[i] = obj[i]
  136. }
  137. },
  138. /**
  139. * 生命周期函数--监听页面显示
  140. */
  141. onShow: function () {
  142. },
  143. /**
  144. * 生命周期函数--监听页面隐藏
  145. */
  146. onHide: function () {
  147. },
  148. /**
  149. * 生命周期函数--监听页面卸载
  150. */
  151. onUnload: function () {
  152. },
  153. /**
  154. * 页面相关事件处理函数--监听用户下拉动作
  155. */
  156. onPullDownRefresh: function () {
  157. this.setData(() => {
  158. wx.stopPullDownRefresh()
  159. })
  160. },
  161. /**
  162. * 页面上拉触底事件的处理函数
  163. */
  164. onReachBottom: function () {
  165. },
  166. /**
  167. * 用户点击右上角分享
  168. */
  169. onShareAppMessage: function () {
  170. if (isFn(sharePage)) return sharePage()
  171. },
  172. /**
  173. * 获取好书推荐
  174. */
  175. getData: function (cb) {
  176. const continuousFn = { fn: this.getData, param: { ...arguments } }
  177. const { libId = '', vipId = '' } = this.data.options
  178. GetBookRecommend({ data: { libId, vipId }, continuousFn }).then(res => {
  179. const { borrowInfoList} = res.data
  180. const headInfoList = (res => {
  181. for (let i in res) {
  182. res[i]['name'] = res[i]['parent_name']
  183. res[i]['percent'] = ~~res[i]['book_count']
  184. }
  185. return res
  186. })(res.data.headInfoList)
  187. this.setData({ borrowInfoList, headInfoList, isLoaded: true })
  188. if (isFn(cb)) cb()
  189. this.goodBooksPicture()
  190. }).catch(res => {
  191. this.setData({ isLoaded: true })
  192. if (isFn(cb)) cb()
  193. })
  194. },
  195. /**
  196. * 好书推荐图表
  197. */
  198. goodBooksPicture: function () {
  199. const { headInfoList = [] } = this.data
  200. const chartComponent = this.selectComponent('#pieSelect')
  201. const fn = (canvas, width, height) => {
  202. const chart = new F2.Chart({
  203. el: canvas,
  204. width,
  205. height
  206. });
  207. chart.source(headInfoList, {
  208. percent: {
  209. formatter: function formatter(data) {
  210. return data.child_count + '个主题' + '\n' + data.book_count + '本书' + '\n' + '已借阅:' + data.bnum + '本'
  211. }
  212. }
  213. });
  214. chart.legend(false);
  215. chart.tooltip(false);
  216. chart.coord('polar', {
  217. transposed: true,
  218. radius: 0.75,
  219. innerRadius: 0.48
  220. });
  221. chart.axis(false);
  222. chart
  223. .interval()
  224. .position('a*percent')
  225. .color('name', ['#FF6464', '#FFCE4A', '#00B7FF', '#FF80A8'])
  226. .adjust('stack')
  227. .style({
  228. lineWidth: 0,
  229. stroke: '#fff',
  230. lineJoin: 'round',
  231. lineCap: 'round'
  232. });
  233. chart.interaction('pie-select', {
  234. cancelable: false,
  235. animate: {
  236. duration: 300,
  237. easing: 'backOut'
  238. },
  239. onStart:res=>{
  240. // console.log(res)
  241. },
  242. onEnd: res => {
  243. // console.log(res)
  244. // const { shape, data, shapeInfo, selected } = res
  245. // const { child_count, book_count, bnum } = data
  246. // if (shape && selected) {
  247. // this.setData({
  248. // message: child_count + '个主题' + '\n' + book_count + '本书' + '\n' + '已借阅:' + bnum + '本'
  249. // })
  250. // }
  251. }
  252. });
  253. chart.render();
  254. return chart;
  255. }
  256. chartComponent.init(fn)
  257. }
  258. })