rdluck 4 жил өмнө
parent
commit
cc8a2339b0

+ 2 - 2
controllers/advice.go

@@ -14,9 +14,9 @@ type AdviceController struct {
 
 // @Title 新增优化建议
 // @Description 新增优化建议接口
-// @Param	request	body company.AddCygxAdviceReq true "type json string"
+// @Param	request	body models.AddCygxAdviceReq true "type json string"
 // @Success Ret=200 新增成功
-// @router /approval/approve [post]
+// @router /add [post]
 func (this *AdviceController) ApplyApprove() {
 	br := new(models.BaseResponse).Init()
 	defer func() {

+ 104 - 4
controllers/user.go

@@ -458,14 +458,14 @@ func (this *UserController) BrowseHistoryList() {
 	}
 	startSize = utils.StartIndex(currentIndex, pageSize)
 
-	endDate:=time.Now().AddDate(0,-1,0).Format(utils.FormatDate)
-	total, err := models.GetArticleUserBrowseHistoryCount(userId,endDate)
+	endDate := time.Now().AddDate(0, -1, 0).Format(utils.FormatDate)
+	total, err := models.GetArticleUserBrowseHistoryCount(userId, endDate)
 	if err != nil {
 		br.Msg = "获取数据失败"
 		br.ErrMsg = "获取数据失败,Err:" + err.Error()
 		return
 	}
-	list, err := models.GetArticleUserBrowseHistoryList(startSize, pageSize, userId,endDate)
+	list, err := models.GetArticleUserBrowseHistoryList(startSize, pageSize, userId, endDate)
 	if err != nil {
 		br.Msg = "获取数据失败"
 		br.ErrMsg = "获取数据失败,Err:" + err.Error()
@@ -494,7 +494,7 @@ func (this *UserController) BrowseHistoryList() {
 	for i := 0; i < lenList; i++ {
 		item := list[i]
 		article := articleMap[item.ArticleId]
-		if article!=nil {
+		if article != nil {
 			list[i].Title = article.Title
 			list[i].TitleEn = article.TitleEn
 			list[i].UpdateFrequency = article.UpdateFrequency
@@ -516,3 +516,103 @@ func (this *UserController) BrowseHistoryList() {
 	br.Data = resp
 }
 
+// @Title 未付费申请试用
+// @Description 未付费申请试用
+// @Param	request	body models.ApplyTryReq true "type json string"
+// @Success 200
+// @router /apply/try [post]
+func (this *UserController) ApplyTryOut() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	user := this.User
+	if user == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,SysUser Is Empty"
+		br.Ret = 408
+		return
+	}
+	mobile := user.Mobile
+	var req models.ApplyTryReq
+	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
+	if err != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+
+	if req.BusinessCardUrl == "" {
+		br.Msg = "请上传名片"
+		return
+	}
+
+	if req.RealName == "" {
+		br.Msg = "请输入姓名"
+		return
+	}
+
+	if req.CompanyName == "" {
+		br.Msg = "请输入公司名称"
+		return
+	}
+
+	if req.BusinessCardUrl != "" && utils.RunMode == "release" {
+		card, err := services.GetBusinessCard(req.BusinessCardUrl)
+		if err != nil {
+			br.Msg = "名片识别失败"
+			br.ErrMsg = "名片识别失败,Err:" + err.Error()
+			return
+		}
+		mobileStr := strings.Join(card.WordsResult.MOBILE, ",")
+		isFlag := true
+		if mobile != "" {
+			if strings.Contains(mobileStr, mobile) || mobileStr == "" {
+				isFlag = true
+			} else {
+				isFlag = false
+			}
+		}
+		if !isFlag {
+			//阿里云识别
+			if utils.RunMode == "release" {
+				aliyunResult, err := services.AliyunBusinessCard(req.BusinessCardUrl)
+				if err != nil {
+					br.Msg = "识别失败"
+					br.ErrMsg = "识别失败,Err:" + err.Error()
+					return
+				}
+				if !aliyunResult.Success {
+					br.Msg = "识别失败"
+					br.ErrMsg = "识别失败"
+					return
+				}
+
+				mobileStr := strings.Join(aliyunResult.TelCell, ",")
+				if mobile != "" {
+					if strings.Contains(mobileStr, mobile) {
+						isFlag = true
+					} else {
+						isFlag = false
+					}
+				}
+			}
+		}
+		if !isFlag {
+			br.Msg = "名片手机号与所填手机号不匹配,请重新填写"
+			br.ErrMsg = "mobile:" + mobile
+			return
+		}
+	}
+
+	err = models.AddApplyRecord(&req, user.Mobile, user.UserId)
+	if err != nil {
+		br.Msg = "申请失败"
+		br.ErrMsg = "申请失败,Err:" + err.Error()
+		return
+	}
+	br.Msg = "申请成功!"
+	br.Ret = 200
+	br.Success = true
+}

+ 1 - 1
models/advice.go

@@ -6,7 +6,7 @@ import (
 )
 
 type CygxAdvice struct {
-	AdviceId      int       `orm:"column(id);pk" description:"优化建议id"`
+	AdviceId      int       `orm:"column(advice_id);pk" description:"优化建议id"`
 	UserId        int       `description:"用户id"`
 	UserRealName  string    `description:"姓名"`
 	CompanyId     int       `description:"公司id"`

+ 2 - 0
models/db.go

@@ -35,5 +35,7 @@ func init() {
 		new(CygxArticleViewRecord),
 		new(CygxAdvice),
 		new(MsgCode),
+		new(Resource),
+		new(CygxApplyRecord),
 	)
 }

+ 46 - 0
models/user.go

@@ -195,3 +195,49 @@ type ArticleBrowseHistoryListResp struct {
 	List   []*ArticleInterviewApplyList
 	Paging *paging.PagingItem
 }
+
+type ApplyTryReq struct {
+	BusinessCardUrl string `description:"名片地址"`
+	RealName        string `description:"姓名"`
+	CompanyName     string `description:"公司名称"`
+	ApplyMethod     int    `description:"1:已付费客户申请试用,2:非客户申请试用"`
+}
+
+type CygxApplyRecord struct {
+	ApplyRecordId   int       `orm:"column(apply_record_id);pk" description:"申请试用id"`
+	BusinessCardUrl string    `description:"名片地址"`
+	RealName        string    `description:"姓名"`
+	CompanyName     string    `description:"公司名称"`
+	Mobile          string    `description:"手机号"`
+	CreateTime      time.Time `description:"创建时间"`
+	ApplyMethod     int       `description:"1:已付费客户申请试用,2:非客户申请试用"`
+}
+
+func AddApplyRecord(item *ApplyTryReq, mobile string, userId int) (err error) {
+	o := orm.NewOrm()
+	o.Begin()
+	defer func() {
+		if err != nil {
+			o.Rollback()
+		} else {
+			o.Commit()
+		}
+	}()
+
+	sql := `INSERT INTO cygx_apply_record (user_id,business_card_url, real_name,company_name, mobile,create_time, apply_method)
+          VALUES(?,?,?,?,?,?,?) `
+	_, err = o.Raw(sql, userId, item.BusinessCardUrl, item.RealName, item.CompanyName, mobile, time.Now(), item.ApplyMethod).Exec()
+	if err != nil {
+		return
+	}
+	msql := `UPDATE  wx_user
+		SET
+		  note = ?,
+		  is_note = 1,
+		  apply_method = ?,
+          real_name=?,
+		  mobile=?
+		WHERE user_id = ? `
+	_, err = o.Raw(msql, item.CompanyName, item.ApplyMethod, item.RealName, mobile, userId).Exec()
+	return
+}

+ 77 - 0
services/aliyun.go

@@ -0,0 +1,77 @@
+package services
+
+import (
+	"crypto/tls"
+	"encoding/base64"
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"strings"
+
+	rhttp "rdluck_tools/http"
+)
+
+const (
+	AlAppKey    = "203889624"
+	AlAppSecret = "voampGGl0yGNx5xA1sZdmZlV2EiBct2P"
+	AlAppCode   = "22553c4ba74545568aba70ac6cfd441d"
+)
+
+type OcrItem struct {
+	Image string `json:"image"`
+}
+
+type OcrResult struct {
+	Addr    []string `json:"addr"`
+	Company []string `json:"company"`
+	Success bool     `json:"success"`
+	TelCell []string `json:"tel_cell"`
+}
+
+///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`
+
+
+	imgBody, err := rhttp.Get(imgUrl)
+	if err != nil {
+		return
+	}
+	imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
+
+	item := new(OcrItem)
+	item.Image = imageBase64
+	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)))
+	if err != nil {
+		fmt.Println("err:" + err.Error())
+		return
+	}
+	req.Header.Set("Content-Type", "application/json")
+	req.Header.Set("Authorization", "APPCODE "+AlAppCode)
+	resp, err := client.Do(req)
+	if err!=nil {
+		fmt.Println("client.Do Err:"+err.Error())
+		return
+	}
+	fmt.Println("resp")
+	fmt.Println(resp)
+	defer resp.Body.Close()
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		fmt.Println("err:" + err.Error())
+		return
+	}
+	fmt.Println("body:",string(body))
+	err = json.Unmarshal(body, &result)
+	return
+}

+ 234 - 0
services/baidu.go

@@ -0,0 +1,234 @@
+package services
+
+import (
+	"encoding/base64"
+	"encoding/json"
+	"errors"
+	"fmt"
+	"hongze/hongze_cygx/utils"
+	"net/url"
+	"rdluck_tools/http"
+)
+
+/*
+//名片识别
+func init() {
+	fmt.Println("start")
+	services.GetBusinessCard()
+	fmt.Println("end")
+}
+*/
+
+type BdAccessToken struct {
+	AccessToken string `json:"access_token"`
+	Error       string
+}
+
+func GetAccessToken() (accessTokenStr string, err error) {
+	baiduApiKey := "nMfRuZ0qBCsVWfGGKekaoMqh"
+	baiduBdSecretKey := "N6xcrUizYijMf47iTDDyPBm3lTOrvVSF"
+	getUrl := `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=` + baiduApiKey + `&client_secret=` + baiduBdSecretKey + `&`
+	body, err := http.Post(getUrl, "")
+	var accessToken BdAccessToken
+	err = json.Unmarshal(body, &accessToken)
+	if err != nil {
+		fmt.Println("err:" + err.Error())
+		return
+	}
+	if accessToken.Error != "" {
+		err = errors.New("获取token失败,Err:" + accessToken.Error)
+		return
+	}
+	accessTokenStr = accessToken.AccessToken
+	return
+}
+
+type BdBussinessCard struct {
+	Errno          int    `json:"error_code"`
+	ErrorMsg       string `json:"error_msg"`
+	Logid          string
+	WordsResultNum int
+	WordsResult    struct {
+		ADDR    []string `json:"ADDR"`
+		FAX     []string `json:"FAX"`
+		MOBILE  []string `json:"MOBILE"`
+		NAME    []string `json:"NAME"`
+		PC      []string `json:"PC"`
+		URL     []string `json:"URL"`
+		TEL     []string `json:"TEL"`
+		COMPANY []string `json:"COMPANY"`
+		TITLE   []string `TITLE`
+		EMAIL   []string `EMAIL`
+	} `json:"words_result"`
+	//{"log_id":1332206693559304192,"error_msg":"target detect error","error_code":282102}
+}
+
+func GetBusinessCard(imgUrl string) (item *BdBussinessCard, err error) {
+	imgBody, err := http.Get(imgUrl)
+	if err != nil {
+		return
+	}
+	//rnStr := utils.GetRandStringNoSpecialChar(5)
+	//savePath := time.Now().Format(utils.FormatDateTimeUnSpace) + rnStr + ".jpg"
+	//err = file.SaveFile(imgBody, savePath)
+	//if err != nil {
+	//	return
+	//}
+	//defer func() {
+	//	os.Remove(savePath)
+	//}()
+	accessToken, err := GetAccessToken()
+	fmt.Println("accessToken")
+	fmt.Println(accessToken)
+	if err != nil {
+		fmt.Print("GetAccessToken Err:" + err.Error())
+	}
+	bussinessCardUrl := `https://aip.baidubce.com/rest/2.0/ocr/v1/business_card?access_token=` + accessToken
+	//ff, _ := os.Open(savePath)
+	//defer ff.Close()
+	//sourcebuffer := make([]byte, 500000)
+	//n, _ := ff.Read(sourcebuffer)
+	//sourcestring := base64.StdEncoding.EncodeToString(sourcebuffer[:n])
+	imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
+	imgData := imageBase64
+	param := url.Values{}
+	param.Set("image", imgData)
+	cardBody, err := http.Post(bussinessCardUrl, param.Encode())
+	fmt.Println("body:",string(cardBody))
+	//fmt.Println(string(cardBody))
+	utils.FileLog.Info("cardBody:%s" + string(cardBody))
+	if err != nil {
+		fmt.Println("err:", err.Error())
+		return
+	}
+	err = json.Unmarshal(cardBody, &item)
+	if err != nil {
+		fmt.Println("line err:", err.Error())
+	}
+	return
+}
+
+type CardAccurate struct {
+	WordsResultNum int `json:"words_result_num"`
+	WordsResult    []struct {
+		Words string `json:"words"`
+	} `json:"words_result"`
+	ErrorCode int    `json:"error_code"`
+	ErrorMsg  string `json:"error_msg"`
+}
+
+//通用文字(高精度)
+func GetBusinessCardByAccurate(imgUrl string) (item *CardAccurate, err error) {
+	imgBody, err := http.Get(imgUrl)
+	if err != nil {
+		return
+	}
+	//rnStr := utils.GetRandStringNoSpecialChar(5)
+	//savePath := time.Now().Format(utils.FormatDateTimeUnSpace) + rnStr + ".jpg"
+	//err = file.SaveFile(imgBody, savePath)
+	//if err != nil {
+	//	return
+	//}
+	//defer func() {
+	//	os.Remove(savePath)
+	//}()
+	//
+	//nowStr:=time.Now().Format(utils.FormatDateTimeUnSpace)
+	//outPath := "./"+nowStr+".jpg"
+	//缩放
+
+	//file, err := os.Open(savePath)
+	//if err != nil {
+	//	fmt.Println("file err:" + err.Error())
+	//	return
+	//}
+	//
+	//// decode jpeg into image.Image
+	//img, err := jpeg.Decode(file)
+	//if err != nil {
+	//	fmt.Println("jpeg Decode:" + err.Error())
+	//	return
+	//}
+	//width:=img.Bounds().Dx()
+	//height:=img.Bounds().Dy()
+	//fmt.Println(width,height)
+	//
+	//if width>=4096 {
+	//	width=4000
+	//}
+	//
+	//if height>=4096 {
+	//	height=4000
+	//}
+	//
+	//file.Close()
+	//
+	//fmt.Println(width,height)
+	//
+	//m := resize.Resize(uint(width),uint(height), img, resize.Lanczos3)
+	//
+	//out, err := os.Create(outPath)
+	//if err != nil {
+	//	fmt.Println("Create:" + err.Error())
+	//	return
+	//}
+	//defer out.Close()
+	//
+	//jpeg.Encode(out, m, nil)
+
+	accessToken, err := GetAccessToken()
+	fmt.Println("accessToken")
+	fmt.Println(accessToken)
+	if err != nil {
+		fmt.Print("GetAccessToken Err:" + err.Error())
+	}
+	bussinessCardUrl := `https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=` + accessToken
+	//ff, _ := os.Open(outPath)
+	//defer ff.Close()
+	//sourcebuffer := make([]byte, 500000)
+	//n, _ := ff.Read(sourcebuffer)
+	//sourcestring := base64.StdEncoding.EncodeToString(sourcebuffer[:n])
+	//imgData := sourcestring
+
+	imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
+
+	param := url.Values{}
+	param.Set("image", imageBase64)
+	cardBody, err := http.Post(bussinessCardUrl, param.Encode())
+	utils.FileLog.Info("GetBusinessCardByAccurate:%s" + string(cardBody))
+	if err != nil {
+		fmt.Println("err:", err.Error())
+		return
+	}
+	err = json.Unmarshal(cardBody, &item)
+	return
+}
+
+
+
+//通用文字(高精度)
+func GetBdGeneralBasic(imgUrl string) (item *CardAccurate, err error) {
+	imgBody, err := http.Get(imgUrl)
+	if err != nil {
+		return
+	}
+
+	accessToken, err := GetAccessToken()
+	if err != nil {
+		fmt.Print("GetAccessToken Err:" + err.Error())
+	}
+	bussinessCardUrl := `https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?access_token=` + accessToken
+
+	imageBase64 := base64.StdEncoding.EncodeToString(imgBody)
+
+	param := url.Values{}
+	param.Set("image", imageBase64)
+	cardBody, err := http.Post(bussinessCardUrl, param.Encode())
+	utils.FileLog.Info("GetBusinessCardByAccurate:%s" + string(cardBody))
+	if err != nil {
+		fmt.Println("err:", err.Error())
+		return
+	}
+	err = json.Unmarshal(cardBody, &item)
+	return
+}