作者 yangfu

im csaccount

... ... @@ -4,18 +4,19 @@ import (
"bytes"
"encoding/json"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/uid"
"html/template"
"math/rand"
"strconv"
"strings"
"time"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/uid"
"ability/internal/repository"
"ability/models"
"ability/protocol"
s_sms "ability/services/sms"
s_im "ability/services/im"
s_sms "ability/services/sms"
"github.com/astaxie/beego"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
... ... @@ -42,7 +43,7 @@ func assertImplement() {
var (
//服务
sms s_sms.ISmsService = &s_sms.YunPianSmsService{}
sms s_sms.ISmsService = &s_sms.YunPianSmsService{}
ConfigRepository repository.IConfigRepository = &repository.ConfigRepository{}
//仓储
UserRepository repository.IUserRepository = &repository.UserRepository{}
... ... @@ -51,9 +52,9 @@ var (
//登录
func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) {
var (
user *models.Users
userInfo *models.UserInfo
result bool
user *models.Users
userInfo *models.UserInfo
result bool
checkImResponse *protocol.CheckImResponse
)
user, err = UserRepository.GetUsersByMobile(request.Phone)
... ... @@ -105,7 +106,7 @@ Success:
if userInfo.CsAccount==0{
userInfo.CsAccount = imGetRandomCSAccount()
}
userInfo.AuthExp = time.Now().Add(time.Second*protocol.TokenExpire)
userInfo.AuthExp = time.Now().Add(time.Second * protocol.TokenExpire)
if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
return
}
... ... @@ -153,7 +154,7 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr
//刷新token
func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) {
var (
userInfo *models.UserInfo
userInfo *models.UserInfo
)
_, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret)
if err != nil {
... ... @@ -330,94 +331,96 @@ Fail:
return
}
func CheckIm(request *protocol.CheckImRequest)(rsp *protocol.CheckImResponse,err error){
var (
)
func CheckIm(request *protocol.CheckImRequest) (rsp *protocol.CheckImResponse, err error) {
var ()
if beego.BConfig.RunMode != "prod" {
return
}
rsp =&protocol.CheckImResponse{}
if !request.IsCreated{
if err = imUserCreate(request,rsp);err!=nil{
rsp = &protocol.CheckImResponse{}
if !request.IsCreated {
if err = imUserCreate(request, rsp); err != nil {
return
}
}else{
if err = imUserInfoUpdate(request,rsp);err!=nil{
} else {
if err = imUserInfoUpdate(request, rsp); err != nil {
return
}
}
if err = imUserRefreshToken(request,rsp);err!=nil{
if err = imUserRefreshToken(request, rsp); err != nil {
return
}
return
}
//create
func imUserCreate(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){
var(
param s_im.UserCreate=s_im.UserCreate{
Accid:request.Uid,
Name:request.Uname,
Icon:request.Icon,
func imUserCreate(request *protocol.CheckImRequest, rsp *protocol.CheckImResponse) (err error) {
var (
param s_im.UserCreate = s_im.UserCreate{
Accid: request.Uid,
Name: request.Uname,
Icon: request.Icon,
}
resp []byte
out s_im.UserCreateResult
out s_im.UserCreateResult
)
if resp,err=s_im.DefaultImClient.Call(param);err!=nil{
if resp, err = s_im.DefaultImClient.Call(param); err != nil {
return
}
if err = json.Unmarshal(resp,&out);err!=nil{
if err = json.Unmarshal(resp, &out); err != nil {
return
}
if out.Code!=200 || (out.Info.Accid!=request.Uid){
if out.Code != 200 || (out.Info.Accid != request.Uid) {
return s_im.ErrorFailCall
}
rsp.ImToken = out.Info.Token
return
}
//update user info
func imUserInfoUpdate(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){
var(
param s_im.UserUpdateUinfo=s_im.UserUpdateUinfo{
Accid:request.Uid,
Name:request.Uname,
Icon:request.Icon,
func imUserInfoUpdate(request *protocol.CheckImRequest, rsp *protocol.CheckImResponse) (err error) {
var (
param s_im.UserUpdateUinfo = s_im.UserUpdateUinfo{
Accid: request.Uid,
Name: request.Uname,
Icon: request.Icon,
}
resp []byte
out s_im.BaseResp
out s_im.BaseResp
)
if resp,err=s_im.DefaultImClient.Call(param);err!=nil{
if resp, err = s_im.DefaultImClient.Call(param); err != nil {
return
}
if err = json.Unmarshal(resp,&out);err!=nil{
if err = json.Unmarshal(resp, &out); err != nil {
return
}
if out.Code!=200{
if out.Code != 200 {
return s_im.ErrorFailCall
}
return
}
//refresh token
func imUserRefreshToken(request *protocol.CheckImRequest,rsp *protocol.CheckImResponse)(err error){
var(
param s_im.UserRefreshToken=s_im.UserRefreshToken{
Accid:request.Uid,
func imUserRefreshToken(request *protocol.CheckImRequest, rsp *protocol.CheckImResponse) (err error) {
var (
param s_im.UserRefreshToken = s_im.UserRefreshToken{
Accid: request.Uid,
}
resp []byte
out s_im.UserCreateResult
out s_im.UserCreateResult
)
if resp,err=s_im.DefaultImClient.Call(param);err!=nil{
if resp, err = s_im.DefaultImClient.Call(param); err != nil {
return
}
if err = json.Unmarshal(resp,&out);err!=nil{
if err = json.Unmarshal(resp, &out); err != nil {
return
}
if out.Code!=200 || (out.Info.Accid!=request.Uid){
if out.Code != 200 || (out.Info.Accid != request.Uid) {
return s_im.ErrorFailCall
}
rsp.ImToken = out.Info.Token
return
}
// 获取客服id
func imGetRandomCSAccount()( acid int64){
kefus,err :=models.GetUsersKefu()
... ... @@ -436,4 +439,3 @@ func imGetRandomCSAccount()( acid int64){
return acid
}
... ...
package auth
import (
"ability/protocol"
"math/rand"
"strings"
"time"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
)
type ILoginAuth interface {
LoginAuth() error
TODO(v interface{}) error
LoginAuth() (*UserIdentity, error) //登录认证操作,返回身份ID和错误
TODO(v interface{}) error //预留,不知道干嘛用,也许可以用来获取取认证结果,或进行认证后附带的操作
}
type UserIdentity struct {
Auth string `json:"auth`
Uid int64 `json:"uid"`
Imtoken string `json:"imtoken"`
Accid int64 `json:"accid"`
Icon string `json:"icon"`
Uname string `json:"uname"`
}
//TODO
... ... @@ -10,12 +29,28 @@ type ILoginAuth interface {
type LoginByPassword struct {
Username string
Password string
err error
}
func NewLoginByPassword(username string, password string) *LoginByPassword {
return &LoginByPassword{
Username: username,
Password: password,
}
}
//手机短信登录
type LoginBySms struct {
Phone string
Code string
err error
}
func NewLoginBySms(phone string, code string) *LoginBySms {
return &LoginBySms{
Phone: phone,
Code: code,
}
}
//其他第三方登录
... ... @@ -30,19 +65,66 @@ var (
_ ILoginAuth = LoginByXxxx{}
)
func (o LoginByPassword) LoginAuth() error {
return nil
func (o LoginByPassword) LoginAuth() (*UserIdentity, error) {
user, err := UserRepository.GetUserInfoByMobile(o.Username)
if err != nil {
log.Error(err)
o.err = common.NewError(2020, err) //账号不存在
return nil, o.err
}
if strings.Compare(user.Passwd, o.Password) != 0 {
o.err = common.NewError(2021, err) //登录密码错误
return nil, o.err
}
identity := &UserIdentity{
Uid: user.Id,
Imtoken: user.ImToken,
Accid: user.CsAccount,
Icon: user.Icon,
Uname: user.Uname,
}
return identity, nil
}
func (o LoginBySms) LoginAuth() error {
return nil
func (o LoginBySms) LoginAuth() (*UserIdentity, error) {
user, err := UserRepository.GetUserInfoByMobile(o.Phone)
if err != nil {
log.Error(err)
o.err = common.NewError(2020, err) //账号不存在
return nil, o.err
}
result, err := CheckSmsCode(o.Phone, o.Code, protocol.SmsLoginCode)
if err != nil && !result {
return nil, err
}
identity := &UserIdentity{
Uid: user.Id,
Imtoken: user.ImToken,
Accid: user.CsAccount,
Icon: user.Icon,
Uname: user.Uname,
}
return identity, nil
}
func (o LoginByXxxx) LoginAuth() error {
return nil
func (o LoginByXxxx) LoginAuth() (*UserIdentity, error) {
//TODO
return nil, nil
}
func (o LoginByPassword) TODO(v interface{}) error {
// rv := reflect.ValueOf(v)
// if rv.Kind() != reflect.Ptr {
// return errors.New("v must be point")
// }
// rv = rv.Elem()
// if ok := rv.CanSet(); !ok {
// panic("v can not set")
// }
// a := "random string"
// ra := reflect.ValueOf(a)
// rv.Set(ra)
//生成并更新authcode
return nil
}
... ... @@ -53,3 +135,15 @@ func (o LoginBySms) TODO(v interface{}) error {
func (o LoginByXxxx) TODO(v interface{}) error {
return nil
}
func randomString(l int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyz"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
... ...