CreateToken 创建访问来令牌.
()
| 710 | |
| 711 | // CreateToken 创建访问来令牌. |
| 712 | func (this *BookController) CreateToken() { |
| 713 | if this.forbidGeneralRole() { |
| 714 | this.JsonResult(6001, "您的角色非作者和管理员,无法创建访问令牌") |
| 715 | } |
| 716 | action := this.GetString("action") |
| 717 | |
| 718 | bookResult, err := this.IsPermission() |
| 719 | |
| 720 | if err != nil { |
| 721 | if err == models.ErrPermissionDenied { |
| 722 | this.JsonResult(403, "权限不足") |
| 723 | } |
| 724 | |
| 725 | if err == orm.ErrNoRows { |
| 726 | this.JsonResult(404, "书籍不存在") |
| 727 | } |
| 728 | |
| 729 | logs.Error("生成阅读令牌失败 =>", err) |
| 730 | this.JsonResult(6002, err.Error()) |
| 731 | } |
| 732 | |
| 733 | book := models.NewBook() |
| 734 | if _, err := book.Find(bookResult.BookId); err != nil { |
| 735 | this.JsonResult(6001, "书籍不存在") |
| 736 | } |
| 737 | |
| 738 | if action == "create" { |
| 739 | if bookResult.PrivatelyOwned == 0 { |
| 740 | this.JsonResult(6001, "公开书籍不能创建阅读令牌") |
| 741 | } |
| 742 | |
| 743 | book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL)) |
| 744 | if err := book.Update(); err != nil { |
| 745 | logs.Error("生成阅读令牌失败 => ", err) |
| 746 | this.JsonResult(6003, "生成阅读令牌失败") |
| 747 | } |
| 748 | //book.PrivateToken = this.BaseUrl() + beego.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken) |
| 749 | tipsFmt := "访问链接:%v 访问密码:%v" |
| 750 | privateToken := fmt.Sprintf(tipsFmt, this.BaseUrl()+beego.URLFor("DocumentController.Index", ":key", book.Identify), book.PrivateToken) |
| 751 | this.JsonResult(0, "ok", privateToken) |
| 752 | } |
| 753 | |
| 754 | book.PrivateToken = "" |
| 755 | if err := book.Update(); err != nil { |
| 756 | logs.Error("CreateToken => ", err) |
| 757 | this.JsonResult(6004, "删除令牌失败") |
| 758 | } |
| 759 | this.JsonResult(0, "ok", "") |
| 760 | } |
| 761 | |
| 762 | // Delete 删除书籍. |
| 763 | func (this *BookController) Delete() { |
nothing calls this directly
no test coverage detected