example_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package work_test
  2. import (
  3. workwx "dashoo.cn/opms_libary/plugin/wechat/work"
  4. "net/http"
  5. "strconv"
  6. "time"
  7. )
  8. const (
  9. corpID = "your_corpid"
  10. corpSecret = "your_corpsecret"
  11. )
  12. func ExampleWorkwx() {
  13. agentID := int64(1234567)
  14. client := workwx.New(corpID)
  15. // there're advanced options
  16. _ = workwx.New(
  17. corpID,
  18. workwx.WithQYAPIHost("http://localhost:8888"),
  19. workwx.WithHTTPClient(&http.Client{}),
  20. )
  21. // work with individual apps
  22. app := client.WithApp(corpSecret, agentID)
  23. app.SpawnAccessTokenRefresher()
  24. // see other examples for more details
  25. }
  26. func ExampleWorkwxApp_ApplyOAEvent() {
  27. agentID := int64(1234567)
  28. client := workwx.New(corpID)
  29. app := client.WithApp(corpSecret, agentID)
  30. app.SpawnAccessTokenRefresher()
  31. appInfo := workwx.OAApplyEvent{
  32. CreatorUserID: "your_userid",
  33. TemplateID: "your_templateid",
  34. UseTemplateApprover: 1,
  35. ApplyData: workwx.OAContents{
  36. Contents: []workwx.OAContent{
  37. {
  38. Control: workwx.OAControlText,
  39. ID: "Text-1608628829793",
  40. Value: workwx.OAContentValue{
  41. Text: "文本",
  42. },
  43. },
  44. {
  45. Control: workwx.OAControlTextarea,
  46. ID: "Textarea-1608628832640",
  47. Value: workwx.OAContentValue{
  48. Text: "多行文本\n可换行",
  49. },
  50. },
  51. {
  52. Control: workwx.OAControlNumber,
  53. ID: "Number-1608632495498",
  54. Value: workwx.OAContentValue{
  55. Number: "123.45",
  56. },
  57. },
  58. {
  59. Control: workwx.OAControlMoney,
  60. ID: "Money-1608632497034",
  61. Value: workwx.OAContentValue{
  62. Money: "678.90",
  63. },
  64. },
  65. {
  66. Control: workwx.OAControlFormula,
  67. ID: "Formula-1608632498148",
  68. Value: workwx.OAContentValue{
  69. Formula: workwx.OAContentFormula{Value: "5.0"},
  70. },
  71. },
  72. {
  73. Control: workwx.OAControlDate,
  74. ID: "Date-1608632499227",
  75. Value: workwx.OAContentValue{
  76. Date: workwx.OAContentDate{Type: "day", Timestamp: strconv.FormatInt(time.Now().Unix(), 10)},
  77. },
  78. },
  79. {
  80. Control: workwx.OAControlDate,
  81. ID: "Date-1608632500394",
  82. Value: workwx.OAContentValue{
  83. Date: workwx.OAContentDate{Type: "hour", Timestamp: strconv.FormatInt(time.Now().Unix(), 10)},
  84. },
  85. },
  86. {
  87. Control: workwx.OAControlDateRange,
  88. ID: "DateRange-1608632502131",
  89. Value: workwx.OAContentValue{
  90. DateRange: workwx.OAContentDateRange{
  91. NewBegin: int(time.Now().Unix()),
  92. NewEnd: int(time.Now().Add(time.Hour * 24).Unix()),
  93. NewDuration: 60 * 60 * 24,
  94. },
  95. },
  96. },
  97. {
  98. Control: workwx.OAControlSelector,
  99. ID: "Selector-1608632503203",
  100. Value: workwx.OAContentValue{
  101. Selector: workwx.OAContentSelector{
  102. Type: "single",
  103. Options: []workwx.OAContentSelectorOption{
  104. {Key: "option-1608632503204"},
  105. },
  106. },
  107. },
  108. },
  109. {
  110. Control: workwx.OAControlSelector,
  111. ID: "Selector-1608632504330",
  112. Value: workwx.OAContentValue{
  113. Selector: workwx.OAContentSelector{
  114. Type: "multi",
  115. Options: []workwx.OAContentSelectorOption{
  116. {Key: "option-1608632504330"},
  117. {Key: "option-1608632504331"},
  118. },
  119. },
  120. },
  121. },
  122. {
  123. Control: workwx.OAControlContact,
  124. ID: "Contact-1608632505579",
  125. Value: workwx.OAContentValue{
  126. Members: []workwx.OAContentMember{{
  127. UserID: "your_userid",
  128. Name: "your_name",
  129. }},
  130. },
  131. },
  132. {
  133. Control: workwx.OAControlContact,
  134. ID: "Contact-1608632506635",
  135. Value: workwx.OAContentValue{
  136. Departments: []workwx.OAContentDepartment{{
  137. OpenAPIID: "39",
  138. Name: "xx部门1",
  139. }, {
  140. OpenAPIID: "40",
  141. Name: "xx部门2",
  142. }},
  143. },
  144. },
  145. {
  146. Control: workwx.OAControlLocation,
  147. ID: "Location-1608632507748",
  148. Value: workwx.OAContentValue{
  149. Location: workwx.OAContentLocation{
  150. Latitude: "30.547239",
  151. Longitude: "104.063291",
  152. Title: "腾讯科技(成都)有限公司(腾讯成都大厦)",
  153. Address: "四川省成都市武侯区天府三街198号腾讯成都大厦A座",
  154. Time: int(time.Now().Unix()),
  155. },
  156. },
  157. },
  158. {
  159. Control: workwx.OAControlRelatedApproval,
  160. ID: "RelatedApproval-1608632509930",
  161. Value: workwx.OAContentValue{
  162. RelatedApproval: []workwx.OAContentRelatedApproval{
  163. {SpNo: "202012220021"},
  164. },
  165. },
  166. },
  167. {
  168. Control: workwx.OAControlTable,
  169. ID: "Table-1608632511066",
  170. Value: workwx.OAContentValue{
  171. Table: []workwx.OAContentTableList{
  172. {
  173. List: []workwx.OAContent{
  174. {
  175. Control: workwx.OAControlText,
  176. ID: "Text-1608632519610",
  177. Value: workwx.OAContentValue{
  178. Text: "第一行第一列",
  179. },
  180. }, {
  181. Control: workwx.OAControlText,
  182. ID: "Text-1608632521106",
  183. Value: workwx.OAContentValue{
  184. Text: "第一行第二列",
  185. },
  186. },
  187. },
  188. },
  189. {
  190. List: []workwx.OAContent{
  191. {
  192. Control: workwx.OAControlText,
  193. ID: "Text-1608632519610",
  194. Value: workwx.OAContentValue{
  195. Text: "第二行第一列",
  196. },
  197. }, {
  198. Control: workwx.OAControlText,
  199. ID: "Text-1608632521106",
  200. Value: workwx.OAContentValue{
  201. Text: "第二行第二列",
  202. },
  203. },
  204. },
  205. },
  206. },
  207. },
  208. },
  209. {
  210. Control: workwx.OAControlVacation,
  211. ID: "Vacation-1608715577151",
  212. Value: workwx.OAContentValue{
  213. Vacation: workwx.OAContentVacation{
  214. Selector: workwx.OAContentSelector{
  215. Type: "single",
  216. Options: []workwx.OAContentSelectorOption{
  217. {
  218. Key: "3",
  219. },
  220. },
  221. },
  222. Attendance: workwx.OAContentVacationAttendance{
  223. DateRange: workwx.OAContentVacationAttendanceDateRange{
  224. Type: "hour",
  225. OAContentDateRange: workwx.OAContentDateRange{
  226. NewBegin: int(time.Now().Unix()),
  227. NewEnd: int(time.Now().Add(time.Hour * 72).Unix()),
  228. NewDuration: 60 * 60 * 72,
  229. },
  230. },
  231. Type: 1,
  232. },
  233. },
  234. },
  235. },
  236. },
  237. },
  238. SummaryList: []workwx.OASummaryList{{SummaryInfo: []workwx.OAText{{
  239. Text: "摘要第1行",
  240. }}}, {SummaryInfo: []workwx.OAText{{
  241. Text: "摘要第2行",
  242. }}}},
  243. }
  244. _, _ = app.ApplyOAEvent(appInfo)
  245. }