自己毕设用到的接口
在bee api 的基础上增加自己的 业务API
1. 前端 传递 带关键字的参数
xxx.vue
var parames = {
isCount:1,
user_id: that.userInfo.Id,
word_type: that.tyepeIndex,
}
2. 后端接收自定义参数,若不存在,继续执行原api的逻辑
congtrol.relearn.go
func (c *ReLearnController) GetAll() {
var isCount int64 = 0
var userId int64 = -1
var wordType int64 = -1
if v, err := c.GetInt64("isCount"); err == nil {
isCount = v
}
if v, err := c.GetInt64("user_id"); err == nil {
userId = v
}
if v, err := c.GetInt64("word_type"); err == nil {
wordType = v
}
if isCount == 1 {
l, err := models.GetCountReLearn(userId, wordType)
if err != nil {
c.Data["json"] = err.Error()
} else {
c.Data["json"] = l
}
} else { //此处开始 为原始API逻辑!!!!!!!!!!!!!!
var fields []string
var sortby []string
var order []string
var query = make(map[string]string)
var limit int64 = 10
var offset int64
// fields: col1,col2,entity.col3
if v := c.GetString("fields"); v != "" {
fields = strings.Split(v, ",")
}
// limit: 10 (default is 10)
if v, err := c.GetInt64("limit"); err == nil {
limit = v
}
// offset: 0 (default is 0)
if v, err := c.GetInt64("offset"); err == nil {
offset = v
}
// sortby: col1,col2
if v := c.GetString("sortby"); v != "" {
sortby = strings.Split(v, ",")
}
// order: desc,asc
if v := c.GetString("order"); v != "" {
order = strings.Split(v, ",")
}
// query: k:v,k:v
if v := c.GetString("query"); v != "" {
for _, cond := range strings.Split(v, ",") {
kv := strings.SplitN(cond, ":", 2)
if len(kv) != 2 {
c.Data["json"] = errors.New("Error: invalid query key/value pair")
c.ServeJSON()
return
}
k, v := kv[0], kv[1]
query[k] = v
}
}
l, err := models.GetAllReLearn(query, fields, sortby, order, offset, limit)
if err != nil {
c.Data["json"] = err.Error()
} else {
c.Data["json"] = l
}
}
c.ServeJSON()
}
3. 然后就是model 文件了
models.relearn.go
func GetCountRelearn(userId int64,wordType int64)(cnt int64,err error){
o:= rom.NewOrm()
qs :=o.QueryTable(new(ReLearn))
cnt,err =qs.Filter("user_id",userId).Filter("word_type",wordType).Count()
return cnt,err
}
4. 最后的效果图
数据库中只有一条数据
前端请求 参数
前端请求结果
爽歪歪,继续敲代码