common.go 4.09 KB
package models

import (
	db "client/friendSystem/database"
	"crypto/md5"
	"encoding/hex"
	"errors"
	"image"
	"image/png"
	"log"
	"net/http"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"github.com/boombuler/barcode"
	"github.com/boombuler/barcode/qr"
	"github.com/gin-gonic/gin"
)

const (
	endpoint        string = "oss-cn-beijing.aliyuncs.com"
	accessKeyID     string = "LTAIgKS9JC0vLIqH"
	accessKeySecret string = "9VDu6RNRC8IdPRV0yrsPi6a64SMkxz"
	bucketName      string = "iceplay"
)

func SetQrCodeImage(uuid string) (err error) {
	code, err := qr.Encode(uuid, qr.L, qr.Unicode)
	// code, err := code39.Encode(uuid)
	if err != nil {
		return
	}
	if uuid != code.Content() {
		err = errors.New("data differs")
		return
	}
	code, err = barcode.Scale(code, 300, 300)
	if err != nil {
		return
	}
	objectKey := "userInfo/Qrcode/" + MD5(uuid)
	log.Println(objectKey)
	if err = writeAndPutPng("Qrcode.png", code, objectKey); err != nil {
		return
	}
	return
}
func writeAndPutPng(filename string, img image.Image, objectKey string) (err error) {
	file, err := os.Create(filename)
	if err != nil {
		return
	}
	err = png.Encode(file, img)
	// err = jpeg.Encode(file, img, &jpeg.Options{100})      //图像质量值为100,是最好的图像显示
	if err != nil {
		return
	}
	file.Close()
	client, err := oss.New(endpoint, accessKeyID, accessKeySecret)
	if err != nil {
		return
	}
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		return
	}
	err = bucket.PutObjectFromFile(objectKey, filename)
	if err != nil {
		return
	}
	err = os.Remove(filename)
	if err != nil {
		return
	}
	return
}

func GetTableFieldName(table string, index int) (result string, err error) {
	sql := `select COLUMN_NAME from information_schema.COLUMNS where table_name = ?`
	log.Println(sql, index)
	r, err := db.Engine.Query(sql, table)
	if err != nil {
		return
	}
	if index > len(r) || index <= 4 {
		err = errors.New("不存在的索引值")
		return
	}
	result = string(r[index-1]["COLUMN_NAME"])
	return
}

func GetTableName(gameId int) (result string, err error) {
	sql := ` select table_game_tableName from ice_game_table where table_game_id=? limit 1`
	r, err := db.Engine.Query(sql, gameId)
	if err != nil {
		return
	}
	result = string(r[0]["table_game_tableName"])
	return
}

func MD5(str string) string {
	w := md5.New()
	w.Write([]byte(str))
	has := w.Sum(nil)
	str = hex.EncodeToString(has)
	return str
}

func GetFriendList(uuid string) (result string, err error) {

	friendRelationship := make([]FriendRelationship, 0)
	sql := ` select friend_relationship_a_id ,friend_relationship_b_id from ice_friend_relationship where  friend_relationship_a_id=? or friend_relationship_b_id =? and relationship_value =1`
	err = db.Engine.Sql(sql, uuid, uuid).Find(&friendRelationship)
	if err != nil {
		return
	}
	/*取出好友uuid*/
	result = "in("
	for _, v := range friendRelationship {
		if v.FriendRelationshipAId == uuid {
			result = result + "'" + v.FriendRelationshipBId + "',"
			// result = result + v.FriendRelationshipBId + ","
		} else {
			result = result + "'" + v.FriendRelationshipAId + "',"
			// result = result + v.FriendRelationshipAId + ","
		}
	}
	result = result + "'" + uuid + "')"

	// result = result + uuid + ")"
	return
}

func ReturnRespones(c *gin.Context, code string, result interface{}) {
	var (
		respondObjectName  string
		respondObjectValue interface{}
	)
	switch code {
	case "1003":
		respondObjectName = "msg"
		msg := "参数错误"
		respondObjectValue = msg
		break
	case "0":
		respondObjectName = "obj"
		res := make(map[string]interface{})
		res["result"] = result
		respondObjectValue = res
		break
	case "1000":
		respondObjectName = "msg"
		msg := result.(error).Error()
		respondObjectValue = msg
		break
	default:
		log.Println("code error")
	}

	c.JSON(http.StatusOK, gin.H{
		"code":            code,
		respondObjectName: respondObjectValue,
	})
	return
}

/*Id是否存在校验*/
func CheckIdExist(uuid string) (err error) {
	log.Println(uuid)
	sql := ` select id from users where guid=?`
	r, err := db.Engine.Query(sql, uuid)
	if err != nil {
		return
	}
	if len(r) == 0 {
		err = errors.New("无效的用户ID")
		return
	}
	return
}