hlyj.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "log"
  6. "time"
  7. "app.yhyue.com/moapp/jybase/date"
  8. "app.yhyue.com/moapp/jybase/redis"
  9. . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  10. "github.com/gogf/gf/v2/frame/g"
  11. "github.com/gogf/gf/v2/util/gconv"
  12. )
  13. type HlyjStruct struct {
  14. Account string
  15. TokenUrl string
  16. Appid string
  17. Secret string
  18. CallUrl string
  19. Integratedid string
  20. CallFlag int
  21. }
  22. func (this *HlyjStruct) GetRedisKey() string {
  23. return "hlyj_access_token"
  24. }
  25. // 获取token
  26. func (this *HlyjStruct) GetAccessToken() string {
  27. //取缓存
  28. if redisToken := redis.GetStr("newother", this.GetRedisKey()); redisToken != "" {
  29. return redisToken
  30. }
  31. str := Get(this.TokenUrl, g.Map{"account": this.Account, "appid": this.Appid, "secret": this.Secret})
  32. token, _ := gconv.Map(str)["accessToken"].(string)
  33. // 计算时间差
  34. invalidTime, _ := gconv.Map(str)["invalidTime"].(string)
  35. t, _ := time.ParseInLocation(date.Date_Full_Layout, invalidTime, time.Local)
  36. duration := time.Since(t)
  37. timeout := -gconv.Int(duration.Seconds()) - 60 //
  38. //缓存
  39. redis.Put("newother", this.GetRedisKey(), token, timeout)
  40. return token
  41. }
  42. func Get(url string, param map[string]interface{}) (str string) {
  43. ctx := context.Background()
  44. if r, err := g.Client().Get(ctx, url, param); err != nil {
  45. g.Log().Error(ctx, err)
  46. } else {
  47. defer r.Close()
  48. str = r.ReadAllString()
  49. log.Println(url, "-", str)
  50. }
  51. return
  52. }
  53. // 打电话
  54. func (this *HlyjStruct) Call(phone string, positionId int64) string {
  55. //获取token
  56. // token := this.GetAccessToken()
  57. // if token == "" {
  58. // log.Println("未获取到token")
  59. // return false
  60. // }
  61. //获取坐席id
  62. integratedid, _ := getSeatNumber(positionId)
  63. if integratedid == "" {
  64. log.Println("未获取到坐席id")
  65. return ""
  66. }
  67. id := JyBiTidb.Insert("call_history", map[string]interface{}{
  68. "phone": phone,
  69. "positionId": positionId,
  70. "seatNumber": integratedid,
  71. "createTime": time.Now().Format(date.Date_Full_Layout),
  72. })
  73. data := map[string]interface{}{"callId": id}
  74. bytes, _ := json.Marshal(&data)
  75. return string(bytes)
  76. //打电话接口
  77. // ret := Get(this.CallUrl, g.Map{"flag": this.CallFlag, "account": this.Account, "integratedid": integratedid, "accessToken": token, "phonenum": phone})
  78. // return ret == "200"
  79. }