followHelper.js 1.5 KB
import loginHelper from './loginHelper.js';
import utils from './utils.js';
let Follow_List = null


const followHelper = {
	getFollowList() {

		if (!loginHelper.hasLogin()) {
			return []
		}

		if(!Follow_List) {
			Follow_List = []
			let temp_data = uni.getStorageSync('follow_list_key')
			console.log(temp_data)
			if (temp_data.length > 0) {
				let temp_list = JSON.parse(temp_data)
				Follow_List.push(...temp_list)
			}
			for (var i = 0; i < Follow_List.length; i++) {
				var item = Follow_List[i]
				if (utils.diffCurrentDay(item.MatchTime) >= 7) {
					Follow_List.splice(i, 1);
				}
			}
			
			let follow_str = JSON.stringify(Follow_List)
			uni.setStorageSync('follow_list_key', follow_str)
		}
		console.log(Follow_List)
		return Follow_List;
	},
	async follow_action(match) {

		if (!loginHelper.hasLogin()) {
			uni.showToast({
				icon: "none",
				title: "未登录",
			});
			return []
		}

		var index_flag = Follow_List.some(function(item, index) {
			if (item.Id === match.Id) {
				Follow_List.splice(index, 1);
			}
			return item.Id === match.Id;
		})

		let follow_str = JSON.stringify(Follow_List)
		await uni.setStorageSync('follow_list_key', follow_str)

		if (index_flag) {
			uni.showToast({
				title: "已取关"
			})
			match.isFollow = false
		} else {
			Follow_List.push(match)
			match.isFollow = true
			uni.showToast({
				title: "已关注"
			})
		}
		return Follow_List;

	}
}

export default followHelper