Complaint.go 2.99 KB
package Api

import (
	"encoding/json"
	"github.com/aarongao/tools"
	"github.com/gin-gonic/gin"
	"gopkg.in/mgo.v2/bson"
	"letu/DB"
	"math"
	"regexp"
	"strconv"
)

// @Title 增加投诉
// @Description 投诉 - 增加投诉
// @Accept  json
// @Produce  json
// @Param   Mobile     18616619599    string     true        "联系电话"
// @Param   Name     高先生    string     true        "姓名"
// @Param   Code     123456    string     true        "验证码"
// @Param   Sex     男    string     true        "性别"
// @Param   ScenicId     5e1ed07524e03431008b4572    string     true        "景区id"
// @Param   Type     1    string     true        "类型"
// @Param   Content     卫生不干净    string     true        "投诉内容"
// @Param   Image     ["http://www.xx.com/123.jpg","http://www.xx.com/123.jpg"]    string     true        "照片数组"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /CreateComplaint? [post]
func CreateComplaint(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin",c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials","true")



	reg := regexp.MustCompile(Regular)
	if !reg.MatchString(c.PostForm("Mobile")) {

		c.JSON(200, tools.ResponseError{
			1,
			"手机号格式不正确",
		})
		return
	}

	if c.PostForm("Mobile") == ""{
		c.JSON(200, tools.ResponseError{
			1,
			"手机号为空",
		})
		return
	}


	// 检查验证码
	cacheCode := DB.Redis.Get("code_"+c.PostForm("Mobile"))
	if cacheCode != c.PostForm("Code") {

		c.JSON(200, tools.ResponseError{
			1,
			"验证码不正确",
		})
		return

	}

	var images []string

	json.Unmarshal([]byte(c.PostForm("Image")), &images)

	DB.CComplaint.Insert(DB.SComplaint{
		c.PostForm("Type"),
		c.PostForm("ScenicId"),
		c.PostForm("Mobile"),
		c.PostForm("Name"),
		c.PostForm("Sex"),
		c.PostForm("Content"),
		images,
	})

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


}





// @Title 查询所有投诉
// @Description 投诉 - 查询所有投诉
// @Accept  json
// @Produce  json
// @Param   Page     1    int     true        "当前第几页"
// @Success 200 {object} tools.ResponseSeccess ""
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllComplaint? [get]
func AllComplaint(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	total,_ := DB.CComplaint.Find(bson.M{}).Count()
	limit,_ := strconv.Atoi(c.Query("Limit"))
	if limit == 0 {
		limit = 50
	}
	currPage, _ := strconv.Atoi(c.Query("Page"))
	if currPage == 0 {
		currPage = 1
	}
	skip := (currPage - 1) * limit

	var aComplaint  = []DB.SComplaint{}
	DB.CComplaint.Find(bson.M{}).Limit(limit).Skip(int(skip)).Sort("-_id").All(&aComplaint)

	c.JSON(200, tools.Page{
		0,
		total,
		currPage,
		int(math.Ceil(float64(total) / float64(limit))),
		limit,
		aComplaint,
	})

}