- 新增 .gitignore(排除 node_modules、私有配置、编译产物) - 新增 cloudbaserc.json,绑定云环境 cloud1-d4g697lte499543d8 - project.config.json:补 miniprogramRoot、开启 useCompilerPlugins typescript,修复预览报 app.json 找不到的问题 - tsconfig.json:移除无效 typeRoots,解除 TypeScript 编译阻断 - miniprogram/app.json:移除 glass-easel(需基础库 3.x,兼容性差) - miniprogram/utils/constants.ts:填入真实云环境 ID - cloudfunctions/updateLocation:位置字段改用 GeoPoint 以支持地理索引 - cloudfunctions/getNearbyPets:改用 geoNear 聚合管道,支持真实距离排序 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
27 lines
790 B
JavaScript
27 lines
790 B
JavaScript
const cloud = require('wx-server-sdk')
|
||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
||
const db = cloud.database()
|
||
|
||
exports.main = async (event, context) => {
|
||
const { OPENID } = cloud.getWXContext()
|
||
const { latitude, longitude } = event
|
||
|
||
if (!latitude || !longitude) return { code: -1, message: '缺少位置' }
|
||
|
||
try {
|
||
await db.collection('users')
|
||
.where({ openid: OPENID })
|
||
.update({
|
||
data: {
|
||
// ✅ 改用 GeoPoint,才能用地理位置索引做距离查询
|
||
lastLocation: db.Geo.Point(longitude, latitude), // 注意:Geo.Point 是 (lng, lat) 顺序
|
||
lastSeen: db.serverDate(),
|
||
isOnline: true,
|
||
},
|
||
})
|
||
return { code: 0, data: null }
|
||
} catch (e) {
|
||
return { code: -1, message: e.message }
|
||
}
|
||
}
|