plane_page.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package plane_v2
  2. import (
  3. "fmt"
  4. "go.uber.org/zap"
  5. )
  6. // ==================== Page API ====================
  7. // ListPages 列出页面
  8. func ListPages(slug, projectID string) ([]byte, error) {
  9. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/", slug, projectID)
  10. resp, err := planeWebGet(url)
  11. if err != nil {
  12. zap.L().Error("[PlaneWeb] ListPages 请求失败", zap.Error(err))
  13. return nil, err
  14. }
  15. if !isWebSuccess(resp.StatusCode) {
  16. zap.L().Error("[PlaneWeb] ListPages 返回错误", zap.String("status", resp.Status))
  17. return nil, fmt.Errorf("ListPages 失败: %s", resp.Status)
  18. }
  19. return resp.Body, nil
  20. }
  21. // CreatePage 创建页面
  22. func CreatePage(slug, projectID string, data map[string]interface{}) ([]byte, error) {
  23. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/", slug, projectID)
  24. resp, err := planeWebPost(url, data)
  25. if err != nil {
  26. zap.L().Error("[PlaneWeb] CreatePage 请求失败", zap.Error(err))
  27. return nil, err
  28. }
  29. if resp.StatusCode != 201 {
  30. zap.L().Error("[PlaneWeb] CreatePage 返回错误",
  31. zap.String("status", resp.Status),
  32. zap.String("body", string(resp.Body)),
  33. )
  34. return nil, fmt.Errorf("CreatePage 失败: %s", resp.Status)
  35. }
  36. return resp.Body, nil
  37. }
  38. // GetPage 获取页面详情
  39. func GetPage(slug, projectID, pageID string) ([]byte, error) {
  40. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/%s/", slug, projectID, pageID)
  41. resp, err := planeWebGet(url)
  42. if err != nil {
  43. zap.L().Error("[PlaneWeb] GetPage 请求失败", zap.Error(err))
  44. return nil, err
  45. }
  46. if !isWebSuccess(resp.StatusCode) {
  47. zap.L().Error("[PlaneWeb] GetPage 返回错误", zap.String("status", resp.Status))
  48. return nil, fmt.Errorf("GetPage 失败: %s", resp.Status)
  49. }
  50. return resp.Body, nil
  51. }
  52. // UpdatePage 更新页面
  53. func UpdatePage(slug, projectID, pageID string, data map[string]interface{}) ([]byte, error) {
  54. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/%s/", slug, projectID, pageID)
  55. resp, err := planeWebPatch(url, data)
  56. if err != nil {
  57. zap.L().Error("[PlaneWeb] UpdatePage 请求失败", zap.Error(err))
  58. return nil, err
  59. }
  60. if !isWebSuccess(resp.StatusCode) {
  61. zap.L().Error("[PlaneWeb] UpdatePage 返回错误",
  62. zap.String("status", resp.Status),
  63. zap.String("body", string(resp.Body)),
  64. )
  65. return nil, fmt.Errorf("UpdatePage 失败: %s", resp.Status)
  66. }
  67. return resp.Body, nil
  68. }
  69. // GetPageDescription 获取页面描述内容
  70. func GetPageDescription(slug, projectID, pageID string) ([]byte, error) {
  71. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/%s/description/", slug, projectID, pageID)
  72. resp, err := planeWebGet(url)
  73. if err != nil {
  74. zap.L().Error("[PlaneWeb] GetPageDescription 请求失败", zap.Error(err))
  75. return nil, err
  76. }
  77. if !isWebSuccess(resp.StatusCode) {
  78. zap.L().Error("[PlaneWeb] GetPageDescription 返回错误", zap.String("status", resp.Status))
  79. return nil, fmt.Errorf("GetPageDescription 失败: %s", resp.Status)
  80. }
  81. return resp.Body, nil
  82. }
  83. // UpdatePageDescription 更新页面描述内容
  84. func UpdatePageDescription(slug, projectID, pageID, descHTML string) ([]byte, error) {
  85. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/%s/description/", slug, projectID, pageID)
  86. payload := map[string]interface{}{
  87. "description_html": descHTML,
  88. }
  89. resp, err := planeWebPatch(url, payload)
  90. if err != nil {
  91. zap.L().Error("[PlaneWeb] UpdatePageDescription 请求失败", zap.Error(err))
  92. return nil, err
  93. }
  94. if !isWebSuccess(resp.StatusCode) {
  95. zap.L().Error("[PlaneWeb] UpdatePageDescription 返回错误",
  96. zap.String("status", resp.Status),
  97. zap.String("body", string(resp.Body)),
  98. )
  99. return nil, fmt.Errorf("UpdatePageDescription 失败: %s", resp.Status)
  100. }
  101. return resp.Body, nil
  102. }
  103. // DeletePage 删除页面
  104. func DeletePage(slug, projectID, pageID string) error {
  105. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/%s/", slug, projectID, pageID)
  106. resp, err := planeWebDelete(url)
  107. if err != nil {
  108. zap.L().Error("[PlaneWeb] DeletePage 请求失败", zap.Error(err))
  109. return err
  110. }
  111. if resp.StatusCode != 204 {
  112. zap.L().Error("[PlaneWeb] DeletePage 返回错误", zap.String("status", resp.Status))
  113. return fmt.Errorf("DeletePage 失败: %s", resp.Status)
  114. }
  115. return nil
  116. }
  117. // ArchivePage 归档页面
  118. func ArchivePage(slug, projectID, pageID string) error {
  119. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/%s/archive/", slug, projectID, pageID)
  120. resp, err := planeWebPost(url, map[string]interface{}{})
  121. if err != nil {
  122. zap.L().Error("[PlaneWeb] ArchivePage 请求失败", zap.Error(err))
  123. return err
  124. }
  125. if !isWebSuccess(resp.StatusCode) {
  126. zap.L().Error("[PlaneWeb] ArchivePage 返回错误", zap.String("status", resp.Status))
  127. return fmt.Errorf("ArchivePage 失败: %s", resp.Status)
  128. }
  129. return nil
  130. }
  131. // UnarchivePage 取消归档页面
  132. func UnarchivePage(slug, projectID, pageID string) error {
  133. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/%s/archive/", slug, projectID, pageID)
  134. resp, err := planeWebDelete(url)
  135. if err != nil {
  136. zap.L().Error("[PlaneWeb] UnarchivePage 请求失败", zap.Error(err))
  137. return err
  138. }
  139. if !isWebSuccess(resp.StatusCode) {
  140. zap.L().Error("[PlaneWeb] UnarchivePage 返回错误", zap.String("status", resp.Status))
  141. return fmt.Errorf("UnarchivePage 失败: %s", resp.Status)
  142. }
  143. return nil
  144. }
  145. // LockPage 锁定页面
  146. func LockPage(slug, projectID, pageID string) error {
  147. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/%s/lock/", slug, projectID, pageID)
  148. resp, err := planeWebPost(url, map[string]interface{}{})
  149. if err != nil {
  150. zap.L().Error("[PlaneWeb] LockPage 请求失败", zap.Error(err))
  151. return err
  152. }
  153. if !isWebSuccess(resp.StatusCode) {
  154. zap.L().Error("[PlaneWeb] LockPage 返回错误", zap.String("status", resp.Status))
  155. return fmt.Errorf("LockPage 失败: %s", resp.Status)
  156. }
  157. return nil
  158. }
  159. // UnlockPage 解锁页面
  160. func UnlockPage(slug, projectID, pageID string) error {
  161. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/%s/lock/", slug, projectID, pageID)
  162. resp, err := planeWebDelete(url)
  163. if err != nil {
  164. zap.L().Error("[PlaneWeb] UnlockPage 请求失败", zap.Error(err))
  165. return err
  166. }
  167. if !isWebSuccess(resp.StatusCode) {
  168. zap.L().Error("[PlaneWeb] UnlockPage 返回错误", zap.String("status", resp.Status))
  169. return fmt.Errorf("UnlockPage 失败: %s", resp.Status)
  170. }
  171. return nil
  172. }
  173. // SetPageAccess 设置页面访问权限 (access: 0=私密, 1=公开)
  174. func SetPageAccess(slug, projectID, pageID string, access int) error {
  175. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/%s/access/", slug, projectID, pageID)
  176. resp, err := planeWebPost(url, map[string]interface{}{"access": access})
  177. if err != nil {
  178. zap.L().Error("[PlaneWeb] SetPageAccess 请求失败", zap.Error(err))
  179. return err
  180. }
  181. if !isWebSuccess(resp.StatusCode) {
  182. zap.L().Error("[PlaneWeb] SetPageAccess 返回错误", zap.String("status", resp.Status))
  183. return fmt.Errorf("SetPageAccess 失败: %s", resp.Status)
  184. }
  185. return nil
  186. }
  187. // DuplicatePage 复制页面
  188. func DuplicatePage(slug, projectID, pageID string) ([]byte, error) {
  189. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/pages/%s/duplicate/", slug, projectID, pageID)
  190. resp, err := planeWebPost(url, map[string]interface{}{})
  191. if err != nil {
  192. zap.L().Error("[PlaneWeb] DuplicatePage 请求失败", zap.Error(err))
  193. return nil, err
  194. }
  195. if !isWebSuccess(resp.StatusCode) {
  196. zap.L().Error("[PlaneWeb] DuplicatePage 返回错误",
  197. zap.String("status", resp.Status),
  198. zap.String("body", string(resp.Body)),
  199. )
  200. return nil, fmt.Errorf("DuplicatePage 失败: %s", resp.Status)
  201. }
  202. return resp.Body, nil
  203. }