Kaynağa Gözat

未付费申请试用

xingzai 2 yıl önce
ebeveyn
işleme
07b4e861a0
6 değiştirilmiş dosya ile 567 ekleme ve 2 silme
  1. 3 2
      controllers/article.go
  2. 249 0
      controllers/user.go
  3. 2 0
      models/user.go
  4. 9 0
      routers/commentsRouter.go
  5. 76 0
      services/aliyun.go
  6. 228 0
      services/baidu.go

+ 3 - 2
controllers/article.go

@@ -56,6 +56,7 @@ func (this *ArticleController) Detail() {
 		br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
 		return
 	}
+	//HasPermission int `description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下(ficc),3:无该品类权限,已提交过申请,4:无该品类权限,未提交过申请,5:潜在客户,未提交过申请,6:潜在客户,已提交过申请"`
 	//`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
 	if user.CompanyId > 1 {
 		companyPermission, err := models.GetCompanyPermission(user.CompanyId)
@@ -74,7 +75,7 @@ func (this *ArticleController) Detail() {
 		detail.Abstract, _ = services.GetReportContentTextSubNew(detail.Abstract)
 		if companyPermission == "" {
 			if applyCount > 0 {
-				hasPermission = 5
+				hasPermission = 6
 			} else {
 				hasPermission = 2
 			}
@@ -195,7 +196,7 @@ func (this *ArticleController) Detail() {
 		if applyCount > 0 {
 			hasPermission = 5
 		} else {
-			hasPermission = 4
+			hasPermission = 6
 		}
 	}
 Loop:

+ 249 - 0
controllers/user.go

@@ -2,7 +2,9 @@ package controllers
 
 import (
 	"encoding/json"
+	"fmt"
 	"hongze/hongze_clpt/models"
+	"hongze/hongze_clpt/services"
 	"hongze/hongze_clpt/utils"
 	"strconv"
 	"strings"
@@ -131,3 +133,250 @@ func (this *UserController) Detail() {
 	br.Msg = "获取成功"
 	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.RealName == "" {
+		req.RealName = user.RealName
+	}
+	if req.CompanyName == "" {
+		req.CompanyName = user.CompanyName
+	}
+	uid := user.UserId
+
+	var title string
+	tryType := req.TryType
+	detailId := req.DetailId
+	if tryType == "Article" {
+		detail, err := models.GetArticleDetailById(detailId)
+		if err != nil {
+			br.Msg = "获取信息失败"
+			br.ErrMsg = "获取信息失败,Err:" + err.Error()
+			return
+		}
+		title = detail.Title
+	}
+
+	fmt.Println(title)
+	//缓存校验
+	cacheKey := fmt.Sprint("xygx:apply_record:add:", uid)
+	ttlTime := utils.Rc.GetRedisTTL(cacheKey)
+	if ttlTime > 0 {
+		br.Msg = "申请失败,申请过于频繁"
+		br.ErrMsg = "申请失败,申请过于频繁"
+		return
+	}
+	utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*10)
+
+	//判断是否已经申请过
+	applyCount, err := models.GetApplyRecordCount(uid)
+	if err != nil && err.Error() != utils.ErrNoRow() {
+		br.Msg = "获取信息失败"
+		br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
+		return
+	}
+
+	if applyCount > 0 {
+		br.Msg = "您已提交申请,请耐心等待。"
+		br.IsSendEmail = false
+		return
+	}
+
+	//判断是否存在申请
+	var sellerMobile string
+	if req.ApplyMethod == 2 {
+		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
+			}
+		}
+	}
+
+	//获取销售信息
+	sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
+	if err != nil && err.Error() != utils.ErrNoRow() {
+		br.Msg = "申请失败"
+		br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
+		return
+	}
+	if sellerItem != nil {
+		sellerMobile = sellerItem.Mobile
+		//推送模板消息
+		mobile := user.Mobile
+		if mobile == "" {
+			mobile = user.Email
+		}
+	}
+	//用户状态,1:潜在客户 、2:现有客户 、3:FICC客户 、4:现有客户(正式,无对应权限) 、5:现有客户(试用,无对应权限)  、6:现有客户(试用暂停) 、7:现有客户(冻结) 、8:现有客户(流失)?
+	CompanyIdType := 1
+	applyMethod := ""
+	cnf, _ := models.GetConfigByCode("tpl_msg")
+	if cnf != nil {
+		if sellerItem != nil {
+			cnf.ConfigValue = sellerItem.Mobile
+			companyItem, err := models.GetCompanyDetailById(user.CompanyId)
+			if err != nil && err.Error() != utils.ErrNoRow() {
+				br.Msg = "获取信息失败"
+				br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
+				return
+			}
+			if companyItem != nil && companyItem.CompanyId > 0 {
+				companyProduct, err := models.GetCompanyProductDetail(user.CompanyId, 2)
+				if err != nil && err.Error() != utils.ErrNoRow() {
+					br.Msg = "获取信息失败"
+					br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
+					return
+				}
+				if companyProduct != nil && companyProduct.IsSuspend == 1 {
+					CompanyIdType = 6
+				} else {
+					switch companyItem.Status {
+					case "正式":
+						CompanyIdType = 4
+					case "试用":
+						CompanyIdType = 5
+					case "冻结":
+						CompanyIdType = 7
+					case "流失":
+						CompanyIdType = 8
+					}
+				}
+				applyMethod = companyItem.Status + "客户申请"
+				if detailId > 0 {
+					if companyProduct != nil && companyProduct.IsSuspend == 1 {
+						applyMethod = "试用暂停客户"
+					} else {
+						if companyItem.Status == "正式" || companyItem.Status == "试用" {
+							applyMethod = companyItem.Status + "客户申请,无对应权限"
+						} else if companyItem.Status == "冻结" || companyItem.Status == "流失" {
+							applyMethod = companyItem.Status + "客户"
+						}
+					}
+					applyMethod = applyMethod + "," + title
+				}
+			}
+		} else {
+			//获取销售信息
+			sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 1)
+			if err != nil && err.Error() != utils.ErrNoRow() {
+				br.Msg = "申请失败"
+				br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
+				return
+			}
+			if sellerItem != nil {
+				CompanyIdType = 3
+				applyMethod = "FICC客户"
+			} else {
+				CompanyIdType = 1
+				applyMethod = "潜在客户"
+			}
+			if detailId > 0 {
+				applyMethod = applyMethod + "," + title
+			}
+		}
+		openIpItem, _ := models.GetUserRecordByMobile(4, cnf.ConfigValue)
+		if openIpItem != nil && openIpItem.OpenId != "" {
+			if req.ApplyMethod != 2 {
+				req.RealName = user.RealName
+				req.CompanyName = user.CompanyName
+			}
+			utils.FileLog.Info("推送消息 %s %s,%s,%s,%s", req.RealName, req.CompanyName, mobile, openIpItem.OpenId, applyMethod)
+			go services.SendPermissionApplyTemplateMsg(req.RealName, req.CompanyName, mobile, applyMethod, openIpItem)
+		}
+	}
+	err = models.AddApplyRecord(&req, user.Mobile, user.CompanyName, uid, user.CompanyId, CompanyIdType)
+	if err != nil {
+		br.Msg = "申请失败"
+		br.ErrMsg = "申请失败,Err:" + err.Error()
+		return
+	}
+	//添加成功后,设置5分钟缓存,不允许重复添加
+	//utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*60)
+
+	br.Msg = "申请成功!"
+	br.Ret = 200
+	br.Success = true
+	br.Data = sellerMobile
+}

+ 2 - 0
models/user.go

@@ -147,6 +147,8 @@ type ApplyTryReq struct {
 	RealName        string `description:"姓名"`
 	CompanyName     string `description:"公司名称"`
 	ApplyMethod     int    `description:"1:已付费客户申请试用,2:非客户申请试用,3:非客户申请试用(ficc下,不需要进行数据校验)"`
+	TryType         string `description:"提交类型,Article:文章、Activity:活动"`
+	DetailId        int    `description:"详情ID"`
 }
 
 type CountryCode struct {

+ 9 - 0
routers/commentsRouter.go

@@ -106,6 +106,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["hongze/hongze_clpt/controllers:UserController"] = append(beego.GlobalControllerRouter["hongze/hongze_clpt/controllers:UserController"],
+        beego.ControllerComments{
+            Method: "ApplyTryOut",
+            Router: `/apply/try`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["hongze/hongze_clpt/controllers:UserController"] = append(beego.GlobalControllerRouter["hongze/hongze_clpt/controllers:UserController"],
         beego.ControllerComments{
             Method: "Detail",

+ 76 - 0
services/aliyun.go

@@ -0,0 +1,76 @@
+package services
+
+import (
+	"crypto/tls"
+	"encoding/base64"
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"strings"
+
+	rhttp "github.com/rdlucklib/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
+}

+ 228 - 0
services/baidu.go

@@ -0,0 +1,228 @@
+package services
+
+import (
+	"encoding/base64"
+	"encoding/json"
+	"errors"
+	"fmt"
+	"github.com/rdlucklib/rdluck_tools/http"
+	"net/url"
+)
+
+/*
+//名片识别
+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))
+	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())
+	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())
+	if err != nil {
+		fmt.Println("err:", err.Error())
+		return
+	}
+	err = json.Unmarshal(cardBody, &item)
+	return
+}