Commit f86d4ced by 李洪

a

parent d368bddf
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
}
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