예제 #1
0
// 创建分组
func (c *Client) UserGroupCreate(name string) (*user.Group, error) {
	if len(name) == 0 {
		return nil, errors.New(`UserGroupCreate: UserGroupCreate: name == ""`)
	}

	token, err := c.Token()
	if err != nil {
		return nil, err
	}

	var request struct {
		Group struct {
			Name string `json:"name"`
		} `json:"group"`
	}

	request.Group.Name = name

	jsonData, err := json.Marshal(&request)
	if err != nil {
		return nil, err
	}

	_url := clientUserGroupCreateURL(token)
	resp, err := c.httpClient.Post(_url, postJSONContentType, bytes.NewReader(jsonData))
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("UserGroupCreate: %s", resp.Status)
	}

	var result struct {
		Group struct {
			Id   int    `json:"id"`
			Name string `json:"name"`
		} `json:"group"`
		Error
	}
	if err = json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return nil, err
	}
	if result.ErrCode != 0 {
		return nil, &result.Error
	}

	var group user.Group
	group.Id = result.Group.Id
	group.Name = result.Group.Name
	return &group, nil
}
예제 #2
0
파일: client_user.go 프로젝트: GOCMS/wechat
// 创建分组
func (c *Client) UserGroupCreate(name string) (*user.Group, error) {
	if len(name) == 0 {
		return nil, errors.New(`name == ""`)
	}

	token, err := c.Token()
	if err != nil {
		return nil, err
	}
	_url := userGroupCreateURL(token)

	var request struct {
		Group struct {
			Name string `json:"name"`
		} `json:"group"`
	}

	request.Group.Name = name

	var result struct {
		Group struct {
			Id   int    `json:"id"`
			Name string `json:"name"`
		} `json:"group"`
		Error
	}
	if err = c.postJSON(_url, &request, &result); err != nil {
		return nil, err
	}

	if result.ErrCode != 0 {
		return nil, &result.Error
	}

	var group user.Group
	group.Id = result.Group.Id
	group.Name = result.Group.Name
	return &group, nil
}