You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.7 KiB

const app = getApp();
Component({
data: {
recommandBrand: [],
brandList: [],
brandForm: {
brand: ''
},
history: [],
alphalist: [],
},
methods: {
toBrandInsight(e) {
let name = e.currentTarget.dataset.id.brandname; //获取点击的name
let img = e.currentTarget.dataset.id.img;
let obj = {
brandname: name,
img: e.currentTarget.dataset.id.img
};
if(wx.getStorageSync('historyBrand').length == 0) {
let arr = [obj];
wx.setStorageSync('historyBrand', arr);
this.setData({history: arr})
} else {
let o = {
brandname: name,
img: e.currentTarget.dataset.id.img
}
let isAdded = false;
for(let i=0;i<this.data.history.length;i++) { //列表中已存在则移到最前,否则在最前添加
if(this.data.history[i].img == o.img) {
isAdded = true;
let arr = wx.getStorageSync('historyBrand');
arr.splice(i, 1);
let replaceArr = [o, ...arr];
wx.setStorageSync('historyBrand', replaceArr);
this.setData({history: replaceArr})
}
}
if(!isAdded) {
let newArr = [o, ...wx.getStorageSync('historyBrand')]
wx.setStorageSync('historyBrand', newArr);
this.setData({history: newArr})
}
}
wx.setStorageSync('sBrand', name);
wx.navigateTo({
url: "/pages/insight/pages/brandInsight/index"+'?img='+img
})
},
},
lifetimes: {
attached() {
wx.showToast({
title: '加载中',
icon: 'loading',
duration: 300000
})
//页面数据
app.globalData.request({
action: 'getUserBrand',
token: wx.getStorageSync('token') || 't%2BrswgjvzGM=',
}).then(res => {
let arr = []
let recommandArr = [];
let alphalist = [];
res.forEach(ele => {
if(ele.firstword == '热门') {
recommandArr.push(ele)
} else {
if(alphalist.indexOf(ele.firstword) == -1) {
alphalist.push(ele.firstword)
}
}
});
alphalist.forEach(ele => {
arr.push({
name: ele,
data: []
});
});
for(let i of res){
for(let j of arr){
if(i.firstword == j.name){
j.data.push(i)
}
}
}
this.setData({
alphalist: alphalist,
brandList:arr,
recommandBrand: recommandArr,
});
if(wx.getStorageSync('historyBrand').length != 0) {
this.setData({history: wx.getStorageSync('historyBrand')})
} else { //首次加载默认为推荐品牌
this.setData({history: recommandArr});
wx.setStorageSync('historyBrand', recommandArr);
}
setTimeout(() =>{
wx.hideToast();
},500)
});
},
},
})