320 lines
9.5 KiB
Go
320 lines
9.5 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"dubbo.apache.org/dubbo-go/v3/common/logger"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/disintegration/imaging"
|
|
"github.com/fogleman/gg"
|
|
site "github.com/fonchain_enterprise/fonchain-main/api/site_sign_in"
|
|
"github.com/fonchain_enterprise/fonchain-main/pkg/e"
|
|
"github.com/fonchain_enterprise/fonchain-main/pkg/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/skip2/go-qrcode"
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func SiteSignInList(c *gin.Context) {
|
|
var signInListReq site.SignInListReq
|
|
if err := c.ShouldBind(&signInListReq); err != nil {
|
|
logger.Errorf("SiteSignInList ShouldBind err", err)
|
|
ResponseQuickMsg(c, e.Ok, e.GetMsg(e.InvalidParams), nil)
|
|
return
|
|
}
|
|
resp, err := GrpcSiteSignInImpl.SiteSignInList(context.Background(), &signInListReq)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Failed, err.Error(), nil)
|
|
return
|
|
}
|
|
ResponseQuickMsg(c, e.Ok, e.GetMsg(e.Success), resp)
|
|
return
|
|
|
|
}
|
|
|
|
func CreCodeImage(c *gin.Context) {
|
|
var creCodeImageReq site.SiteF
|
|
if err := c.ShouldBind(&creCodeImageReq); err != nil {
|
|
logger.Errorf("CreCodeImage ShouldBind err", err)
|
|
ResponseQuickMsg(c, e.Ok, e.GetMsg(e.InvalidParams), nil)
|
|
return
|
|
}
|
|
|
|
//生成二维码上传到服务器,并且存到数据库
|
|
codeInfo := "https://signin.fontree.cn/?company=" + utils.ToString(creCodeImageReq.Id)
|
|
qrCode, err := qrcode.New(codeInfo, qrcode.Low)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Ok, "生成二维码失败", nil)
|
|
return
|
|
}
|
|
qrpath := "/" + utils.ToString(time.Now().Unix()) + "qrcode.png"
|
|
file, err := os.Create(qrpath)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Ok, "创建二维码文件失败", nil)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
err = png.Encode(file, qrCode.Image(700))
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Ok, "解码二维码失败", nil)
|
|
return
|
|
}
|
|
|
|
QRCodeUrl, err := PutBos(file.Name(), ImageType, true)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Ok, "上传二维码失败", nil)
|
|
return
|
|
}
|
|
creCodeImageReq.CodeImage = QRCodeUrl
|
|
resp, err := GrpcSiteSignInImpl.CreCodeImage(context.Background(), &creCodeImageReq)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Failed, err.Error(), nil)
|
|
return
|
|
}
|
|
ResponseQuickMsg(c, e.Ok, e.GetMsg(e.Success), resp)
|
|
return
|
|
|
|
}
|
|
|
|
func DownloadCodeImage(c *gin.Context) {
|
|
var downloadCodeImageReq site.SiteF
|
|
if err := c.ShouldBind(&downloadCodeImageReq); err != nil {
|
|
logger.Errorf("DownloadCodeImage ShouldBind err", err)
|
|
ResponseQuickMsg(c, e.Ok, e.GetMsg(e.InvalidParams), nil)
|
|
return
|
|
}
|
|
|
|
bgImage, err := imaging.Open("./data/qrcode.png")
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Ok, "打开背景图片失败", nil)
|
|
return
|
|
}
|
|
response, err := http.Get(downloadCodeImageReq.CodeImage)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Ok, "获取二维码图片失败", nil)
|
|
return
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
qrCode, _, err := image.Decode(response.Body)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Ok, "解码二维码图片失败", nil)
|
|
return
|
|
}
|
|
|
|
// 创建结果图像
|
|
drawImg := image.NewRGBA(bgImage.Bounds())
|
|
draw.Draw(drawImg, bgImage.Bounds(), bgImage, image.Point{}, draw.Src)
|
|
|
|
centerX := (bgImage.Bounds().Dx() - qrCode.Bounds().Dx()) / 2
|
|
centerY := (bgImage.Bounds().Dy() - qrCode.Bounds().Dy()) / 2
|
|
|
|
draw.Draw(drawImg, image.Rect(centerX, centerY, centerX+qrCode.Bounds().Dx(), centerY+qrCode.Bounds().Dy()), qrCode, image.Point{}, draw.Over)
|
|
|
|
//添加文字
|
|
|
|
fontPath := "./data/font1716.ttf"
|
|
|
|
dc := gg.NewContextForImage(bgImage)
|
|
err = dc.LoadFontFace(fontPath, 48)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Ok, "读取文字字体失败", nil)
|
|
return
|
|
}
|
|
|
|
textWidth, _ := dc.MeasureString(downloadCodeImageReq.Name)
|
|
textX := (float64(bgImage.Bounds().Dx()) - textWidth) / 2 // 计算文字的 x 坐标,居中显示
|
|
outImage, _ := addLabel(drawImg, downloadCodeImageReq.Name, int(textX), bgImage.Bounds().Dy()/2+400, color.RGBA{255, 255, 255, 255}, 48, fontPath)
|
|
|
|
// 保存结果图像
|
|
|
|
qrpath := "/" + utils.ToString(time.Now().Unix()) + "qrcode.jpg"
|
|
resultImageFile, err := os.Create(qrpath)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Ok, "创建结果图像文件失败", nil)
|
|
return
|
|
}
|
|
defer resultImageFile.Close()
|
|
err = jpeg.Encode(resultImageFile, outImage, nil)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Ok, "保存结果图像失败", nil)
|
|
return
|
|
}
|
|
QRCodeUrl, err := PutBos(resultImageFile.Name(), ImageType, true)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Ok, "上传结果图像失败", nil)
|
|
return
|
|
}
|
|
downloadResp := &site.DownloadResp{}
|
|
downData := &site.CodeImageF{CodeUrl: QRCodeUrl}
|
|
downloadResp.Data = downData
|
|
downloadResp.Msg = "success"
|
|
ResponseQuickMsg(c, e.Ok, e.GetMsg(e.Success), downloadResp)
|
|
fmt.Println("返回结果" + time.Now().String())
|
|
return
|
|
|
|
}
|
|
|
|
// QuerySiteByApi 通过百度地图API查询地址
|
|
func QuerySiteByApi(c *gin.Context) {
|
|
var baiDuApiReq site.BaiDuApiReq
|
|
if err := c.ShouldBind(&baiDuApiReq); err != nil {
|
|
logger.Errorf("QuerySiteFByApi ShouldBind err", err)
|
|
ResponseQuickMsg(c, e.Ok, e.GetMsg(e.InvalidParams), nil)
|
|
return
|
|
}
|
|
type SearchResult struct {
|
|
Status int `json:"status"`
|
|
Message string `json:"message"`
|
|
ResultType string `json:"result_type"`
|
|
Results []struct {
|
|
Name string `json:"name"`
|
|
Location struct {
|
|
Lat float64 `json:"lat"`
|
|
Lng float64 `json:"lng"`
|
|
} `json:"location"`
|
|
Address string `json:"address"`
|
|
Province string `json:"province"`
|
|
City string `json:"city"`
|
|
Area string `json:"area"`
|
|
Uid string `json:"uid"`
|
|
Street_id string `json:"street_id"`
|
|
} `json:"results"`
|
|
}
|
|
|
|
baiDuApiReq.Query = strings.TrimSpace(baiDuApiReq.Query)
|
|
url := "https://api.map.baidu.com/place/v2/search?query=" + baiDuApiReq.Query + "&output=json&ak=" + baiDuApiReq.Ak + "®ion=" + baiDuApiReq.Region + "&ret_coordtype=" + baiDuApiReq.RetCoordtype
|
|
resp, err := http.Get(url)
|
|
fmt.Println("=========================url====================================")
|
|
fmt.Println(url)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Failed, "通过API获取地址失败", nil)
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
fmt.Println("================API查询结果================================")
|
|
fmt.Println(resp)
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Failed, "读取返回结果失败", nil)
|
|
return
|
|
}
|
|
fmt.Println("================APIBody================================")
|
|
fmt.Println(body)
|
|
|
|
var results SearchResult
|
|
err = json.Unmarshal(body, &results)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Failed, "反序列化失败", nil)
|
|
return
|
|
}
|
|
if results.Status != 0 {
|
|
ResponseQuickMsg(c, e.Failed, "通过API获取地址失败", nil)
|
|
return
|
|
}
|
|
|
|
//resultList := make([]*site.Results, 0)
|
|
//for _, result := range results.Results {
|
|
// location := &site.Location{Lat: result.Location.Lat, Lng: result.Location.Lng}
|
|
// singleResult := &site.Results{
|
|
// Name: result.Name,
|
|
// Location: location,
|
|
// Address: result.Address,
|
|
// }
|
|
// resultList = append(resultList, singleResult)
|
|
//}
|
|
//
|
|
//res := &site.BaiDuApiRes{Results: resultList}
|
|
|
|
ResponseQuickMsg(c, e.Ok, e.GetMsg(e.Success), results)
|
|
return
|
|
}
|
|
|
|
type ReverseGeocodingReq struct {
|
|
Ak string `json:"ak"`
|
|
Coordtype string `json:"coordtype"`
|
|
RetCoordtype string `json:"retCoordtype"`
|
|
Location string `json:"location"`
|
|
Output string `json:"output"`
|
|
}
|
|
|
|
type ReverseGeocodingRes struct {
|
|
Status int `json:"status"`
|
|
Result struct {
|
|
Location struct {
|
|
Lng float64 `json:"lng"`
|
|
Lat float64 `json:"lat"`
|
|
} `json:"location"`
|
|
FormattedAddress string `json:"formatted_address"`
|
|
Edz struct {
|
|
Name string `json:"name"`
|
|
} `json:"edz"`
|
|
Business string `json:"business"`
|
|
AddressComponent struct {
|
|
Country string `json:"country"`
|
|
CountryCodeIso string `json:"country_code_iso"`
|
|
CountryCodeIso2 string `json:"country_code_iso2"`
|
|
CountryCode int `json:"country_code"`
|
|
Province string `json:"province"`
|
|
City string `json:"city"`
|
|
CityLevel int `json:"city_level"`
|
|
District string `json:"district"`
|
|
Town string `json:"town"`
|
|
TownCode string `json:"town_code"`
|
|
Distance string `json:"distance"`
|
|
Direction string `json:"direction"`
|
|
Adcode string `json:"adcode"`
|
|
Street string `json:"street"`
|
|
StreetNumber string `json:"street_number"`
|
|
} `json:"addressComponent"`
|
|
} `json:"result"`
|
|
}
|
|
|
|
// ReverseGeocoding 百度自选地图点
|
|
func ReverseGeocoding(c *gin.Context) {
|
|
var reverseGeocodingReq ReverseGeocodingReq
|
|
if err := c.ShouldBind(&reverseGeocodingReq); err != nil {
|
|
logger.Errorf("ReverseGeocoding ShouldBind err", err)
|
|
ResponseQuickMsg(c, e.Ok, e.GetMsg(e.InvalidParams), nil)
|
|
return
|
|
}
|
|
|
|
url := "https://api.map.baidu.com/reverse_geocoding/v3/?ak=" + reverseGeocodingReq.Ak + "&output=" + reverseGeocodingReq.Output + "&coordtype=" + reverseGeocodingReq.Coordtype + "&location=" + reverseGeocodingReq.Location + "&ret_coordtype=" + reverseGeocodingReq.RetCoordtype
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Failed, "通过API获取地址失败", nil)
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Failed, "读取返回结果失败", nil)
|
|
return
|
|
}
|
|
|
|
var results ReverseGeocodingRes
|
|
err = json.Unmarshal(body, &results)
|
|
if err != nil {
|
|
ResponseQuickMsg(c, e.Failed, "反序列化失败", nil)
|
|
return
|
|
}
|
|
if results.Status != 0 {
|
|
ResponseQuickMsg(c, e.Failed, "通过API获取地址失败", nil)
|
|
return
|
|
}
|
|
|
|
ResponseQuickMsg(c, e.Ok, e.GetMsg(e.Success), results)
|
|
return
|
|
|
|
}
|