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
cae1938d
Commit
cae1938d
authored
Jul 01, 2020
by
李洪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
用户
parent
d5cdb5f2
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
495 additions
and
6 deletions
+495
-6
db.go
db/db.go
+4
-3
db_struct.go
db/db_struct.go
+17
-0
im_user_db.go
helper/im_user_db.go
+172
-0
main.go
main.go
+2
-0
common.pb.go
pb/common.pb.go
+4
-3
im_user.go
sevice/im_user/im_user.go
+207
-0
im_user.proto
u-proto/im_user.proto
+89
-0
No files found.
db/db.go
View file @
cae1938d
...
@@ -2,11 +2,12 @@ package db
...
@@ -2,11 +2,12 @@ package db
import
(
import
(
"fmt"
"fmt"
"github.com/astaxie/beego/orm"
"github.com/go-redis/redis/v8"
"log"
"log"
"sync"
"sync"
"github.com/astaxie/beego/orm"
"github.com/go-redis/redis/v8"
_
"github.com/go-sql-driver/mysql"
_
"github.com/go-sql-driver/mysql"
)
)
...
@@ -54,7 +55,7 @@ func newMysqlClient() {
...
@@ -54,7 +55,7 @@ func newMysqlClient() {
log
.
Println
(
"db connect error => "
,
err
)
log
.
Println
(
"db connect error => "
,
err
)
return
return
}
}
orm
.
RegisterModel
(
new
(
CompanyApp
),
new
(
ImBase
),
new
(
ImChatRoom
))
orm
.
RegisterModel
(
new
(
CompanyApp
),
new
(
ImBase
),
new
(
ImChatRoom
)
,
new
(
ImUser
)
)
MysqlClient
=
orm
.
NewOrm
()
MysqlClient
=
orm
.
NewOrm
()
log
.
Println
(
"MySQL connect success"
)
log
.
Println
(
"MySQL connect success"
)
}
}
db/db_struct.go
View file @
cae1938d
...
@@ -25,3 +25,20 @@ type ImChatRoom struct {
...
@@ -25,3 +25,20 @@ type ImChatRoom struct {
Createtime
string
Createtime
string
Updatetime
string
Updatetime
string
}
}
type
ImUser
struct
{
Id
int64
Appid
string
Appkey
string
Name
string
Mobile
string
Disable
int64
MuteEstoppel
int64
MuteAudioVideo
int64
Ext
string
Createtime
string
Updatetime
string
LoginStatus
int64
Platfrom
string
Edition
string
}
helper/im_user_db.go
0 → 100644
View file @
cae1938d
package
helper
import
(
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"im-microservice/db"
"im-microservice/pb"
"github.com/astaxie/beego/orm"
"github.com/segmentio/ksuid"
)
const
(
u_table_name
=
"im_user"
)
func
createAppid
()
string
{
id
:=
ksuid
.
New
()
h
:=
md5
.
New
()
h
.
Write
([]
byte
(
id
.
String
()))
return
hex
.
EncodeToString
(
h
.
Sum
(
nil
))
}
//通过手机号获取用户数据
func
GetImUserByMobile
(
mobile
string
)
(
db
.
ImUser
,
error
)
{
var
c_user
db
.
ImUser
fmt
.
Println
(
db
.
MysqlClient
)
err
:=
db
.
MysqlClient
.
QueryTable
(
u_table_name
)
.
Filter
(
"mobile"
,
mobile
)
.
One
(
&
c_user
)
if
err
!=
nil
{
return
c_user
,
err
}
return
c_user
,
nil
}
//im用户注册
func
CreateImUser
(
name
string
,
mobile
string
)
(
db
.
ImUser
,
error
)
{
appid
:=
createAppid
()
var
c_user
db
.
ImUser
c_user
.
Appid
=
appid
c_user
.
Name
=
name
c_user
.
Mobile
=
mobile
c_user
.
Createtime
=
GetNowTime
()
_
,
err
:=
db
.
MysqlClient
.
Insert
(
&
c_user
)
if
err
!=
nil
{
return
c_user
,
err
}
return
c_user
,
nil
}
//用户登录更新登录信息
func
SaveUpdatetime
(
mobile
string
,
platfrom
string
)
(
int64
,
error
)
{
updatetime
:=
GetNowTime
()
orm_params
:=
make
(
orm
.
Params
)
orm_params
[
"login_status"
]
=
1
orm_params
[
"updatetime"
]
=
updatetime
orm_params
[
"platfrom"
]
=
platfrom
res
,
err
:=
db
.
MysqlClient
.
QueryTable
(
u_table_name
)
.
Filter
(
"mobile"
,
mobile
)
.
Update
(
orm_params
)
if
err
!=
nil
{
return
0
,
err
}
return
res
,
nil
}
//用户退出
func
SaveLoginOut
(
appid
string
)
(
int64
,
error
)
{
orm_params
:=
make
(
orm
.
Params
)
orm_params
[
"login_status"
]
=
0
res
,
err
:=
db
.
MysqlClient
.
QueryTable
(
u_table_name
)
.
Filter
(
"appid"
,
appid
)
.
Update
(
orm_params
)
if
err
!=
nil
{
return
0
,
err
}
return
res
,
nil
}
//用户设置
func
SetImUserMute
(
appid
string
,
field
string
,
mute
bool
)
(
int64
,
error
)
{
orm_params
:=
make
(
orm
.
Params
)
if
mute
==
true
{
orm_params
[
field
]
=
1
}
else
{
orm_params
[
field
]
=
0
}
res
,
err
:=
db
.
MysqlClient
.
QueryTable
(
u_table_name
)
.
Filter
(
"appid"
,
appid
)
.
Update
(
orm_params
)
if
err
!=
nil
{
return
0
,
err
}
return
res
,
nil
}
//获取用户信息
func
GetImUserByAppid
(
appid
string
)
(
db
.
ImUser
,
error
)
{
var
c_user
db
.
ImUser
fmt
.
Println
(
db
.
MysqlClient
)
err
:=
db
.
MysqlClient
.
QueryTable
(
u_table_name
)
.
Filter
(
"appid"
,
appid
)
.
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
()
mute_estoppel
=
request
.
GetMuteEstoppel
()
mute_audio_video
=
request
.
GetMuteAudioVideo
()
ext
=
request
.
GetExt
()
login_status
=
request
.
GetLoginStatus
()
platfrom
=
request
.
GetPlatfrom
()
page
=
request
.
GetPage
()
edition
=
request
.
GetEdition
()
)
if
page
==
0
{
page
=
1
}
count_db
:=
db
.
MysqlClient
.
QueryTable
(
u_table_name
)
if
name
!=
""
{
count_db
=
count_db
.
Filter
(
"name__icontains"
,
name
)
}
if
mobile
!=
""
{
count_db
=
count_db
.
Filter
(
"mobile"
,
mobile
)
}
if
disable
!=
0
{
count_db
=
count_db
.
Filter
(
"disable"
,
disable
)
}
if
mute_estoppel
!=
0
{
count_db
=
count_db
.
Filter
(
"mute_estoppel"
,
mute_estoppel
)
}
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
platfrom
!=
""
{
count_db
=
count_db
.
Filter
(
"platfrom"
,
platfrom
)
}
if
edition
!=
""
{
count_db
=
count_db
.
Filter
(
"edition"
,
edition
)
}
ctn
,
err
:=
count_db
.
Count
()
if
err
!=
nil
{
return
}
if
ctn
==
0
{
err
=
errors
.
New
(
"没有查询到任何数据"
)
return
}
//获取
offset
,
total_page
:=
Paginate
(
ctn
,
20
,
page
)
// 查询数据
var
im_user
[]
db
.
ImUser
if
_
,
err
=
count_db
.
Limit
(
20
,
offset
)
.
All
(
&
im_user
);
err
!=
nil
{
return
}
results
=
make
(
map
[
string
]
interface
{})
results
[
"list"
]
=
im_user
results
[
"total_page"
]
=
total_page
results
[
"total_count"
]
=
ctn
results
[
"page"
]
=
page
results
[
"page_size"
]
=
20
return
}
main.go
View file @
cae1938d
...
@@ -5,6 +5,7 @@ import (
...
@@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/hex"
"errors"
"errors"
"fmt"
"fmt"
"im-microservice/sevice/im_user"
"log"
"log"
"net"
"net"
"regexp"
"regexp"
...
@@ -108,6 +109,7 @@ func main() {
...
@@ -108,6 +109,7 @@ func main() {
s
:=
grpc
.
NewServer
()
s
:=
grpc
.
NewServer
()
pb
.
RegisterConfigureSeviceServer
(
s
,
&
ic
.
ConfigureSevice
{})
pb
.
RegisterConfigureSeviceServer
(
s
,
&
ic
.
ConfigureSevice
{})
pb
.
RegisterChatRoomServiceServer
(
s
,
&
im_chat_room
.
ImChatRoomService
{})
pb
.
RegisterChatRoomServiceServer
(
s
,
&
im_chat_room
.
ImChatRoomService
{})
pb
.
RegisterImUserServer
(
s
,
&
im_user
.
ImUserServer
{})
if
err
:=
s
.
Serve
(
lis
);
err
!=
nil
{
if
err
:=
s
.
Serve
(
lis
);
err
!=
nil
{
log
.
Fatalf
(
"failed to serve: %v"
,
err
)
log
.
Fatalf
(
"failed to serve: %v"
,
err
)
}
}
...
...
pb/common.pb.go
View file @
cae1938d
// Code generated by protoc-gen-go. DO NOT EDIT.
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// versions:
// protoc-gen-go v1.2
4
.0
// protoc-gen-go v1.2
5
.0
// protoc v3.12.3
// protoc v3.12.3
// source: u-proto/common.proto
// source: u-proto/common.proto
package
pb
package
pb
import
(
import
(
reflect
"reflect"
sync
"sync"
proto
"github.com/golang/protobuf/proto"
proto
"github.com/golang/protobuf/proto"
protoreflect
"google.golang.org/protobuf/reflect/protoreflect"
protoreflect
"google.golang.org/protobuf/reflect/protoreflect"
protoimpl
"google.golang.org/protobuf/runtime/protoimpl"
protoimpl
"google.golang.org/protobuf/runtime/protoimpl"
reflect
"reflect"
sync
"sync"
)
)
const
(
const
(
...
...
sevice/im_user/im_user.go
0 → 100644
View file @
cae1938d
package
im_user
import
(
"context"
"errors"
"im-microservice/db"
"im-microservice/helper"
"im-microservice/pb"
)
type
ImUserServer
struct
{
pb
.
UnimplementedImUserServer
}
type
ImUser
struct
{
Name
string
`validate:"required"`
Mobile
string
`validate:"required,numeric,len=11"`
Code
string
`validate:"required"`
Platfrom
string
`validate:"required"`
}
type
ImUserSet
struct
{
Appid
string
`validate:"required"`
Mute
bool
`validate:"required"`
}
type
ImUserQ
struct
{
Appid
string
`validate:"required"`
}
//注册
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
(),
}
// 参数验证是否合法
if
err
=
helper
.
Valiator
(
imUser
);
err
!=
nil
{
return
}
// 从redis获取短信验证码
cache_code
,
err
:=
helper
.
GetCode
(
imUser
.
Mobile
)
if
err
!=
nil
{
return
}
//对比验证码
if
cache_code
!=
imUser
.
Code
{
err
=
errors
.
New
(
"验证码不匹配"
)
return
}
reply
=
&
pb
.
ImUserReply
{}
// 是否已经注册过, 如果已经注册返回用户信息
if
c_user
,
_
:=
helper
.
GetImUserByMobile
(
imUser
.
Mobile
);
c_user
.
Appid
!=
""
{
reply
.
Appid
=
c_user
.
Appid
return
}
// 企业用户注册失败
c_user
,
err
:=
helper
.
CreateImUser
(
imUser
.
Name
,
imUser
.
Mobile
)
if
err
!=
nil
{
return
}
reply
.
Appid
=
c_user
.
Appid
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
(),
Platfrom
:
in
.
GetPlatfrom
(),
}
// 参数验证是否合法
if
err
=
helper
.
Valiator
(
imUser
);
err
!=
nil
{
return
}
// 从redis获取短信验证码
cache_code
,
err
:=
helper
.
GetCode
(
imUser
.
Mobile
)
if
err
!=
nil
{
return
}
//对比验证码
if
cache_code
!=
imUser
.
Code
{
err
=
errors
.
New
(
"验证码不匹配"
)
return
}
reply
=
&
pb
.
ImUserReply
{}
c_user
,
_
:=
helper
.
GetImUserByMobile
(
imUser
.
Mobile
)
if
c_user
.
Appid
==
""
{
err
=
errors
.
New
(
"用户不存在"
)
return
}
//更新用户登录时间
if
res
,
_
:=
helper
.
SaveUpdatetime
(
imUser
.
Mobile
,
imUser
.
Platfrom
);
res
!=
1
{
err
=
errors
.
New
(
"用户登录时间更新失败"
)
return
}
reply
.
Appid
=
c_user
.
Appid
return
}
//用户退出
func
(
u
*
ImUserServer
)
ImUserLoginOut
(
ctx
context
.
Context
,
in
*
pb
.
ImUserOutRequest
)
(
reply
*
pb
.
ImUserSetReply
,
err
error
)
{
imUserOut
:=
&
ImUserQ
{
Appid
:
in
.
GetAppid
(),
}
if
res
,
_
:=
helper
.
SaveLoginOut
(
imUserOut
.
Appid
);
res
!=
1
{
err
=
errors
.
New
(
"用户更改登录状态信息失败"
)
return
}
reply
=
&
pb
.
ImUserSetReply
{}
return
}
//用户禁言
func
(
u
*
ImUserServer
)
ImUserForbiddenWords
(
ctx
context
.
Context
,
in
*
pb
.
ImUserSetRequest
)
(
reply
*
pb
.
ImUserSetReply
,
err
error
)
{
imUserSet
:=
&
ImUserSet
{
Appid
:
in
.
GetAppid
(),
Mute
:
in
.
GetMute
(),
}
if
res
,
_
:=
helper
.
SetImUserMute
(
imUserSet
.
Appid
,
"mute_estoppel"
,
imUserSet
.
Mute
);
res
!=
1
{
err
=
errors
.
New
(
"用户禁言失败"
)
return
}
reply
=
&
pb
.
ImUserSetReply
{}
return
}
//用户禁音视频
func
(
u
*
ImUserServer
)
ImUserForbiddenAV
(
ctx
context
.
Context
,
in
*
pb
.
ImUserSetRequest
)
(
reply
*
pb
.
ImUserSetReply
,
err
error
)
{
imUserSet
:=
&
ImUserSet
{
Appid
:
in
.
GetAppid
(),
Mute
:
in
.
GetMute
(),
}
if
res
,
_
:=
helper
.
SetImUserMute
(
imUserSet
.
Appid
,
"mute_audio_video"
,
imUserSet
.
Mute
);
res
!=
1
{
err
=
errors
.
New
(
"用户禁言失败"
)
return
}
reply
=
&
pb
.
ImUserSetReply
{}
return
}
//获取用户信息
func
(
u
*
ImUserServer
)
ImUserOne
(
ctx
context
.
Context
,
in
*
pb
.
ImUserOneRequest
)
(
reply
*
pb
.
ImUserOneReply
,
err
error
)
{
imUserId
:=
&
ImUserQ
{
Appid
:
in
.
GetAppid
(),
}
// 参数验证是否合法
if
err
=
helper
.
Valiator
(
imUserId
);
err
!=
nil
{
return
}
reply
=
&
pb
.
ImUserOneReply
{}
c_user
,
err
:=
helper
.
GetImUserByAppid
(
imUserId
.
Appid
)
if
err
!=
nil
{
return
}
reply
=
getImUser
(
c_user
)
return
}
func
getImUser
(
imuser
db
.
ImUser
)
*
pb
.
ImUserOneReply
{
return
&
pb
.
ImUserOneReply
{
Id
:
int64
(
imuser
.
Id
),
Appid
:
imuser
.
Appid
,
Name
:
imuser
.
Name
,
Mobile
:
imuser
.
Mobile
,
Disable
:
int64
(
imuser
.
Disable
),
MuteEstoppel
:
int64
(
imuser
.
MuteEstoppel
),
MuteAudioVideo
:
int64
(
imuser
.
MuteAudioVideo
),
Ext
:
imuser
.
Ext
,
Createtime
:
imuser
.
Createtime
,
Updatetime
:
imuser
.
Updatetime
,
LoginStatus
:
int64
(
imuser
.
LoginStatus
),
Platfrom
:
imuser
.
Platfrom
,
Edition
:
imuser
.
Edition
,
}
}
//获取用户列表
func
(
u
*
ImUserServer
)
ImUserList
(
ctx
context
.
Context
,
in
*
pb
.
ImUserListRequest
)
(
reply
*
pb
.
ImUserListReply
,
err
error
)
{
results
,
err
:=
helper
.
GetImUserList
(
in
)
if
err
!=
nil
{
return
}
reply
=
&
pb
.
ImUserListReply
{
Paginate
:
&
pb
.
Page
{}}
im_user_list
:=
results
[
"list"
]
.
([]
db
.
ImUser
)
for
_
,
v
:=
range
im_user_list
{
reply
.
List
=
append
(
reply
.
List
,
getImUser
(
v
))
}
reply
.
Paginate
.
PageSize
=
int64
(
results
[
"page_size"
]
.
(
int
))
reply
.
Paginate
.
PagetNo
=
results
[
"page"
]
.
(
int64
)
reply
.
Paginate
.
TotalCount
=
results
[
"total_count"
]
.
(
int64
)
reply
.
Paginate
.
TotalPage
=
results
[
"total_page"
]
.
(
int64
)
return
}
u-proto/im_user.proto
0 → 100644
View file @
cae1938d
syntax
=
"proto3"
;
package
pb
;
import
"u-proto/common.proto"
;
//注册登录
message
ImUserRequest
{
Common
Common
=
1
;
string
Name
=
2
;
string
Mobile
=
3
;
string
Code
=
4
;
string
Platfrom
=
5
;
}
message
ImUserReply
{
string
Appid
=
2
;
}
//用户退出
message
ImUserOutRequest
{
Common
Common
=
1
;
string
Appid
=
2
;
}
//用户设置禁言,禁音视频
message
ImUserSetRequest
{
Common
Common
=
1
;
bool
Mute
=
2
;
string
Appid
=
3
;
}
message
ImUserSetReply
{}
//获取单个用户信息
message
ImUserOneRequest
{
Common
Common
=
1
;
string
Appid
=
2
;
}
message
ImUserOneReply
{
int64
Id
=
1
;
string
Appid
=
2
;
string
Name
=
3
;
string
Mobile
=
4
;
int64
Disable
=
5
;
int64
MuteEstoppel
=
6
;
int64
MuteAudioVideo
=
7
;
string
Ext
=
8
;
string
Createtime
=
9
;
string
Updatetime
=
10
;
int64
LoginStatus
=
11
;
string
Platfrom
=
12
;
string
Edition
=
13
;
}
//获取用户列表
message
ImUserListRequest
{
Common
Common
=
1
;
int64
Page
=
2
;
string
Mobile
=
3
;
string
Name
=
4
;
int64
Disable
=
5
;
int64
MuteEstoppel
=
6
;
int64
MuteAudioVideo
=
7
;
string
Ext
=
8
;
string
Createtime
=
9
;
string
Updatetime
=
10
;
int64
LoginStatus
=
11
;
string
Platfrom
=
12
;
string
Edition
=
13
;
}
message
ImUserListReply
{
repeated
ImUserOneReply
List
=
1
;
Page
Paginate
=
2
;
}
service
ImUser
{
rpc
ImUserRegister
(
ImUserRequest
)
returns
(
ImUserReply
)
{}
rpc
ImUserLogin
(
ImUserRequest
)
returns
(
ImUserReply
)
{}
rpc
ImUserLoginOut
(
ImUserOutRequest
)
returns
(
ImUserSetReply
)
{}
rpc
ImUserForbiddenWords
(
ImUserSetRequest
)
returns
(
ImUserSetReply
)
{}
rpc
ImUserForbiddenAV
(
ImUserSetRequest
)
returns
(
ImUserSetReply
)
{}
rpc
ImUserOne
(
ImUserOneRequest
)
returns
(
ImUserOneReply
)
{}
rpc
ImUserList
(
ImUserListRequest
)
returns
(
ImUserListReply
){}
}
\ No newline at end of file
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