wsController.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * @Author: wfhou
  3. * @Date: 2022-05-17 10:07:52
  4. * @LastEditors: wfhou
  5. * @LastEditTime: 2022-05-17 14:44:58
  6. * @FilePath: \SCD\controllers\wsController.go
  7. * @Description:
  8. *
  9. * Copyright (c) 2022 by wfhou/jujutong, All Rights Reserved.
  10. */
  11. package controllers
  12. import (
  13. "fmt"
  14. "log"
  15. "scd_check_tools/conf"
  16. "scd_check_tools/tools"
  17. )
  18. type WsController struct {
  19. BaseController
  20. }
  21. func init() {
  22. }
  23. /**
  24. * @description: 获取ws长连接地址
  25. * @Author: wfhou
  26. * @method: get
  27. * @url: /api/ws/getconnect
  28. * @content-type: application/json
  29. * @return json格式的ws连接地址
  30. */
  31. // @Summary 获取ws长连接地址
  32. // @Description 获取ws长连接地址
  33. // @Tags 基础服务接口
  34. // @Accept x-www-form-urlencoded
  35. // @Produce json
  36. // @Param ws_url query string true "WS连接地址获取接口地址"
  37. // @Param token_url query string true "token获取接口地址"
  38. // @Success 200 {object} ResultOK 成功
  39. // @Failure 500 {object} ResultError 失败
  40. // @router /ws/getconnect [get]
  41. func (c *WsController) GetWsConnect() {
  42. //登录的url地址
  43. token_url := conf.GlobalConfig["token_url"]
  44. //获取ws的url地址
  45. get_ws_url := conf.GlobalConfig["ws_url"]
  46. //获取用户名和密码
  47. appKey := conf.GlobalConfig["appkey"]
  48. appSecret := conf.GlobalConfig["appsecret"]
  49. fss := fmt.Sprintf("{\"appKey\":\"%s\",\"appSecret\":\"%s\"}", appKey, appSecret)
  50. res, err := tools.PostJson(token_url, fss)
  51. if err != nil {
  52. log.Println(err)
  53. c.Data["json"] = c.ResultError("获取电梯平台的token出错!")
  54. c.ServeJSON()
  55. return
  56. }
  57. if res == nil {
  58. c.Data["json"] = c.ResultError("获取ws地址的接口调用失败!")
  59. c.ServeJSON()
  60. return
  61. }
  62. resTmp := res.(map[string]interface{})
  63. resTmp2 := resTmp["data"].(map[string]interface{})
  64. var accessToken interface{}
  65. if resTmp2["code"] == 0 {
  66. log.Println(err)
  67. c.Data["json"] = c.ResultError("返回code错误!")
  68. c.ServeJSON()
  69. return
  70. } else {
  71. accessToken = resTmp2["accessToken"]
  72. get_ws_url = get_ws_url + accessToken.(string)
  73. }
  74. wsRes, err := tools.PostJson(get_ws_url, "{}")
  75. if wsRes == nil || err != nil {
  76. log.Println(err)
  77. c.Data["json"] = c.ResultError("获取电梯平台的wsurl出错!")
  78. c.ServeJSON()
  79. return
  80. }
  81. c.Data["json"] = c.ResultOK(wsRes, 0)
  82. c.ServeJSON()
  83. }