沐云 發表於 2020-8-9 17:08:00

uni-app 接口封装

<p>uni-app 的请求接口在官方文档里是这样的</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">uni.request({
    url: </span>'https://www.example.com/request', <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)">    data: {
      text: </span>'uni.request'<span style="color: rgba(0, 0, 0, 1)">
    },
    header: {
      </span>'custom-header': 'hello' <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)">    },
    success: (res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(res.data);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.text = 'request success'<span style="color: rgba(0, 0, 0, 1)">;
    }
});</span></pre>
</div>
<p>在common 中 http.js文档中进行如下封装:</p>
<div class="cnblogs_code">
<pre>const baseUrl = '****'<span style="color: rgba(0, 0, 0, 1)">;    //请求地址
const httpRequest </span>= (opts, data) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    let httpDefaultOpts </span>=<span style="color: rgba(0, 0, 0, 1)"> {<br>     // url: "/dpc/"+opts.url, //本地的重定向接口地址
      url: baseUrl</span>+<span style="color: rgba(0, 0, 0, 1)">opts.url, //正式环境中接口地址
      data: data,
      beforeSend :</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(xmlHttp){
            xmlHttp.setRequestHeader(</span>"If-Modified-Since","0"<span style="color: rgba(0, 0, 0, 1)">);
            xmlHttp.setRequestHeader(</span>"Cache-Control","no-cache"<span style="color: rgba(0, 0, 0, 1)">);
      },
      method: opts.method,
      header: opts.method </span>== 'GET' ?<span style="color: rgba(0, 0, 0, 1)"> {
      </span>'X-Requested-With': 'XMLHttpRequest'<span style="color: rgba(0, 0, 0, 1)">,
      </span>"Accept": "application/json"<span style="color: rgba(0, 0, 0, 1)">,
      </span>"Content-Type": "application/json; charset=UTF-8"<span style="color: rgba(0, 0, 0, 1)">
    } : {
       </span>'content-type': 'application/x-www-form-urlencoded'<span style="color: rgba(0, 0, 0, 1)">
    },
      dataType: </span>'json'<span style="color: rgba(0, 0, 0, 1)">,
    }
    let promise </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Promise(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(resolve, reject) {
      uni.request(httpDefaultOpts).then(
            (res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                resolve(res[</span>1<span style="color: rgba(0, 0, 0, 1)">])
            }
      ).</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(
            (response) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                reject(response)
            }
      )
    })
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> promise
};
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
    baseUrl,
    httpRequest
}</span></pre>
</div>
<p>&nbsp;</p>
<p>在页面main.js中引入http.js</p>
<p>在需要接口请求的部分可以这样写:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">http.httpRequest({
                                        url: '/api/mineralInfo',//接口地址
                                        method: 'POST'   //请求方式
                                },{
                                        submitStatus:1   //参数部分
                                }).then(
                                        res =&gt; {
                                                if(res.data.code == "0"){
                                                        //请求成功
                                                }
                                        },
                                        error =&gt; {
                                                console.log('网络请求错误,请稍后重试!');
                                                return;
                                        }
                                );
</pre>
</div>
<p>  </p>
<p>最后一定记得&nbsp;</p>
<p>修改manifest.json&nbsp; 中 h5部分</p>
<p>否则接口会显示302,接口重定向错误。</p>
<div class="cnblogs_code">
<pre>"devServer"<span style="color: rgba(0, 0, 0, 1)"> : {
             </span>"proxy"<span style="color: rgba(0, 0, 0, 1)"> : {
                </span>"/dpc"<span style="color: rgba(0, 0, 0, 1)"> : {
                  </span>"target" : "***", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">域名</span>
                  "changeOrigin" : <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
                  </span>"secure" : <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
                  </span>"pathRewrite"<span style="color: rgba(0, 0, 0, 1)"> : {
                        </span>"^/dpc" : ""<span style="color: rgba(0, 0, 0, 1)">
                  }
                }
            },
            </span>"https" : <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
      }</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/yeziyou/p/13463531.html
頁: [1]
查看完整版本: uni-app 接口封装