Sanakey
2021-07-03 a3c13b29850a908c95116604b6327b3949801ed4
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
199
200
201
202
203
204
205
206
207
208
import MyValidator from "../../utils/myValidator";
let validatorInstance:MyValidator;
const app = getApp<IAppOption>();
Page({
  data: {
    Telephone:'',
    Street:'',
    LinkMan:'',
    Seq:'',
    location: {} as any,
  },
  async onLoad(options) {
    console.log(app);
    let seq = options.seq;
    if (seq != undefined) {
      console.log('seq',options);
      this.setData({
        Seq:options.seq
      })
      await this.getAddressDetails(seq);
    }
 
  },
  onReady() {
    this.initValidator()
  },
  onShow() {
 
  },
  async getAddressDetails(paymentaddress:string) {
     let [error, result] = await wx.$utils.to(
       wx.$http.request({
         url:`/shopping/address.do?m=getAddressDetail`,
         data: {
           paymentaddress
         }
       })
     )
     if (error){
       console.log(error);
       return;
     }
     console.log(result);
     this.setData({
        ...result.list,
       [`location.address`]:result.list.Address,
       [`location.name`]:result.list.AddressName,
       [`location.longitude`]:result.list.Longitude,
       [`location.latitude`]:result.list.Latitude,
 
     })
  },
 
  //从微信读取
  readFromWx() {
    wx.chooseAddress({
      success:(res)=> {
        console.log(res);
        let LinkMan = res.userName;
        let Street = res.detailInfo;
        let Telephone = res.telNumber;
        //
        this.setData({
          LinkMan,
          Telephone,
          Street
        });
      }
    })
  },
 
  getLocation(e:any){
    // console.log('获取到地址定位',e);
    this.setData({
      location:e.detail
    })
  },
 
  checkLinkMan(e:any){
    let { value } = e.detail
    console.log(e);
    validatorInstance.checkField('LinkMan',value);
  },
  checkTelephone(e:any){
    let { value } = e.detail
    console.log(e);
    validatorInstance.checkField('Telephone',value);
  },
  checkStreet(e:any){
    let { value } = e.detail
    console.log(e);
    validatorInstance.checkField('Street',value);
  },
  // checkField(type: string,value: string){
  //   validatorInstance.checkField(type,value);
  // },
  onSave() {
    if (!this.data.Seq) {
      let locationComponent = this.selectComponent('#location-plugin');
      let location = locationComponent.getLocation();
      if (!location) return;
      this.setData({location});
    }
 
    if (validatorInstance.checkAllData()){
      console.log('....提交订单成功');
      this.saveAddress();
    } else {
      console.log('还有错误未处理');
    }
 
  },
  async saveAddress() {
    let ac = 'new';
    if (this.data.Seq){
      ac = 'edit';
    }
    let data = {
      paymentAddress:this.data.Seq,
      ac,
      linkMan:this.data.LinkMan,
      telephone:this.data.Telephone,
      street:this.data.Street,
      addressName:this.data.location.name,
      address:this.data.location.address,
      longitude:this.data.location.longitude,
      latitude:this.data.location.latitude,
    }
    console.log('data',data);
    let [error, result] = await wx.$utils.to(
      wx.$http.request({
        url:`/shopping/address.do?m=updateAddress`,
        data: data,
        method:'POST'
      })
    )
    if (error){
      console.log(error);
      return;
    }
    console.log(result);
    wx.navigateBack();
  },
   onDelete() {
     wx.pro.showModal({
       title: '提示',
       content: '确定要删除该地址吗?',
     }).then((res:any) => {
       // on close
       console.log(res);
       if (res.confirm){
         console.log('确认');
         this.deleteAddress();
       }
     });
  },
  async deleteAddress() {
    let [error, result] = await wx.$utils.to(
      wx.$http.request({
        url:`/shopping/address.do?m=delAddress`,
        data: {
          paymentaddress:this.data.Seq,
        },
      })
    )
    if (error){
      console.log(error);
      return;
    }
    console.log(result);
    wx.navigateBack();
  },
 
  // validatorInstance(){},
  initValidator(){
    // 实例化
    validatorInstance = new MyValidator({
      rules: {
        LinkMan: {
          required: true,
          rangelength: [2, 32]
        },
        Telephone: {
          required: true,
          mobile: true,
        },
        Street: {
          required: true,
        },
      },
      messages: {
        LinkMan: {
          required: '请输入用户名',
          rangelength: '联系人姓名必须在2 至 32 字符之间!'
        },
        Telephone: {
          required: '请输入手机号',
          mobile: '手机号格式不正确'
        },
        Street: {
          required: '请输入街道地址'
        },
      },
      multiCheck:true
    },this)
  },
})
export {}