123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package service
- import (
- "context"
- "encoding/json"
- "log"
- "time"
- "app.yhyue.com/moapp/jybase/date"
- "app.yhyue.com/moapp/jybase/redis"
- . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/util/gconv"
- )
- type HlyjStruct struct {
- Account string
- TokenUrl string
- Appid string
- Secret string
- CallUrl string
- Integratedid string
- CallFlag int
- }
- func (this *HlyjStruct) GetRedisKey() string {
- return "hlyj_access_token"
- }
- // 获取token
- func (this *HlyjStruct) GetAccessToken() string {
- //取缓存
- if redisToken := redis.GetStr("newother", this.GetRedisKey()); redisToken != "" {
- return redisToken
- }
- str := Get(this.TokenUrl, g.Map{"account": this.Account, "appid": this.Appid, "secret": this.Secret})
- token, _ := gconv.Map(str)["accessToken"].(string)
- // 计算时间差
- invalidTime, _ := gconv.Map(str)["invalidTime"].(string)
- t, _ := time.ParseInLocation(date.Date_Full_Layout, invalidTime, time.Local)
- duration := time.Since(t)
- timeout := -gconv.Int(duration.Seconds()) - 60 //
- //缓存
- redis.Put("newother", this.GetRedisKey(), token, timeout)
- return token
- }
- func Get(url string, param map[string]interface{}) (str string) {
- ctx := context.Background()
- if r, err := g.Client().Get(ctx, url, param); err != nil {
- g.Log().Error(ctx, err)
- } else {
- defer r.Close()
- str = r.ReadAllString()
- log.Println(url, "-", str)
- }
- return
- }
- // 打电话
- func (this *HlyjStruct) Call(phone string, positionId int64) string {
- //获取token
- // token := this.GetAccessToken()
- // if token == "" {
- // log.Println("未获取到token")
- // return false
- // }
- //获取坐席id
- integratedid, _ := getSeatNumber(positionId)
- if integratedid == "" {
- log.Println("未获取到坐席id")
- return ""
- }
- id := JyBiTidb.Insert("call_history", map[string]interface{}{
- "phone": phone,
- "positionId": positionId,
- "seatNumber": integratedid,
- "createTime": time.Now().Format(date.Date_Full_Layout),
- })
- data := map[string]interface{}{"callId": id}
- bytes, _ := json.Marshal(&data)
- return string(bytes)
- //打电话接口
- // ret := Get(this.CallUrl, g.Map{"flag": this.CallFlag, "account": this.Account, "integratedid": integratedid, "accessToken": token, "phonenum": phone})
- // return ret == "200"
- }
|