1. The logic of this is the user login is successful, we store token and user information to the local
2.config file is stored baseUrl
1 const config = { 2 3 url: 'https:XXX/index.php' 4 } 5 export { 6 config 7 }
3. library request requset.js, http request function returns a promise object, using the micro-channel wx.request interface when the request is successful Resolve, reject the request fails
. 1 Import { 2 config . 3 } from './config.js' . 4 Import from Toast '../ui/dist/toast/toast' . 5 . 6 // Public request interface method . 7 var OnOff = to true . 8 const = HTTP (Method , URL, Data) => { . 9 10 // loading animation . 11 wx.showLoading ({ 12 is title: 'loading ...' , 13 is showCancel: to true , 14 mask: to true 15 }); 16 . 17 // user information objects 18 is . 19 the let LoginInfo = wx.getStorageSync ( 'login_info') the JSON.parse (wx.getStorageSync ( 'login_info'? )): { 20 is token: null , 21 is ID: null 22 is }; 23 is return new new Promise ( function (Resolve, Reject) { 24 25 wx.request ({ 26 is Method: Method, // request type 27 URL: URL config.url +, // request address 28 header: { // request header 29 'Content-type': 'file application / JSON' , 30 'token' : loginInfo.token, 31 is 'ID' : loginInfo.id 32 }, 33 is Data: Data, // request parameter 34 is Success: RES => { 35 WX .hideLoading (); // completion of the request to close a loading animation 36 // token expires clear user information (return status codes according to the corresponding rear line knockdown) 37 [ IF (res.data.error_code == 21000 ) { 38 is the console.log ( "Login timeout" ) 39 IF (! onoff)return 40 wx.showModal ({ 41 is title: 'prompt' , 42 is Content: 'login has expired' , 43 is confirmText: "log in again" , 44 is showCancel: to false , 45 Success (RES) { 46 is IF (res.confirm) { 47 wx.removeStorageSync ( "login_info" ); 48 wx.setStorageSync ( 'isLogin', to false ); 49 wx.reLaunch({ url: "/pages/login/pages/guide/guide" }); 50 // onoff=!onoff; 51 } 52 } 53 }) 54 55 return 56 } 57 if (res.statusCode == 200) { 58 onoff=true; 59 resolve(res.data); //成功回调 60 } else { 61 the console.log (res.data, 666 ) 62 is wx.showToast ({ 63 is title: res.data.msg || 'Network Error!' , 64 icon: 'none' , 65 DURATION: 2000 66 }) 67 } 68 } , 69 fail: ERR => { 70 the console.log (ERR, "request failed" ) 71 is Reject (ERR); // failure callback 72 wx.hideLoading (); //请求完成关闭加载动画 73 83 }, 84 complete: info => { 85 86 87 } 88 }) 89 }) 90 } 91 92 const Get = (url, data) => http('GET', url, data) 93 const Post = (url, data) => http('POST', url, data) 94 const PUT = (url, data) => http('PUT', url, data) 95 const DELETE = (url, data) => http('DELETE', url, data) 96 export { 97 Get, 98 Post, 99 DELETE, 100 PUT 101 }
4.api file
. 1 Import { 2 the Get, . 3 Post, . 4 the PUT . 5 } from '../utils/request.js' . 6 // account login interfaces . 7 Export function loginHandle (the params) { . 8 return Post ( '/ API / V1 / Login' , the params) . 9 }
The page calls
import {
loginHandle
} from '../utils/request.js'
loginHandle (). the then (RES => {
the console.log (RES, "request was successful return value")
})