Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
im-microservice
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Li Feifei
im-microservice
Commits
263bf26a
Commit
263bf26a
authored
Jul 08, 2020
by
李洪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
用户修改
parent
76f8001f
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
120 additions
and
108 deletions
+120
-108
db_struct.go
db/db_struct.go
+8
-2
main.go
sevice/im_friend_request/main.go
+3
-3
im_user.go
sevice/im_user/im_user.go
+70
-61
im_user_db.go
sevice/im_user/im_user_db.go
+39
-42
No files found.
db/db_struct.go
View file @
263bf26a
...
...
@@ -56,11 +56,17 @@ type ImUser struct {
Accid
string
Appkey
string
Name
string
Icon
string
Token
string
Sign
string
Mobile
string
Disable
int64
Email
string
Birth
string
Gender
int64
Valid
int64
MuteEstoppel
int64
MuteAudioVideo
int64
Ex
t
string
Ex
string
Createtime
string
Edition
string
}
...
...
sevice/im_friend_request/main.go
View file @
263bf26a
...
...
@@ -22,7 +22,7 @@ func (fs *ImFriendService) All(ctx context.Context, in *pb.ImFriendListReq) (rep
return
}
// 添加用户是否存在
if
_
,
err
=
im_user
.
GetImUserByAccid
(
in
.
Accid
);
err
!=
nil
{
if
_
,
err
=
im_user
.
GetImUserByAccid
Appkey
(
in
.
Accid
,
in
.
Common
.
Appkey
);
err
!=
nil
{
err
=
status
.
Error
(
codes
.
NotFound
,
"用户不存在"
)
return
}
...
...
@@ -51,12 +51,12 @@ func (fs *ImFriendService) Add(ctx context.Context, in *pb.ImFriendAddReq) (repl
return
}
// 添加用户是否存在
if
_
,
err
=
im_user
.
GetImUserByAccid
(
in
.
Accid
);
err
!=
nil
{
if
_
,
err
=
im_user
.
GetImUserByAccid
Appkey
(
in
.
Accid
,
in
.
Common
.
Appkey
);
err
!=
nil
{
err
=
status
.
Error
(
codes
.
NotFound
,
"用户不存在"
)
return
}
// 被添加用户存在
if
_
,
err
=
im_user
.
GetImUserByAccid
(
in
.
Faccid
);
err
!=
nil
{
if
_
,
err
=
im_user
.
GetImUserByAccid
Appkey
(
in
.
Faccid
,
in
.
Common
.
Appkey
);
err
!=
nil
{
err
=
status
.
Error
(
codes
.
NotFound
,
"被添加用户不存在"
)
return
}
...
...
sevice/im_user/im_user.go
View file @
263bf26a
...
...
@@ -3,7 +3,7 @@ package im_user
import
(
"context"
"im-microservice/db"
"im-microservice/helper"
"im-microservice/pb"
"google.golang.org/grpc/status"
...
...
@@ -31,93 +31,103 @@ type ImUserQ struct {
}
//注册
func
(
u
*
ImUserServer
)
ImUserRegister
(
ctx
context
.
Context
,
in
*
pb
.
ImUserRequest
)
(
reply
*
pb
.
ImUserReply
,
err
error
)
{
imUser
:=
&
ImUser
{
Name
:
in
.
GetName
(),
Mobile
:
in
.
GetMobile
(),
Code
:
in
.
GetCode
(),
}
// 从redis获取短信验证码
cache_code
,
err
:=
helper
.
GetCode
(
imUser
.
Mobile
)
if
err
!=
nil
{
return
}
//对比验证码
if
cache_code
!=
imUser
.
Code
{
err
=
status
.
Error
(
302
,
"验证码不匹配"
)
return
}
reply
=
&
pb
.
ImUserReply
{}
func
(
u
*
ImUserServer
)
ImUserRegister
(
ctx
context
.
Context
,
in
*
pb
.
ImUserRequest
)
(
reply
*
pb
.
ImUserRegReply
,
err
error
)
{
// 是否已经注册过, 如果已经注册返回用户信息
if
c_user
,
_
:=
GetImUserBy
Mobile
(
imUser
.
Mobile
);
c_user
.
Accid
!=
""
{
err
=
status
.
Error
(
10438
,
"
手机号已注册
"
)
if
c_user
,
_
:=
GetImUserBy
AccidAppkey
(
in
.
GetAccid
(),
in
.
Common
.
Appkey
);
c_user
.
Accid
!=
""
{
err
=
status
.
Error
(
10438
,
"
用户已存在
"
)
return
}
// 企业用户注册失败
c_user
,
err
:=
CreateImUser
(
i
mUser
.
Name
,
imUser
.
Mobile
)
c_user
,
err
:=
CreateImUser
(
i
n
)
if
err
!=
nil
{
err
=
status
.
Error
(
500
,
"注册失败"
)
return
}
reply
=
&
pb
.
ImUserRegReply
{}
reply
.
Accid
=
c_user
.
Accid
reply
.
Name
=
c_user
.
Name
reply
.
Token
=
c_user
.
Token
return
}
//用户登录
func
(
u
*
ImUserServer
)
ImUserLogin
(
ctx
context
.
Context
,
in
*
pb
.
ImUserRequest
)
(
reply
*
pb
.
ImUserReply
,
err
error
)
{
imUser
:=
&
ImUser
{
Name
:
in
.
GetName
(),
Mobile
:
in
.
GetMobile
(),
Code
:
in
.
GetCode
(),
Platform
:
in
.
GetPlatform
(),
}
// 从redis获取短信验证码
cache_code
,
err
:=
helper
.
GetCode
(
imUser
.
Mobile
)
if
err
!=
nil
{
//修改用户信息
func
(
u
*
ImUserServer
)
ImuserSave
(
ctx
context
.
Context
,
in
*
pb
.
ImUserRequest
)
(
reply
*
pb
.
ImUserSetReply
,
err
error
)
{
//查询用户是否存在
c_user
,
_
:=
GetImUserByAccidAppkey
(
in
.
GetAccid
(),
in
.
Common
.
Appkey
)
if
c_user
.
Token
==
""
{
err
=
status
.
Error
(
404
,
"用户不存在"
)
return
}
//修改
m
:=
make
(
map
[
string
]
interface
{})
if
in
.
Name
!=
""
{
m
[
"name"
]
=
in
.
Name
}
if
in
.
Icon
!=
""
{
m
[
"icon"
]
=
in
.
Icon
}
if
in
.
Sign
!=
""
{
m
[
"sign"
]
=
in
.
Sign
}
if
in
.
Email
!=
""
{
m
[
"email"
]
=
in
.
Email
}
if
in
.
Birth
!=
""
{
m
[
"birth"
]
=
in
.
Birth
}
if
in
.
Mobile
!=
""
{
m
[
"mobile"
]
=
in
.
Mobile
}
if
in
.
Gender
!=
0
{
m
[
"gender"
]
=
in
.
Gender
}
if
in
.
Ex
!=
""
{
m
[
"ex"
]
=
in
.
Ex
}
//对比验证码
if
cache_code
!=
imUser
.
Code
{
err
=
status
.
Error
(
302
,
"验证码不匹配"
)
if
res
,
_
:=
UpdateImUser
(
in
.
GetAccid
(),
in
.
Common
.
Appkey
,
m
);
res
!=
1
{
err
=
status
.
Error
(
500
,
"修改用户信息失败"
)
return
}
reply
=
&
pb
.
ImUserReply
{}
c_user
,
_
:=
GetImUserByMobile
(
imUser
.
Mobile
)
if
c_user
.
Accid
==
""
{
reply
=
&
pb
.
ImUserSetReply
{}
return
}
//用户登录
func
(
u
*
ImUserServer
)
ImUserLogin
(
ctx
context
.
Context
,
in
*
pb
.
ImUserLoginRequest
)
(
reply
*
pb
.
ImUserRegReply
,
err
error
)
{
//获取用户信息
c_user
,
_
:=
GetImUserByAccidAppkey
(
in
.
GetAccid
(),
in
.
Common
.
Appkey
)
if
c_user
.
Token
==
""
{
err
=
status
.
Error
(
404
,
"用户不存在"
)
return
}
//更新用户登录时间
_
,
err
=
LoginHandle
(
c_user
.
Id
,
i
mUser
.
Platform
)
_
,
err
=
LoginHandle
(
c_user
.
Id
,
i
n
.
GetPlatform
()
)
if
err
!=
nil
{
//err = status.Error(500, "用户登录时间更新失败")
return
}
reply
=
&
pb
.
ImUserRegReply
{}
reply
.
Token
=
c_user
.
Token
reply
.
Accid
=
c_user
.
Accid
reply
.
Name
=
c_user
.
Name
return
}
//用户退出
func
(
u
*
ImUserServer
)
ImUserLoginOut
(
ctx
context
.
Context
,
in
*
pb
.
ImUserOutRequest
)
(
reply
*
pb
.
ImUserSetReply
,
err
error
)
{
imUserOut
:=
&
ImUserQ
{
Accid
:
in
.
GetAccid
(),
Platform
:
in
.
GetPlatform
(),
}
reply
=
&
pb
.
ImUserSetReply
{}
c_user
,
_
:=
GetImUserByAccid
(
imUserOut
.
Accid
)
func
(
u
*
ImUserServer
)
ImUserLoginOut
(
ctx
context
.
Context
,
in
*
pb
.
ImUserLoginRequest
)
(
reply
*
pb
.
ImUserSetReply
,
err
error
)
{
c_user
,
_
:=
GetImUserByAccidAppkey
(
in
.
GetAccid
(),
in
.
Common
.
Appkey
)
if
c_user
.
Accid
==
""
{
err
=
status
.
Error
(
404
,
"用户不存在"
)
return
}
_
,
err
=
LoginOutHandle
(
c_user
.
Id
,
i
mUserOut
.
Platform
)
_
,
err
=
LoginOutHandle
(
c_user
.
Id
,
i
n
.
GetPlatform
()
)
if
err
!=
nil
{
err
=
status
.
Error
(
500
,
"数据库操作失败"
)
return
}
reply
=
&
pb
.
ImUserSetReply
{}
return
reply
,
nil
}
...
...
@@ -128,7 +138,7 @@ func (u *ImUserServer) ImUserForbiddenWords(ctx context.Context, in *pb.ImUserSe
mute
=
1
}
//检测该用户
c_user
,
err
:=
GetImUserByAccid
(
in
.
GetAccid
()
)
c_user
,
err
:=
GetImUserByAccid
Appkey
(
in
.
GetAccid
(),
in
.
Common
.
Appkey
)
if
err
!=
nil
{
err
=
status
.
Error
(
404
,
"该用户不存在"
)
return
...
...
@@ -140,7 +150,7 @@ func (u *ImUserServer) ImUserForbiddenWords(ctx context.Context, in *pb.ImUserSe
m
:=
make
(
map
[
string
]
interface
{})
m
[
"mute_estoppel"
]
=
mute
if
res
,
_
:=
UpdateImUser
(
in
.
GetAccid
(),
m
);
res
!=
1
{
if
res
,
_
:=
UpdateImUser
(
in
.
GetAccid
(),
in
.
Common
.
Appkey
,
m
);
res
!=
1
{
err
=
status
.
Error
(
500
,
"用户禁言失败"
)
return
}
...
...
@@ -155,7 +165,7 @@ func (u *ImUserServer) ImUserForbiddenAV(ctx context.Context, in *pb.ImUserSetRe
mute
=
1
}
//检测该用户
c_user
,
err
:=
GetImUserByAccid
(
in
.
GetAccid
()
)
c_user
,
err
:=
GetImUserByAccid
Appkey
(
in
.
GetAccid
(),
in
.
Common
.
Appkey
)
if
err
!=
nil
{
err
=
status
.
Error
(
404
,
"该用户不存在"
)
return
...
...
@@ -167,7 +177,7 @@ func (u *ImUserServer) ImUserForbiddenAV(ctx context.Context, in *pb.ImUserSetRe
m
:=
make
(
map
[
string
]
interface
{})
m
[
"mute_audio_video"
]
=
mute
if
res
,
_
:=
UpdateImUser
(
in
.
GetAccid
(),
m
);
res
!=
1
{
if
res
,
_
:=
UpdateImUser
(
in
.
GetAccid
(),
in
.
Common
.
Appkey
,
m
);
res
!=
1
{
err
=
status
.
Error
(
500
,
"用户禁音视频失败"
)
return
}
...
...
@@ -178,12 +188,9 @@ func (u *ImUserServer) ImUserForbiddenAV(ctx context.Context, in *pb.ImUserSetRe
//获取用户信息
func
(
u
*
ImUserServer
)
ImUserOne
(
ctx
context
.
Context
,
in
*
pb
.
ImUserOneRequest
)
(
reply
*
pb
.
ImUserOneReply
,
err
error
)
{
imUserId
:=
&
ImUserQ
{
Accid
:
in
.
GetAccid
(),
}
reply
=
&
pb
.
ImUserOneReply
{}
c_user
,
err
:=
GetImUserByAccid
(
imUserId
.
Accid
)
c_user
,
err
:=
GetImUserByAccid
Appkey
(
in
.
GetAccid
(),
in
.
Common
.
Appkey
)
if
err
!=
nil
{
return
}
...
...
@@ -195,12 +202,14 @@ func getImUser(imuser db.ImUser) *pb.ImUserOneReply {
return
&
pb
.
ImUserOneReply
{
Id
:
int64
(
imuser
.
Id
),
Accid
:
imuser
.
Accid
,
Appkey
:
imuser
.
Appkey
,
Name
:
imuser
.
Name
,
Mobile
:
imuser
.
Mobile
,
Disable
:
int64
(
imuser
.
Disable
),
Gender
:
imuser
.
Gender
,
Valid
:
int64
(
imuser
.
Valid
),
MuteEstoppel
:
int64
(
imuser
.
MuteEstoppel
),
MuteAudioVideo
:
int64
(
imuser
.
MuteAudioVideo
),
Ex
t
:
imuser
.
Ext
,
Ex
:
imuser
.
Ex
,
Createtime
:
imuser
.
Createtime
,
Edition
:
imuser
.
Edition
,
}
...
...
sevice/im_user/im_user_db.go
View file @
263bf26a
...
...
@@ -2,8 +2,8 @@ package im_user
import
(
"bytes"
"crypto/md5"
"encoding/hex"
//
"crypto/md5"
//
"encoding/hex"
"errors"
"fmt"
"im-microservice/db"
...
...
@@ -24,20 +24,30 @@ func CreateCaptcha() string {
return
fmt
.
Sprintf
(
"%04v"
,
rand
.
New
(
rand
.
NewSource
(
time
.
Now
()
.
UnixNano
()))
.
Int31n
(
10000
))
}
func
createAccid
()
string
{
func
randSeq
(
n
int
)
string
{
var
letters
=
[]
rune
(
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
b
:=
make
([]
rune
,
n
)
for
i
:=
range
b
{
b
[
i
]
=
letters
[
rand
.
Intn
(
len
(
letters
))]
}
return
string
(
b
)
}
func
createToken
()
string
{
var
buffer
bytes
.
Buffer
kid
:=
ksuid
.
New
()
buffer
.
WriteString
(
kid
.
String
())
buffer
.
WriteString
(
CreateCaptcha
())
h
:=
md5
.
New
()
h
.
Write
([]
byte
(
buffer
.
String
()))
return
hex
.
EncodeToString
(
h
.
Sum
(
nil
))
buffer
.
WriteString
(
randSeq
(
37
))
return
buffer
.
String
()
// h := md5.New()
// h.Write([]byte(buffer.String()))
// return hex.EncodeToString(h.Sum(nil))
}
//通过
手机号获取用户数据
func
GetImUserBy
Mobile
(
mobile
string
)
(
db
.
ImUser
,
error
)
{
//通过
accid和appkey获取用户信息
func
GetImUserBy
AccidAppkey
(
accid
string
,
appkey
string
)
(
db
.
ImUser
,
error
)
{
var
c_user
db
.
ImUser
err
:=
db
.
MysqlClient
.
QueryTable
(
u_table_name
)
.
Filter
(
"
mobile"
,
mobile
)
.
One
(
&
c_user
)
err
:=
db
.
MysqlClient
.
QueryTable
(
u_table_name
)
.
Filter
(
"
accid"
,
accid
)
.
Filter
(
"appkey"
,
appkey
)
.
One
(
&
c_user
)
if
err
!=
nil
{
return
c_user
,
err
}
...
...
@@ -45,12 +55,20 @@ func GetImUserByMobile(mobile string) (db.ImUser, error) {
}
//im用户注册
func
CreateImUser
(
name
string
,
mobile
string
)
(
db
.
ImUser
,
error
)
{
accid
:=
createAccid
()
func
CreateImUser
(
in
*
pb
.
ImUserRequest
)
(
db
.
ImUser
,
error
)
{
token
:=
createToken
()
var
c_user
db
.
ImUser
c_user
.
Accid
=
accid
c_user
.
Name
=
name
c_user
.
Mobile
=
mobile
c_user
.
Accid
=
in
.
GetAccid
()
c_user
.
Appkey
=
in
.
Common
.
Appkey
c_user
.
Name
=
in
.
GetName
()
c_user
.
Icon
=
in
.
GetIcon
()
c_user
.
Token
=
token
c_user
.
Sign
=
in
.
GetSign
()
c_user
.
Email
=
in
.
GetEmail
()
c_user
.
Birth
=
in
.
GetBirth
()
c_user
.
Mobile
=
in
.
GetMobile
()
c_user
.
Gender
=
in
.
GetGender
()
c_user
.
Ex
=
in
.
GetEx
()
c_user
.
Createtime
=
helper
.
GetNowTime
()
_
,
err
:=
db
.
MysqlClient
.
Insert
(
&
c_user
)
if
err
!=
nil
{
...
...
@@ -61,7 +79,7 @@ func CreateImUser(name string, mobile string) (db.ImUser, error) {
}
//修改用户信息
func
UpdateImUser
(
accid
string
,
m
map
[
string
]
interface
{})
(
int64
,
error
)
{
func
UpdateImUser
(
accid
string
,
appkey
string
,
m
map
[
string
]
interface
{})
(
int64
,
error
)
{
orm_params
:=
make
(
orm
.
Params
)
for
k
,
v
:=
range
m
{
switch
v
.
(
type
)
{
...
...
@@ -76,34 +94,21 @@ func UpdateImUser(accid string, m map[string]interface{}) (int64, error) {
orm_params
[
k
]
=
c_v
}
}
res
,
err
:=
db
.
MysqlClient
.
QueryTable
(
u_table_name
)
.
Filter
(
"accid"
,
accid
)
.
Update
(
orm_params
)
res
,
err
:=
db
.
MysqlClient
.
QueryTable
(
u_table_name
)
.
Filter
(
"accid"
,
accid
)
.
Filter
(
"appkey"
,
appkey
)
.
Update
(
orm_params
)
if
err
!=
nil
{
return
0
,
err
}
return
res
,
nil
}
//获取用户信息
func
GetImUserByAccid
(
accid
string
)
(
db
.
ImUser
,
error
)
{
var
c_user
db
.
ImUser
err
:=
db
.
MysqlClient
.
QueryTable
(
u_table_name
)
.
Filter
(
"accid"
,
accid
)
.
One
(
&
c_user
)
if
err
!=
nil
{
return
c_user
,
err
}
return
c_user
,
nil
}
//获取用户列表
func
GetImUserList
(
request
*
pb
.
ImUserListRequest
)
(
results
map
[
string
]
interface
{},
err
error
)
{
var
(
mobile
=
request
.
GetMobile
()
name
=
request
.
GetName
()
disable
=
request
.
GetDisable
()
valid
=
request
.
GetValid
()
mute_estoppel
=
request
.
GetMuteEstoppel
()
mute_audio_video
=
request
.
GetMuteAudioVideo
()
ext
=
request
.
GetExt
()
login_status
=
request
.
GetLoginStatus
()
platform
=
request
.
GetPlatform
()
page
=
request
.
GetPage
()
edition
=
request
.
GetEdition
()
)
...
...
@@ -117,8 +122,8 @@ func GetImUserList(request *pb.ImUserListRequest) (results map[string]interface{
if
mobile
!=
""
{
count_db
=
count_db
.
Filter
(
"mobile"
,
mobile
)
}
if
disable
!=
0
{
count_db
=
count_db
.
Filter
(
"
disable"
,
disable
)
if
valid
!=
0
{
count_db
=
count_db
.
Filter
(
"
valid"
,
valid
)
}
if
mute_estoppel
!=
0
{
count_db
=
count_db
.
Filter
(
"mute_estoppel"
,
mute_estoppel
)
...
...
@@ -126,15 +131,7 @@ func GetImUserList(request *pb.ImUserListRequest) (results map[string]interface{
if
mute_audio_video
!=
0
{
count_db
=
count_db
.
Filter
(
"mute_audio_video"
,
mute_audio_video
)
}
if
ext
!=
""
{
count_db
=
count_db
.
Filter
(
"ext__icontains"
,
ext
)
}
if
login_status
!=
0
{
count_db
=
count_db
.
Filter
(
"login_status"
,
login_status
)
}
if
platform
!=
""
{
count_db
=
count_db
.
Filter
(
"platform"
,
platform
)
}
if
edition
!=
""
{
count_db
=
count_db
.
Filter
(
"edition"
,
edition
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment