index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // 云函数入口文件
  2. const cloud = require('wx-server-sdk')
  3. const TcbRouter = require('tcb-router')
  4. cloud.init()
  5. //初始化数据库
  6. const db = cloud.database()
  7. // 云函数入口函数
  8. exports.main = async (event, context) => {
  9. const app = new TcbRouter({
  10. event
  11. })
  12. //获取all总条数
  13. //const total = await db.collection('category_all').count().total
  14. // 获取税收分类编码
  15. app.router('getCategoryTop', async (ctx, next) => {
  16. ctx.body = await db.collection('category_top').skip(event.start).limit(event.count).get().then(res => {
  17. return res
  18. })
  19. })
  20. //获取产品信息
  21. app.router('getCategoryAll', async (ctx, next) => {
  22. let params = {
  23. search_code: event.search_code
  24. }
  25. if (event.keyword) {
  26. params.keyword = {
  27. $regex: '.*' + event.keyword,
  28. $options: 'i'
  29. }
  30. /* ctx.body = await db.collection('category_all').where(_.or([{
  31. keyword: db.RegExp({
  32. $regex: '.*' + event.keyword,
  33. $options: 'i'
  34. })
  35. }])).and([{
  36. search_code: event.search_code
  37. }]).skip(event.start).limit(event.count).get().then(res => {
  38. return res
  39. }) */
  40. }
  41. ctx.body = await db.collection('category_all').where(params).skip(event.start).limit(event.count).get().then(res => {
  42. return res
  43. })
  44. })
  45. return app.serve()
  46. }