Sanakey
2021-07-09 3fc241d02fb8b671289de3d9d80bf848349c04d4
提交 | 用户 | age
6eb4ec 1 import MyValidator from "../../utils/myValidator";
S 2 let validatorInstance:MyValidator;
3 const app = getApp<IAppOption>();
4 Page({
5   data: {
6     Telephone:'',
7     Street:'',
8     LinkMan:'',
9     Seq:'',
10     location: {} as any,
11   },
12   async onLoad(options) {
13     console.log(app);
14     let seq = options.seq;
15     if (seq != undefined) {
16       console.log('seq',options);
17       this.setData({
18         Seq:options.seq
19       })
20       await this.getAddressDetails(seq);
21     }
22
23   },
24   onReady() {
25     this.initValidator()
26   },
27   onShow() {
28
29   },
30   async getAddressDetails(paymentaddress:string) {
31      let [error, result] = await wx.$utils.to(
32        wx.$http.request({
33          url:`/shopping/address.do?m=getAddressDetail`,
34          data: {
35            paymentaddress
36          }
37        })
38      )
39      if (error){
40        console.log(error);
41        return;
42      }
43      console.log(result);
44      this.setData({
45         ...result.list,
46        [`location.address`]:result.list.Address,
47        [`location.name`]:result.list.AddressName,
48        [`location.longitude`]:result.list.Longitude,
49        [`location.latitude`]:result.list.Latitude,
50
51      })
52   },
53
a3c13b 54   //从微信读取
S 55   readFromWx() {
56     wx.chooseAddress({
57       success:(res)=> {
58         console.log(res);
59         let LinkMan = res.userName;
60         let Street = res.detailInfo;
61         let Telephone = res.telNumber;
62         //
63         this.setData({
64           LinkMan,
65           Telephone,
66           Street
67         });
68       }
69     })
70   },
71
72   getLocation(e:any){
73     // console.log('获取到地址定位',e);
74     this.setData({
75       location:e.detail
76     })
77   },
78
6eb4ec 79   checkLinkMan(e:any){
S 80     let { value } = e.detail
81     console.log(e);
82     validatorInstance.checkField('LinkMan',value);
83   },
84   checkTelephone(e:any){
85     let { value } = e.detail
86     console.log(e);
87     validatorInstance.checkField('Telephone',value);
88   },
89   checkStreet(e:any){
90     let { value } = e.detail
91     console.log(e);
92     validatorInstance.checkField('Street',value);
93   },
94   // checkField(type: string,value: string){
95   //   validatorInstance.checkField(type,value);
96   // },
97   onSave() {
98     if (!this.data.Seq) {
99       let locationComponent = this.selectComponent('#location-plugin');
100       let location = locationComponent.getLocation();
3fc241 101       if (!location) {
S 102         wx.showToast({
103           title:'请选择收货地址',
104           icon: 'none',
105           duration:3000
106         })
107         return
108       };
6eb4ec 109       this.setData({location});
S 110     }
111
112     if (validatorInstance.checkAllData()){
113       console.log('....提交订单成功');
114       this.saveAddress();
115     } else {
116       console.log('还有错误未处理');
117     }
118
119   },
120   async saveAddress() {
121     let ac = 'new';
122     if (this.data.Seq){
123       ac = 'edit';
124     }
125     let data = {
27d5ae 126       paymentAddress:this.data.Seq,
6eb4ec 127       ac,
S 128       linkMan:this.data.LinkMan,
129       telephone:this.data.Telephone,
130       street:this.data.Street,
131       addressName:this.data.location.name,
132       address:this.data.location.address,
133       longitude:this.data.location.longitude,
134       latitude:this.data.location.latitude,
135     }
136     console.log('data',data);
137     let [error, result] = await wx.$utils.to(
138       wx.$http.request({
139         url:`/shopping/address.do?m=updateAddress`,
140         data: data,
141         method:'POST'
142       })
143     )
144     if (error){
145       console.log(error);
146       return;
147     }
148     console.log(result);
27d5ae 149     wx.navigateBack();
6eb4ec 150   },
S 151    onDelete() {
152      wx.pro.showModal({
153        title: '提示',
154        content: '确定要删除该地址吗?',
155      }).then((res:any) => {
156        // on close
157        console.log(res);
158        if (res.confirm){
159          console.log('确认');
160          this.deleteAddress();
161        }
162      });
163   },
164   async deleteAddress() {
165     let [error, result] = await wx.$utils.to(
166       wx.$http.request({
167         url:`/shopping/address.do?m=delAddress`,
168         data: {
169           paymentaddress:this.data.Seq,
170         },
171       })
172     )
173     if (error){
174       console.log(error);
175       return;
176     }
177     console.log(result);
178     wx.navigateBack();
179   },
180
181   // validatorInstance(){},
182   initValidator(){
183     // 实例化
184     validatorInstance = new MyValidator({
185       rules: {
186         LinkMan: {
187           required: true,
188           rangelength: [2, 32]
189         },
190         Telephone: {
191           required: true,
192           mobile: true,
193         },
194         Street: {
195           required: true,
196         },
197       },
198       messages: {
199         LinkMan: {
200           required: '请输入用户名',
201           rangelength: '联系人姓名必须在2 至 32 字符之间!'
202         },
203         Telephone: {
204           required: '请输入手机号',
205           mobile: '手机号格式不正确'
206         },
207         Street: {
208           required: '请输入街道地址'
209         },
210       },
211       multiCheck:true
212     },this)
213   },
214 })
215 export {}