feathersjs的安装和使用

(初次使用feathersjs,按照个人在项目时心得,写的总结,有不正确的地方,请大家指出)

1、先是安装feathersjs,@feathersjs/client一个包含Feathers客户端模块的表(@feathersjs/feathers、@feathersjs/errors、@feathersjs/rest-client、@feathersjs/socketio-client、@feathersjs/primus-client、@feathersjs/authentication-client)

$ npm install @feathersjs/client --save

2、但是需要和其他模块并行使用,现在与@feathersjs/rest-client一块使用,安装

$ npm install @feathersjs/rest-client --save

3,main.js里面写

import feathers from '@feathersjs/client';
import rest from '@feathersjs/rest-client';
//接口的url
const restClient = rest('http://behave.qixiansheng.net:10021')
const app = feathers();
app.configure(restClient.axios(axios));
app.hooks({
    before:{
        all: [function(context){
            //token根据自己的需求,进行判断,传参
            var token = ''
            if(token){
                token = window.localStorage.getItem("token")
            }
            context.params.headers =  { 
                'Content-Type': 'application/json',
                'x-access-token': token
         }
        }]
    },
    error:{
        all:[
         (ctx) =>{
                let message;
                switch(ctx.error.code)
                {
                    case '400':
                        message = "请求错误";
                        break;
                    case '401':
                        message = "未授权";
                        break;
                    case '403':
                        message = "禁止执行访问";
                        break;
                    case '404':
                        message = "找不到页面";
                        break;
                    case '407':
                        message = "需要代理身份验证";
                        break;
                    case '408':
                        message = "请求超时";
                        break;
                    case '409':
                        message = "请求冲突";
                        break;
                    case '415':
                        message = "服务器错误";
                        break;
                    case '500':
                        message = "服务器的内部错误 ";
                        break;
                    default:
                    message = "服务器请求失败";
                }
         }
        ]
    }
})
Vue.prototype.feather = app;

3,建文件夹,建个feathersjs的公共js.例如

4,common.js里面,把增删改查封装的函数都写出来

import Vue from 'vue'
//tableName是表名,data添加的数据,option是筛选条件,id是表的id值
//创建
export async function createTable(tableName, data){
    var app = Vue.prototype.feather
    try{
        let response = await app.service(tableName).create(data)
        response.code = 200
        return response;
    }catch(e){
        console.log(`create ${tableName} error:`, e);
        return {code: e.code, data:null}
    }
}
//查找
export async function findTable(tableName, option){
    var app = Vue.prototype.feather
    try{
        let response = await app.service(tableName).find(option)
        response.code = 200
        return response;
    }catch(e){
        console.log(`find ${tableName} error:`, e);
        return {code: e.code, data:null}
    }
}
//编辑修改
// let res= await patchTable('project',id,{deleted:0})删除的例子,这种只是让查不到,并不是删除
//数据库里面的信息,可根据自己需要,封装删除函数(app.service(tableName).remove(id,option))
export async function patchTable(tableName,id,data){
    var app = Vue.prototype.feather
    if(!option){
        option = {}
    }
    try{
        let response = await app.service(tableName).patch(id,option)
        response.code = 200
        return response;
    }catch(e){
        console.log(`patch ${tableName} error:`, e);
        return {code: e.code, data:null}
    }
}
//查找
export async function getTable(tableName,id,option){
    var app = Vue.prototype.feather
    if(!option){
        option = {}
    }
    try{
        let response = await app.service(tableName).get(id, option)
        response.code = 200
        return response;
    }catch(e){
        console.log(`get ${tableName} error:`, e);
        return {code: e.code, data:null}
    }
}

5,函数用法示范

import {patchTable, findTable,getTable, createTable} from '../api/common'
//创建
async go(){
    let resone=await createTable('express',{
        project_id:'555',
        express_company:'科技有限公司'
        
     })
     if(resone.code == 200){
                        
     }else{
       this.$toast('网络请求失败');
     }
}
//查找
async go(){
    let resone=await findTable('express',{
         query:{//根据需求不同,根据feathersjs文档在这里面写查询条件
             $sort: {//倒叙
                  createdAt: -1
             },
             //查询出来表里面的某个参数
             $select:['express_company'],
         }
        
     })
     if(resone.code == 200){
                        
     }else{
       this.$toast('网络请求失败');
     }
}
//编辑
async go(){
    let resone=await patchTable('express',id,{
        project_id:'555',
        express_company:'科技有限公司'
        
     })
     if(resone.code == 200){
                        
     }else{
       this.$toast('网络请求失败');
     }
}
//查找
async go(){
     let resone=await getTable('express',id, query:{
        select:['delivery_information'],
     })
     if(resone.code == 200){
                        
     }else{
       this.$toast('网络请求失败');
     }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值