1. 新建utils文件夹,在该文件夹下新建request.js文件
其中的内容如下
const GET = 'get' ;
const POST = 'post' ;
const baseURL = ' ' ;
module. exports = function ( options ) {
return new Promise ( function ( resolve, reject ) {
let header = {
'content-type' : 'application/json'
} ;
wx. request ( {
url : baseURL + options. url,
method : options. method,
data : options. method === POST ? JSON . stringify ( options. data) : options. data,
header : header,
success ( res ) {
wx. showLoading ( {
title : '加载中...' ,
success : function ( ) {
resolve ( res. data)
}
} )
} ,
fail : function ( err ) {
reject ( err)
wx. showToast ( {
title : '网络繁忙,请稍后重试~' ,
icon : 'none'
} )
} ,
complete : function ( ) {
wx. hideLoading ( ) ;
}
} )
} )
}
2. 新建一个api文件夹,在该文件夹中新建对应的 XXX.js文件
在该文件中写入已下代码
const request = require ( '../utils/request' )
export function regist ( data ) {
return request ( {
url : '/user/register' ,
method : 'get' ,
data
} )
}
export function product ( data ) {
return request ( {
url : '/cart/updateNum' ,
method : 'post' ,
data
} )
}
3. 在page文件夹的js文件中进行进入
const { regist, product} = require ( '../../../api/login' )
regist ( params) . then ( res => {
console. log ( res) ;
} )
product ( ) . then ( res => {
console. log ( res, 111111 ) ;
} )