booklist.js 758 B

12345678910111213141516171819202122232425262728
  1. const { mysql } = require('../qcloud')
  2. module.exports = async (ctx) => {
  3. const {page, openid} = ctx.request.query
  4. const size = 10
  5. const mysqlSelect = mysql('books')
  6. .select('books.*', 'cSessionInfo.user_info')
  7. .join('cSessionInfo', 'books.openid', 'cSessionInfo.open_id')
  8. .orderBy('books.id', 'desc')
  9. let books
  10. if (openid) {
  11. // 根据opid过滤
  12. books = await mysqlSelect.where('books.openid', openid)
  13. } else {
  14. // 全部图书 分页
  15. books = await mysqlSelect.limit(size).offset(Number(page) * size)
  16. }
  17. ctx.state.data = {
  18. list: books.map(v => {
  19. const info = JSON.parse(v.user_info)
  20. return Object.assign({}, v, {
  21. user_info: {
  22. nickName: info.nickName
  23. }
  24. })
  25. })
  26. }
  27. }