Commit 23ab5719 by Li Feifei

解决冲突

parents 573d7fa4 f86d4ced
......@@ -2,11 +2,12 @@ package db
import (
"fmt"
"github.com/astaxie/beego/orm"
"github.com/go-redis/redis/v8"
"log"
"sync"
"github.com/astaxie/beego/orm"
"github.com/go-redis/redis/v8"
_ "github.com/go-sql-driver/mysql"
)
......@@ -54,7 +55,9 @@ func newMysqlClient() {
log.Println("db connect error => ", err)
return
}
orm.RegisterModel(new(CompanyApp), new(ImBase), new(ImChatRoom), new(ImUserRelationship))
orm.RegisterModel(new(CompanyApp), new(ImBase), new(ImChatRoom), new(ImUserRelationship), new(ImUser))
MysqlClient = orm.NewOrm()
log.Println("MySQL connect success")
}
......@@ -26,7 +26,6 @@ type ImChatRoom struct {
Updatetime string
}
type ImUserRelationship struct {
Id int
Accid string
......@@ -39,3 +38,20 @@ type ImUserRelationship struct {
Createtime string
Updatetime string
}
type ImUser struct {
Id int64
Accid string
Appkey string
Name string
Mob adile string
Disable int64
MuteEstoppel int64
MuteAudioVideo int64
Ext string
Createtime string
Updatetime string
LoginStatus int64
Platfrom string
Edition string
}
......@@ -5,6 +5,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"im-microservice/sevice/im_user"
"log"
"net"
"regexp"
......@@ -108,6 +109,7 @@ func main() {
s := grpc.NewServer()
pb.RegisterConfigureSeviceServer(s, &ic.ConfigureSevice{})
pb.RegisterChatRoomServiceServer(s, &im_chat_room.ImChatRoomService{})
pb.RegisterImUserServer(s, &im_user.ImUserServer{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
......
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.24.0
// protoc-gen-go v1.25.0
// protoc v3.12.3
// source: u-proto/common.proto
package pb
import (
reflect "reflect"
sync "sync"
proto "github.com/golang/protobuf/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
......
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, _ := 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, _ := GetImUserByMobile(imUser.Mobile)
if c_user.Appid == "" {
err = errors.New("用户不存在")
return
}
//更新用户登录时间
if res, _ := 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, _ := 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, _ := 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, _ := 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 := 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 := 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
}
package im_user
import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"im-microservice/db"
"im-microservice/helper"
"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 = helper.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 := helper.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 := helper.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
}
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment