Parcourir la source

下载\收藏等功能

k.zhang il y a 2 ans
Parent
commit
fc4696913c

+ 128 - 0
apis/bizhi/apis/collection.go

@@ -0,0 +1,128 @@
+package apis
+
+import (
+	"duoduo/apis/bizhi/models"
+	"duoduo/models/bizhi"
+	"duoduo/models/mysqlBz"
+	"duoduo/tools"
+	"duoduo/tools/app"
+	"fmt"
+	"github.com/gin-gonic/gin"
+)
+
+//插入收藏
+func InsertCollection(c *gin.Context) {
+	var inData models.InsertCollectionRequest
+	var sqlData bizhi.Collection
+	var bizhi mysqlBz.BiZhi
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+	fmt.Println(inData)
+	bizhi.ID = inData.BiZhiId
+	res, err := bizhi.BiZhiDes()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+	sqlData.OpenID = inData.OpenId
+	sqlData.Url = res.Url
+	sqlData.CreateTime = tools.GetCurrntTimeStr()
+	sqlData.BizhiID = inData.BiZhiId
+	err = sqlData.Insert()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+	app.OK(c, nil, "成功")
+}
+
+//删除收藏
+func DelCollection(c *gin.Context) {
+	var inData models.DelCollectionRequest
+	var sqlData bizhi.Collection
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+	sqlData.BizhiID = inData.BiZhiId
+	sqlData.OpenID = inData.OpenId
+	err = sqlData.Del()
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+	app.OK(c, nil, "成功")
+}
+
+//收藏列表
+func ListCollection(c *gin.Context) {
+	var inData models.ListCollectionRequest
+	var sqlData bizhi.Collection
+	var outData []ListCollectionResponse
+	var list []bizhi.CollectionBiZhi
+	var count int
+
+	var pageSize = 10
+	var pageIndex = 1
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	if inData.PageSize != 0 {
+		pageSize = inData.PageSize
+	}
+	if inData.PageIndex != 0 {
+		pageIndex = inData.PageIndex
+	}
+
+	sqlData.OpenID = inData.OpenId
+	if inData.Type == "1" {
+		list, count, err = sqlData.GetOpenIdList(pageSize, pageIndex)
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+	} else if inData.Type == "2" {
+		list, count, err = sqlData.GetOpenIdDownList(pageSize, pageIndex)
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+	} else {
+		app.Error(c, 500, nil, "类型错误")
+		return
+	}
+
+	for i := 0; i < len(list); i++ {
+		data := ListCollectionResponse{}
+		data.CollectionBiZhi = list[i]
+		data.Cover = list[i].Url
+		data.Cover1 = list[i].Url + "?imageView/1/w/350/h/467"
+		data.CoverHeight = "467"
+		data.CoverWidth = "350"
+		data.Loaded = false
+		outData = append(outData, data)
+
+	}
+
+	app.PageOK(c, outData, count, pageIndex, pageSize, app.Success)
+}
+
+//随机获取图片请求参数
+type ListCollectionResponse struct {
+	bizhi.CollectionBiZhi
+	Cover1      string `json:"Cover1"`
+	Cover       string `json:"Cover"`
+	CoverHeight string `json:"CoverHeight"`
+	CoverWidth  string `json:"CoverWidth"`
+	Loaded      bool   `json:"loaded"`
+}

+ 38 - 0
apis/bizhi/apis/download.go

@@ -0,0 +1,38 @@
+package apis
+
+import (
+	"duoduo/apis/bizhi/models"
+	"duoduo/models/bizhi"
+	"duoduo/models/mysqlBz"
+	"duoduo/tools"
+	"duoduo/tools/app"
+	"github.com/gin-gonic/gin"
+)
+
+func Download(c *gin.Context) {
+	var inData models.InsertDownloadRequest
+	var sqlData bizhi.Download
+	var bizhi mysqlBz.BiZhi
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+	bizhi.ID = inData.BiZhiId
+	res, err := bizhi.BiZhiDes()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+	sqlData.OpenID = inData.OpenId
+	sqlData.Url = res.Url
+	sqlData.CreateTime = tools.GetCurrntTimeStr()
+	sqlData.BizhiID = inData.BiZhiId
+	err = sqlData.Insert()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+	app.OK(c, nil, "成功")
+}

+ 44 - 1
apis/bizhi/apis/round_picture.go

@@ -4,6 +4,7 @@ import (
 	"duoduo/models/bizhi"
 	"duoduo/models/mysqlBz"
 	"duoduo/tools/app"
+	"fmt"
 	"github.com/gin-gonic/gin"
 )
 
@@ -81,11 +82,53 @@ func PictureGroup(c *gin.Context) {
 
 	sqlData.Label = inData.Label
 	sqlData.ID = inData.Id
-	data, err := sqlData.BiZhiGroup(sqlData.Label)
+	data, err := sqlData.BiZhiGroup(sqlData.Label, inData.OpenId)
 	if err != nil {
 		app.Error(c, 500, err, err.Error())
 		return
 	}
+	fmt.Println("%+v", data)
 	app.OK(c, data, app.Success)
 
 }
+
+func Search(c *gin.Context) {
+	inData := bizhi.SearchRequest{}
+	sqlData := mysqlBz.BiZhi{}
+	outData := []bizhi.RoundPictureResponse{}
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	var pageSize = 10
+	var pageIndex = 1
+
+	if inData.PageSize != 0 {
+		pageSize = inData.PageSize
+	}
+	if inData.PageIndex != 0 {
+		pageIndex = inData.PageIndex
+	}
+	sqlData.Label = inData.Label
+	list, count, err := sqlData.GetLabelList(pageSize, pageIndex)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	for i := 0; i < len(list); i++ {
+		data := bizhi.RoundPictureResponse{}
+		data.BiZhi = list[i]
+		data.Cover = list[i].Url
+		data.Cover1 = list[i].Url + "?imageView/1/w/350/h/467"
+		data.CoverHeight = "467"
+		data.CoverWidth = "350"
+		data.Loaded = false
+		outData = append(outData, data)
+
+	}
+	app.PageOK(c, outData, count, pageIndex, pageSize, "成功")
+
+}

+ 18 - 0
apis/bizhi/models/collection.go

@@ -0,0 +1,18 @@
+package models
+
+type InsertCollectionRequest struct {
+	BiZhiId int    `json:"biZhiId"`
+	OpenId  string `json:"openId"`
+}
+
+type DelCollectionRequest struct {
+	BiZhiId int    `json:"biZhiId"`
+	OpenId  string `json:"openId"`
+}
+
+type ListCollectionRequest struct {
+	OpenId    string `json:"openId"`
+	PageSize  int    `json:"pageSize"`
+	PageIndex int    `json:"pageIndex"`
+	Type      string `json:"type"`
+}

+ 6 - 0
apis/bizhi/models/download.go

@@ -0,0 +1,6 @@
+package models
+
+type InsertDownloadRequest struct {
+	BiZhiId int    `json:"biZhiId"`
+	OpenId  string `json:"openId"`
+}

+ 3 - 3
apis/bizhi/models/user.go

@@ -18,9 +18,9 @@ type GetUserScoreRequest struct {
 
 type SubUserScoreRequest struct {
 	OpenId string `json:"openId"`
-	Score  int    `json:"score"`
-	Type   int    `json:"type"`
-	Des    string `json:"des"`
+	Score  int    `json:"score"` //积分
+	Type   int    `json:"type"`  //1-新用户 2-签到领取积分 3-看广告获取积分 4-邀请好友奖励积分 5-其他 6-下载图片
+	Des    string `json:"des"`   //描述
 	Num    int    `json:"num"`
 }
 

+ 18 - 13
apis/bizhi/router.go

@@ -9,18 +9,23 @@ import (
 func InitBiZhiRouter(engine *gin.RouterGroup) {
 	v1 := engine.Group("v1")
 	{
-		v1.POST("/upload", apis.Upload)                   //下载图片
-		v1.POST("/round", apis.RondPicture)               //随机图片
-		v1.POST("/set.user", apis.OpenIdSet)              //用户注册
-		v1.POST("/get.score", apis.GetUserScore)          //校验积分
-		v1.POST("/sub.score", apis.SubUserScore)          //+-积分
-		v1.POST("/score.list", apis.ScoreLogList)         //积分消费记录
-		v1.POST("/openid.list", apis.GetUserList)         //邀请用户列表
-		v1.POST("/bizhi.label", apis.PictureLabel)        //壁纸审核
-		v1.POST("/label.list", apis.LabelList)            //标签
-		v1.POST("/label.insert", apis.LabelInert)         //插入标签
-		v1.POST("/bizhi.update", apis.PictureLabelUpdate) //更新标签
-		v1.POST("/bizhi.group", apis.PictureGroup)        //获取组图
-		v1.POST("/update.score", apis.UpdateUser)         //更新用户头像昵称
+		v1.POST("/upload", apis.Upload)                      //下载图片
+		v1.POST("/round", apis.RondPicture)                  //随机图片
+		v1.POST("/set.user", apis.OpenIdSet)                 //用户注册
+		v1.POST("/get.score", apis.GetUserScore)             //校验积分
+		v1.POST("/sub.score", apis.SubUserScore)             //+-积分
+		v1.POST("/score.list", apis.ScoreLogList)            //积分消费记录
+		v1.POST("/openid.list", apis.GetUserList)            //邀请用户列表
+		v1.POST("/bizhi.label", apis.PictureLabel)           //壁纸审核
+		v1.POST("/label.list", apis.LabelList)               //标签
+		v1.POST("/label.insert", apis.LabelInert)            //插入标签
+		v1.POST("/bizhi.update", apis.PictureLabelUpdate)    //更新标签
+		v1.POST("/bizhi.group", apis.PictureGroup)           //获取组图
+		v1.POST("/update.score", apis.UpdateUser)            //更新用户头像昵称
+		v1.POST("/insert.collection", apis.InsertCollection) //插入收藏
+		v1.POST("/del.collection", apis.DelCollection)       //删除收藏
+		v1.POST("/insert.download", apis.Download)           //插入下载
+		v1.POST("/collection.list", apis.ListCollection)     //获取用户收藏信息
+		v1.POST("/search", apis.Search)                      //图片检索
 	}
 }

+ 74 - 0
models/bizhi/collection.go

@@ -0,0 +1,74 @@
+package bizhi
+
+import orm "duoduo/database"
+
+type Collection struct {
+	ID         int    `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
+	OpenID     string `gorm:"column:open_id;type:varchar(255)" json:"openId"`
+	BizhiID    int    `gorm:"column:bizhi_id;type:bigint(20)" json:"bizhiId"`     // 壁纸id
+	Url        string `gorm:"column:url;type:varchar(255)" json:"url"`            // 壁纸url
+	CreateTime string `gorm:"column:create_time;type:datetime" json:"createTime"` // 创建时间
+}
+
+type CollectionBiZhi struct {
+	ID         int    `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
+	OpenID     string `gorm:"column:open_id;type:varchar(255)" json:"openId"`
+	BizhiID    int    `gorm:"column:bizhi_id;type:bigint(20)" json:"bizhiId"`     // 壁纸id
+	Url        string `gorm:"column:url;type:varchar(255)" json:"url"`            // 壁纸url
+	CreateTime string `gorm:"column:create_time;type:datetime" json:"createTime"` // 创建时间
+	Label      string `gorm:"column:label;type:varchar(255)" json:"label"`        // 标签
+}
+
+func (m *Collection) TableName() string {
+	return "collection"
+}
+
+func (m *Collection) Insert() error {
+	result := orm.BzMysql.Table(m.TableName()).Create(&m)
+	if result.Error != nil {
+		err := result.Error
+		return err
+	}
+	return nil
+}
+
+func (m *Collection) Del() error {
+	result := orm.BzMysql.Table(m.TableName()).Where("bizhi_id = ? and open_id = ?", m.BizhiID, m.OpenID).Delete(nil)
+	if result.Error != nil {
+		err := result.Error
+		return err
+	}
+	return nil
+}
+
+//list 接口使用
+func (m *Collection) GetOpenIdList(pageSize int, pageIndex int) ([]CollectionBiZhi, int, error) {
+	var doc []CollectionBiZhi
+
+	table := orm.BzMysql.Table("collection c LEFT JOIN bizhi b ON b.id = c.bizhi_id")
+
+	table = table.Where("open_id = ?  ", m.OpenID)
+	//SELECT c.*,b.label FROM collection c LEFT JOIN bizhi b ON b.id = c.bizhi_id;
+	var count int
+	if err := table.Select("c.*,b.label").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
+		return nil, 0, err
+	}
+	table.Count(&count)
+	return doc, count, nil
+}
+
+//list 接口使用
+func (m *Collection) GetOpenIdDownList(pageSize int, pageIndex int) ([]CollectionBiZhi, int, error) {
+	var doc []CollectionBiZhi
+
+	table := orm.BzMysql.Table("download c LEFT JOIN bizhi b ON b.id = c.bizhi_id")
+
+	table = table.Where("open_id = ?  ", m.OpenID)
+	//SELECT c.*,b.label FROM collection c LEFT JOIN bizhi b ON b.id = c.bizhi_id;
+	var count int
+	if err := table.Select("c.*,b.label").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
+		return nil, 0, err
+	}
+	table.Count(&count)
+	return doc, count, nil
+}

+ 24 - 0
models/bizhi/download.go

@@ -0,0 +1,24 @@
+package bizhi
+
+import orm "duoduo/database"
+
+type Download struct {
+	ID         int    `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
+	OpenID     string `gorm:"column:open_id;type:varchar(255)" json:"openId"`
+	BizhiID    int    `gorm:"column:bizhi_id;type:bigint(20)" json:"bizhiId"`     // 壁纸id
+	Url        string `gorm:"column:url;type:varchar(255)" json:"url"`            // 壁纸url
+	CreateTime string `gorm:"column:create_time;type:datetime" json:"createTime"` // 创建时间
+}
+
+func (m *Download) TableName() string {
+	return "download"
+}
+
+func (m *Download) Insert() error {
+	result := orm.BzMysql.Table(m.TableName()).Create(&m)
+	if result.Error != nil {
+		err := result.Error
+		return err
+	}
+	return nil
+}

+ 9 - 2
models/bizhi/round_picture.go

@@ -25,6 +25,13 @@ type PictureLabelUpdateRequest struct {
 
 //图片id
 type PictureGroupRequest struct {
-	Id    int    `json:"id"`
-	Label string `json:"label"`
+	Id     int    `json:"id"`
+	Label  string `json:"label"`
+	OpenId string `json:"openId"`
+}
+
+type SearchRequest struct {
+	Label     string `json:"label"`
+	PageSize  int    `json:"pageSize"`
+	PageIndex int    `json:"pageIndex"`
 }

+ 1 - 1
models/bizhi/score_log.go

@@ -33,7 +33,7 @@ func (m *ScoreLog) ScoreLogList(pageSize int, pageIndex int) ([]ScoreLog, int, e
 	table = table.Where("open_id = ?  ", m.OpenID)
 
 	var count int
-	if err := table.Select("id,open_id,score,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') as create_time").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
+	if err := table.Select("id,open_id,score,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') as create_time,des,type").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
 		return nil, 0, err
 	}
 	table.Count(&count)

+ 39 - 6
models/mysqlBz/bizhi.go

@@ -15,6 +15,15 @@ type BiZhi struct {
 	UpdateTime string `gorm:"column:update_time;type:datetime" json:"update_time"` // 更新时间
 }
 
+type BiZhiCollection struct {
+	ID         int    `gorm:"column:id;type:bigint(20);primary_key" json:"id"`     // 壁纸id
+	Url        string `gorm:"column:url;type:varchar(255)" json:"url"`             // 壁纸链接
+	Label      string `gorm:"column:label;type:varchar(255)" json:"label"`         // 标签
+	CreateTime string `gorm:"column:create_time;type:datetime" json:"create_time"` // 插入时间
+	UpdateTime string `gorm:"column:update_time;type:datetime" json:"update_time"` // 更新时间
+	BiZhiId    int    `gorm:"column:bizhi_id;type:bigint(20)" json:"biZhiId"`
+}
+
 func (m *BiZhi) TableName() string {
 	return "bizhi"
 }
@@ -88,13 +97,13 @@ func (o *BiZhi) BiZhiLabelUpdate(label string) error {
 	return nil
 }
 
-func (o *BiZhi) BiZhiGroup(label string) ([]BiZhi, error) {
-	var doc []BiZhi
-	var data []BiZhi
-	var bizhi BiZhi
-	table := orm.BzMysql.Table("bizhi ").Where("label = ?", label)
+func (o *BiZhi) BiZhiGroup(label string, openid string) ([]BiZhiCollection, error) {
+	var doc []BiZhiCollection
+	var data []BiZhiCollection
+	var bizhi BiZhiCollection
+	table := orm.BzMysql.Table("bizhi b ").Where("label = ?", label)
 
-	if err := table.Select("*").Order("id desc").Limit(30).Find(&doc).Error; err != nil {
+	if err := table.Select("b.*,(SELECT bizhi_id FROM collection WHERE bizhi_id = b.id and open_id = ?) as bizhi_id", openid).Order("id desc").Limit(30).Find(&doc).Error; err != nil {
 		return doc, err
 	}
 
@@ -103,6 +112,7 @@ func (o *BiZhi) BiZhiGroup(label string) ([]BiZhi, error) {
 			bizhi.Url = doc[i].Url
 			bizhi.ID = doc[i].ID
 			bizhi.Label = doc[i].Label
+			bizhi.BiZhiId = doc[i].BiZhiId
 		}
 	}
 	data = append(data, bizhi)
@@ -117,3 +127,26 @@ func (o *BiZhi) BiZhiGroup(label string) ([]BiZhi, error) {
 
 	return data, nil
 }
+
+func (o *BiZhi) BiZhiDes() (BiZhi, error) {
+	var doc BiZhi
+
+	err := orm.BzMysql.Table("bizhi").Where("id = ?", o.ID).First(&doc).Error
+	if err != nil {
+		return doc, err
+	}
+	return doc, nil
+}
+
+//list 接口使用
+func (o *BiZhi) GetLabelList(pageSize int, pageIndex int) ([]BiZhi, int, error) {
+	var doc []BiZhi
+
+	table := orm.BzMysql.Table("bizhi").Where("label like ?", "%"+o.Label+"%")
+	var count int
+	if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
+		return nil, 0, err
+	}
+	table.Count(&count)
+	return doc, count, nil
+}