index.js 4.8 KB
Newer Older
小森森 已提交
1 2 3 4
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
小森森 已提交
5 6
    env: cloud.DYNAMIC_CURRENT_ENV,
    traceUser: true
小森森 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
})
const db = cloud.database()
const _ = db.command
const $ = _.aggregate

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    const openId = wxContext.OPENID

    // 查询房子
    if (event.type === 'query') {
        let limit = 10
        let dbname = event.key
        let page = event.page

        let res = await db.collection(dbname).aggregate()
            .skip(page)
            .limit(limit)
            .sort({
                updateTime: -1
            })
            .lookup({
                from: 'Entrust',
                localField: 'ID',
                foreignField: '_id',
                as: 'EntrustInfo',
            })
            .match({
小森森 已提交
36
                'EntrustInfo.publish': true
小森森 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
            })
            .project({
                'ID': true,
                'updateTime': true,
                'EntrustInfo.title': true,
                'EntrustInfo.FormData.houseStyle': true,
                'EntrustInfo.FormData.roomStyle': true,
                'EntrustInfo.FormData.area': true,
                'EntrustInfo.FormData.location': true,
                'EntrustInfo.FormData.Tags': true,
                'EntrustInfo.FormData.averagePrice': true,
                'EntrustInfo.FormData.totalPrice': true,
                'EntrustInfo.photoInfo': true
            })
            .replaceRoot({
                newRoot: $.mergeObjects([$.arrayElemAt(['$EntrustInfo', 0]), '$$ROOT'])
            })
            .project({
                '_id': false,
                'EntrustInfo': false
            })
            .end()
        return res
    }

    // 房型筛选
    if (event.type == 'housetype') {
        let limit = 10
        let dbname = event.key
        let page = event.page
        let RoomStyle = event.RoomStyle
        let res = await db.collection(dbname).aggregate()
            .skip(page)
            .limit(limit)
            .sort({
                updateTime: -1
            })
            .lookup({
                from: 'Entrust',
                localField: 'ID',
                foreignField: '_id',
                as: 'EntrustInfo',
            })
            .match({
小森森 已提交
81
                'EntrustInfo.publish': true,
小森森 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                'EntrustInfo.FormData.roomStyle': RoomStyle
            })
            .project({
                'ID': true,
                'updateTime': true,
                'EntrustInfo.title': true,
                'EntrustInfo.FormData.houseStyle': true,
                'EntrustInfo.FormData.roomStyle': true,
                'EntrustInfo.FormData.area': true,
                'EntrustInfo.FormData.location': true,
                'EntrustInfo.FormData.Tags': true,
                'EntrustInfo.FormData.averagePrice': true,
                'EntrustInfo.FormData.totalPrice': true,
                'EntrustInfo.photoInfo': true
            })
            .replaceRoot({
                newRoot: $.mergeObjects([$.arrayElemAt(['$EntrustInfo', 0]), '$$ROOT'])
            })
            .project({
                '_id': false,
                'EntrustInfo': false
            })
            .end()
        return res
    }

    // 价格筛选
    if (event.type == 'houseprice') {
        let limit = 10
        let page = 0
        let dbname = event.key
        let min = parseInt(event.min)
        let max = parseInt(event.max)

        let res = await db.collection(dbname).aggregate()
            .skip(page)
            .limit(limit)
            .sort({
                updateTime: -1
            })
            .lookup({
                from: 'Entrust',
                localField: 'ID',
                foreignField: '_id',
                as: 'EntrustInfo',
            })
            .match({
小森森 已提交
129
                'EntrustInfo.publish': true,
小森森 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
                'EntrustInfo.FormData.totalPrice': _.gte(min).lte(max)
            })
            .project({
                'ID': true,
                'updateTime': true,
                'EntrustInfo.title': true,
                'EntrustInfo.FormData.houseStyle': true,
                'EntrustInfo.FormData.roomStyle': true,
                'EntrustInfo.FormData.area': true,
                'EntrustInfo.FormData.location': true,
                'EntrustInfo.FormData.Tags': true,
                'EntrustInfo.FormData.averagePrice': true,
                'EntrustInfo.FormData.totalPrice': true,
                'EntrustInfo.photoInfo': true
            })
            .replaceRoot({
                newRoot: $.mergeObjects([$.arrayElemAt(['$EntrustInfo', 0]), '$$ROOT'])
            })
            .project({
                '_id': false,
                'EntrustInfo': false
            })
            .end()
小森森 已提交
153
        return res
小森森 已提交
154 155 156
    }

}