275 lines
5.3 KiB
JavaScript
275 lines
5.3 KiB
JavaScript
const {
|
||
api
|
||
} = require("../../utils/api")
|
||
|
||
// pages/garden/index.js
|
||
Page({
|
||
|
||
/**
|
||
* 页面的初始数据
|
||
*/
|
||
data: {
|
||
list: [],
|
||
timer: null,
|
||
name: '',
|
||
leftList: [],
|
||
rightList: [],
|
||
leftHeight: 0,
|
||
rightHeight: 0,
|
||
hotList: [],
|
||
info: null,
|
||
needCare: 0,
|
||
inviteCode: ''
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面加载
|
||
*/
|
||
onLoad(options) {
|
||
const code = options.inviteCode
|
||
if (code) {
|
||
api('/personal/inviteCode/accept', 'GET', {
|
||
inviteCode: code
|
||
})
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面初次渲染完成
|
||
*/
|
||
onReady() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面显示
|
||
*/
|
||
onShow() {
|
||
this.fetchList()
|
||
this.init()
|
||
},
|
||
|
||
init() {
|
||
const tmp = wx.getStorageSync('weather')
|
||
if (tmp) {
|
||
this.setData({
|
||
info: tmp
|
||
})
|
||
} else {
|
||
wx.getFuzzyLocation({
|
||
type: 'wgs84',
|
||
success(res) {
|
||
const data = {
|
||
latitude: res.latitude + "",
|
||
longitude: res.longitude + ""
|
||
}
|
||
api('/auth/getLocation', 'GET', data).then(res => {
|
||
if (res.code === 200) {
|
||
wx.setStorageSync('city', res.data)
|
||
api('/auth/getWeather', 'GET', res.data).then(res => {
|
||
if (res.code === 200) {
|
||
wx.setStorageSync('weather', res.data)
|
||
this.setData({info:res.data})
|
||
}
|
||
})
|
||
|
||
}
|
||
})
|
||
},
|
||
fail(err) {
|
||
console.log(err);
|
||
}
|
||
})
|
||
}
|
||
const inviteCode = wx.getStorageSync('inviteCode')
|
||
if (inviteCode) {
|
||
this.setData({
|
||
inviteCode: inviteCode
|
||
})
|
||
}
|
||
|
||
|
||
api('/plant/needCare', 'GET').then(res => {
|
||
if (res.code === 200) {
|
||
this.setData({
|
||
needCare: res.data
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
goTask() {
|
||
wx.switchTab({
|
||
url: '../garden/index',
|
||
})
|
||
},
|
||
|
||
|
||
fetchList() {
|
||
api('/plant/page', 'POST', {
|
||
current: 0,
|
||
pageSize: 999,
|
||
name: this.data.name
|
||
}, 'json').then(res => {
|
||
if (res.code == 200) {
|
||
if (res.data === null) {
|
||
return
|
||
}
|
||
|
||
if (res.data.list.length === 0) {
|
||
this.fetchHotList()
|
||
return
|
||
}
|
||
this.setData({
|
||
leftList: [],
|
||
rightList: []
|
||
})
|
||
const tmps = res.data.list.map(e => {
|
||
e.pic = e.imgList[0].url
|
||
e.cover = e.imgList[0]
|
||
return e
|
||
})
|
||
|
||
this.addItems(tmps)
|
||
|
||
this.setData({
|
||
list: tmps
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
fetchHotList() {
|
||
api('/claim/list', 'POST', {
|
||
current: 0,
|
||
pageSize: 10,
|
||
name: '',
|
||
keyword: ''
|
||
}, 'json').then(res => {
|
||
if (res.code === 200) {
|
||
const tmps = res.data.list.map(e => {
|
||
e.pic = e.imgList[0].url
|
||
e.status = e.points < this.data.points ? 0 : 1
|
||
return e
|
||
}).sort((a, b) => {
|
||
// 1. 优先比较 status,0 在前,1 在后
|
||
if (a.status !== b.status) {
|
||
return a.status - b.status;
|
||
}
|
||
// 2. status 相同的情况下,比较 points,从小到大排序
|
||
return a.points - b.points;
|
||
})
|
||
this.setData({
|
||
hotList: tmps
|
||
})
|
||
}
|
||
})
|
||
},
|
||
addItems(newItems) {
|
||
let h1 = 0;
|
||
let h2 = 0;
|
||
newItems.forEach((item, index) => {
|
||
|
||
if (index % 2 === 0) {
|
||
this.data.leftList.push(item);
|
||
h1 += item.cover.height / item.cover.width;
|
||
} else {
|
||
this.data.rightList.push(item);
|
||
h2 += item.cover.height / item.cover.width;
|
||
}
|
||
this.setData({
|
||
leftList: this.data.leftList,
|
||
rightList: this.data.rightList,
|
||
});
|
||
|
||
});
|
||
},
|
||
|
||
goStore(e){
|
||
console.log(e);
|
||
const id = e.currentTarget.dataset.id
|
||
wx.navigateTo({
|
||
url: '../store/info?id='+ id,
|
||
})
|
||
},
|
||
goInfo(e) {
|
||
console.log(e);
|
||
const id = e.currentTarget.dataset.id
|
||
wx.navigateTo({
|
||
url: '../garden/info?id=' + id,
|
||
})
|
||
},
|
||
|
||
onchange(e) {
|
||
const value = e.detail.value
|
||
if (this.data.timer != null) {
|
||
clearTimeout(this.data.timer)
|
||
}
|
||
this.data.timer = setTimeout(() => {
|
||
this.setData({
|
||
name: value
|
||
})
|
||
this.fetchList()
|
||
}, 500)
|
||
},
|
||
|
||
|
||
goAdd() {
|
||
const tmp = wx.getStorageSync('user')
|
||
if (tmp.phone.length === 0) {
|
||
wx.navigateTo({
|
||
url: '../login/index',
|
||
})
|
||
return
|
||
}
|
||
wx.navigateTo({
|
||
url: '../add/index',
|
||
})
|
||
},
|
||
/**
|
||
* 生命周期函数--监听页面隐藏
|
||
*/
|
||
onHide() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面卸载
|
||
*/
|
||
onUnload() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面相关事件处理函数--监听用户下拉动作
|
||
*/
|
||
onPullDownRefresh() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面上拉触底事件的处理函数
|
||
*/
|
||
onReachBottom() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 用户点击右上角分享
|
||
*/
|
||
onShareAppMessage() {
|
||
return {
|
||
title: '发现一个宝藏养花神器',
|
||
imageUrl: "https://res.catter.cn/pub/2025/12/08/20251208112039694.png",
|
||
path: '/pages/index/index?inviteCode=' + this.data.inviteCode
|
||
};
|
||
},
|
||
|
||
onShareTimeline() {
|
||
return {
|
||
title: '发现一个宝藏养花神器',
|
||
imageUrl: "https://res.catter.cn/pub/2025/12/08/20251208112039694.png",
|
||
path: '/pages/index/index?inviteCode=' + this.data.inviteCode
|
||
};
|
||
}
|
||
}) |