vue-admin前后端分离管理后台模板二次开发中适配url接口

前言

提示:这里可以添加本文要记录的大概内容:

vue-admin前后端分离管理后台模板二次开发中适配url接口


提示:以下是本篇文章正文内容,下面案例可供参考

一、编写request请求文件

const https = require('https');
// 不需要 body 的请求
const noBodyReqs = ['head','get','copy','purge','unlock'];
// 可能需要 body 的请求
const hasBodyReqs = ['post','put','patch','delete','options','link','unlink','lock','propfind','view'];
 
const HttpsClient = {};
 
noBodyReqs.concat(hasBodyReqs).forEach(method => {
    HttpsClient[method] = obj => {
        return new Promise(function (cb) {
            const options = {
                host: obj.host,
                port: 443,
                path: obj.path,
                method,
                headers: obj.headers ? obj.headers : {},
            };
            const req = https.request(options, res => {
                let chunks = Buffer.from([]);
                let chunksLength = chunks.length;
                res.on('data', chunk => {
                    chunks = Buffer.concat([chunks,chunk],chunksLength + chunk.length);
                    chunksLength = chunks.length;
                });
                res.on('end', () => {
                    cb(chunks.toString());
                });
            });
 
            req.on('error', e => {console.log(`request error: ${e}`);});
 
            if(hasBodyReqs.indexOf(method) !== -1) req.write(obj.body);
            req.end();
        });
    }
});
 
module.exports = HttpsClient;

二、使用步骤

1.寻找页面请求方法

代码如下(示例):

//该模板大多数情况都是调用store中的方法
this.$store.dispatch('user/login', this.loginForm).then((res) => {
							if(res){
								this.$router.push({
									path: this.redirect || '/'
								})
							}else{
								alert('Login failed, account or password error!');
							}
							this.loading = false
						}).catch(() => {
							this.loading = false
						})
 // user login(store文件中的modules的user模块)
   login({ commit }, userInfo) {
    const { username, password,type} = userInfo
    return new Promise((resolve, reject) => {
  login({ username: username.trim(), password: password ,type:type}).then(res => {
        commit('SET_TOKEN', "admin-token")
        setToken("admin-token")
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
 //api文件中的user.js文件
 const HttpsClient = require('@/utils/HttpsClient');//导入request
export  function login(data) {
	var ddl = JSON.stringify(data)
	return HttpsClient.post({
		host: '*******',//接口地址(去掉https://)
		path: '/adminget',//接口名称
		headers: {
			'content-type': 'application/json;charset=UTF-8',
			"Access-Control-Allow-Origin": "*"
		},
		body: ddl
	}).then(res => {
		return eval("(" + res + ")");//数据的解码(json格式化)
	}).catch((err) => {
		return err;
	});
}

总结

提示:这里对文章进行总结:

整个模板是用的mock模拟数据返回,需要先了解mock的用法,理清整个方法的调用过程,理一下请求方法的流程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值