DealyMessage.go 5.23 KB
package Api

import (
	"github.com/aarongao/tools"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/push"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"letu/Config"
	"letu/DB"
	"letu/Lib/Auth"
	"letu/Lib/DelayMessage"
)

// @Title 查询用户的定时提醒
// @Description 查询用户的定时提醒
// @Accept  json
// @Produce  json
// @Param   UserId     5dfb03070a9ac17ac7a82054    string     true        "用户id"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Success 200 {object} tools.ResponseSeccess "DelayTime=执行时间;Type=类型(0请求url地址1发送app通知);Fail失败次数;Title=通知标题;Content=通知内容;DeviceToken=设备id"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /DealyMessage/Info? [get]
func DealyMessageInfo(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	_user, _ := c.Get("UserInfo")
	user := _user.(*DB.SMember)

	err := Auth.CheckUserAuth(c.Query("UserId"), user)
	if err != nil {
		c.JSON(200, tools.ResponseError{
			401,
			"没有权限",
		})
		return
	}

	var aDelayMessage []DelayMessage.Message
	cur, err := DelayMessage.CDelayMessage.Find(tools.GetContext(), bson.M{"UserId": c.Query("UserId")})
	defer cur.Close(tools.GetContext())
	if err == nil {
		for cur.Next(tools.GetContext()) {
			var e DelayMessage.Message
			cur.Decode(&e)
			aDelayMessage = append(aDelayMessage, e)
		}
	}

	if aDelayMessage == nil {
		aDelayMessage = []DelayMessage.Message{}
	}
	c.JSON(200, tools.ResponseSeccess{
		0,
		aDelayMessage,
	})
}

// @Title 创建提醒
// @Description 创建提醒
// @Accept  json
// @Produce  json
// @Param   UserId     5dfb03070a9ac17ac7a82054    string     true        "用户id"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Param   DeviceToken     5dfb03070a9ac17ac7a82054    string     true        "设备id"
// @Param   Title     表演时间提醒    string     true        "标题"
// @Param   Content     5分钟后有表演    string     true        "内容"
// @Param   DelayTime     1579066863    string     true        "到达这个时间戳就执行"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /DealyMessage/Create? [post]
func CreateDealyMessage(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")


	_user, _ := c.Get("UserInfo")
	user := _user.(*DB.SMember)

	err := Auth.CheckUserAuth(c.PostForm("UserId"), user)
	if err != nil {
		c.JSON(200, tools.ResponseError{
			401,
			"没有权限",
		})
		return
	}

	_, err = primitive.ObjectIDFromHex(c.PostForm("UserId"))
	if err != nil {
		c.JSON(200, tools.ResponseError{
			1,
			"id不正确",
		})
		return
	}

	err = DelayMessage.GlobalDM.AddTaskForAppMessage(c.PostForm("DelayTime"), c.PostForm("DeviceToken"), c.PostForm("Title"), c.PostForm("Content"), c.PostForm("UserId"))

	if err == nil {

		c.JSON(200, tools.ResponseSeccess{
			0,
			"ok",
		})
	} else {

		c.JSON(200, tools.ResponseError{
			1,
			err.Error(),
		})
	}

}

// @Title 删除提醒
// @Description 删除提醒
// @Accept  json
// @Produce  json
// @Param   id     5dfb03070a9ac17ac7a82054    string     true        "提醒id"
// @Param   UserId     5dfb03070a9ac17ac7a82054    string     true        "用户id"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /DealyMessage/Remove? [post]
func RemoveDealyMessage(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	_user, _ := c.Get("UserInfo")
	user := _user.(*DB.SMember)

	err := Auth.CheckUserAuth(c.PostForm("UserId"), user)
	if err != nil {
		c.JSON(200, tools.ResponseError{
			401,
			"没有权限",
		})
		return
	}

	_, err = primitive.ObjectIDFromHex(c.PostForm("UserId"))
	if err != nil {
		c.JSON(200, tools.ResponseError{
			1,
			"用户id不正确",
		})
		return
	}

	DelayMessage.GlobalDM.DelTaskForId(c.PostForm("id"))

	c.JSON(200, tools.ResponseSeccess{
		0,
		"ok",
	})

}


func PushNoticeToiOS(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	// 推送app消息
	client, err := push.NewClientWithAccessKey("cn-hangzhou", "LTAI4FdQeNMQXRU6u5J3EFQc", "PwvyF5rRNBWLDya41WrCpvENevYZGi")

	request := push.CreatePushNoticeToiOSRequest()
	request.ApnsEnv = Config.Info.Env
	request.AppKey = "28332889"
	request.Scheme = "https"
	request.Target = "DEVICE"
	request.TargetValue = "30076ed6fdb740e49d882433931bc34a"
	request.Title = c.Query("title")
	request.Body = c.Query("body")

	response, err := client.PushNoticeToiOS(request)
	if err != nil {
		c.JSON(200, tools.ResponseError{
			1,
			err.Error(),
		})
		return
	}
	c.JSON(200, tools.ResponseSeccess{
		0,
		response,
	})
}