Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

CnChunfeng / crm_server

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
BlameHistoryPermalink
Switch branch/tag
  • crm_server
  • internal
  • service
  • dingding.go
  • 朱继来's avatar
    添加跟进用户任务 · e3c1fe0b
    朱继来 committed 4 years ago
    e3c1fe0b Browse Files
dingding.go 5.03 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
package service

import (
	"crm-server/configs"
	"crm-server/internal/common"
	"crm-server/internal/dao"
	"encoding/json"
	"fmt"
	"github.com/ichunt2019/logger"
	"io/ioutil"
	"net/http"
	"net/url"
	"strconv"
	"time"
)

const APIMD5STR string = "fh6y5t4rr351d2c3bryi"
const APIDOMAIN string = configs.Api_url+"/msg/sendMessageByAuto"
const ADMINTEL  int64 = 17600091664
const DINGALERTURL string = configs.Api_url+"/msg/dingalert"
var TASK_TYPE_VAL = map[int]string{1:"领取分配", 2:"未成交释放(30)", 3:"已成交释放(180)", 4:"快速找料", 5:"老客户活跃", 6:"BOM询价", 7:"H5留言", 8:"发票提醒", 9:"开发板方案", 10:"跟进用户"}

func AdminErr(err error)  {
	if err != nil {
		fmt.Println(err)
		SendMessage(ADMINTEL,err.Error())
	}
}


func SendMessage(mobile int64 , content string){

	if mobile != 0 {
		timeNow := time.Now().Unix()
		requestContent,_ := json.Marshal(map[string]string{"content":content})
		requestTel,_ := json.Marshal([]int64{mobile})

		resp,err := http.PostForm(APIDOMAIN,url.Values{
			"data" : {string(requestContent)},
			"touser" : {string(requestTel)},
			"keyword" : {"register_nopay_notify"},
			"k1" : {strconv.FormatInt(int64(timeNow),10)},
			"k2" : {common.Md5(common.Md5(strconv.FormatInt(int64(timeNow),10))+APIMD5STR)},
			"is_ignore" : {},
		})

		if err != nil {
			fmt.Print(err)
		}
		defer resp.Body.Close()
	}
}

/**
	bom询价无结果 钉钉推送
	user_id 内部用户ID
	client_account 客户账号
 */
func SendBomMsg(user_id int64 , client_account string){
	if user_id != 0 {
		timeNow := time.Now().Unix()
		requestContent,_ := json.Marshal(map[string]string{"account":client_account})
		requestTel,_ := json.Marshal([]int64{user_id})

		resp,err := http.PostForm(APIDOMAIN,url.Values{
			"data" : {string(requestContent)},
			"touser" : {string(requestTel)},
			"keyword" : {"bom_no_result"},
			"k1" : {strconv.FormatInt(int64(timeNow),10)},
			"k2" : {common.Md5(common.Md5(strconv.FormatInt(int64(timeNow),10))+APIMD5STR)},
			"is_ignore" : {},
		})

		if err != nil {
			fmt.Print(err)
		}
		defer resp.Body.Close()
	}
}

func DingAlert(token string, text string, at string) []byte {
	curr_time := time.Now().Unix()

	resp, err := http.PostForm(DINGALERTURL, url.Values{
		"token": {token},
		"text": {text},
		"at": {at},
		"k1": {strconv.FormatInt(int64(curr_time), 10)},
		"k2": {common.Md5(common.Md5(strconv.FormatInt(int64(curr_time), 10)) + APIMD5STR)},
	})
	defer resp.Body.Close()

	if err != nil {
		logger.Info(err.Error())
	}

	body, _ := ioutil.ReadAll(resp.Body) // 获取接口返回数据
	return body
}

/**
新任务记录钉钉推送
crm_user_id CRM用户ID
sale_id 客服ID
task_type 1-领取分配, 2-未成交释放(30), 3-已成交释放(180), 4-快速找料, 5-老客户活跃, 6-BOM找料, 7-H5留言,8-发票提醒,9-开发板方案
limit_time 截止时间
*/
func SendNewTaskMsg(crm_user_id int, sale_id int , task_type int, limit_time string){
	if sale_id == 0 {
		return
	}

	if TASK_TYPE_VAL[task_type] == "" {
		return
	}

	var user_id int64 // 客服前台ID
	dao.GetCmsDb().Get(&user_id, "select user_id from lie_intracode where admin_id = ?", sale_id)

	if user_id == 0 {
		DingAlert(configs.Ding_crm_task_token, "任务告警:新增"+TASK_TYPE_VAL[task_type]+"任务记录钉钉推送失败,内部用户ID("+strconv.Itoa(sale_id)+")未绑定前台账号,CRM用户ID:"+strconv.Itoa(crm_user_id), "")
		return
	}

	timeNow := time.Now().Unix()
	requestContent,_ := json.Marshal(map[string]string{"type": TASK_TYPE_VAL[task_type], "limit_time": limit_time})
	requestTel,_ := json.Marshal([]int64{user_id})

	resp,err := http.PostForm(APIDOMAIN,url.Values{
		"data" : {string(requestContent)},
		"touser" : {string(requestTel)},
		"keyword" : {"new-task-notify"},
		"k1" : {strconv.FormatInt(int64(timeNow),10)},
		"k2" : {common.Md5(common.Md5(strconv.FormatInt(int64(timeNow),10))+APIMD5STR)},
		"is_ignore" : {},
	})

	if err != nil {
		DingAlert(configs.Ding_crm_task_token, "任务告警:新增"+TASK_TYPE_VAL[task_type]+"任务记录钉钉推送失败,CRM用户ID:"+strconv.Itoa(crm_user_id), "")
	}
	defer resp.Body.Close()
}

// CRM任务推送邮件
func SendMail(crm_user_id int, sale_id int, task_type int, contents string) bool {
	if sale_id == 0 {
		return false
	}

	var email string // 客服邮箱
	dao.GetCmsDb().Get(&email, "select email from user_info where userId = ?", sale_id)

	timeNow := time.Now().Unix()
	requestContent,_ := json.Marshal(map[string]string{"contents": contents})
	requestTel,_ := json.Marshal([]string{email})

	resp,err := http.PostForm(APIDOMAIN,url.Values{
		"data" : {string(requestContent)},
		"touser" : {string(requestTel)},
		"keyword" : {"crm-task-send-mail"},
		"k1" : {strconv.FormatInt(int64(timeNow),10)},
		"k2" : {common.Md5(common.Md5(strconv.FormatInt(int64(timeNow),10))+APIMD5STR)},
		"is_ignore" : {"1"},
	})
	defer resp.Body.Close()

	if err != nil {
		DingAlert(configs.Ding_crm_task_token, "任务告警:"+TASK_TYPE_VAL[task_type]+"任务记录邮件推送失败,CRM用户ID:"+strconv.Itoa(crm_user_id), "")
		return false
	}

	return true
}