info.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package zoho
  2. import (
  3. "crm-api/api"
  4. "crm-api/pkg/lfshook"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "github.com/gin-gonic/gin"
  10. "gopkg.in/ini.v1"
  11. )
  12. // @tags PBX-zoho
  13. // @Summary 获取zoho数据中心列表
  14. // @Description 获取zoho数据中心列表
  15. // @Security oauth-serverinfo
  16. // @Accept json
  17. // @Produce json
  18. // @Router /api/zoho/oauth-serverinfo [get]
  19. func getOauthServerinfo(ctx *gin.Context) {
  20. fmt.Printf("getOauthServerinfo ............\n")
  21. // var ZohoOauthServerUrl = "https://accounts.zoho.com/oauth/serverinfo"
  22. // data := httpRequest(ctx, "GET", ZohoOauthServerUrl)
  23. // 获取配置文件信息
  24. confPath := "/etc/asterisk/crm_api.conf"
  25. cfg, err := ini.Load(confPath)
  26. if err != nil {
  27. lfshook.NewLogger().Error(err)
  28. return
  29. }
  30. ZohoOauthServerUrl := cfg.Section("general").Key("zohoAuthUrl").String()
  31. if ZohoOauthServerUrl == "" {
  32. lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set zohoAuthUrl")
  33. return
  34. }
  35. getURL := fmt.Sprintf("%s/oauth/serverinfo", ZohoOauthServerUrl)
  36. fmt.Printf("getURL = %s\n", getURL)
  37. data := httpRequest(ctx, "GET", getURL)
  38. /* =====================================================================================
  39. // 创建HTTP客户端
  40. client := &http.Client{}
  41. // 创建请求
  42. req, err := http.NewRequest("GET", ZohoOauthServerUrl, nil)
  43. if err != nil {
  44. fmt.Println("创建请求时发生错误:", err)
  45. return
  46. }
  47. // 发送请求
  48. resp, err := client.Do(req)
  49. if err != nil {
  50. fmt.Println("发送请求时发生错误:", err)
  51. return
  52. }
  53. defer resp.Body.Close()
  54. // 读取请求后的响应
  55. data, err := ioutil.ReadAll(resp.Body)
  56. // if err != nil {
  57. // fmt.Println("读取请求后的响应时发生错误:", err)
  58. // return
  59. // }
  60. if err != nil {
  61. // 读取数据错误
  62. lfshook.NewLogger().Warn("ioutil ReadAll failed :", err.Error())
  63. api.Error(ctx, http.StatusInternalServerError, err.Error())
  64. return
  65. }
  66. lfshook.NewLogger().Infof("data %+v", string(data))
  67. // * ===================================================================================== */
  68. infoData := OauthServerInfoResp{}
  69. err = json.Unmarshal([]byte(data), &infoData)
  70. if err != nil {
  71. // 转换数据错误
  72. lfshook.NewLogger().Warn("json unmarshal failed :", err.Error())
  73. api.Error(ctx, http.StatusInternalServerError, err.Error())
  74. return
  75. }
  76. // 打印请求后的响应
  77. fmt.Printf("data = %+v\n", string(data))
  78. // api.Success(ctx, string(data))
  79. api.Success(ctx, infoData)
  80. }
  81. // @tags PBX-zoho
  82. // @Summary 获取device_code
  83. // @Description 获取device_code
  84. // @Security device_code
  85. // @Accept json
  86. // @Produce json
  87. // @Router /api/zoho/device-code [post]
  88. func getDeviceCode(ctx *gin.Context) {
  89. fmt.Printf("getDeviceCode ............\n")
  90. // 获取配置文件信息
  91. confPath := "/etc/asterisk/crm_api.conf"
  92. cfg, err := ini.Load(confPath)
  93. if err != nil {
  94. lfshook.NewLogger().Error(err)
  95. return
  96. }
  97. ZohoOauthUrl := cfg.Section("general").Key("zohoAuthUrl").String()
  98. ZohoClientId := cfg.Section("general").Key("zohoClientId").String()
  99. if ZohoOauthUrl == "" || ZohoClientId == "" {
  100. lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set zohoAuthUrl or zohoClientId")
  101. return
  102. }
  103. // 创建请求
  104. // https://accounts.zoho.com/oauth/v3/device/code?scope=PhoneBridge.call.log,PhoneBridge.zohoone.search&client_id=1004.LWJCJZD5O9DB6SZLL5YJEWHT7LH0BV
  105. // &grant_type=device_request&access_type=offline
  106. getURL := fmt.Sprintf("%s/oauth/v3/device/code?scope=PhoneBridge.call.log,PhoneBridge.zohoone.search&client_id=%s&grant_type=device_request&access_type=offline", ZohoOauthUrl, ZohoClientId)
  107. fmt.Printf("getURL = %s\n", getURL)
  108. data := httpRequest(ctx, "POST", getURL)
  109. /* =====================================================================================
  110. req, err := http.NewRequest("POST", getURL, nil)
  111. if err != nil {
  112. fmt.Println("创建请求时发生错误:", err)
  113. return
  114. }
  115. // 创建HTTP客户端
  116. client := &http.Client{}
  117. // req.Header.Set("Authorization", "Bearer "+accessToken) // 获取token时不需要
  118. // 发送请求
  119. resp, err := client.Do(req)
  120. if err != nil {
  121. fmt.Println("发送请求时发生错误:", err)
  122. return
  123. }
  124. defer resp.Body.Close()
  125. // 读取请求后的响应
  126. data, err := ioutil.ReadAll(resp.Body)
  127. if err != nil {
  128. // 读取数据错误
  129. lfshook.NewLogger().Warn("ioutil ReadAll failed :", err.Error())
  130. api.Error(ctx, http.StatusInternalServerError, err.Error())
  131. return
  132. }
  133. lfshook.NewLogger().Infof("data %+v", string(data))
  134. // * ===================================================================================== */
  135. userCodeData := UserCodeResp{}
  136. err = json.Unmarshal([]byte(data), &userCodeData)
  137. if err != nil {
  138. // 转换数据错误
  139. lfshook.NewLogger().Warn("json unmarshal failed :", err.Error())
  140. api.Error(ctx, http.StatusInternalServerError, err.Error())
  141. return
  142. }
  143. // 将获取的 device_code 写入文件 crm_api.conf
  144. cfg.Section("general").Key("zohoCode").SetValue(userCodeData.DeviceCode)
  145. err = cfg.SaveTo(confPath)
  146. if err != nil {
  147. lfshook.NewLogger().Error(err)
  148. api.Error(ctx, http.StatusInternalServerError, err.Error())
  149. return
  150. }
  151. // 打印请求后的响应
  152. fmt.Printf("data = %+v\n", string(data))
  153. // api.Success(ctx, string(data))
  154. api.Success(ctx, userCodeData)
  155. }
  156. // @tags PBX-zoho
  157. // @Summary 获取token
  158. // @Description 获取token
  159. // @Security ZohoToken
  160. // @Accept json
  161. // @Produce json
  162. // @Router /api/zoho/gettoken [post]
  163. func getToken(ctx *gin.Context) {
  164. fmt.Printf("getToken ............\n")
  165. // 获取配置文件信息
  166. confPath := "/etc/asterisk/crm_api.conf"
  167. cfg, err := ini.Load(confPath)
  168. if err != nil {
  169. lfshook.NewLogger().Error(err)
  170. return
  171. }
  172. ZohoAuthUrl := cfg.Section("general").Key("zohoAuthUrl").String()
  173. ZohoCode := cfg.Section("general").Key("zohoCode").String()
  174. ZohoClientId := cfg.Section("general").Key("zohoClientId").String()
  175. ZohoClientSecret := cfg.Section("general").Key("zohoClientSecret").String()
  176. if ZohoAuthUrl == "" || ZohoCode == "" || ZohoClientId == "" || ZohoClientSecret == "" {
  177. lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set zohoAuthUrl or zohoCode or zohoClientId or zohoClientSecret")
  178. return
  179. }
  180. // 创建请求
  181. // https://accounts.zoho.com/oauth/v3/device/token?code=1004.d4c145db9ec33e64f955290f0905ff1e.eebc39b33f228c3bee806d6f8200c50f
  182. // &client_id=1004.LWJCJZD5O9DB6SZLL5YJEWHT7LH0BV&client_secret=fc3aef43dc58af8a49d3ed597710924200b03f74d0&grant_type=device_token
  183. getURL := fmt.Sprintf("%s/oauth/v3/device/token?code=%s&client_id=%s&client_secret=%s&grant_type=device_token", ZohoAuthUrl, ZohoCode, ZohoClientId, ZohoClientSecret)
  184. fmt.Printf("getURL = %s\n", getURL)
  185. data := httpRequest(ctx, "POST", getURL)
  186. /* =====================================================================================
  187. req, err := http.NewRequest("POST", getURL, nil)
  188. if err != nil {
  189. fmt.Println("创建请求时发生错误:", err)
  190. return
  191. }
  192. // 创建HTTP客户端
  193. client := &http.Client{}
  194. // req.Header.Set("Authorization", "Bearer "+accessToken) // 获取token时不需要
  195. // 发送请求
  196. resp, err := client.Do(req)
  197. if err != nil {
  198. fmt.Println("发送请求时发生错误:", err)
  199. return
  200. }
  201. defer resp.Body.Close()
  202. // 读取请求后的响应
  203. data, err := ioutil.ReadAll(resp.Body)
  204. if err != nil {
  205. // 读取数据错误
  206. lfshook.NewLogger().Warn("ioutil ReadAll failed :", err.Error())
  207. api.Error(ctx, http.StatusInternalServerError, err.Error())
  208. return
  209. }
  210. lfshook.NewLogger().Infof("data %+v", string(data))
  211. // * ===================================================================================== */
  212. tokenData := TokenResp{}
  213. err = json.Unmarshal([]byte(data), &tokenData)
  214. if err != nil {
  215. // 转换数据错误
  216. lfshook.NewLogger().Warn("json unmarshal failed :", err.Error())
  217. api.Error(ctx, http.StatusInternalServerError, err.Error())
  218. return
  219. }
  220. // 将获取的 refresh_token access_token 写入文件 crm_api.conf
  221. cfg.Section("general").Key("zohoRefreshToken").SetValue(tokenData.RefreshToken)
  222. cfg.Section("general").Key("zohoAccessToken").SetValue(tokenData.AccessToken)
  223. err = cfg.SaveTo(confPath)
  224. if err != nil {
  225. lfshook.NewLogger().Error(err)
  226. api.Error(ctx, http.StatusInternalServerError, err.Error())
  227. return
  228. }
  229. // 打印请求后的响应
  230. fmt.Printf("data = %+v\n", string(data))
  231. // api.Success(ctx, string(data))
  232. api.Success(ctx, tokenData)
  233. }
  234. // @tags PBX-zoho
  235. // @Summary 刷新token
  236. // @Description 刷新token
  237. // @Security ZohoToken
  238. // @Accept json
  239. // @Produce json
  240. // @Router /api/zoho/refresh-token [post]
  241. func refreshToken(ctx *gin.Context) {
  242. fmt.Printf("refreshToken ............\n")
  243. // 获取配置文件信息
  244. // confPath := "/etc/asterisk/vtiger_api.conf"
  245. confPath := "/etc/asterisk/crm_api.conf"
  246. cfg, err := ini.Load(confPath)
  247. if err != nil {
  248. lfshook.NewLogger().Error(err)
  249. return
  250. }
  251. ZohoAuthUrl := cfg.Section("general").Key("zohoAuthUrl").String()
  252. ZohoRefreshToken := cfg.Section("general").Key("zohoRefreshToken").String()
  253. ZohoClientId := cfg.Section("general").Key("zohoClientId").String()
  254. ZohoClientSecret := cfg.Section("general").Key("zohoClientSecret").String()
  255. if ZohoAuthUrl == "" || ZohoRefreshToken == "" || ZohoClientId == "" || ZohoClientSecret == "" {
  256. lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set zohoAuthUrl or zohoRefreshToken or zohoClientId or zohoClientSecret")
  257. return
  258. }
  259. // 创建请求
  260. // https://accounts.zoho.com/oauth/v2/token?refresh_token=1004.86c8c0e3db7bfe9133598825bef28eb9.17a82a3bf3e675c504f478c1b0b5c456
  261. // &client_id=1004.LWJCJZD5O9DB6SZLL5YJEWHT7LH0BV&client_secret=fc3aef43dc58af8a49d3ed597710924200b03f74d0&grant_type=refresh_token
  262. getURL := fmt.Sprintf("%s/oauth/v2/token?refresh_token=%s&client_id=%s&client_secret=%s&grant_type=refresh_token", ZohoAuthUrl, ZohoRefreshToken, ZohoClientId, ZohoClientSecret)
  263. fmt.Printf("getURL = %s\n", getURL)
  264. data := httpRequest(ctx, "POST", getURL)
  265. /* =====================================================================================
  266. req, err := http.NewRequest("POST", getURL, nil)
  267. if err != nil {
  268. fmt.Println("创建请求时发生错误:", err)
  269. return
  270. }
  271. // 创建HTTP客户端
  272. client := &http.Client{}
  273. // req.Header.Set("Authorization", "Bearer "+accessToken) // 刷新token时不需要
  274. // 发送请求
  275. resp, err := client.Do(req)
  276. if err != nil {
  277. fmt.Println("发送请求时发生错误:", err)
  278. return
  279. }
  280. defer resp.Body.Close()
  281. // 读取请求后的响应
  282. data, err := ioutil.ReadAll(resp.Body)
  283. if err != nil {
  284. // 读取数据错误
  285. lfshook.NewLogger().Warn("ioutil ReadAll failed :", err.Error())
  286. api.Error(ctx, http.StatusInternalServerError, err.Error())
  287. return
  288. }
  289. lfshook.NewLogger().Infof("data %+v", string(data))
  290. // * ===================================================================================== */
  291. refreshTokenData := RefreshTokenResp{}
  292. err = json.Unmarshal([]byte(data), &refreshTokenData)
  293. if err != nil {
  294. // 转换数据错误
  295. lfshook.NewLogger().Warn("json unmarshal failed :", err.Error())
  296. api.Error(ctx, http.StatusInternalServerError, err.Error())
  297. return
  298. }
  299. // 将获取的 access_token 写入文件 crm_api.conf
  300. cfg.Section("general").Key("zohoAccessToken").SetValue(refreshTokenData.AccessToken)
  301. err = cfg.SaveTo(confPath)
  302. if err != nil {
  303. lfshook.NewLogger().Error(err)
  304. api.Error(ctx, http.StatusInternalServerError, err.Error())
  305. return
  306. }
  307. // 打印请求后的响应
  308. fmt.Printf("data = %+v\n", string(data))
  309. // api.Success(ctx, string(data))
  310. api.Success(ctx, refreshTokenData)
  311. }
  312. // HTTP请求
  313. func httpRequest(ctx *gin.Context, method string, url string) []byte {
  314. fmt.Printf("httpRequest ............\n")
  315. // 创建HTTP客户端
  316. client := &http.Client{}
  317. // 创建请求
  318. // req, err := http.NewRequest("POST", getURL, nil)
  319. req, err := http.NewRequest(method, url, nil)
  320. if err != nil {
  321. fmt.Println("创建请求时发生错误:", err)
  322. return nil
  323. }
  324. // 发送请求
  325. resp, err := client.Do(req)
  326. if err != nil {
  327. fmt.Println("发送请求时发生错误:", err)
  328. return nil
  329. }
  330. defer resp.Body.Close()
  331. // 读取请求后的响应
  332. data, err := ioutil.ReadAll(resp.Body)
  333. if err != nil {
  334. // 读取数据错误
  335. lfshook.NewLogger().Warn("ioutil ReadAll failed :", err.Error())
  336. api.Error(ctx, http.StatusInternalServerError, err.Error())
  337. return nil
  338. }
  339. lfshook.NewLogger().Infof("data %+v", string(data))
  340. // 打印请求后的响应
  341. fmt.Printf("data = %+v\n", string(data))
  342. return data
  343. }