Sanakey
2021-06-04 355a84d740a2a9dfa1d67cb2d2e11843d42809ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// const app = getApp<IAppOption>();
Component({
  /**
   * 组件的属性列表
   */
  options: {
    addGlobalClass: true,
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    sellerList:{
      type: Array,
      value: []
    },
    from:{
      type:String,
      value: ''
    }
    // validatorInstance:{
    //   type:Object,
    //   observer: function (newVal, oldVal, changedPath) {
    //     // 属性被改变时执行的函数(可选),也可以写成在methods段中定义的方法名字符串
    //     // 通常 newVal 就是新设置的数据, oldVal 是旧数据
    //
    //     console.log(newVal,oldVal,changedPath);
    //   }
    // }
  },
 
  /**
   * 组件的初始数据
   */
  data: {
    selectedSellerList:[
      // {DocItem: 10, SellerId: 1, SellerName: "好几十", Telephone: "15555555555", isDefaultSellerName: false},
      // {DocItem: 10, SellerId: 2, SellerName: "好", Telephone: "15588555555", isDefaultSellerName: false},
      // {DocItem: 10, SellerId: 3, SellerName: "555", Telephone: "18888555555", isDefaultSellerName: false},
    ] as any,
    selectedSeller:'',
    selectedSellerIdList:[] as any,
    index: -1,
    isFirstOpen:true
  },
 
  lifetimes:{
    async ready() {
      console.log('seller edit ...ready, from:',this.data.from);
      if (this.data.from != 'edit'){
        await this.getDefaultSeller();
      }
    }
  },
  pageLifetimes:{
    show(){
      let {selectedSellerIdList,selectedSeller,isFirstOpen} = this.data;
      console.log('isFirstOpen',isFirstOpen);
      if (isFirstOpen) return;
      let newSelectedSellerList = [] as any;
      console.log('>>>>>>>>>>>>>>组件页面show开始>>>>>>>>>>>>>>');
      console.log('页面show时 新建活动页当前的selectedSellerIdList',selectedSellerIdList);
      if (selectedSellerIdList.length>0){
        let selectedSellerList = this.getSelectedSellerList();
        console.log('页面show时 新建活动页当前的selectedSellerList',selectedSellerList);
 
        if (selectedSellerIdList.length != selectedSellerList.length) {
          selectedSellerIdList.forEach((idItem:any)=>{
            selectedSellerList.forEach((item:any)=>{
              if (item.sellerId == idItem) {
                newSelectedSellerList.push(item)
              }
            })
          })
          selectedSellerList = newSelectedSellerList;
 
          console.log('营销人员被删除,selectedSellerList数据更新后',newSelectedSellerList);
          this.setData({
            selectedSellerList
          })
        }
        console.log('selectedSellerIdList是否包含selectedSeller',selectedSellerIdList.includes(selectedSeller.toString() as never),selectedSellerIdList,selectedSeller.toString());
        if (!selectedSellerIdList.includes(selectedSeller.toString() as never)) {
          this.setData({
            selectedSeller: selectedSellerList[0].sellerId,
            index: 0
          })
 
        }
        console.log('页面show时 新建活动页当前的selectedSeller',this.data.selectedSeller,this.data.index);
      }
      console.log('<<<<<<<<<<<<<<<组件页面show结束<<<<<<<<<<<<<<<');
    }
  },
  observers: {
    'sellerList': function(val) {
      console.log('监听sellers',val,this.data.sellerList)
      if (val.length > 0) {
        let selectedSellerList = val;
        let selectedSellerIdList = [] as any;
        console.log('营销人员列表sellers。。。与id、、、',selectedSellerList,selectedSellerIdList);
        selectedSellerList.forEach((item:any,index:number)=>{
          selectedSellerIdList.push(item&&item.sellerId&&item.sellerId.toString());
          if (item.isDefaultSellerName){
            this.setData({
              selectedSeller: item.sellerId,
              index
            })
          }
        })
        this.setData({selectedSellerList,selectedSellerIdList});
      }
 
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
 
    getSelectedSellerList(){
      let {selectedSellerIdList} = this.data;
      let selectedSellerList = [] as any;
      let sellerList = wx.globalData.sellerList;
      selectedSellerIdList.forEach((id:any)=>{
        sellerList.forEach((item:any)=>{
          if (item.sellerId == Number(id)) {
            selectedSellerList.push(item)
          }
        })
      })
      this.setData({
        selectedSellerList
      })
      return selectedSellerList;
    },
 
    // 父组件获取待提交的营销人员数据
    getSellerList(){
      let { selectedSellerList, index} = this.data;
      selectedSellerList.forEach((item:any)=>{
        item.isDefaultSellerName = false;
      })
      if (index >=0&&selectedSellerList.length>0){
        selectedSellerList[index].isDefaultSellerName = true;
      }
      console.log('待提交的营销人员数据',selectedSellerList);
      return selectedSellerList;
    },
    checkDefaultSeller(){
      let { selectedSellerList } = this.data;
      return !(selectedSellerList.every((currentValue:any)=>{
        return currentValue.isDefaultSellerName == false;
      }))
    },
    addSeller(){
      wx.navigateTo({
        url: `/pages/sellerList/sellerList`,
        success: (res)=> {
          let selectedSellerIdList = this.data.selectedSellerIdList;
          console.log('新建活动页当前的selectedSellerIdList',selectedSellerIdList);
          this.setData({
            isFirstOpen:false
          })
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('getSellerListFromPromotionsPage', {
            selectedSellerIdList
          })
        },
        events: {
          // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
          getSelectedSellerList: (data: any)=> {
            console.log('获取营销人员选择页选中的data',data)
            this.setData({
              selectedSellerIdList:data.selectedSellerIdList,
              // selectedSellerList:data.selectedSellerList
            })
          },
        }
      })
    },
    async getDefaultSeller(){
      let [error, result] = await wx.$utils.to(
        wx.$http.request({
          url:`/shopping/panicBuyingSeller.do?m=getDefaultSeller`,
        })
      )
      if (error){
        console.log(error);
        return;
      }
      console.log(result);
      if (result.sellerList.length>0){
        this.setData({
          sellerList:result.sellerList
        })
      }
    }
  }
})