364 lines
12 KiB
Go
364 lines
12 KiB
Go
/*
|
|
* @Description: In User Settings Edit
|
|
* @Author: your name
|
|
* @Date: 2019-08-28 17:35:29
|
|
* @LastEditTime: 2019-08-30 11:25:44
|
|
* @LastEditors: Please set LastEditors
|
|
*/
|
|
package versionmod
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"91porn-server/models"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
var mdb *db.MongoDB
|
|
|
|
const (
|
|
Normal = 0
|
|
TF = 1 //ios 测试包
|
|
VESTBAG = 2 //马甲包
|
|
|
|
Ios = "ios"
|
|
Adr = "android"
|
|
H5 = "h5"
|
|
)
|
|
|
|
// VersionBody request body
|
|
type VersionBody struct {
|
|
Code int64 `json:"code" bson:"code"` //排序字段
|
|
VersionName string `json:"verName" bson:"verName"` //版本名字
|
|
Platform string `json:"platform" bson:"platform"` //平台 ios
|
|
Description string `json:"description" bson:"description"` //描述
|
|
ForcedUpdate bool `json:"forcedUpdate" bson:"ForcedUpdate"` //是否强制更新
|
|
URL string `json:"url" bson:"url"` //下载链接
|
|
IosUrl string `json:"iosUrl" bson:"iosUrl"` //商店包下载链接
|
|
}
|
|
|
|
// Version 版本检测表
|
|
type Version struct {
|
|
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` //id
|
|
Code int64 `json:"code" bson:"code"` //排序字段
|
|
VersionName string `json:"verName" bson:"verName"` //版本名字
|
|
Platform string `json:"platform" bson:"platform" ` //平台 ios
|
|
Description string `json:"description" bson:"description"` //描述
|
|
URL string `json:"url" bson:"url"` //下载链接
|
|
ForcedUpdate bool `json:"forcedUpdate" bson:"forcedUpdate"` //是否强制更新
|
|
IsActive bool `json:"isActive" bson:"isActive"` //是否启用
|
|
BuildID string `json:"buildId" bson:"buildId"` //包安装ID
|
|
PassCheck bool `json:"passCheck" bson:"passCheck"` //是否提审通过
|
|
Origin int64 `json:"origin" bson:"origin"` //表示来源 0 表示 正常 1 TF版
|
|
IsUpgrade bool `json:"isUpgrade" bson:"isUpgrade"` //是否是当前版本升级到新版
|
|
SpecVersion string `json:"specVersion" bson:"specVersion"` //指定升级的版本
|
|
CreatedAt time.Time `form:"createdAt" json:"createdAt" bson:"createdAt"` //记录创建时间
|
|
VersionRange VersionRange `json:"versionRange" bson:"versionRange"` //版本范围 适用于版本版本升级的范围 如果全部为空 则表示全部版本都可升级
|
|
}
|
|
|
|
type VersionRange struct {
|
|
Major string `json:"major" bson:"major"`
|
|
Minor string `json:"minor" bson:"minor"`
|
|
}
|
|
|
|
// VersionUpdateReq 版本检测表
|
|
type VersionUpdateReq struct {
|
|
Platform *string `json:"platform" bson:"platform,omitempty" ` //平台 ios
|
|
Description *string `json:"description" bson:"description,omitempty"` //描述
|
|
URL *string `json:"url" form:"url" bson:"url,omitempty"` //下载链接
|
|
ForcedUpdate *bool `json:"forcedUpdate" bson:"forcedUpdate,omitempty"` //是否强制更新
|
|
IsActive *bool `json:"isActive" bson:"isActive,omitempty"` //是否启用
|
|
BuildID *string `json:"buildId" bson:"buildId,omitempty"` //包安装ID
|
|
PassCheck *bool `json:"passCheck" bson:"passCheck,omitempty"` //是否提审通过
|
|
Origin *int64 `json:"origin,omitempty" bson:"origin,omitempty"` //表示来源 0 表示 正常 1 TF版
|
|
IsUpgrade *bool `json:"isUpgrade" bson:"isUpgrade"` //是否是当前版本升级到新版
|
|
SpecVersion *string `json:"specVersion" bson:"specVersion,omitempty"` //指定升级的版本
|
|
VersionRange VersionRange `json:"versionRange" bson:"versionRange"`
|
|
}
|
|
|
|
// VersionQueryReq 查询参数
|
|
type VersionQueryReq struct {
|
|
ID *string `form:"id" json:"_id,omitempty" bson:"_id"` //id
|
|
VersionName *string `form:"verName" json:"verName,omitempty" bson:"verName"` //版本名字
|
|
Platform *string `form:"platform" json:"platform,omitempty" bson:"platform" ` //平台 ios
|
|
Description *string `form:"description" json:"description,omitempty" bson:"description"` //描述
|
|
URL *string `form:"url" json:"url,omitempty" bson:"url"` //下载链接
|
|
ForcedUpdate *bool `form:"forcedUpdate" json:"forcedUpdate,omitempty" bson:"forcedUpdate"` //是否强制更新
|
|
}
|
|
|
|
func Init() {
|
|
mdb = db.Init(table)
|
|
initIndex()
|
|
}
|
|
|
|
const table = models.Version
|
|
|
|
// InitIndex 设置index
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "verName", Value: 1}, {Key: "buildId", Value: 1}, {Key: "platform", Value: 1}},
|
|
Options: options.Index().SetUnique(true).SetPartialFilterExpression(bson.M{"buildId": bson.M{"$gt": ""}}),
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "buildId", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "origin", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "platform", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "isActive", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "createdAt", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll().CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
func coll() *db.MongoTool {
|
|
return mdb.Coll(table)
|
|
}
|
|
|
|
func insertCheck(nv *Version) error {
|
|
ov, err := FindVersion(nv.Platform)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if nv.Code >= ov.Code {
|
|
return nil
|
|
}
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "insertCheck", table, "errors.New", err),
|
|
log.Any("nv", nv),
|
|
)
|
|
return errors.New("version is too low")
|
|
}
|
|
|
|
// Insert 插入一条记录
|
|
func Insert(v *Version) error {
|
|
if err := insertCheck(v); err != nil {
|
|
return err
|
|
}
|
|
v.CreatedAt = time.Now()
|
|
if _, err := coll().InsertOne(v); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err),
|
|
log.Any("v", v),
|
|
)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Delete 删除
|
|
func Delete(id primitive.ObjectID) error {
|
|
if _, err := coll().DeleteById(id); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Delete", table, "DeleteById", err),
|
|
log.Any("id", id),
|
|
)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Update 修改
|
|
func Update(id primitive.ObjectID, set VersionUpdateReq) error {
|
|
if _, err := coll().UpdateOne(bson.M{"_id": id}, bson.M{"$set": set}); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Update", table, "UpdateOne", err),
|
|
log.Any("id", id),
|
|
log.Any("set", set),
|
|
)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// FindMany 查找所有版本
|
|
func FindMany(filter bson.M, opts *options.FindOptions) (total int64, data []*Version, err error) {
|
|
if err = coll().Find(&data, filter, opts); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindMany", table, "Find", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return
|
|
}
|
|
total, err = coll().Count(filter)
|
|
if err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindMany", table, "Count", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return
|
|
}
|
|
if total == 0 {
|
|
data = make([]*Version, 0)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// Verlist 查找所有版本
|
|
func Verlist() (data []*Version) {
|
|
var infos []*Version
|
|
filter := bson.M{"isActive": true, "origin": 0} //排除掉 ios tf 版的
|
|
if err := coll().Find(&infos, filter); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Verlist", table, "Find", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return
|
|
}
|
|
var maxIOS, maxAn, maxH5 Version
|
|
data = make([]*Version, 0, len(infos)*3)
|
|
for _, v := range infos {
|
|
if v == nil {
|
|
continue
|
|
}
|
|
if v.Platform == Ios && v.Code > maxIOS.Code {
|
|
maxIOS = *v
|
|
}
|
|
if v.Platform == Adr && v.Code > maxAn.Code {
|
|
maxAn = *v
|
|
}
|
|
if v.Platform == H5 && v.Code > maxH5.Code {
|
|
maxH5 = *v
|
|
}
|
|
}
|
|
data = append(data, &maxIOS)
|
|
data = append(data, &maxAn)
|
|
data = append(data, &maxH5)
|
|
return
|
|
}
|
|
|
|
/**
|
|
* @description: FindVersion 查找最新版本信息
|
|
* @param {type}
|
|
* @return:
|
|
*/
|
|
func FindVersion(pName string) (data Version, err error) {
|
|
opts := options.FindOneOptions{
|
|
Sort: bson.D{{Key: "code", Value: -1}},
|
|
}
|
|
if err = coll().FindOne(&data, bson.M{"platform": pName, "origin": 0, "isActive": true}, &opts); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindVersion", table, "FindOne", err),
|
|
log.Any("pName", pName),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func FindVersionExcluedBuildId(pName string) (data Version, err error) {
|
|
opts := options.FindOneOptions{
|
|
Sort: bson.D{{Key: "code", Value: -1}},
|
|
}
|
|
if err = coll().FindOne(&data, bson.M{"platform": pName, "origin": 0, "isActive": true}, &opts); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindVersionExcluedBuildId", table, "FindOne", err),
|
|
log.Any("pName", pName),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 是否通过提包审核
|
|
func CheckPass(ver, buildId string) (pass bool, err error) {
|
|
data := Version{}
|
|
if err = coll().FindOne(&data, bson.M{"verName": ver, "buildId": buildId}); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "CheckPass", table, "FindOne", err),
|
|
log.Any("ver", ver),
|
|
log.Any("buildId", buildId),
|
|
)
|
|
return
|
|
}
|
|
return data.PassCheck, nil
|
|
}
|
|
|
|
func FindVersionBaseOnBuildId(buildId string) (data *Version, err error) {
|
|
opts := options.FindOneOptions{
|
|
Sort: bson.D{{Key: "code", Value: -1}},
|
|
}
|
|
if err = coll().FindOne(&data, bson.M{"buildId": buildId, "isActive": true, "passCheck": true}, &opts); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindVersionBaseOnBuildId", table, "FindOne", err),
|
|
log.Any("buildId", buildId),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 根据平台buildId 查找版本信息 目的是确认当前buildId 对应的包是否是IOS TF包
|
|
func FindVersionBaseOnBuildIdAndPlatForm(buildId, platform string) (data *Version, err error) {
|
|
opts := options.FindOneOptions{
|
|
Sort: bson.D{{Key: "code", Value: -1}},
|
|
}
|
|
if err = coll().FindOne(&data, bson.M{"buildId": buildId, "platform": platform, "isActive": true}, &opts); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindVersionBaseOnBuildIdAndPlatForm", table, "FindOne", err),
|
|
log.Any("buildId", buildId),
|
|
log.Any("platform", platform),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 根据平台 版本 还有 build 查看当前版本呢信息
|
|
func FindOneVersionByPlatVerBuild(platform, ver, buildId string) (data *Version, err error) {
|
|
if err = coll().FindOne(&data, bson.M{"platform": platform, "verName": ver, "buildId": buildId}); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOneVersionByPlatVerBuild", table, "FindOne", err),
|
|
log.Any("platform", platform),
|
|
log.Any("verName", ver),
|
|
log.Any("buildId", buildId),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 根据平台和版本 查找版本信息 限于正常版
|
|
func FindOneVersionByPlatVer(platform, ver string) (data *Version, err error) {
|
|
if err = coll().FindOne(&data, bson.M{"platform": platform, "verName": ver, "origin": 0}); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOneVersionByPlatVer", table, "FindOne", err),
|
|
log.Any("platform", platform),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func FindOneByID(id primitive.ObjectID) (data *Version, err error) {
|
|
if err = coll().FindOne(&data, bson.M{"_id": id}); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOneByID", table, "FindOne", err),
|
|
log.Any("id", id),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// FindTF 查找所有版本
|
|
func FindTF() (data []*Version) {
|
|
var limit int64 = 1
|
|
opt := options.FindOptions{
|
|
Limit: &limit,
|
|
Sort: bson.D{{Key: "code", Value: -1}},
|
|
}
|
|
filter := bson.M{"isActive": true, "origin": 1} //排除掉 ios tf 版的
|
|
if err := coll().Find(&data, filter, &opt); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Verlist", table, "Find", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|