parent
80f3cfda74
commit
faaf0a7178
@ -0,0 +1,82 @@
|
||||
// Copyright © 2024 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||||
"github.com/openimsdk/tools/apiresp"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
)
|
||||
|
||||
type PhoneSNApi struct {
|
||||
db database.PhoneSN
|
||||
}
|
||||
|
||||
func NewPhoneSNApi(db database.PhoneSN) *PhoneSNApi {
|
||||
return &PhoneSNApi{db: db}
|
||||
}
|
||||
|
||||
type phoneGetSNInfoReq struct {
|
||||
Phone string `json:"phone" binding:"required"`
|
||||
}
|
||||
|
||||
type phoneGetSNInfoResp struct {
|
||||
IsSnd bool `json:"is_snd"`
|
||||
UserID int64 `json:"userID"`
|
||||
}
|
||||
|
||||
type phoneSetSNInfoReq struct {
|
||||
Phone string `json:"phone" binding:"required"`
|
||||
UserID int64 `json:"userID"`
|
||||
IsSnd bool `json:"is_snd"`
|
||||
}
|
||||
|
||||
// GetSNInfo POST /phone/get_sn_info
|
||||
func (a *PhoneSNApi) GetSNInfo(c *gin.Context) {
|
||||
var req phoneGetSNInfoReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
apiresp.GinError(c, errs.ErrArgs.WrapMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
phone := strings.TrimSpace(req.Phone)
|
||||
if phone == "" {
|
||||
apiresp.GinError(c, errs.ErrArgs.WrapMsg("phone is empty"))
|
||||
return
|
||||
}
|
||||
info, err := a.db.GetByPhone(c, phone)
|
||||
if err != nil {
|
||||
apiresp.GinError(c, err)
|
||||
return
|
||||
}
|
||||
resp := phoneGetSNInfoResp{IsSnd: false, UserID: 0}
|
||||
if info != nil {
|
||||
resp.IsSnd = info.IsSnd
|
||||
resp.UserID = info.UserID
|
||||
}
|
||||
apiresp.GinSuccess(c, resp)
|
||||
}
|
||||
|
||||
// SetSNInfo POST /phone/set_sn_info
|
||||
func (a *PhoneSNApi) SetSNInfo(c *gin.Context) {
|
||||
var req phoneSetSNInfoReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
apiresp.GinError(c, errs.ErrArgs.WrapMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
phone := strings.TrimSpace(req.Phone)
|
||||
if phone == "" {
|
||||
apiresp.GinError(c, errs.ErrArgs.WrapMsg("phone is empty"))
|
||||
return
|
||||
}
|
||||
if err := a.db.Upsert(c, phone, req.UserID, req.IsSnd); err != nil {
|
||||
apiresp.GinError(c, err)
|
||||
return
|
||||
}
|
||||
apiresp.GinSuccess(c, nil)
|
||||
}
|
||||
@ -0,0 +1,69 @@
|
||||
// Copyright © 2024 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
||||
package mgo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||||
"github.com/openimsdk/tools/db/mongoutil"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func NewPhoneSNMongo(db *mongo.Database) (database.PhoneSN, error) {
|
||||
coll := db.Collection(database.PhoneSNInfoName)
|
||||
_, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{
|
||||
Keys: bson.D{{Key: "phone", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errs.Wrap(err)
|
||||
}
|
||||
return &phoneSNMgo{coll: coll}, nil
|
||||
}
|
||||
|
||||
type phoneSNMgo struct {
|
||||
coll *mongo.Collection
|
||||
}
|
||||
|
||||
func (p *phoneSNMgo) GetByPhone(ctx context.Context, phone string) (*model.PhoneSNInfo, error) {
|
||||
if phone == "" {
|
||||
return nil, nil
|
||||
}
|
||||
doc, err := mongoutil.FindOne[*model.PhoneSNInfo](ctx, p.coll, bson.M{"phone": phone})
|
||||
if err != nil {
|
||||
if errs.ErrRecordNotFound.Is(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (p *phoneSNMgo) Upsert(ctx context.Context, phone string, userID int64, isSnd bool) error {
|
||||
if phone == "" {
|
||||
return errs.ErrArgs.WrapMsg("phone is empty")
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
filter := bson.M{"phone": phone}
|
||||
setDoc := bson.M{
|
||||
"is_snd": isSnd,
|
||||
"user_id": userID,
|
||||
"update_time": now,
|
||||
}
|
||||
update := bson.M{
|
||||
"$set": setDoc,
|
||||
"$setOnInsert": bson.M{"phone": phone},
|
||||
}
|
||||
opts := options.Update().SetUpsert(true)
|
||||
_, err := p.coll.UpdateOne(ctx, filter, update, opts)
|
||||
return errs.Wrap(err)
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
// Copyright © 2024 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||||
)
|
||||
|
||||
// PhoneSN 手机号 is_snd 持久化
|
||||
type PhoneSN interface {
|
||||
// GetByPhone 按手机号查询;无记录时返回 (nil, nil)
|
||||
GetByPhone(ctx context.Context, phone string) (*model.PhoneSNInfo, error)
|
||||
// Upsert 写入或更新 is_snd 与 user_id
|
||||
Upsert(ctx context.Context, phone string, userID int64, isSnd bool) error
|
||||
}
|
||||
@ -1 +1 @@
|
||||
Subproject commit 91e8d26ed5b8ebd2711483bb558e797fa40f4612
|
||||
Subproject commit 9f0b38eb5c5015da3969d6711a140c3ba12956bb
|
||||
Loading…
Reference in new issue