123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- package eta_trial
- import (
- "encoding/json"
- "eta/eta_api/models"
- "eta/eta_api/models/eta_trial"
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/utils"
- "fmt"
- "io/ioutil"
- "net/http"
- "strings"
- )
- // EtaTrialUserReq 更新试用客户最后登录时间和次数请求体
- type EtaTrialUserReq struct {
- Mobile string `description:"手机号"`
- }
- // UpdateEtaTrialUserLogin eta_forum_hub服务-更新试用客户最后登录时间和次数
- func UpdateEtaTrialUserLogin(pars EtaTrialUserReq) (res bool, err error) {
- defer func() {
- if err != nil {
- alarm_msg.SendAlarmMsg(fmt.Sprintf("GetEtaTrialMobileCount, 新增试用客户手工录入权限失败, ErrMsg: %s", err.Error()), 3)
- }
- }()
- reqJson, e := json.Marshal(pars)
- if e != nil {
- err = fmt.Errorf("data json marshal err: %s", e.Error())
- return
- }
- _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/update_login")
- resp := new(models.BaseResponse)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- if resp.Ret != 200 {
- err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
- return
- }
- res = true
- return
- }
- // GetEtaTrialUserReq 获取试用客户请求体
- type GetEtaTrialUserReq struct {
- Mobile string `description:"手机号"`
- }
- type EtaTrialUserResp struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data eta_trial.EtaTrialUserItem
- Success bool `description:"true 执行成功,false 执行失败"`
- }
- // GetEtaTrialUser eta_forum_hub服务-获取试用客户信息
- func GetEtaTrialUser(pars GetEtaTrialUserReq) (res eta_trial.EtaTrialUserItem, err error) {
- defer func() {
- if err != nil {
- alarm_msg.SendAlarmMsg(fmt.Sprintf("GetEtaTrialUser, 获取试用客户信息, ErrMsg: %s", err.Error()), 3)
- }
- }()
- reqJson, e := json.Marshal(pars)
- if e != nil {
- err = fmt.Errorf("data json marshal err: %s", e.Error())
- return
- }
- _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/mobile_fetch")
- resp := new(EtaTrialUserResp)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- if resp.Ret != 200 {
- err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
- return
- }
- res = resp.Data
- return
- }
- // DisableEtaTrialUser eta_forum_hub服务-禁用试用客户
- func DisableEtaTrialUser(pars EtaTrialUserReq) (res bool, err error) {
- defer func() {
- if err != nil {
- alarm_msg.SendAlarmMsg(fmt.Sprintf("DisableEtaTrialUser eta_forum_hub服务-禁用试用客户, ErrMsg: %s", err.Error()), 3)
- }
- }()
- reqJson, e := json.Marshal(pars)
- if e != nil {
- err = fmt.Errorf("data json marshal err: %s", e.Error())
- return
- }
- _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/disable")
- resp := new(models.BaseResponse)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- if resp.Ret != 200 {
- err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
- return
- }
- res = true
- return
- }
- // RemoveEtaTrialUser eta_forum_hub服务-删除试用客户
- func RemoveEtaTrialUser(pars EtaTrialUserReq) (res bool, err error) {
- defer func() {
- if err != nil {
- alarm_msg.SendAlarmMsg(fmt.Sprintf("RemoveEtaTrialUser eta_forum_hub服务-删除试用客户失败, ErrMsg: %s", err.Error()), 3)
- }
- }()
- reqJson, e := json.Marshal(pars)
- if e != nil {
- err = fmt.Errorf("data json marshal err: %s", e.Error())
- return
- }
- _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/remove")
- resp := new(models.BaseResponse)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- if resp.Ret != 200 {
- err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
- return
- }
- res = true
- return
- }
- // EtaTrialUserEditReq 更新用户信息请求体
- type EtaTrialUserEditReq struct {
- RealName string `description:"姓名"`
- Position string `description:"职务"`
- Mobile string `description:"手机号"`
- Enabled int `description:"禁启用"`
- }
- // EditEtaTrialUser eta_forum_hub服务-编辑试用客户
- func EditEtaTrialUser(pars EtaTrialUserEditReq) (res bool, err error) {
- defer func() {
- if err != nil {
- alarm_msg.SendAlarmMsg(fmt.Sprintf("EditEtaTrialUser eta_forum_hub服务-编辑试用客户失败, ErrMsg: %s", err.Error()), 3)
- }
- }()
- reqJson, e := json.Marshal(pars)
- if e != nil {
- err = fmt.Errorf("data json marshal err: %s", e.Error())
- return
- }
- _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/edit")
- resp := new(models.BaseResponse)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- if resp.Ret != 200 {
- err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
- return
- }
- res = true
- return
- }
- // UpdateUserIndexNum eta_forum_hub服务-更新用户累计新增指标数
- func UpdateUserIndexNum(pars EtaTrialUserReq) (res bool, err error) {
- defer func() {
- if err != nil {
- alarm_msg.SendAlarmMsg(fmt.Sprintf(" UpdateUserIndexNum eta_forum_hub服务-更新用户累计新增指标数失败, ErrMsg: %s", err.Error()), 3)
- }
- }()
- reqJson, e := json.Marshal(pars)
- if e != nil {
- err = fmt.Errorf("data json marshal err: %s", e.Error())
- return
- }
- _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/update_index_num")
- resp := new(models.BaseResponse)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- if resp.Ret != 200 {
- err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
- return
- }
- res = true
- return
- }
- // UpdateUserChartNum eta_forum_hub服务-更新用户累计新增图表数
- func UpdateUserChartNum(pars EtaTrialUserReq) (res bool, err error) {
- defer func() {
- if err != nil {
- alarm_msg.SendAlarmMsg(fmt.Sprintf("UpdateUserChartNum eta_forum_hub服务-更新用户累计新增图表数失败, ErrMsg: %s", err.Error()), 3)
- }
- }()
- reqJson, e := json.Marshal(pars)
- if e != nil {
- err = fmt.Errorf("data json marshal err: %s", e.Error())
- return
- }
- _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/update_chart_num")
- resp := new(models.BaseResponse)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- if resp.Ret != 200 {
- err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
- return
- }
- res = true
- return
- }
- // UpdateEtaTrialUserActiveTimeReq 更新试用客户活跃时长请求体
- type UpdateEtaTrialUserActiveTimeReq struct {
- Mobile string `description:"手机号"`
- UserName string `description:"用户姓名"`
- ActiveTime int `description:"活跃时长, 单位秒"`
- Part string `description:"活跃板块"`
- }
- // UpdateEtaTrialUserActiveTime eta_forum_hub服务-更新试用客户活跃时长
- func UpdateEtaTrialUserActiveTime(pars UpdateEtaTrialUserActiveTimeReq) (res bool, err error) {
- defer func() {
- if err != nil {
- alarm_msg.SendAlarmMsg(fmt.Sprintf("UpdateEtaTrialUserActiveTime eta_forum_hub服务-更新试用客户活跃时长失败, ErrMsg: %s", err.Error()), 3)
- }
- }()
- reqJson, e := json.Marshal(pars)
- if e != nil {
- err = fmt.Errorf("data json marshal err: %s", e.Error())
- return
- }
- _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/update_active_time")
- resp := new(models.BaseResponse)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- if resp.Ret != 200 {
- err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
- return
- }
- res = true
- return
- }
- // UpdateEtaTrialUserLoginDurationReq 更新试用客户登录时长请求体
- type UpdateEtaTrialUserLoginDurationReq struct {
- Mobile string `description:"手机号"`
- UserName string `description:"用户姓名"`
- ActiveTime int `description:"活跃时长, 单位秒"`
- }
- // UpdateEtaTrialUserLoginDuration eta_forum_hub服务-更新试用客户登录时长
- func UpdateEtaTrialUserLoginDuration(pars UpdateEtaTrialUserLoginDurationReq) (res bool, err error) {
- defer func() {
- if err != nil {
- alarm_msg.SendAlarmMsg(fmt.Sprintf("UpdateEtaTrialUserLoginDuration eta_forum_hub服务-更新试用客户登录时长失败, ErrMsg: %s", err.Error()), 3)
- }
- }()
- reqJson, e := json.Marshal(pars)
- if e != nil {
- err = fmt.Errorf("data json marshal err: %s", e.Error())
- return
- }
- _, resultByte, err := post(string(reqJson), "/v1/eta_trial/user/update_login_duration")
- resp := new(models.BaseResponse)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- if resp.Ret != 200 {
- err = fmt.Errorf("result: %s, errmsg: %s", resp.Msg, resp.ErrMsg)
- return
- }
- res = true
- return
- }
- // post
- func post(paramStr string, urlStr string) (resp *models.BaseResponse, result []byte, err error) {
- if utils.ETA_FORUM_HUB_URL == "" {
- err = fmt.Errorf("ETA社区桥接服务地址为空")
- return
- }
- postUrl := utils.ETA_FORUM_HUB_URL + urlStr
- result, err = HttpPost(postUrl, paramStr, "application/json")
- if err != nil {
- err = fmt.Errorf("调用ETA社区桥接服务接口失败 error:%s", err.Error())
- return
- }
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- return
- }
- func HttpPost(url, postData string, params ...string) ([]byte, error) {
- body := ioutil.NopCloser(strings.NewReader(postData))
- client := &http.Client{}
- req, err := http.NewRequest("POST", url, body)
- if err != nil {
- return nil, err
- }
- contentType := "application/x-www-form-urlencoded;charset=utf-8"
- if len(params) > 0 && params[0] != "" {
- contentType = params[0]
- }
- req.Header.Set("Content-Type", contentType)
- req.Header.Set("authorization", utils.MD5(utils.ETA_FORUM_HUB_NAME_EN+utils.ETA_FORUM_HUB_MD5_KEY))
- resp, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- b, err := ioutil.ReadAll(resp.Body)
- utils.FileLog.Debug("HttpPost:" + string(b))
- return b, err
- }
- // get
- func get(paramStr string, urlStr string) (resp *models.BaseResponse, result []byte, err error) {
- if utils.ETA_FORUM_HUB_URL == "" {
- err = fmt.Errorf("ETA社区桥接服务地址为空")
- return
- }
- urlStr = urlStr + "?" + paramStr
- getUrl := utils.ETA_FORUM_HUB_URL + urlStr
- result, err = HttpGet(getUrl)
- if err != nil {
- err = fmt.Errorf("调用ETA社区桥接服务接口失败 error:%s", err.Error())
- return
- }
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- return
- }
- func HttpGet(url string) ([]byte, error) {
- client := &http.Client{}
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- return nil, err
- }
- req.Header.Set("authorization", utils.MD5(utils.ETA_FORUM_HUB_NAME_EN+utils.ETA_FORUM_HUB_MD5_KEY))
- resp, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- b, err := ioutil.ReadAll(resp.Body)
- utils.FileLog.Debug("HttpPost:" + string(b))
- return b, err
- }
|