xingzai 1 gadu atpakaļ
vecāks
revīzija
b037a8820c
4 mainītis faili ar 151 papildinājumiem un 51 dzēšanām
  1. 56 48
      controllers/company_user.go
  2. 1 1
      models/cygx/aliyun_ocr_log.go
  3. 82 2
      services/aliyun.go
  4. 12 0
      utils/common.go

+ 56 - 48
controllers/company_user.go

@@ -485,7 +485,9 @@ func (this *CompanyUserController) AddUser() {
 			}
 		}
 	}
-	if req.BusinessCardUrl != "" && utils.RunMode == "release" {
+
+	//if req.BusinessCardUrl != "" && utils.RunMode == "release" {
+	if req.BusinessCardUrl != "" {
 		// 获取图片识别客户名称的配置
 		crmConfig, err := company.GetConfigDetailByCode("card_recognition_company_name")
 		if err != nil {
@@ -577,7 +579,7 @@ func (this *CompanyUserController) AddUser() {
 		//如果百度未识别出来手机号或者公司名称,那么进行阿里识别
 		if !isFlag || (len(companyNameSlice) == 0 && crmConfig.ConfigValue == "true") {
 			//阿里云识别
-			aliyunResult, err := services.AliyunBusinessCardOcr(req.BusinessCardUrl)
+			aliyunResult, err := services.AliyunOcrBusinessCard(req.BusinessCardUrl)
 			if err != nil {
 				br.Msg = "识别失败"
 				br.ErrMsg = "识别失败,Err:" + err.Error()
@@ -1926,7 +1928,8 @@ func (this *CompanyUserController) EditUser() {
 			}
 		}
 	}
-	if wxUser.BusinessCardUrl != req.BusinessCardUrl && utils.RunMode == "release" {
+	//if wxUser.BusinessCardUrl != req.BusinessCardUrl && utils.RunMode == "release" {
+	if wxUser.BusinessCardUrl != req.BusinessCardUrl {
 		// 获取图片识别手机号的配置
 		crmConfig2, err := company.GetConfigDetailByCode("card_recognition_tel")
 		if err != nil {
@@ -1969,7 +1972,7 @@ func (this *CompanyUserController) EditUser() {
 
 		if !isFlag {
 			//阿里云识别
-			aliyunResult, err := services.AliyunBusinessCardOcr(req.BusinessCardUrl)
+			aliyunResult, err := services.AliyunOcrBusinessCard(req.BusinessCardUrl)
 			if err != nil {
 				br.Msg = "识别失败"
 				br.ErrMsg = "识别失败,Err:" + err.Error()
@@ -2493,31 +2496,31 @@ func (this *CompanyController) PotentialUserMove() {
 			}
 			if !isFlag {
 				//阿里云识别
-				if utils.RunMode == "release" {
-					aliyunResult, err := services.AliyunBusinessCardOcr(req.BusinessCardUrl)
-					if err != nil {
-						br.Msg = "识别失败"
-						br.ErrMsg = "识别失败,Err:" + err.Error()
-						return
-					}
-					if !aliyunResult.Success {
-						br.Msg = "识别失败"
-						br.ErrMsg = "识别失败"
-						return
-					}
-					if len(aliyunResult.TelCell) > 0 {
-						mobileStr := strings.Join(aliyunResult.TelCell, ",")
-						if req.Mobile != "" {
-							if strings.Contains(mobileStr, req.Mobile) {
-								isFlag = true
-							} else {
-								isFlag = false
-							}
+				//if utils.RunMode == "release" {
+				aliyunResult, err := services.AliyunOcrBusinessCard(req.BusinessCardUrl)
+				if err != nil {
+					br.Msg = "识别失败"
+					br.ErrMsg = "识别失败,Err:" + err.Error()
+					return
+				}
+				if !aliyunResult.Success {
+					br.Msg = "识别失败"
+					br.ErrMsg = "识别失败"
+					return
+				}
+				if len(aliyunResult.TelCell) > 0 {
+					mobileStr := strings.Join(aliyunResult.TelCell, ",")
+					if req.Mobile != "" {
+						if strings.Contains(mobileStr, req.Mobile) {
+							isFlag = true
+						} else {
+							isFlag = false
 						}
-					} else {
-						isFlag = true
 					}
+				} else {
+					isFlag = true
 				}
+				//}
 			}
 
 			if !isFlag && crmConfig2.ConfigValue == "true" {
@@ -5078,31 +5081,31 @@ func (this *CompanyController) ApplyRecordPotentialUserMove() {
 			}
 			if !isFlag {
 				//阿里云识别
-				if utils.RunMode == "release" {
-					aliyunResult, err := services.AliyunBusinessCardOcr(req.BusinessCardUrl)
-					if err != nil {
-						br.Msg = "识别失败"
-						br.ErrMsg = "识别失败,Err:" + err.Error()
-						return
-					}
-					if !aliyunResult.Success {
-						br.Msg = "识别失败"
-						br.ErrMsg = "识别失败"
-						return
-					}
-					if len(aliyunResult.TelCell) > 0 {
-						mobileStr := strings.Join(aliyunResult.TelCell, ",")
-						if req.Mobile != "" {
-							if strings.Contains(mobileStr, req.Mobile) {
-								isFlag = true
-							} else {
-								isFlag = false
-							}
+				//if utils.RunMode == "release" {
+				aliyunResult, err := services.AliyunOcrBusinessCard(req.BusinessCardUrl)
+				if err != nil {
+					br.Msg = "识别失败"
+					br.ErrMsg = "识别失败,Err:" + err.Error()
+					return
+				}
+				if !aliyunResult.Success {
+					br.Msg = "识别失败"
+					br.ErrMsg = "识别失败"
+					return
+				}
+				if len(aliyunResult.TelCell) > 0 {
+					mobileStr := strings.Join(aliyunResult.TelCell, ",")
+					if req.Mobile != "" {
+						if strings.Contains(mobileStr, req.Mobile) {
+							isFlag = true
+						} else {
+							isFlag = false
 						}
-					} else {
-						isFlag = true
 					}
+				} else {
+					isFlag = true
 				}
+				//}
 			}
 
 			if !isFlag && crmConfig2.ConfigValue == "true" {
@@ -6062,6 +6065,11 @@ func (this *CompanyUserController) UpadteUserYanxuanPermission() {
 		return
 	}
 
+	if userInfo.BusinessCardUrl == "" {
+		br.Msg = "该联系人尚未添加名片,请点击编辑,上传名片后再开通试用"
+		return
+	}
+
 	companyInfo, err := company.GetCompanyById(userInfo.CompanyId)
 	if err != nil {
 		br.Msg = "查找客户失败"

+ 1 - 1
models/cygx/aliyun_ocr_log.go

@@ -12,7 +12,7 @@ type AliyunOcrImgUrlReq struct {
 type AliyunOcrLog struct {
 	Id         int       `orm:"column(id);pk"`
 	ImgUrl     string    `description:"图片地址"`
-	Result     string    `description:"手机号"`
+	Result     string    `description:"返回内容"`
 	CreateTime time.Time `description:"创建时间"`
 }
 

+ 82 - 2
services/aliyun.go

@@ -9,12 +9,14 @@ import (
 	ocr20191230 "github.com/alibabacloud-go/ocr-20191230/v3/client"
 	util "github.com/alibabacloud-go/tea-utils/v2/service"
 	"github.com/alibabacloud-go/tea/tea"
+	rhttp "github.com/rdlucklib/rdluck_tools/http"
+	"hongze/hz_crm_api/models/cygx"
 	"hongze/hz_crm_api/utils"
+	"io"
 	"io/ioutil"
 	"net/http"
 	"strings"
-
-	rhttp "github.com/rdlucklib/rdluck_tools/http"
+	"time"
 )
 
 const (
@@ -23,6 +25,11 @@ const (
 	AlAppCode   = "22553c4ba74545568aba70ac6cfd441d"
 )
 
+var (
+	//文档 https://market.aliyun.com/apimarket/detail/cmapi013591?spm=5176.product-detail.detail.4.6d4f3dcaS0qPFI&endpoint=a3dd145f3865492ba6f02d0c09dd00a1
+	AliyunOcrBusinessCardCode = "6985365523f847c196e8e2222eaba550" // 阿里云OCR名片识别的code
+)
+
 type OcrItem struct {
 	Image string `json:"image"`
 }
@@ -157,3 +164,76 @@ func AliyunBusinessCardOcr(imgUrl string) (result OcrResult, err error) {
 
 	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"`
+}
+
+// 阿里云名片识别更换2024-01-02
+func AliyunOcrBusinessCard(imageurl string) (result *OcrBusinessCardResult, err error) {
+	url := "https://bizcard.market.alicloudapi.com/rest/160601/ocr/ocr_business_card.jso"
+	method := "POST"
+
+	payload := strings.NewReader(`{
+    "image":"` + imageurl + `"
+}`)
+	client := &http.Client{}
+	req, err := http.NewRequest(method, url, payload)
+
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	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))
+		}
+	}
+	result.TelCell = telCell
+
+	logItem := new(cygx.AliyunOcrLog)
+	logItem.ImgUrl = imageurl
+	logItem.Result = string(body)
+	logItem.CreateTime = time.Now()
+	err = cygx.AddAliyunOcrLog(logItem)
+	return
+}

+ 12 - 0
utils/common.go

@@ -28,6 +28,7 @@ import (
 	"strconv"
 	"strings"
 	"time"
+	"unicode"
 )
 
 // 随机数种子
@@ -2169,3 +2170,14 @@ func GetHtmlContentText(content string) (contentSub string, err error) {
 	contentSub = body
 	return
 }
+
+// 获取字符串中的阿拉伯数字,并返回字符串
+func GetArabicNumbers(str string) string {
+	var numbers []rune
+	for _, char := range str {
+		if unicode.IsDigit(char) {
+			numbers = append(numbers, char)
+		}
+	}
+	return string(numbers)
+}