首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

前端后台1个接口处理多个事件的实践

前端后台1个接口处理多个事件的实践

批量删除和删除可以用一个接口---前端代码

//删除确认

asynchandleDel(str){

//判断是否登陆过

if(localStorage.token){

leturl ='/api/koa/commodity/del';//删除商品接口

letid ='';

//如果调用删除带有'batch'参数,就做批量删除

if(str=='batch'){

//将elementui的结果处理成[1,2,3,4,5]数组

id=this.multipleSelection.map((item)=>{

letarr = [];

arr.push(item.id);

returnarr;

});

}else{

//如果不是批量删除,直接拿到id

id=this.id;

}

//传给后台的参数

letparams = {

id,

token:localStorage.token

}

try{

//请求接口

letresData =await this.api(url,params);

if(resData.data.success){

this.$notify.success({

title:'删除商品成功',

message: resData.data.msg,

});

}else{

this.$notify.error({

title:'删除商品失败',

message: resData.data.msg,

});

}

this.myInit();

}catch(e){

this.$notify.error({

title:'似乎出问题了',

message: e,

});

}

}else{

this.$notify.error({

title:'似乎出问题了',

message:'当前未登录,禁止删除',

});

}

//关闭对话框

this.dialogVisible=false;

},

批量删除和删除可以用一个接口---后台代码

//PC删除一个商品

commodity.del=asyncctx => {

//默认后台什么都没做

letresData = {success:,msg: msg.nothing};

letjwtData ='';

//拿到前端的参数

letqueryData = {

ID:ctx.request.body.id?ctx.request.body.id:'',

token:ctx.request.body.token?ctx.request.body.token:'',

}

//前端的参数没问题

if(queryData.ID&&queryData.token){

try{

//读取证书的私钥,通过私钥解密token

letcert = fs.readFileSync('/var/ssl/privkey.pem');

//解密得到的值是之前存进去的用户名

jwtData = jwt.verify(queryData.token,cert);

}catch(e) {

//token解密错误

resData = {

success:,

msg: msg.tokenError

};

}

//只有xiongman才能删除

if(jwtData.name=='xiongman'){

letsql1 ='';

//如果前端传来的id是数组,就做批量删除

if(queryData.IDinstanceofArray){

sql1 =`DELETE FROM commodity_tb WHERE ID IN ($)`;

}else{

//否则单个删除

sql1 =`DELETE FROM commodity_tb WHERE ID="$"`;

}

try{

letdata1 =awaitquery(sql1);

resData = {

success:1,

msg:msg.del,

data:data1

};

}catch(e) {

resData = {

success:,

msg: msg.database

};

}

}else{

resData = {

success:,

msg: msg.admin

};

}

}else{

resData = {

success:,

msg: msg.front

};

}

//将结果传给前端

ctx.body= resData;

};

筛选、列表、分页可以共用一个接口---前端代码

//获取商品列表&筛选

asyncgetCommodityList() {

//后台接口

leturl ='/api/koa/commodity/list';

//参数有或者没有都传给后台

letparams = {

name:this.formQuery.name?this.formQuery.name:'',

classifyID:this.formQuery.classify?this.formQuery.classify:'',

priceStart:this.formQuery.priceStart?this.formQuery.priceStart:'',

priceEnd:this.formQuery.priceEnd?this.formQuery.priceEnd:'',

salesVolumeStart:this.formQuery.salesVolumeStart?this.formQuery.salesVolumeStart:'',

salesVolumeEnd:this.formQuery.salesVolumeEnd?this.formQuery.salesVolumeEnd:'',

pageSize:this.pagination.pageSize?this.pagination.pageSize:'',

currentPage:this.pagination.currentPage?this.pagination.currentPage:'',

}

try{

//执行接口请求

letresData =await this.myApi(url,params);

this.tableData= resData.data.data;

this.classifyData= resData.data.classify;

this.classifyFilters= resData.data.classify.map(item=>{

letjson = {};

json.text= item.label;

json.value= item.label;

returnjson;

});

this.pagination.total= resData.data.total;

}catch(e){

this.$notify.error({

title:'似乎出问题了',

message: e,

});

}

},

筛选、列表、分页可以共用一个接口---后台代码

//PC端获取商品列表

commodity.list=asyncctx => {

//默认后台什么都没做

letresData = {success:,msg:msg.nothing};

//拿到前端传来的参数

letqueryData = {

name:ctx.query.name?ctx.query.name:'',

classifyID:ctx.query.classifyID?ctx.query.classifyID:'',

priceStart:ctx.query.priceStart?ctx.query.priceStart:'',

priceEnd:ctx.query.priceEnd?ctx.query.priceEnd:'',

salesVolumeStart:ctx.query.salesVolumeStart?ctx.query.salesVolumeStart:'',

salesVolumeEnd:ctx.query.salesVolumeEnd?ctx.query.salesVolumeEnd:'',

pageSize:ctx.query.pageSize?ctx.query.pageSize:30,

currentPage:ctx.query.currentPage?ctx.query.currentPage:1,

}

//拿到商品分类信息

letsql1 =`SELECT ID as value,name as label FROM commodity_classify_tb`;

letsql2 ='';

letsql3 ='';

//拿到列表数据和页码

if(queryData.classifyID&&queryData.priceStart&&queryData.priceEnd&&queryData.salesVolumeStart&&queryData.salesVolumeEnd){

//如果筛选条件包括分类,价格区间,销量区间

sql2 =`SELECT ID as id,name,price,discount,salesVolume,src,classifyID FROM commodity_tb WHERE name LIKE "%$%" AND classifyID="$" AND price BETWEEN$AND$AND salesVolume BETWEEN$AND$ORDER BY ID DESC LIMIT${(queryData.currentPage-1) * queryData.pageSize},$`;

sql3 =`SELECT COUNT(*) AS total FROM commodity_tb WHERE name LIKE "%$%" AND classifyID="$" AND price BETWEEN$AND$AND salesVolume BETWEEN$AND$`;

}else if(queryData.classifyID&&queryData.priceStart&&queryData.priceEnd){

//如果筛选条件包括分类,价格区间

sql2 =`SELECT ID as id,name,price,discount,salesVolume,src,classifyID FROM commodity_tb WHERE name LIKE "%$%" AND classifyID="$" AND price BETWEEN$AND$ORDER BY ID DESC LIMIT${(queryData.currentPage-1) * queryData.pageSize},$`;

sql3 =`SELECT COUNT(*) AS total FROM commodity_tb WHERE name LIKE "%$%" AND classifyID="$" AND price BETWEEN$AND$`;

}else if(queryData.classifyID&&queryData.salesVolumeStart&&queryData.salesVolumeEnd){

//如果筛选条件包括分类,销量区间

sql2 =`SELECT ID as id,name,price,discount,salesVolume,src,classifyID FROM commodity_tb WHERE name LIKE "%$%" AND classifyID="$" AND salesVolume BETWEEN$AND$ORDER BY ID DESC LIMIT${(queryData.currentPage-1) * queryData.pageSize},$`;

sql3 =`SELECT COUNT(*) AS total FROM commodity_tb WHERE name LIKE "%$%" AND classifyID="$" AND salesVolume BETWEEN$AND$`;

}else if(queryData.priceStart&&queryData.priceEnd&&queryData.salesVolumeStart&&queryData.salesVolumeEnd){

//如果筛选条件包括价格区间,销量区间

sql2 =`SELECT ID as id,name,price,discount,salesVolume,src,classifyID FROM commodity_tb WHERE name LIKE "%$%" AND price BETWEEN$AND$AND salesVolume BETWEEN$AND$ORDER BY ID DESC LIMIT${(queryData.currentPage-1) * queryData.pageSize},$`;

sql3 =`SELECT COUNT(*) AS total FROM commodity_tb WHERE name LIKE "%$%" AND price BETWEEN$AND$AND salesVolume BETWEEN$AND$`;

}else if(queryData.classifyID){

//如果筛选条件是分类

sql2 =`SELECT ID as id,name,price,discount,salesVolume,src,classifyID FROM commodity_tb WHERE name LIKE "%$%" AND classifyID="$" ORDER BY ID DESC`;

sql3 =`SELECT COUNT(*) AS total FROM commodity_tb WHERE name LIKE "%$%" AND classifyID="$"`;

}else if(queryData.priceStart&&queryData.priceEnd){

//如果筛选条件是价格区间

sql2 =`SELECT ID as id,name,price,discount,salesVolume,src,classifyID FROM commodity_tb WHERE name LIKE "%$%" AND price BETWEEN$AND$ORDER BY ID DESC LIMIT${(queryData.currentPage-1) * queryData.pageSize},$`;

sql3 =`SELECT COUNT(*) AS total FROM commodity_tb WHERE name LIKE "%$%" AND price BETWEEN$AND$`;

}else if(queryData.salesVolumeStart&&queryData.salesVolumeEnd){

//如果筛选条件是销量区间

sql2 =`SELECT ID as id,name,price,discount,salesVolume,src,classifyID FROM commodity_tb WHERE name LIKE "%$%" AND salesVolume BETWEEN$AND$ORDER BY ID DESC LIMIT${(queryData.currentPage-1) * queryData.pageSize},$`;

sql3 =`SELECT COUNT(*) AS total FROM commodity_tb WHERE name LIKE "%$%" AND salesVolume BETWEEN$AND$`;

}else{

//没有筛选条件或者只有商品名称查询

sql2 =`SELECT ID as id,name,price,discount,salesVolume,src,classifyID FROM commodity_tb WHERE name LIKE "%$%" ORDER BY ID DESC LIMIT${(queryData.currentPage-1) * queryData.pageSize},$`;

sql3 =`SELECT COUNT(*) AS total FROM commodity_tb WHERE name LIKE "%$%"`;

}

try{

//执行获取分类的查询

letdata1 =awaitquery(sql1);

//执行获取列表的查询

letdata2 =awaitquery(sql2);

//执行获取分页的查询

letdata3 =awaitquery(sql3)

//将数据整理成前端需要的结果

letnewData = data2.map((item2)=>{

letjson = {};

json.id= item2.id;

json.name= item2.name;

json.price= item2.price;

json.discount= item2.discount;

json.salesVolume= item2.salesVolume;

json.src= item2.src;

json.total= item2.total;

json.classify= data1.filter((item1)=>{

returnitem2.classifyID==item1.value;

})[].label;

returnjson;

});

resData = {

data:newData,

classify:data1,

total:data3[].total,

success:1,

msg:msg.success

};

}catch(e) {

resData = {

success:,

msg: msg.database

};

}

//将结果数据传给前端

ctx.body= resData;

};

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180612G1SOW400?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券