├── .gitignore
├── .idea
├── .name
├── compiler.xml
├── copyright
│ ├── MIT.xml
│ └── profiles_settings.xml
├── encodings.xml
├── misc.xml
└── modules.xml
├── LICENSE
├── README.md
├── golang_netease_im_sdk.iml
├── nimSdk
├── accountUtil.go
├── checkSumBuilder.go
├── friendUtil.go
├── neteaseUtil.go
└── profileUtil.go
└── test
├── accountUtil_test.go
├── checkSumBuilder_test.go
└── profileUtil_test.go
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | # Created by https://www.gitignore.io/api/intellij,go
3 |
4 | ### Intellij ###
5 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
6 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
7 |
8 | # User-specific stuff:
9 | .idea/workspace.xml
10 | .idea/tasks.xml
11 | .idea/dictionaries
12 | .idea/vcs.xml
13 | .idea/jsLibraryMappings.xml
14 |
15 | # Sensitive or high-churn files:
16 | .idea/dataSources.ids
17 | .idea/dataSources.xml
18 | .idea/dataSources.local.xml
19 | .idea/sqlDataSources.xml
20 | .idea/dynamic.xml
21 | .idea/uiDesigner.xml
22 |
23 | # Gradle:
24 | .idea/gradle.xml
25 | .idea/libraries
26 |
27 | # Mongo Explorer plugin:
28 | .idea/mongoSettings.xml
29 |
30 | ## File-based project format:
31 | *.iws
32 |
33 | ## Plugin-specific files:
34 |
35 | # IntelliJ
36 | /out/
37 |
38 | # mpeltonen/sbt-idea plugin
39 | .idea_modules/
40 |
41 | # JIRA plugin
42 | atlassian-ide-plugin.xml
43 |
44 | # Crashlytics plugin (for Android Studio and IntelliJ)
45 | com_crashlytics_export_strings.xml
46 | crashlytics.properties
47 | crashlytics-build.properties
48 | fabric.properties
49 |
50 | ### Intellij Patch ###
51 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
52 |
53 | # *.iml
54 | # modules.xml
55 |
56 |
57 | ### Go ###
58 | # Compiled Object files, Static and Dynamic libs (Shared Objects)
59 | *.o
60 | *.a
61 | *.so
62 |
63 | # Folders
64 | _obj
65 | _test
66 |
67 | # Architecture specific extensions/prefixes
68 | *.[568vq]
69 | [568vq].out
70 |
71 | *.cgo1.go
72 | *.cgo2.c
73 | _cgo_defun.c
74 | _cgo_gotypes.go
75 | _cgo_export.*
76 |
77 | _testmain.go
78 |
79 | *.exe
80 | *.test
81 | *.prof
82 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | golang_netease_im_sdk
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/.idea/copyright/MIT.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 mind-stack-cn
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Golang 网易云信SDK
2 |
3 | [网易云信开发手册](http://dev.netease.im/docs)
4 |
5 | ## 实现功能
6 | * 创建云信ID
7 | * 云信ID更新
8 | * 更新并获取新token
9 | * 封禁云信ID
10 | * 解禁云信ID
11 | * 更新用户名片
12 | * 获取用户名片
13 | * TODO...
14 |
15 | ## 使用说明
16 | ````
17 | ...
18 | util.Init("#YOUR APP KEY", "#YOUR APP SEC KEY")// 初始化
19 | res, err := util.CreateAccid("zhangsan", "", "", "", "")
20 | ...
21 | ````
22 |
23 |
--------------------------------------------------------------------------------
/golang_netease_im_sdk.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/nimSdk/accountUtil.go:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 tony
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package nimSdk
25 |
26 | import (
27 | "net/url"
28 | "fmt"
29 | "encoding/json"
30 | "errors"
31 | "strconv"
32 | )
33 |
34 | var CREATE_ACCOUNT_ID_URL = UrlPair{"POST", NETEASE_BASE_URL + "/user/create.action"}
35 |
36 | var UPDATE_ACCOUNT_ID_URL = UrlPair{"POST", NETEASE_BASE_URL + "/user/update.action"}
37 |
38 | var REFRESH_TOKEN_URL = UrlPair{"POST", NETEASE_BASE_URL + "/user/refreshToken.action"}
39 |
40 | var BLOCK_URL = UrlPair{"POST", NETEASE_BASE_URL + "/user/block.action"}
41 |
42 | var UNBLOCK_URL = UrlPair{"POST", NETEASE_BASE_URL + "/user/unblock.action"}
43 |
44 | type NIMUInfo struct {
45 | Accid string //云信ID
46 | Name string //云信ID昵称,最大长度64字节,用来PUSH推送时显示的昵称
47 | Icon string //云信ID头像URL,第三方可选填,最大长度1024
48 | Token string //token
49 | }
50 |
51 | type nimCreateIDResp struct {
52 | NetEaseResp
53 | Info NIMUInfo
54 | }
55 |
56 | // 创建云信ID
57 | // 第三方帐号导入到云信平台
58 | // accId string 云信ID,最大长度32字节,必须保证一个 APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成, 不区分大小写, 会统一小写处理,请注意以此接口返回结果中的accid为准)
59 | // name string 云信ID昵称,最大长度64字节,用来PUSH推送 时显示的昵称
60 | // props string json属性,第三方可选填,最大长度1024字节
61 | // icon string 云信ID头像URL,第三方可选填,最大长度1024
62 | // token string 云信ID可以指定登录token值,最大长度128字节, 并更新,如果未指定,会自动生成token,并在创建成功后返回
63 | func CreateAccid(accid string, name string, props string, icon string, token string) (*NIMUInfo, error) {
64 | // format request body
65 | v := url.Values{}
66 | if len(accid) <= 0 {
67 | return nil, fmt.Errorf("accid is empty")
68 | }
69 | v.Add("accid", accid);
70 | if len(name) > 0 {
71 | v.Add("name", name)
72 | }
73 | if len(props) > 0 {
74 | v.Add("props", props)
75 | }
76 | if len(icon) > 0 {
77 | v.Add("icon", icon)
78 | }
79 | if len(token) > 0 {
80 | v.Add("token", token)
81 | }
82 |
83 | resStr,err := DoNeteaseHttpRequest(v, CREATE_ACCOUNT_ID_URL.method, CREATE_ACCOUNT_ID_URL.url)
84 |
85 | if err != nil {
86 | return nil,err
87 | }
88 |
89 | var resp nimCreateIDResp
90 | err = json.Unmarshal([]byte(resStr), &resp)
91 | if err != nil {
92 | return nil,err
93 | }
94 |
95 | if resp.Code != 200 {
96 | return nil,errors.New("error code "+strconv.Itoa(resp.Code))
97 | }
98 |
99 | return &resp.Info,nil
100 | }
101 |
102 | // 云信ID更新
103 | // 云信ID基本信息更新
104 | // accId string 云信ID,最大长度32字节,必须保证一个 APP内唯一(只允许字母、数字、半角下划线_、@、半角点以及半角-组成, 不区分大小写, 会统一小写处理,请注意以此接口返回结果中的accid为准)
105 | // name string 云信ID昵称,最大长度64字节,用来PUSH推送 时显示的昵称
106 | // props string json属性,第三方可选填,最大长度1024字节
107 | // icon string 云信ID头像URL,第三方可选填,最大长度1024
108 | // token string 云信ID可以指定登录token值,最大长度128字节, 并更新,如果未指定,会自动生成token,并在创建成功后返回
109 | func UpdateAccid(accid string, name string, props string, icon string, token string) (string, error) {
110 | // format request body
111 | v := url.Values{}
112 | if len(accid) <= 0 {
113 | return "", fmt.Errorf("accid is empty")
114 | }
115 | v.Add("accid", accid);
116 | if len(name) > 0 {
117 | v.Add("name", name)
118 | }
119 | if len(props) > 0 {
120 | v.Add("props", props)
121 | }
122 | if len(icon) > 0 {
123 | v.Add("icon", icon)
124 | }
125 | if len(token) > 0 {
126 | v.Add("token", token)
127 | }
128 |
129 | return DoNeteaseHttpRequest(v, UPDATE_ACCOUNT_ID_URL.method, UPDATE_ACCOUNT_ID_URL.url)
130 | }
131 |
132 | // 更新并获取新token
133 | // WebServer更新云信ID的token,同时返回新的token
134 | // accId string 云信ID,最大长度32字节,必须保证一个 APP内唯一
135 | func RefreshToken(accid string) (string, error) {
136 | // format request body
137 | v := url.Values{}
138 | if len(accid) <= 0 {
139 | return "", fmt.Errorf("accid is empty")
140 | }
141 | v.Add("accid", accid);
142 | return DoNeteaseHttpRequest(v, REFRESH_TOKEN_URL.method, REFRESH_TOKEN_URL.url)
143 | }
144 |
145 | // 封禁云信ID
146 | // 第三方禁用某个云信ID的IM功能,封禁云信ID后,此ID将不能登陆云信imserver
147 | // accId string 云信ID,最大长度32字节,必须保证一个 APP内唯一
148 | func Block(accid string) (string, error) {
149 | // format request body
150 | v := url.Values{}
151 | if len(accid) <= 0 {
152 | return "", fmt.Errorf("accid is empty")
153 | }
154 | v.Add("accid", accid);
155 | return DoNeteaseHttpRequest(v, BLOCK_URL.method, BLOCK_URL.url)
156 | }
157 |
158 | // 解禁云信ID
159 | // 解禁被封禁的云信ID
160 | // accId string 云信ID,最大长度32字节,必须保证一个 APP内唯一
161 | func Unblock(accid string) (string, error) {
162 | // format request body
163 | v := url.Values{}
164 | if len(accid) <= 0 {
165 | return "", fmt.Errorf("accid is empty")
166 | }
167 | v.Add("accid", accid);
168 | return DoNeteaseHttpRequest(v, UNBLOCK_URL.method, UNBLOCK_URL.url)
169 | }
170 |
--------------------------------------------------------------------------------
/nimSdk/checkSumBuilder.go:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 tony
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package nimSdk
25 |
26 | import (
27 | "crypto/sha1"
28 | "encoding/hex"
29 | )
30 |
31 | func GetCheckSum(appSecret string, nonce string, curTime string) string{
32 | s:= appSecret + nonce + curTime
33 | h:= sha1.New()
34 | h.Write([]byte(s))
35 | sha1_hash:= hex.EncodeToString(h.Sum(nil))
36 | return sha1_hash
37 | }
38 |
--------------------------------------------------------------------------------
/nimSdk/friendUtil.go:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 tony
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package nimSdk
25 |
26 | var FRIENDSHIP_URL = UrlPair{"POST", NETEASE_BASE_URL + "/friend/add.action"}
27 |
28 | var SPECIALRELATION_URL = UrlPair{"POST", NETEASE_BASE_URL + "/user/setSpecialRelation.action"}
29 |
30 |
--------------------------------------------------------------------------------
/nimSdk/neteaseUtil.go:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 tony
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package nimSdk
25 |
26 | import (
27 | "net/http"
28 | "strings"
29 | "time"
30 | "github.com/satori/go.uuid"
31 | "io/ioutil"
32 | "strconv"
33 | "net/url"
34 | )
35 |
36 | const NETEASE_BASE_URL = "https://api.netease.im/nimserver"
37 |
38 | var APPKEY = ""
39 |
40 | var APPSECRETKEY = ""
41 |
42 | type UrlPair struct {
43 | method, url string
44 | }
45 |
46 | type NetEaseResp struct {
47 | Code int //状态码
48 | }
49 |
50 | func Init(appKey string, appSecretKey string){
51 | APPKEY = appKey
52 | APPSECRETKEY = appSecretKey
53 | }
54 |
55 | // 执行云信http请求
56 | func DoNeteaseHttpRequest(v url.Values, reqMethod string, reqUrl string)(string, error){
57 | req, _ := http.NewRequest(reqMethod, reqUrl, strings.NewReader(v.Encode()))
58 |
59 | // add checksum
60 | AddNeteaseHttpHeader(req)
61 |
62 | // send request
63 | client := &http.Client{}
64 | resp, err := client.Do(req)
65 | if err != nil {
66 | return "", err;
67 | }
68 |
69 | // read response
70 | defer resp.Body.Close()
71 | respBody, err := ioutil.ReadAll(resp.Body)
72 | if err != nil {
73 | return "", err;
74 | }
75 |
76 | return string(respBody), nil;
77 | }
78 |
79 |
80 | // 在HttpHeader中添加参数用于校验
81 | func AddNeteaseHttpHeader(req *http.Request) {
82 | // nonce & curTime in second
83 | var nonce string = strings.Replace(uuid.NewV4().String(), "-", "", -1)
84 | var curTime string = strconv.FormatInt(time.Now().Unix(), 10)
85 |
86 | // add request header
87 | req.Header.Add("AppKey", APPKEY);
88 | req.Header.Add("Nonce", nonce);
89 | req.Header.Add("CurTime", curTime);
90 | req.Header.Add("CheckSum", GetCheckSum(APPSECRETKEY, nonce, curTime));
91 | req.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
92 | }
93 |
--------------------------------------------------------------------------------
/nimSdk/profileUtil.go:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 tony
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package nimSdk
25 |
26 | import (
27 | "encoding/json"
28 | "fmt"
29 | "net/url"
30 | "errors"
31 | "strconv"
32 | // "log"
33 | )
34 |
35 | var UPDATE_PROFILE_URL = UrlPair{"POST", NETEASE_BASE_URL + "/user/updateUinfo.action"}
36 |
37 | var GET_PROFILE_URL = UrlPair{"POST", NETEASE_BASE_URL + "/user/getUinfos.action"}
38 |
39 | type NIMUserProfile struct {
40 | NIMUInfo
41 | Sign string //用户签名,最大长度256字节
42 | Emai string //用户email,最大长度64字节
43 | Birth string //用户生日,最大长度16字节
44 | Mobile string //用户mobile,最大长度32字节,只支持国内号码
45 | Gender int //用户性别,0表示未知,1表示男,2女表示女,其它会报参数错误
46 | Ex string //用户名片扩展字段,最大长度1024字节,用户可自行扩展,建议封装成JSON字符串
47 | }
48 |
49 | // 更新用户名片
50 | // accId string 云信ID,最大长度32字节,必须保证一个 APP内唯一
51 | // name string 用户昵称,最大长度64字节
52 | // icon string 用户icon,最大长度1024字节
53 | // sign string 用户签名,最大长度256字节
54 | // email string 用户email,最大长度64字节
55 | // birth string 用户生日,最大长度16字节
56 | // mobile string 用户mobile,最大长度32字节
57 | // gender string 用户性别,0表示未知,1表示男,2女表示女,其它会报参数错误
58 | // ex string 用户名片扩展字段,最大长度1024字节,用户可自行扩展,建议封装成JSON字符串
59 | func UpdateProfile(accid string, name string, icon string, sign string, email string, birth string, mobile string, gender string, ex string) (bool, error) {
60 | // format request body
61 | v := url.Values{}
62 | if len(accid) <= 0 {
63 | return false, fmt.Errorf("accid is empty")
64 | }
65 | v.Add("accid", accid)
66 | if len(name) > 0 {
67 | v.Add("name", name)
68 | }
69 | if len(icon) > 0 {
70 | v.Add("icon", icon)
71 | }
72 | if len(sign) > 0 {
73 | v.Add("token", sign)
74 | }
75 | if len(email) > 0 {
76 | v.Add("email", email)
77 | }
78 | if len(birth) > 0 {
79 | v.Add("birth", birth)
80 | }
81 | if len(mobile) > 0 {
82 | v.Add("mobile", mobile)
83 | }
84 | if len(gender) > 0 {
85 | v.Add("gender", gender)
86 | }
87 | if len(ex) > 0 {
88 | v.Add("ex", ex)
89 | }
90 |
91 | resStr,err := DoNeteaseHttpRequest(v, UPDATE_PROFILE_URL.method, UPDATE_PROFILE_URL.url)
92 | // log.Println(resStr)
93 |
94 | if err != nil {
95 | return false,err
96 | }
97 |
98 | var resp NetEaseResp
99 | err = json.Unmarshal([]byte(resStr), &resp)
100 | if err != nil {
101 | return false,err
102 | }
103 |
104 | if resp.Code != 200 {
105 | return false,errors.New("error code "+strconv.Itoa(resp.Code))
106 | }
107 |
108 | return true,nil
109 | }
110 |
111 |
112 | type NIMGetProfileResp struct {
113 | NetEaseResp
114 | Uinfos []NIMUserProfile
115 | }
116 |
117 | // 获取用户名片
118 | // 获取用户名片,可批量
119 | // accId string 云信ID,最大长度32字节,必须保证一个 APP内唯一
120 | func GetProfile(accids []string) ([]NIMUserProfile, error) {
121 | // format request body
122 | v := url.Values{}
123 | if len(accids) <= 0 {
124 | return make([]NIMUserProfile,0), fmt.Errorf("accids is empty")
125 | }
126 | jsonVal, _ := json.Marshal(accids)
127 | v.Add("accids", string(jsonVal))
128 |
129 | resStr,err := DoNeteaseHttpRequest(v, GET_PROFILE_URL.method, GET_PROFILE_URL.url)
130 | // log.Println(resStr)
131 |
132 | if err != nil {
133 | return make([]NIMUserProfile,0),err
134 | }
135 |
136 | var resp NIMGetProfileResp
137 | err = json.Unmarshal([]byte(resStr), &resp)
138 | if err != nil {
139 | return make([]NIMUserProfile,0),err
140 | }
141 |
142 | if resp.Code != 200 {
143 | return make([]NIMUserProfile,0),errors.New("error code "+strconv.Itoa(resp.Code))
144 | }
145 |
146 | return resp.Uinfos,nil
147 | }
148 |
--------------------------------------------------------------------------------
/test/accountUtil_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 tony
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package test
25 |
26 | import (
27 | "testing"
28 | "time"
29 | "fmt"
30 | "github.com/stretchr/testify/assert"
31 | "github.com/mind-stack-cn/golang_netease_im_sdk/nimSdk"
32 | )
33 |
34 | func init() {
35 | nimSdk.Init("#YOUR APP KEY", "#YOUR APP SEC KEY")
36 | }
37 |
38 | func Test_TimeFormat(t *testing.T) {
39 | fmt.Println(time.Now().Unix())
40 | }
41 |
42 | func Test_CreateAccid(t *testing.T) {
43 | res, err := nimSdk.CreateAccid("lisi", "", "", "", "")
44 | assert.Nil(t, err, "error showedup while creating account id")
45 | if err == nil{
46 | fmt.Println(res)
47 | }else{
48 | fmt.Println(err.Error())
49 | }
50 | }
51 |
52 | func Test_RefreshToken(t *testing.T) {
53 | res, err := nimSdk.RefreshToken("lisi")
54 | assert.Nil(t, err, "error showedup while refresh account id")
55 | if err == nil{
56 | fmt.Println(res)
57 | }else{
58 | fmt.Println(err.Error())
59 | }
60 | }
61 |
62 | func Test_BlockAccid(t *testing.T) {
63 | res, err := nimSdk.Block("lisi")
64 | assert.Nil(t, err, "error showedup while blocking account id")
65 | if err == nil{
66 | fmt.Println(res)
67 | }else{
68 | fmt.Println(err.Error())
69 | }
70 | }
71 |
72 | func Test_UnBlockAccid(t *testing.T) {
73 | res, err := nimSdk.Unblock("lisi")
74 | assert.Nil(t, err, "error showedup while unblocking account id")
75 | if err == nil{
76 | fmt.Println(res)
77 | }else{
78 | fmt.Println(err.Error())
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/test/checkSumBuilder_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 tony
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package test
25 |
26 | import "testing"
27 |
28 | import (
29 | "github.com/stretchr/testify/assert"
30 | "github.com/mind-stack-cn/golang_netease_im_sdk/nimSdk"
31 | )
32 |
33 | func Test_GetCheckSum(t *testing.T) {
34 | var checkSum string = nimSdk.GetCheckSum("c0bd7b34271f", "aa84f22d5bde42fb91c34fca5b443fc6", "1462873857")
35 | assert.Equal(t, checkSum, "b071260cb654e91da79882deef07b77845fc3e9d", "check sum should equal")
36 | }
37 |
--------------------------------------------------------------------------------
/test/profileUtil_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2016 tony
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package test
25 |
26 | import (
27 | "testing"
28 | "fmt"
29 | "github.com/stretchr/testify/assert"
30 | "github.com/mind-stack-cn/golang_netease_im_sdk/nimSdk"
31 | )
32 |
33 |
34 | func init() {
35 | nimSdk.Init("#YOUR APP KEY", "#YOUR APP SEC KEY")
36 | }
37 |
38 | func Test_UpdateProfile(t *testing.T) {
39 | res, err := nimSdk.UpdateProfile("lisi", "lisilisi", "", "", "", "", "", "", "")
40 | assert.Nil(t, err, "error showedup while creating account id")
41 | if err == nil{
42 | fmt.Println(res)
43 | }else{
44 | fmt.Println(err.Error())
45 | }
46 | }
47 |
48 | func Test_GetProfile(t *testing.T) {
49 | res, err := nimSdk.GetProfile([]string{"lisi"})
50 | assert.Nil(t, err, "error showedup while creating account id")
51 | if err == nil{
52 | fmt.Println(res)
53 | }else{
54 | fmt.Println(err.Error())
55 | }
56 | }
57 |
--------------------------------------------------------------------------------