func ArticleFavarite(ctx *ink.Context) { userId := ctx.TokenGet("id").(string) articleId := getParam(ctx, "articleId").(string) isFavarite := getParam(ctx, "favarite").(bool) ret := articleFavarite(userId, articleId, isFavarite) returnRet(ctx, ret, isFavarite) }
func ArticleLike(ctx *ink.Context) { userId := ctx.TokenGet("id").(string) articleId := getParam(ctx, "articleId").(string) isLike := getParam(ctx, "like").(bool) ret := articleLike(userId, articleId, isLike) returnRet(ctx, ret, isLike) }
func UserConfig(ctx *ink.Context) { defer exceptHandle(ctx) userId := ctx.TokenGet("id").(string) key := getParam(ctx, "key").(string) value := getParam(ctx, "value").(string) switch key { case "mail": if !validType("mail", value) { panic("邮箱格式不正确") } if userExist(value) { panic("账户已被使用") } case "name": if !validate("[A-Za-z0-9_]+", value) { panic("账户名称只接受字母数字下划线") } case "nick": case "motto": case "avatar": // base64 encode case "link": default: panic("要更新的选项不存在") } if userConfig(userId, key, value) { returnRet(ctx, true, nil) return } panic("更新失败,内部错误") }
func CommentAdd(ctx *ink.Context) { userId := ctx.TokenGet("id").(string) articleId := getParam(ctx, "articleId").(string) content := getParam(ctx, "content").(string) ret := articleCommentAdd(userId, articleId, content) returnRet(ctx, ret, nil) }
func UserTimeline(ctx *ink.Context) { userId := ctx.TokenGet("id").(string) ret := userTimeline(userId) if ret != nil { returnRet(ctx, true, ret) return } returnRet(ctx, false, "获取动态失败") }
func UserCheckToken(ctx *ink.Context) { userId := ctx.TokenGet("id") fmt.Println(userId) if userId != nil { returnRet(ctx, true, nil) return } returnRet(ctx, false, nil) }
func returnRet(ctx *ink.Context, status bool, result interface{}) { time.Sleep(1 * time.Second) data := Map{ "status": status, "result": result, } ret, _ := json.Marshal(data) ctx.Write(ret) }
func ArticleRemove(ctx *ink.Context) { userId := ctx.TokenGet("id").(string) articleId := getParam(ctx, "articleId").(string) ret := articleRemove(userId, articleId) if ret { returnRet(ctx, true, nil) return } returnRet(ctx, false, "文章删除失败") }
func CircleFocus(ctx *ink.Context) { userId := ctx.TokenGet("id").(string) circle := getParam(ctx, "circle").(string) isFocus := getParam(ctx, "focus").(bool) if !isInArray(circle, CIRCLES) { returnRet(ctx, false, "指定圈子错误") return } ret := circleFocus(userId, circle, isFocus) returnRet(ctx, ret, isFocus) }
func UserLogin(ctx *ink.Context) { mail := getParam(ctx, "mail").(string) pass := getParam(ctx, "pass").(string) user := userLogin(mail, pass) if user != nil { userId := user.Id.Hex() token := ctx.TokenNew() ctx.TokenSet("id", userId) returnRet(ctx, true, Map{ "token": token, }) return } returnRet(ctx, false, "账户或密码错误") }
func ArticleUpdate(ctx *ink.Context) { userId := ctx.TokenGet("id").(string) articleId := getParam(ctx, "articleId").(string) articleTitle := getParam(ctx, "title").(string) articleContent := getParam(ctx, "content").(string) articleCircles := getParam(ctx, "circles").([]interface{}) articlePublic := getParam(ctx, "public").(bool) for _, circle := range articleCircles { if !isInArray(circle.(string), CIRCLES) { returnRet(ctx, false, "指定圈子错误") return } } ret := articleUpdate(userId, articleId, articleTitle, articleContent, articleCircles, articlePublic) if len(ret) != 0 { returnRet(ctx, true, ret) return } returnRet(ctx, false, "文章更新失败") }
func ArticleList(ctx *ink.Context) { userId := ctx.TokenGet("id").(string) filter := getParam(ctx, "filter").(string) // ["private", "public", "favarite"] if !isInArray(filter, []string{"private", "public", "favarite"}) { returnRet(ctx, false, "指定过滤条件错误") return } ret := articleList(userId, filter) if ret != nil { // for idx, item := range *ret { // content := []rune(item.Content) // fmt.Println(item.Content) // if len(content) > 140 { // (*ret)[idx].Content = string(content[0:140]) // } // } returnRet(ctx, true, *ret) return } returnRet(ctx, true, "文章获取失败") }
func UserRegister(ctx *ink.Context) { defer exceptHandle(ctx) mail := getParam(ctx, "mail").(string) pass := getParam(ctx, "pass").(string) if !validType("mail", mail) { panic("邮箱格式不正确") } if passLen := len(pass); passLen < 6 || passLen > 16 { panic("密码位数在6-16之间") } if userExist(mail) { panic("账户已被使用") } if userId := userRegister(mail, pass); userId != "" { token := ctx.TokenNew() ctx.TokenSet("id", userId) returnRet(ctx, true, Map{ "token": token, }) return } panic("注册失败,内部错误") }
/* helper method */ func PreHandle(ctx *ink.Context) { // auth check path := ctx.Req.URL.Path if path != "/user/login" && path != "/user/check_token" && path != "/article/get" && path != "/user/register" && path != "/user/page" { userId := ctx.TokenGet("id") if userId == nil { returnRet(ctx, false, "权限验证失败") ctx.Stop() return } } // parse request json data if path != "/article/upload" { decoder := json.NewDecoder(ctx.Req.Body) data := make(Map) err := decoder.Decode(&data) if err != nil { returnRet(ctx, false, "json parse error") ctx.Stop() return } ctx.Ware["data"] = data } }