Commit 681557ea14b9953248819085e3c0b47b50b90b44

Authored by wangfan
1 parent 0972ab39

c端预定

... ... @@ -2,6 +2,8 @@
import util from '../../utils/util';
import App from '../../utils/api';
const {regeneratorRuntime} = global
import PubSub from '../../utils/PubSub'
console.log(PubSub)
Page({
/**
... ... @@ -11,14 +13,33 @@ Page({
sellers:[],
hasMore:true,
},
sellers:[],
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
PubSub.on(
'follow',
data=>{
console.log('follow.msg',data)
this.formatSellers(data);
}
)
},
formatSellers(data){
let sellers=[];
this.sellers.forEach(seller=>{
if(seller.id==data.sid){
seller.hasFollow=data.hasFollow
}
if(seller.hasFollow){
sellers.push(seller)
}
})
this.setData({
sellers:sellers
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
... ... @@ -30,7 +51,12 @@ Page({
* 生命周期函数--监听页面显示
*/
onShow: function () {
if(this.hasMore){
if(this.hasMore || this.data.sellers.length==0){
this.sellers=[]
this.offset=1
this.setData({
sellers:[]
})
this.getPos()
}
},
... ... @@ -46,7 +72,7 @@ Page({
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
PubSub.off('follow')
},
/**
... ... @@ -94,13 +120,14 @@ Page({
}else{
seller.seller_tel=[]
}
seller.hasFollow=true
})
sellers=[...this.data.sellers,...sellers]
this.setData({
sellers:[...this.data.sellers,...sellers],
sellers:sellers,
hasMore:this.hasMore,
})
this.sellers=sellers
})
},
getPos(){
... ...
... ... @@ -6,7 +6,8 @@
<view>您还没有收藏任何餐厅哦</view>
</view>
<view class="follow" wx:else="">
<view class="seller" wx:for="{{sellers}}" wx:key="sellers.key" bindtap="gotoDetail" data-sid="{{item.id}}">
<view class="seller" wx:for="{{sellers}}" wx:key="sellers.key"
bindtap="gotoDetail" data-sid="{{item.id}}" wx:if="{{item.hasFollow}}">
<sb-tr seller="{{item}}" hasStat="{{true}}"></sb-tr>
</view>
<view class="seller flex-vc" wx:if="{{!hasMore}}">没有更多了</view>
... ...
... ... @@ -2,6 +2,7 @@
import util from '../../utils/util';
import App from '../../utils/api';
const {regeneratorRuntime} = global
import PubSub from '../../utils/PubSub'
Page({
/**
... ... @@ -135,10 +136,18 @@ Page({
if(this.data.IsCollectionSeller){//已收藏
param.cancel=1
}
let hasFollow=!this.data.IsCollectionSeller
this.setData({
IsCollectionSeller:!this.data.IsCollectionSeller
IsCollectionSeller:hasFollow
})
App.Customer.SetCollectionRel(param);
PubSub.emit(
'follow',
{
sid:this.options.id,
hasFollow:hasFollow,
}
)
},
getPos(){
this._getPos().then(res => {
... ...
/**
* Created by goto9 on 2019/3/8.
*/
class PubSub {
constructor() {
this.PubSubCache = {
$uid: 0
};
}
on(type, handler) {
let cache = this.PubSubCache[type] || (this.PubSubCache[type] = {});
handler.$uid = handler.$uid || this.PubSubCache.$uid++;
cache[handler.$uid] = handler;
}
emit(type, ...param) {
let cache = this.PubSubCache[type],
key,
tmp;
if(!cache) return;
for(key in cache) {
tmp = cache[key];
cache[key].call(this, ...param);
}
}
off(type, handler) {
let counter = 0,
$type,
cache = this.PubSubCache[type];
if(handler == null) {
if(!cache) return true;
return !!this.PubSubCache[type] && (delete this.PubSubCache[type]);
} else {
!!this.PubSubCache[type] && (delete this.PubSubCache[type][handler.$uid]);
}
for($type in cache) {
counter++;
}
return !counter && (delete this.PubSubCache[type]);
}
}
export default new PubSub()
\ No newline at end of file
... ...