Uni-app---封装request.js
<p>最近在用Uni-app做混合APP开发,对于众多的接口请求,为了方便管理,采用封装request来方便管理。简单代码示例:</p><p>在utils目录新建config.js文件,用于做配置</p>
<div class="cnblogs_code">
<pre>let host = ""<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(process.env.NODE_ENV === 'development'<span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 开发环境</span>
host = "http://www.dianphp.com/"<span style="color: rgba(0, 0, 0, 1)">;
}</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 生成环境</span>
host = "http://www.dianphp.com/"<span style="color: rgba(0, 0, 0, 1)">;
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> host;</pre>
</div>
<p>在utils目录新建request.js文件,用于分装请求</p>
<div class="cnblogs_code">
<pre>import host from './config.js'<span style="color: rgba(0, 0, 0, 1)">
const header </span>=<span style="color: rgba(0, 0, 0, 1)"> {}
const request </span>= (url='',method='POST',data={}) =><span style="color: rgba(0, 0, 0, 1)"> {
header[</span>'content-type'] = "application/x-www-form-urlencoded"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Promise((resolve,reject) =><span style="color: rgba(0, 0, 0, 1)"> {
uni.request({
method:</span>'post'<span style="color: rgba(0, 0, 0, 1)">,
url:host </span>+<span style="color: rgba(0, 0, 0, 1)"> url,
data:data,
header:header,
dataType:</span>'json'<span style="color: rgba(0, 0, 0, 1)">
}).then((response) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
let </span>=<span style="color: rgba(0, 0, 0, 1)"> response;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 登录过期</span>
<span style="color: rgba(0, 0, 255, 1)">if</span>(res.code == 10086<span style="color: rgba(0, 0, 0, 1)">){
uni.showToast({
title:</span>'登录过期,请重新登录'<span style="color: rgba(0, 0, 0, 1)">,
duration:</span>2000<span style="color: rgba(0, 0, 0, 1)">
});
};
resolve(res.data);
}).</span><span style="color: rgba(0, 0, 255, 1)">catch</span>((error) =><span style="color: rgba(0, 0, 0, 1)"> {
let </span>=<span style="color: rgba(0, 0, 0, 1)"> error;
reject(err);
});
});
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> request</pre>
</div>
<p>新建api目录,存放api.js</p>
<div class="cnblogs_code">
<pre>import request from '@/utils/request.js'<span style="color: rgba(0, 0, 0, 1)">
module.exports </span>=<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 登录</span>
<span style="color: rgba(0, 0, 0, 1)"> login(data){
</span><span style="color: rgba(0, 0, 255, 1)">return</span> request('api/user/login','post'<span style="color: rgba(0, 0, 0, 1)">,data);
}
}</span></pre>
</div>
<p>在需要进行http请求的页面如何使用?</p>
<p>第一步:引入api.js</p>
<div class="cnblogs_code">
<pre>import api from '@/api/api.js'</pre>
</div>
<p>第二步:使用api</p>
<div class="cnblogs_code">
<pre>api.login(<span style="color: rgba(0, 0, 255, 1)">this</span>.accountPassword).then(res =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(res);
});</span></pre>
</div>
<p>其他方法示例:</p>
<p>utils目录新建request.js用于分装请求 </p>
<div class="cnblogs_code">
<pre>export <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> class Request {
http (router,data</span>=<span style="color: rgba(0, 0, 0, 1)">{},method) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 基础地址</span>
let path = 'http://tm.jiangzi.com/v1'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回promise</span>
<span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Promise((resolve,reject) =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 请求</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.request({
url: `${path}${router}`,
data: data,
method:method,
header: {
</span>'content-type': 'application/json'<span style="color: rgba(0, 0, 0, 1)"> ,
</span>'X-Requested-With':'XMLHttpRequest'<span style="color: rgba(0, 0, 0, 1)">
},
success: (res) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将结果抛出</span>
<span style="color: rgba(0, 0, 0, 1)"> resolve(res.data)
}
})
})
}
}</span></pre>
</div>
<p>新建api.js</p>
<div class="cnblogs_code">
<pre>import Request from './request.js'<span style="color: rgba(0, 0, 0, 1)">;
let request </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Request().http
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">请求方法</span>
export <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
getPageData: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (data) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> request('/category',data,'POST'<span style="color: rgba(0, 0, 0, 1)">)
},
getSearch: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (data) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> request('/search',data,'POST'<span style="color: rgba(0, 0, 0, 1)">)
},
getrichtext: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (url) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> request(url,'','GET'<span style="color: rgba(0, 0, 0, 1)">)
}
}</span></pre>
</div>
<p>组件里面调用:</p>
<div class="cnblogs_code">
<pre>import api from '../../static/js/api.js'<span style="color: rgba(0, 0, 0, 1)">;
api.getPageData(</span>'').then(res =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(res)
})</span></pre>
</div>
<p>另外,还可以将api.js进行全局引入:</p>
<p>第一步:在main.js里面进行引入:</p>
<div class="cnblogs_code">
<pre>import api from './api/api.js'</pre>
</div>
<p>第二步:放到全局</p>
<div class="cnblogs_code">
<pre>Vue.prototype.$api = api</pre>
</div>
<p>接口调用:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">this</span>.$api.login({username:'张三',password:'123456'}).then((res) =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(res);
});</span></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/e0yu/p/13469653.html
頁:
[1]