Jelajahi Sumber

Merge branch 'mfyx_2.0' of http://8.136.199.33:3000/cxzhang/hongze_mfyx into debug

xingzai 1 tahun lalu
induk
melakukan
e5064d07de
5 mengubah file dengan 166 tambahan dan 1 penghapusan
  1. 48 0
      controllers/user.go
  2. 19 0
      models/config.go
  3. 1 0
      models/db.go
  4. 86 1
      services/aliyun.go
  5. 12 0
      utils/common.go

+ 48 - 0
controllers/user.go

@@ -2023,6 +2023,54 @@ func (this *UserController) UploadUserBusinessCard() {
 		br.Msg = "请填写姓名"
 		return
 	}
+	mobile := req.Mobile
+	key := "check_ocr_business_card"
+	conf, e := models.GetConfigByCode(key)
+	if e != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取首页头部导航失败, Err: " + e.Error()
+		return
+	}
+	if conf.ConfigValue == "" {
+		br.Msg = "获取失败"
+		br.ErrMsg = "首页头部导航配置值有误"
+		return
+	}
+	checkItem := new(models.CheckOcrBusinessCard)
+	//list := new(*models.BannerUrlYxResp)
+	if e = json.Unmarshal([]byte(conf.ConfigValue), &checkItem); e != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "首页头部导航配置值解析失败, Err: " + e.Error()
+		return
+	}
+
+	if checkItem.CheckMobile || checkItem.CheckCompany {
+		aliyunResult, err := services.AliyunOcrBusinessCard(req.BusinessCardUrl)
+		if err != nil {
+			br.Msg = "识别失败"
+			br.ErrMsg = "识别失败,Err:" + err.Error()
+			return
+		}
+		//校验手机号
+		if checkItem.CheckMobile {
+			mobileStr := strings.Join(aliyunResult.TelCell, ",")
+			if !strings.Contains(mobileStr, mobile) {
+				br.Msg = "名片手机号和绑定手机号不一致,请重新上传"
+				br.ErrMsg = "手机号不一致"
+				return
+			}
+		}
+		//校验公司
+		if checkItem.CheckCompany {
+			companyNameStr := strings.Join(aliyunResult.Company, ",")
+			if !strings.Contains(companyNameStr, companyName) {
+				br.Msg = "名片手机号和绑定手机号不一致,请重新上传"
+				br.ErrMsg = "公司信息不一致"
+				return
+			}
+		}
+	}
+
 	item := new(order.CygxUserBusinessCard)
 	item.BusinessCardUrl = businessCardUrl
 	item.UserId = uid

+ 19 - 0
models/config.go

@@ -111,3 +111,22 @@ type MicroRoadShowDefaultImg struct {
 	ImgUrl              string `description:"背景图"`
 	ShareImg            string `description:"分享图"`
 }
+
+// CheckOcrBusinessCard  OCR 名片校验
+type CheckOcrBusinessCard struct {
+	CheckMobile  bool `description:"是否校验手机号"`
+	CheckCompany bool `description:"是否校验公司名称"`
+}
+
+type AliyunOcrLog struct {
+	Id         int       `orm:"column(id);pk"`
+	ImgUrl     string    `description:"图片地址"`
+	Result     string    `description:"返回内容"`
+	CreateTime time.Time `description:"创建时间"`
+}
+
+func AddAliyunOcrLog(item *AliyunOcrLog) (err error) {
+	o := orm.NewOrm()
+	_, err = o.Insert(item)
+	return
+}

+ 1 - 0
models/db.go

@@ -177,6 +177,7 @@ func init() {
 		new(CygxAskserieVideoCollect),
 		new(CygxVoiceAndVideoHistory),
 		new(CygxArticleAndYanxuanRecord),
+		new(AliyunOcrLog),
 	)
 
 	initOrder() // 订单模块

+ 86 - 1
services/aliyun.go

@@ -5,9 +5,13 @@ import (
 	"encoding/base64"
 	"encoding/json"
 	"fmt"
+	"hongze/hongze_mfyx/models"
+	"hongze/hongze_mfyx/utils"
+	"io"
 	"io/ioutil"
 	"net/http"
 	"strings"
+	"time"
 
 	rhttp "github.com/rdlucklib/rdluck_tools/http"
 )
@@ -18,6 +22,11 @@ const (
 	AlAppCode   = "22553c4ba74545568aba70ac6cfd441d"
 )
 
+var (
+	//文档 https://market.aliyun.com/apimarket/detail/cmapi013591?spm=5176.product-detail.detail.4.6d4f3dcaS0qPFI&endpoint=a3dd145f3865492ba6f02d0c09dd00a1
+	AliyunOcrBusinessCardCode = "22553c4ba74545568aba70ac6cfd441d" // 阿里云OCR名片识别的code
+)
+
 type OcrItem struct {
 	Image string `json:"image"`
 }
@@ -29,7 +38,7 @@ type OcrResult struct {
 	TelCell []string `json:"tel_cell"`
 }
 
-///ocr_business_card.json
+// /ocr_business_card.json
 func AliyunBusinessCard(imgUrl string) (result *OcrResult, err error) {
 	postUrl := `https://dm-57.data.aliyun.com/rest/160601/ocr/ocr_business_card.json`
 
@@ -74,3 +83,79 @@ func AliyunBusinessCard(imgUrl string) (result *OcrResult, err error) {
 	err = json.Unmarshal(body, &result)
 	return
 }
+
+// 阿里云名片识别返回类
+type OcrBusinessCardResult struct {
+	TelCell    []string `json:"tel_cell"`
+	Success    bool     `json:"success"`
+	Name       string   `json:"name"`
+	Company    []string `json:"company"`
+	Addr       []string `json:"addr"`
+	Department []string `json:"department"`
+	Title      []string `json:"title"`
+	RequestID  string   `json:"request_id"`
+	Email      []string `json:"email"`
+	TelWork    []string `json:"tel_work"`
+}
+
+//func init() {
+//	AliyunOcrBusinessCard("https://hzstatic.hzinsights.com/static/images/202403/20240327/kjafdja3BWwThD1ZJexE0S4Skqn2.jpg")
+//}
+
+// 阿里云名片识别更换2024-01-02
+func AliyunOcrBusinessCard(imageurl string) (result *OcrBusinessCardResult, err error) {
+	postUrl := "https://bizcard.market.alicloudapi.com/rest/160601/ocr/ocr_business_card.json"
+	//method := "POST"
+
+	item := new(OcrItem)
+	item.Image = imageurl
+	jsonBody, err := json.Marshal(item)
+	if err != nil {
+		fmt.Println("json.Marshal Err:" + err.Error())
+		return
+	}
+	tr := &http.Transport{
+		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+	}
+	client := &http.Client{Transport: tr}
+	req, err := http.NewRequest("POST", postUrl, strings.NewReader(string(jsonBody)))
+	req.Header.Add("Authorization", "APPCODE "+AliyunOcrBusinessCardCode)
+	req.Header.Add("Content-Type", "application/json")
+
+	res, err := client.Do(req)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	defer res.Body.Close()
+
+	body, err := io.ReadAll(res.Body)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	err = json.Unmarshal(body, &result)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+
+	//处理返回的手机号格式,并进行数据合并
+	var telCell []string
+	if len(result.TelCell) > 0 {
+		for _, v := range result.TelCell {
+			telCell = append(telCell, utils.GetArabicNumbers(v))
+		}
+	}
+	if len(result.TelWork) > 0 {
+		for _, v := range result.TelWork {
+			telCell = append(telCell, utils.GetArabicNumbers(v))
+		}
+	}
+	logItem := new(models.AliyunOcrLog)
+	logItem.ImgUrl = imageurl
+	logItem.Result = string(body)
+	logItem.CreateTime = time.Now()
+	err = models.AddAliyunOcrLog(logItem)
+	return
+}

+ 12 - 0
utils/common.go

@@ -20,6 +20,7 @@ import (
 	"strconv"
 	"strings"
 	"time"
+	"unicode"
 )
 
 // 随机数种子
@@ -970,3 +971,14 @@ func GetOrdernum() string {
 	orderSn := "OD" + time.Now().Format(FormatDateTimeUnSpace) + fmt.Sprintf("%04d", randomPart)
 	return orderSn
 }
+
+// 获取字符串中的阿拉伯数字,并返回字符串
+func GetArabicNumbers(str string) string {
+	var numbers []rune
+	for _, char := range str {
+		if unicode.IsDigit(char) {
+			numbers = append(numbers, char)
+		}
+	}
+	return string(numbers)
+}