student.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package student
  2. import (
  3. context2 "context"
  4. "fmt"
  5. "github.com/silenceper/wechat/v2/miniprogram/context"
  6. "github.com/silenceper/wechat/v2/util"
  7. )
  8. const (
  9. // quickCheckStudentIdentityURL 快速获取学生身份
  10. quickCheckStudentIdentityURL = "https://api.weixin.qq.com/intp/quickcheckstudentidentity?access_token=%s"
  11. )
  12. // Student 学生身份核验
  13. type Student struct {
  14. *context.Context
  15. }
  16. // NewStudent init
  17. func NewStudent(ctx *context.Context) *Student {
  18. return &Student{ctx}
  19. }
  20. // BindStatus 学生身份绑定状态
  21. type BindStatus int
  22. const (
  23. // BindStatusUnbound 未绑定
  24. BindStatusUnbound BindStatus = 1
  25. // BindStatusAuditing 审核中
  26. BindStatusAuditing BindStatus = 2
  27. // BindStatusBound 已绑定
  28. BindStatusBound BindStatus = 3
  29. )
  30. // QuickCheckStudentIdentityRequest 快速获取学生身份请求参数
  31. type QuickCheckStudentIdentityRequest struct {
  32. OpenID string `json:"openid"` // 用户在业务方下的openid,需为已开通接口权限小程序对应的用户openid
  33. WxStudentCheckCode string `json:"wx_studentcheck_code"` // 用户授权查询code,由授权插件返回,code有效期两小时
  34. }
  35. // QuickCheckStudentIdentityResponse 快速获取学生身份返回参数
  36. type QuickCheckStudentIdentityResponse struct {
  37. util.CommonError
  38. BindStatus BindStatus `json:"bind_status"` // 绑定状态:1-未绑定,2-审核中,3-已绑定
  39. IsStudent bool `json:"is_student"` // 用户学生身份绑定状态说明,true-是学生,false-不是学生
  40. }
  41. // QuickCheckStudentIdentity 快速获取学生身份
  42. // see https://developers.weixin.qq.com/miniprogram/dev/server/API/student/api_quickcheckstudentidentity.html
  43. func (student *Student) QuickCheckStudentIdentity(in *QuickCheckStudentIdentityRequest) (out QuickCheckStudentIdentityResponse, err error) {
  44. return student.QuickCheckStudentIdentityContext(context2.Background(), in)
  45. }
  46. // QuickCheckStudentIdentityContext 快速获取学生身份
  47. func (student *Student) QuickCheckStudentIdentityContext(ctx context2.Context, in *QuickCheckStudentIdentityRequest) (out QuickCheckStudentIdentityResponse, err error) {
  48. var accessToken string
  49. if accessToken, err = student.GetAccessTokenContext(ctx); err != nil {
  50. return
  51. }
  52. uri := fmt.Sprintf(quickCheckStudentIdentityURL, accessToken)
  53. var response []byte
  54. if response, err = util.PostJSONContext(ctx, uri, in); err != nil {
  55. return
  56. }
  57. // 使用通用方法返回错误
  58. err = util.DecodeWithError(response, &out, "student.QuickCheckStudentIdentity")
  59. return
  60. }