作者 唐旭辉

新增

... ... @@ -2,6 +2,7 @@ package auth
type ILoginAuth interface {
LoginAuth() error
TODO(v interface{}) error
}
//TODO
... ... @@ -34,3 +35,15 @@ func (o LoginBySms) LoginAuth() error {
func (o LoginByXxxx) LoginAuth() error {
return nil
}
func (o LoginByPassword) TODO(v interface{}) error {
return nil
}
func (o LoginBySms) TODO(v interface{}) error {
return nil
}
func (o LoginByXxxx) TODO(v interface{}) error {
return nil
}
... ...
package im
import (
"fmt"
)
type ImParam interface {
Format() map[string]string
GetPath() string
}
type BaseResp struct {
Code int `json:"code"`
}
//TokenInfo 云通信Token
type TokenInfo struct {
Token string `json:"token"`
Accid string `json:"accid"`
Name string `json:"name"`
}
type UserCreateResult struct {
BaseResp
Info TokenInfo `json:"info"`
}
//UserCreate 云通信用户
type UserCreate struct {
Accid string //网易云通信ID,最大长度32字符
Name string //ID昵称,最大长度64字符。
Props string //json属性,开发者可选填,最大长度1024字符
Icon string //ID头像URL,开发者可选填,最大长度1024字符
Token string
/**云通信ID可以指定登录token值,最大长度128字符,
并更新,如果未指定,会自动生成token,并在
创建成功后返回**/
Sign string //签名
Email string
Birth string
Mobile string
Gender int //0未知,1男,2女
Ex string //扩展字段
}
var (
_ ImParam = UserCreate{}
)
func (p UserCreate) Format() map[string]string {
return map[string]string{
"accid": p.Accid,
"name": p.Name,
"props": p.Props,
"icon": p.Icon,
"token": p.Token,
"sign": p.Sign,
"email": p.Email,
"birth": p.Birth,
"mobile": p.Mobile,
"gender": fmt.Sprintf("%d", p.Gender),
"ex": p.Ex,
}
}
func (p UserCreate) GetPath() string {
return "/user/create.action"
}
... ...
package im
import (
"crypto/sha1"
"encoding/hex"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
func buildCheckSum(appSecret string, nonce string, curTime string) string {
str := []byte(appSecret + nonce + curTime)
sh := sha1.New()
sh.Write(str)
result := hex.EncodeToString(sh.Sum(nil))
return strings.ToLower(result)
}
type ImClient struct {
baseUrl string
appKey string
appSecret string
}
func (i ImClient) buildHeader() http.Header {
var h = http.Header{}
curTime := strconv.FormatInt(time.Now().Unix(), 10)
nonce := strconv.FormatInt(time.Now().Unix()+rand.Int63n(5000), 10)
checkSum := buildCheckSum(i.appSecret, nonce, curTime)
h.Set("Content-Type", "application/x-www-form-urlencoded")
h.Set("AppKey", i.appKey)
h.Set("Nonce", nonce)
h.Set("CurTime", curTime)
h.Set("CheckSum", checkSum)
return h
}
func (i ImClient) httpDo(path string, posts map[string]string) ([]byte, error) {
client := http.Client{}
reqURL := i.baseUrl + path
params := url.Values{}
for k, v := range posts {
params.Add(k, v)
}
req, err := http.NewRequest("POST", reqURL, strings.NewReader(params.Encode()))
if err != nil {
return nil, err
}
req.Header = i.buildHeader()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (i ImClient) Call(param ImParam) ([]byte, error) {
return i.httpDo(param.GetPath(), param.Format())
}
... ...