What I want to achieve is that I can pull up to refresh, pull down to load, and hit TAB to request the server. I think the best way is to separate the date and month into one method, and then pull up to refresh, pull down to load, click TAB to call the date and month methods respectively. I tried Baidu/Google and saw most of the chain notation to get user data for example:

// All the operations here are examples. wxLogin() .then(=>res{ console.log(res.code) return wxRequest.getUserOpenId(url, Then (=>res{console.log(res.openId) return wxRequest.getUserPhone()); // Request user phone}).catch()Copy the code

On the index.js page; GetUserOpenId () and getUserPhone() are wrapped in another JS. Or just getUserMessage().then(res=>).catch(); So you get the user data directly.

    wxRequest.getUserMessage()
.then(=>res{
                     console.log(res.code);
})
.catch()
Copy the code

Of course, I can’t show you what’s wrong with this example, so what if I want to get the date and month data as shown above? Do I write the same thing as the first one, pull up load and write a bunch of code there, pull down refresh and write a bunch of code there, switch TAB and write a bunch of code again? Or do I care if you pull up refresh, or if you pull down load, I just get the data together through an interface? So I went to Baidu/Google to find out that their writing is not perfect enough.

Page({data:{}, onLoad: function () {this.readUsermesage (); }, readUserMesage(): function () {this.showloading (); let parameters = 'user? ptId=aaaaaaaaaaaaaa'; api.getRequest({ parameters: parameters, }).then(res => { return this.readDayData(); }). Catch (error => {this.hideloading (); }); }, readMonthData() {var url = 'dailypay? xxxxx=aaaa&yyy=bbbbbb'; var parameters = { parameters: Url} return api.getrequest (parameters) Then (res => {console.log(json.stringify (error)); this.hideLoading(); }); }, readDayData() {var url = 'dailypay? xxxxx=aaaa&yyy=bbbbbb'; var parameters = { parameters: Url} return api.getrequest (parameters) Catch (error => {this.hideloading ();}).catch(error => {this.hideloading (); }); }, onPullDownRefresh() {// Pull refresh if (this.data.currentIndex == 0) {// If the current date is this.readdayData (); } else {// If the current month is this.readMonthData(); }}, onReachBottom() {if (this.data.currentIndex == 0) {this.readdayData (); } else { this.readMonthData(); )}}},Copy the code

Okay, so that’s my method presentation. This reduces the amount of code.

Author: A winter



Links:Small program Promise requests server writing continuously, reducing the amount of code



Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.