相关文章推荐
小百科
›
Http请求 | uView - 多平台快速开发的UI框架 - uni-app UI框架
app
小程序
软件
vue
博学的乌冬面
3 月前
(opens new window)
Gitee
(opens new window)
开发指南
介绍
便捷工具
网络
Http请求
API集中管理
工具库
节流防抖
对象深度克隆
对象深度合并
时间格式化
路由跳转
数组乱序
全局唯一标识符
颜色转换
颜色值
对象转URL参数
规则校验
md5加密
随机数值
去除空格
节点布局信息
小程序分享
#
Http请求
</div></div> <p>该插件适用于一般的请求场景,只支持<code>post</code>、<code>get</code>、<code>put</code>和<code>delete</code>请求,目前不适用于其他的请求形式,比如上传,下载等。插件定位为 小而美,而不是大而全,目标是切合实际,开箱即用。</p> <h3 id="平台差异说明"><a href="#平台差异说明" class="header-anchor">#</a> 平台差异说明</h3> <table><thead><tr><th style="text-align:center;">App</th> <th style="text-align:center;">H5</th> <th style="text-align:center;">微信小程序</th> <th style="text-align:center;">支付宝小程序</th> <th style="text-align:center;">百度小程序</th> <th style="text-align:center;">头条小程序</th> <th style="text-align:center;">QQ小程序</th></tr></thead> <tbody><tr><td style="text-align:center;">√</td> <td style="text-align:center;">√</td> <td style="text-align:center;">√</td> <td style="text-align:center;">√</td> <td style="text-align:center;">√</td> <td style="text-align:center;">√</td> <td style="text-align:center;">√</td></tr></tbody></table> <p>由于某些小程序平台的限制:</p> <ul><li>delete请求,不支持支付宝和头条小程序(HX2.6.15)</li> <li>put请求,不支持支付宝小程序(HX2.6.15)</li></ul> <h3 id="基本使用"><a href="#基本使用" class="header-anchor">#</a> 基本使用</h3> <h4 id="get-post-put-delete-url-params-header-then-res-catch-res"><a href="#get-post-put-delete-url-params-header-then-res-catch-res" class="header-anchor">#</a> get | post | put | delete(url, params, header).then(res => {}).catch(res => {})</h4> <ul><li><code>url</code> <String> 请求的URL,可以完整的URL(http开头),或者是路径的一部分,这时会自动拼接上<code>baseUrl</code>(一般为api的域名部分)</li> <li><code>params</code> <Object> 请求的参数,对象形式,如"{name: 'lisa', age: 23}",该参数是可选的</li> <li><code>header</code> <Object> 请求的header,对象形式,如果token等字段,建议通过配置写入,该参数是可选的</li></ul> <p><code>get</code>和<code>post</code>都挂载在<code>$u</code>对象下,其中<code>get</code>和<code>post</code>使用方法完全一致,只是一个为<code>this.$u.get</code>,一个为<code>this.$u.post</code>,使用如下:</p> <p>一般来说,只在<code>then</code>中接收返回值即可,<code>catch</code>无特殊情况,无需编写和理会,因为如果服务端返回的不是200状态,插件内部会弹出model提示</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"> <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span> <span class="token function">onLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 不带header</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'http://www.example.com'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">id</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token literal-property property">menu</span><span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">res</span> <span class="token operator">=></span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 带上header(对象形式),由于header为第三个参数,如果不需要请求参数,第二个参数传一个空对象"{ }"即可</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'http://www.example.com'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">token</span><span class="token operator">:</span> <span class="token string">'xyz'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">res</span> <span class="token operator">=></span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> </span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span> </code></pre></div><h3 id="配置参数"><a href="#配置参数" class="header-anchor">#</a> 配置参数</h3> <p>配置参数的时候,需要调用<code>$u.http.setConfig()</code>方法,传递一个对象作为参数。</p> <ul><li>强烈建议在此配置统一请求的<code>baseUrl</code></li> <li>同时看情况是否开启(默认关闭)请求加载中的loading,该功能需要设置一个时间(默认800ms),如果超过此时间,请求尚未返回,则显示一个loading,直至返回后,取消loading。</li></ul> <p>说明:请求loading超时时间的意义为,一般情况下,请求会在几十毫秒返回,时间极短,无需loading,如果显示loading,会导致 动画一闪而过,体验不好。如果用户网络慢,或者服务器堵塞,可能一个请求需要几秒钟,这时请求达到设定时间(800ms), 就会显示loading,几秒钟后请求返回,loading消失。</p> <br/> <div class="custom-block tip"><p class="custom-block-title">提示</p> <p>uView在<strong>1.1.0</strong>版本后,建议将拦截器内容部分,写入到独立的外部JS文件中,而不是写在App.vue中,这样有更好的可读性,可维护性。<br/> uView已经为用户考虑好了所有的情况,并详细指导您如何在外部JS中引用vuex变量以及Vue的<code>this</code>实例等。<br/> 建议在根目录下新建<code>/common/http.interceptor.js</code>文件,也即创建<code>common</code>目录(如果没有的话),再创建<code>http.interceptor.js</code>文件,将拦截器相关代码写在里面。</p></div> <br/> <p>以下为可选的配置参数,<strong>这个配置是一次配置,全局通用的</strong>。</p> <div class="language-js extra-class"><pre class="language-js"><code>config <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token literal-property property">baseUrl</span><span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token comment">// 请求的本域名</span> <span class="token literal-property property">method</span><span class="token operator">:</span> <span class="token string">'POST'</span><span class="token punctuation">,</span> <span class="token comment">// 设置为json,返回后会对数据进行一次JSON.parse()</span> <span class="token literal-property property">dataType</span><span class="token operator">:</span> <span class="token string">'json'</span><span class="token punctuation">,</span> <span class="token literal-property property">showLoading</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token comment">// 是否显示请求中的loading</span> <span class="token literal-property property">loadingText</span><span class="token operator">:</span> <span class="token string">'请求中...'</span><span class="token punctuation">,</span> <span class="token comment">// 请求loading中的文字提示</span> <span class="token literal-property property">loadingTime</span><span class="token operator">:</span> <span class="token number">800</span><span class="token punctuation">,</span> <span class="token comment">// 在此时间内,请求还没回来的话,就显示加载中动画,单位ms</span> <span class="token literal-property property">originalData</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token comment">// 是否在拦截器中返回服务端的原始数据</span> <span class="token literal-property property">loadingMask</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token comment">// 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透</span> <span class="token comment">// 配置请求头信息</span> <span class="token literal-property property">header</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token string-property property">'content-type'</span><span class="token operator">:</span> <span class="token string">'application/json;charset=UTF-8'</span> </code></pre></div><p>具体写法,建议在写在<code>/common/http.interceptor.js</code>(如无此文件夹和文件,请手动创建,一次配置,全局通用),写完之后,请在根目录的<code>main.js</code> 的<code>new Vue()</code>和<code>app.$mount()</code>之间引入此文件,在<code>new Vue()</code>后面的原因是,外部JS文件需要引用vue的实例,也即<code>this</code>对象,要等<code>main.js</code>中通过<code>new</code>创建了实例之后才能引用。 在<code>app.$mount()</code>之前的原因是,在Vue挂载<code>this</code>实例(也即初始化<code>App.vue</code>)之前配置请求信息,所以在<code>App.vue</code>中也能正常发出请求。</p> <p>以下为在<code>main.js</code>中的引入示例:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// main.js</span> <span class="token comment">// 此为main.js本身已有内容</span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token operator">...</span>App <span class="token comment">// http拦截器,此为需要加入的内容,如果不是写在common目录,请自行修改引入路径</span> <span class="token keyword">import</span> httpInterceptor <span class="token keyword">from</span> <span class="token string">'@/common/http.interceptor.js'</span> <span class="token comment">// 这里需要写在最后,是为了等Vue创建对象完成,引入"app"对象(也即页面的"this"实例)</span> Vue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>httpInterceptor<span class="token punctuation">,</span> app<span class="token punctuation">)</span> app<span class="token punctuation">.</span><span class="token function">$mount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> </code></pre></div><p>下面为配置信息的具体内容:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// common/http.interceptor.js</span> <span class="token comment">// 这里的Vue为Vue对象(非创建出来的实例),vm为main.js中“Vue.use(httpInterceptor, app)”这一句的第二个参数,</span> <span class="token comment">// 为一个Vue的实例,也即每个页面的"this"</span> <span class="token comment">// 如果需要了解这个install方法是什么,请移步:https://uviewui.com/components/vueUse.html</span> <span class="token keyword">const</span> <span class="token function-variable function">install</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">Vue<span class="token punctuation">,</span> vm</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// 此为自定义配置参数,具体参数见上方说明</span> <span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>$u<span class="token punctuation">.</span>http<span class="token punctuation">.</span><span class="token function">setConfig</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">baseUrl</span><span class="token operator">:</span> <span class="token string">'https://api.example.com'</span><span class="token punctuation">,</span> <span class="token literal-property property">loadingText</span><span class="token operator">:</span> <span class="token string">'努力加载中~'</span><span class="token punctuation">,</span> <span class="token literal-property property">loadingTime</span><span class="token operator">:</span> <span class="token number">800</span><span class="token punctuation">,</span> <span class="token comment">// 设置自定义头部content-type</span> <span class="token comment">// header: {</span> <span class="token comment">// 'content-type': 'xxx'</span> <span class="token comment">// ......</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span> install </code></pre></div><h3 id="请求拦截和响应拦截-如配置-每次请求都会执行"><a href="#请求拦截和响应拦截-如配置-每次请求都会执行" class="header-anchor">#</a> 请求拦截和响应拦截(如配置,每次请求都会执行)</h3> <p><strong>此两个拦截,是可选配置的</strong></p> <h4 id="何谓请求拦截"><a href="#何谓请求拦截" class="header-anchor">#</a> 何谓请求拦截?</h4> <p>顾名思义,就是在请求发出之前,对请求做一些额外处理,比如对不同api接口,携带不同的<code>header</code>参数,或者(也是最重要) 配置统一的token到<code>header</code>中,这样就不用每次请求,都写token相关的部分到<code>this.$u.post()</code>的第三个请求头参数中,如下:</p> <h4 id="u-http-interceptor-request-config"><a href="#u-http-interceptor-request-config" class="header-anchor">#</a> $u.http.interceptor.request = (config) => { ... }</h4> <ul><li><code>config</code> <Object> 此<code>config</code>参数,带有header和url属性,header属性是为了添加请求头信息,url属性是为了对某些url进行特别处理</li></ul> <p>一般建议对此进行token的配置,说明:<strong>由于本拦截是每次请求都会调用的,如果在此写入token到header中,即使再次您重新登录,token发生变化,也无需再次调用本拦截</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// common/http.interceptor.js</span> <span class="token keyword">const</span> <span class="token function-variable function">install</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">Vue<span class="token punctuation">,</span> vm</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// 此为自定义配置参数,具体参数见上方说明</span> <span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>$u<span class="token punctuation">.</span>http<span class="token punctuation">.</span><span class="token function">setConfig</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token comment">// ......</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 请求拦截部分,如配置,每次请求前都会执行</span> <span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>$u<span class="token punctuation">.</span>http<span class="token punctuation">.</span>interceptor<span class="token punctuation">.</span><span class="token function-variable function">request</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">config</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// 引用token</span> <span class="token comment">// 方式一,存放在vuex的token,假设使用了uView封装的vuex方式</span> <span class="token comment">// 见:https://uviewui.com/components/globalVariable.html</span> <span class="token comment">// config.header.token = vm.token;</span> <span class="token comment">// 方式二,如果没有使用uView封装的vuex方法,那么需要使用$store.state获取</span> <span class="token comment">// config.header.token = vm.$store.state.token;</span> <span class="token comment">// 方式三,如果token放在了globalData,通过getApp().globalData获取</span> <span class="token comment">// config.header.token = getApp().globalData.username;</span> <span class="token comment">// 方式四,如果token放在了Storage本地存储中,拦截是每次请求都执行的</span> <span class="token comment">// 所以哪怕您重新登录修改了Storage,下一次的请求将会是最新值</span> <span class="token comment">// const token = uni.getStorageSync('token');</span> <span class="token comment">// config.header.token = token;</span> config<span class="token punctuation">.</span>header<span class="token punctuation">.</span>Token <span class="token operator">=</span> <span class="token string">'xxxxxx'</span><span class="token punctuation">;</span> <span class="token comment">// 可以对某个url进行特别处理,此url参数为this.$u.get(url)中的url值</span> <span class="token keyword">if</span><span class="token punctuation">(</span>config<span class="token punctuation">.</span>url <span class="token operator">==</span> <span class="token string">'/user/login'</span><span class="token punctuation">)</span> config<span class="token punctuation">.</span>header<span class="token punctuation">.</span>noToken <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment">// 最后需要将config进行return</span> <span class="token keyword">return</span> config<span class="token punctuation">;</span> <span class="token comment">// 如果return一个false值,则会取消本次请求</span> <span class="token comment">// if(config.url == '/user/rest') return false; // 取消某次请求</span> <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span> install </code></pre></div><h4 id="何谓响应拦截"><a href="#何谓响应拦截" class="header-anchor">#</a> 何谓响应拦截?</h4> <p>响应拦截,意味着是在请求返回时,对返回的数据进行一些处理,如不同的状态对应的关系,比如约定状态码200为成功, 则把返回数据返回到<code>this.$u.post().then()</code>的<code>then</code>中,如果为201(约定为token失效,需要登录),则可以在在拦截中进行toast 提示,并跳转到登录页。</p> <div class="custom-block tip"><p class="custom-block-title">注意</p> <p>响应拦截器中默认返回的是<code>response.data</code>,如果您的需求比较特殊,需要返回<code>response</code>,请在"$u.http.setConfig"配置 <code>originalData</code>为<code>true</code>,如果配置了,服务端返回的"response.statusCode"不为"200"的时候,将不会自动弹出"modal"框,请自行 在响应拦截器中配置相关行为。</p></div> <h4 id="u-http-interceptor-response-res"><a href="#u-http-interceptor-response-res" class="header-anchor">#</a> $u.http.interceptor.response = (res) => { ... }</h4> <ul><li><code>res</code> <Object> 该参数为服务器返回的数据,具体可打印查看</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// common/http.interceptor.js</span> <span class="token keyword">const</span> <span class="token function-variable function">install</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">Vue<span class="token punctuation">,</span> vm</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// 此为自定义配置参数,具体参数见上方说明</span> <span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>$u<span class="token punctuation">.</span>http<span class="token punctuation">.</span><span class="token function">setConfig</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token comment">// ......</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 请求拦截部分,如配置,每次请求前都会执行,见上方说明</span> <span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>$u<span class="token punctuation">.</span>http<span class="token punctuation">.</span>interceptor<span class="token punctuation">.</span><span class="token function-variable function">request</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">config</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// ......</span> <span class="token keyword">return</span> config<span class="token punctuation">;</span> <span class="token comment">// 响应拦截,如配置,每次请求结束都会执行本方法</span> <span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>$u<span class="token punctuation">.</span>http<span class="token punctuation">.</span>interceptor<span class="token punctuation">.</span><span class="token function-variable function">response</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>code <span class="token operator">==</span> <span class="token number">200</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// res为服务端返回值,可能有code,result等字段</span> <span class="token comment">// 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到</span> <span class="token comment">// 如果配置了originalData为true,请留意这里的返回值</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span>result<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>code <span class="token operator">==</span> <span class="token number">201</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 假设201为token失效,这里跳转登录</span> vm<span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">toast</span><span class="token punctuation">(</span><span class="token string">'验证失败,请重新登录'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// 此为uView的方法,详见路由相关文档</span> vm<span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string">'/pages/user/login'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1500</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment">// 如果返回false,则会调用Promise的reject回调,</span> <span class="token comment">// 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中,res为服务端的返回值</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span> install </code></pre></div><p><strong>实践:</strong> 上面写完了请求配置(强烈建议),请求拦截(可选,推荐),响应拦截(可选,推荐),下面为<code>post</code>和<code>get</code>请求的示例:</p> <div class="language-html extra-class"><pre class="language-html"><code>// /pages/index/index.vue <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"> <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span> <span class="token function">onLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'/ebapi/store_api/hot_search'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">id</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">res</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>result <span class="token operator">=</span> res<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'/ebapi/public_api/index'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">id</span><span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">res</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>result <span class="token operator">=</span> res<span class="token punctuation">;</span> </span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span> </code></pre></div><h3 id="如何使用await进行同步操作"><a href="#如何使用await进行同步操作" class="header-anchor">#</a> 如何使用await进行同步操作</h3> <ul><li>有时候我们需要同步的写法,即下一步的结果依赖于上一步的请求返回结果,当然我们可以把下一步的写法,放入请求的<code>then</code>回调中, 但这是不直观的,比如我们需要打印请求的返回结果,可以这样做:</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span> <span class="token literal-property property">methods</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 正确,但是需要在回调中打印</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'/user/login'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">res</span> <span class="token operator">=></span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 错误,如果想要使用同步的形式,无需then回调,且需要await关键字</span> <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'/user/login'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">res</span> <span class="token operator">=></span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 错误,无需then回调是对的,但是ret会为undefined,因为登录是一个异步过程(可能需要几十毫秒)</span> <span class="token comment">// 下一行打印的时候,请求还没返回,所以ret会为undefined</span> <span class="token keyword">let</span> ret <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'/user/login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span><span class="token punctuation">;</span> </code></pre></div><p>上面演示了错误的示例,如果下一步需要依赖上一步的返回结果,并且需要需要同步的写法,我们可以结合"async/await", 如不理解这个写法,可直接使用,或者搜索相关知识。<br/> 说明:我们在某个地方使用<code>await</code>,意味着调用的函数本身或者生命周期,必须要加上<code>async</code>前缀,否则出错</p> <p>此为在onLoad生命周期中的请求示例:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span> <span class="token comment">// 可以放心在生命周期前加上async,不会导致问题</span> <span class="token keyword">async</span> <span class="token function">onLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">let</span> ret <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'/user/login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 此处在函数体外写了async,并且使用了await等待返回,所以可以打印ret结果</span> <span class="token comment">// 意味着这里的console.log是等待了几十毫秒请求返回后才执行的</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span><span class="token punctuation">;</span> </code></pre></div><p>此为在methods方法中的请求示例:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span> <span class="token literal-property property">methods</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token keyword">async</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">let</span> ret <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'/user/login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 此处在函数体外写了async,并且使用了await等待返回,所以可以打印ret结果</span> <span class="token comment">// 意味着这里的console.log是等待了几十毫秒请求返回后才执行的</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span><span class="token punctuation">;</span> </code></pre></div><h3 id="api集中管理"><a href="#api集中管理" class="header-anchor">#</a> API集中管理</h3> <p>这里说的集中管理,是指把各个请求,统一放到一个文件中进行管理,这样的好处是不用每次调用<code>this.$u.get</code>时都需要传入<code>url</code>参数,一些固定的 请求参数也可以不用显式的传入,如下为配置后的使用示例:</p> <div class="language- extra-class"><pre class="language-text"><code>this.$u.api.getSearch().then(res => { console.log(res); </code></pre></div><p>此部分内容放到了另外的专题,请移步:<a href="/js/apiManage.html">API集中管理</a></p> <h3 id="完整示例"><a href="#完整示例" class="header-anchor">#</a> 完整示例</h3> <p>如果您不想阅读上方的内容的,或者觉得繁琐,下面给出一个完整的示例,复制修改某些提示的值即可直接使用</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// /common/http.interceptor.js</span> <span class="token comment">// 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token变量</span> <span class="token keyword">const</span> <span class="token function-variable function">install</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">Vue<span class="token punctuation">,</span> vm</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// 此为自定义配置参数,具体参数见上方说明</span> <span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>$u<span class="token punctuation">.</span>http<span class="token punctuation">.</span><span class="token function">setConfig</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">baseUrl</span><span class="token operator">:</span> <span class="token string">'https://api.example.com'</span><span class="token punctuation">,</span> <span class="token literal-property property">loadingText</span><span class="token operator">:</span> <span class="token string">'努力加载中~'</span><span class="token punctuation">,</span> <span class="token literal-property property">loadingTime</span><span class="token operator">:</span> <span class="token number">800</span><span class="token punctuation">,</span> <span class="token comment">// ......</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 请求拦截,配置Token等参数</span> <span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>$u<span class="token punctuation">.</span>http<span class="token punctuation">.</span>interceptor<span class="token punctuation">.</span><span class="token function-variable function">request</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">config</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// 引用token</span> <span class="token comment">// 方式一,存放在vuex的token,假设使用了uView封装的vuex方式</span> <span class="token comment">// 见:https://uviewui.com/components/globalVariable.html</span> <span class="token comment">// config.header.token = vm.token;</span> <span class="token comment">// 方式二,如果没有使用uView封装的vuex方法,那么需要使用$store.state获取</span> <span class="token comment">// config.header.token = vm.$store.state.token;</span> <span class="token comment">// 方式三,如果token放在了globalData,通过getApp().globalData获取</span> <span class="token comment">// config.header.token = getApp().globalData.username;</span> <span class="token comment">// 方式四,如果token放在了Storage本地存储中,拦截是每次请求都执行的</span> <span class="token comment">// 所以哪怕您重新登录修改了Storage,下一次的请求将会是最新值</span> <span class="token comment">// const token = uni.getStorageSync('token');</span> <span class="token comment">// config.header.token = token;</span> config<span class="token punctuation">.</span>header<span class="token punctuation">.</span>Token <span class="token operator">=</span> <span class="token string">'xxxxxx'</span><span class="token punctuation">;</span> <span class="token comment">// 可以对某个url进行特别处理,此url参数为this.$u.get(url)中的url值</span> <span class="token keyword">if</span><span class="token punctuation">(</span>config<span class="token punctuation">.</span>url <span class="token operator">==</span> <span class="token string">'/user/login'</span><span class="token punctuation">)</span> config<span class="token punctuation">.</span>header<span class="token punctuation">.</span>noToken <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment">// 最后需要将config进行return</span> <span class="token keyword">return</span> config<span class="token punctuation">;</span> <span class="token comment">// 如果return一个false值,则会取消本次请求</span> <span class="token comment">// if(config.url == '/user/rest') return false; // 取消某次请求</span> <span class="token comment">// 响应拦截,判断状态码是否通过</span> <span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>$u<span class="token punctuation">.</span>http<span class="token punctuation">.</span>interceptor<span class="token punctuation">.</span><span class="token function-variable function">response</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>code <span class="token operator">==</span> <span class="token number">200</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// res为服务端返回值,可能有code,result等字段</span> <span class="token comment">// 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到</span> <span class="token comment">// 如果配置了originalData为true,请留意这里的返回值</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span>result<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>code <span class="token operator">==</span> <span class="token number">201</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 假设201为token失效,这里跳转登录</span> vm<span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">toast</span><span class="token punctuation">(</span><span class="token string">'验证失败,请重新登录'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// 此为uView的方法,详见路由相关文档</span> vm<span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string">'/pages/user/login'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1500</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment">// 如果返回false,则会调用Promise的reject回调,</span> <span class="token comment">// 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中,res为服务端的返回值</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span> install </code></pre></div><p>具体请求使用示例:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// login.vue</span> <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span> <span class="token literal-property property">methods</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token comment">// post示例</span> <span class="token function">sumbitByPost</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'/user/login'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">username</span><span class="token operator">:</span> <span class="token string">'lisa'</span><span class="token punctuation">,</span> <span class="token literal-property property">password</span><span class="token operator">:</span> <span class="token string">'123456'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">res</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// res为服务端返回的数据</span> <span class="token comment">// get示例</span> <span class="token function">sumbitByGet</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$u<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'/user/login'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">username</span><span class="token operator">:</span> <span class="token string">'lisa'</span><span class="token punctuation">,</span> <span class="token literal-property property">password</span><span class="token operator">:</span> <span class="token string">'123456'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">res</span> <span class="token operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// res为服务端返回的数据</span> API集中管理
推荐文章