github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/dkf/account/account.go (about)

     1  // 客户账号管理
     2  package account
     3  
     4  import (
     5  	"crypto/md5"
     6  	"encoding/hex"
     7  	"errors"
     8  
     9  	"github.com/chanxuehong/wechat/mp/core"
    10  )
    11  
    12  // Add 添加客服账号.
    13  //
    14  //	account:         完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符。
    15  //	nickname:        客服昵称,最长6个汉字或12个英文字符
    16  //	password:        客服账号登录密码
    17  //	isPasswordPlain: 标识 password 是否为明文格式, true 表示是明文密码, false 表示是密文密码.
    18  func Add(clt *core.Client, account, nickname, password string, isPasswordPlain bool) (err error) {
    19  	const incompleteURL = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token="
    20  
    21  	if password == "" {
    22  		return errors.New("empty password")
    23  	}
    24  	if isPasswordPlain {
    25  		md5Sum := md5.Sum([]byte(password))
    26  		password = hex.EncodeToString(md5Sum[:])
    27  	}
    28  
    29  	request := struct {
    30  		Account  string `json:"kf_account"`
    31  		Nickname string `json:"nickname"`
    32  		Password string `json:"password"`
    33  	}{
    34  		Account:  account,
    35  		Nickname: nickname,
    36  		Password: password,
    37  	}
    38  	var result core.Error
    39  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    40  		return
    41  	}
    42  	if result.ErrCode != core.ErrCodeOK {
    43  		err = &result
    44  		return
    45  	}
    46  	return
    47  }
    48  
    49  // Update 设置客服信息(增量更新, 不更新的可以留空).
    50  //
    51  //	account:         完整客服账号,格式为:账号前缀@公众号微信号
    52  //	nickname:        客服昵称,最长6个汉字或12个英文字符
    53  //	password:        客服账号登录密码
    54  //	isPasswordPlain: 标识 password 是否为明文格式, true 表示是明文密码, false 表示是密文密码.
    55  func Update(clt *core.Client, account, nickname, password string, isPasswordPlain bool) (err error) {
    56  	const incompleteURL = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token="
    57  
    58  	if isPasswordPlain && password != "" {
    59  		md5Sum := md5.Sum([]byte(password))
    60  		password = hex.EncodeToString(md5Sum[:])
    61  	}
    62  
    63  	request := struct {
    64  		Account  string `json:"kf_account"`
    65  		Nickname string `json:"nickname,omitempty"`
    66  		Password string `json:"password,omitempty"`
    67  	}{
    68  		Account:  account,
    69  		Nickname: nickname,
    70  		Password: password,
    71  	}
    72  	var result core.Error
    73  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    74  		return
    75  	}
    76  	if result.ErrCode != core.ErrCodeOK {
    77  		err = &result
    78  		return
    79  	}
    80  	return
    81  }
    82  
    83  // Delete 删除客服账号
    84  func Delete(clt *core.Client, kfAccount string) (err error) {
    85  	// TODO
    86  	//	incompleteURL := "https://api.weixin.qq.com/customservice/kfaccount/del?kf_account=" +
    87  	//		url.QueryEscape(kfAccount) + "&access_token="
    88  	incompleteURL := "https://api.weixin.qq.com/customservice/kfaccount/del?kf_account=" +
    89  		kfAccount + "&access_token="
    90  
    91  	var result core.Error
    92  	if err = clt.GetJSON(incompleteURL, &result); err != nil {
    93  		return
    94  	}
    95  	if result.ErrCode != core.ErrCodeOK {
    96  		err = &result
    97  		return
    98  	}
    99  	return
   100  }