vehicle.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. package ctrl_v1
  2. import (
  3. "context"
  4. "encoding/json"
  5. "gd_gateway/apis"
  6. "gd_gateway/common.in/config"
  7. "gd_gateway/common.in/httper"
  8. "gd_gateway/common.in/jsonrpc2"
  9. "gd_gateway/common.in/task"
  10. "gd_gateway/common.in/utils"
  11. "gd_gateway/errors"
  12. "gd_gateway/params/param_base"
  13. "gd_gateway/params/param_v1_0"
  14. "gd_gateway/rpc_apis_v1"
  15. dutils "gd_gateway/utils"
  16. "github.com/astaxie/beego"
  17. beegoctx "github.com/astaxie/beego/context"
  18. "go.uber.org/zap"
  19. "strings"
  20. "time"
  21. )
  22. // Operations about vehicle
  23. type CenterController struct {
  24. metadata interface{} // 中继元数据
  25. beego.Controller
  26. LogID string
  27. }
  28. func (u *CenterController) handleGetCheckImpl(ctx context.Context, uctx *beegoctx.Context, req *param_v1_0.ParamQuery, startTime uint64, appkey string) (cReply apis.ManagementCheckApiReply, err error) {
  29. // 透传通用信息
  30. cReq := apis.ManagementCheckApiReq{
  31. Method: u.Ctx.Request.Method,
  32. Router: u.Ctx.Request.URL.Path,
  33. Ip: u.Ctx.Input.IP(),
  34. RequestParam: []byte(req.Data),
  35. OriginRequestParam: req.Data,
  36. }
  37. // 填写头部信息
  38. cReq.Header = make(map[string]string, 0)
  39. if appkey != ""{
  40. cReq.Header["app_key"] = appkey
  41. }else{
  42. cReq.Header["app_key"] = req.Appkey
  43. }
  44. cReq.Header["ts"] = req.Timestamp
  45. cReq.Header["encrypt_type"] = req.EncryptType
  46. cReq.Header["sign"] = req.Sign
  47. //cReq.Header["is_sign"] = req.IsSign
  48. cReq.Header["replace_app_key"] = req.ReplaceAppKey
  49. // 验证接口权限
  50. cReply, err = rpc_apis_v1.AuthCheck.ManagementCheckApi(ctx, &cReq)
  51. //fmt.Printf("auth_check:%+v\n, err:%+v\n", cReply, err)
  52. // 生成流水号
  53. orderNo := utils.GetResponseOrderNo(cReply.MerchantApiInfo.ResponseParamConf)
  54. uctx.Input.Context.Request.Header.Add("order_no", orderNo)
  55. if err != nil || cReply.ErrCode != 0 {
  56. // 若不为 0,则覆盖 err 错误信息
  57. if cReply.ErrCode != 0 {
  58. err = jsonrpc2.NewJsonError(cReply.ErrCode, cReply.ErrMsg)
  59. }
  60. //err = errors.ErrorTransform(err)
  61. // 写入失败日志
  62. dutils.WriteInvalidAccessLog(
  63. ctx,
  64. u.Ctx.Input.IP(),
  65. utils.MarshalJsonString(cReq),
  66. cReply.MerchantApiInfo.MerchantId,
  67. cReply.MerchantApiInfo.BaseApiId,
  68. int64(startTime),
  69. err)
  70. if cReply.MerchantApiInfo.MerchantDataApiId > 0 && (cReply.ErrCode == 1102 || cReply.ErrCode == 1103 || cReply.ErrCode == 1020) {
  71. accessLog := dutils.NewAccessLogNew(req.Data, false, []apis.ThirdpartLogWrite{}, u.Ctx.Input.IP(), &cReply.MerchantApiInfo)
  72. accessLog.OrderNo = orderNo
  73. dutils.WriteAccessLog(
  74. ctx,
  75. accessLog,
  76. startTime,
  77. cReply.MerchantApiInfo.MerchantChildApiId,
  78. err)
  79. }
  80. if cReply.ErrCode == 1102{
  81. err = errors.BadParaFormat
  82. }
  83. }
  84. return
  85. }
  86. // @Title query vehicle info
  87. // @Description 查询车辆相关信息
  88. // @Param appkey header string false "appkey"
  89. // @Param token header string false "token"
  90. // @Param timestamp header string false "时间戳"
  91. // @Param sign header string false "签名"
  92. // @Param code path string true "code"
  93. // @Success 200 {object} params.param_v1_0.CommonResponse "响应信息"
  94. // @Failure 500 服务器错误
  95. // @router /query/:code [get]
  96. func (u *CenterController) Vehicle() {
  97. startTime := uint64(time.Now().UnixNano())
  98. req := &param_v1_0.ParamQuery{}
  99. cReply := apis.ManagementCheckApiReply{}
  100. ctx := utils.NewContextFromBeegoCtx(u.Ctx)
  101. getParamsTask := func() error {
  102. httper.FillRequireParams(u.Ctx, req, u.LogID)
  103. if req.Sign == "" && req.Token == ""{
  104. return jsonrpc2.NewJsonError(1103, "token/签名为空")
  105. }
  106. if req.Sign != ""{
  107. if req.Timestamp == "" {
  108. return jsonrpc2.NewJsonError(1103, "时间搓为空")
  109. }
  110. if req.Appkey == ""{
  111. return jsonrpc2.NewJsonError(1103, "app key 为空")
  112. }
  113. }
  114. return nil
  115. }
  116. // 验证签名,访问次数等
  117. handleCheckTask := func() error {
  118. var err error
  119. var username string
  120. if req.Token != ""{
  121. _, username, err = utils.ParseToken(req.Token)
  122. if err != nil {
  123. return err
  124. }
  125. }
  126. queryAll := u.Ctx.Input.Context.Request.URL.Query()
  127. m := map[string]string{}
  128. for k, v := range queryAll {
  129. if len(v) == 0 {
  130. m[k] = ""
  131. continue
  132. }
  133. m[k] = v[0]
  134. }
  135. if len(m) == 0 {
  136. return jsonrpc2.NewJsonError(1103, "参数为空")
  137. }
  138. queryAllByte, err := json.Marshal(m)
  139. req.Data = string(queryAllByte)
  140. req.EncryptType = strings.ToUpper(req.EncryptType)
  141. cReply, err = u.handleGetCheckImpl(ctx, u.Ctx, req, startTime, username)
  142. return err
  143. }
  144. // 处理
  145. handleDataTask := func() error {
  146. orderNo := u.Ctx.Input.Context.Request.Header.Get("order_no")
  147. resp := param_v1_0.CommonResponse{
  148. SimpleResp: param_base.SimpleResp{
  149. Code: 0,
  150. Msg: "成功",
  151. },
  152. OrderNo: orderNo,
  153. }
  154. // 只取勾选字段
  155. params, _ := utils.GetRequestParamNew(
  156. cReply.MerchantApiInfo,
  157. cReply.MerchantApiInfo.DecryptParam)
  158. // rpc 调用参数
  159. vReq := apis.CommonReq{}
  160. if err := json.Unmarshal(params, &vReq.Data); err != nil {
  161. }
  162. dutils.FillMerchantApiInfo(&vReq.MerchantApiInfo, &cReply.MerchantApiInfo)
  163. result := NewResult(int64(startTime), cReply.MerchantApiInfo.MinimalTimeConsuming)
  164. go func(result *Result) {
  165. code := u.Ctx.Input.Param(":code")
  166. vReq.Code = code
  167. reply, err := rpc_apis_v1.Vehicle.Query(ctx, &vReq)
  168. accessLog := dutils.NewAccessLogNew(req.Data, reply.IsReuse, reply.LReq, u.Ctx.Input.IP(), &cReply.MerchantApiInfo)
  169. accessLog.OrderNo = resp.OrderNo
  170. if err != nil {
  171. l.Error("rpc",
  172. zap.String("call", code),
  173. zap.String("args", utils.MarshalJsonString(req)),
  174. zap.String("error", err.Error()))
  175. } else {
  176. if reply.ErrCode == 0 {
  177. accessLog.RawResponseParams, _ = utils.GetRawResponseParam(cReply.MerchantApiInfo, &reply.Data)
  178. accessLog.ResponseParams, err = utils.GetResponseParam(cReply.MerchantApiInfo, &reply.Data,req.EncryptType)
  179. if err == nil {
  180. result.Data = accessLog.ResponseParams
  181. }
  182. } else {
  183. err = jsonrpc2.NewJsonError(reply.ErrCode, reply.ErrMsg)
  184. }
  185. }
  186. var respErr error
  187. // 定时任务已经返回
  188. if CheckTimeLock(result) {
  189. respErr = errors.NoRecord
  190. err = errors.ApiTimeOut
  191. } else {
  192. if cReply.MerchantApiInfo.IsRawErrorCode {
  193. respErr = errors.ErrorTransform(err)
  194. } else {
  195. respErr = errors.ErrorToNorecord(err)
  196. }
  197. result.Chan <- respErr
  198. }
  199. // 写日志并计数
  200. dutils.WriteAccessLogAndCountCode(ctx,
  201. accessLog,
  202. startTime,
  203. cReply.MerchantApiInfo,
  204. err,
  205. respErr)
  206. }(&result)
  207. data, err := GetResult(int64(cReply.MerchantApiInfo.Timeout), &result)
  208. dutils.HttpStateCodeReturn(u.Ctx, cReply.MerchantApiInfo, err)
  209. if err != nil {
  210. return err
  211. } else {
  212. if req.EncryptType == ""{
  213. m := map[string]interface{}{}
  214. if data!= ""{
  215. json.Unmarshal([]byte(data), &m)
  216. }
  217. resp.Data = m
  218. } else {
  219. resp.Data = data
  220. }
  221. httper.JSON(u.Ctx, resp, u.LogID)
  222. return nil
  223. }
  224. }
  225. // 执行以上定义的任务
  226. task.Do(&task.PanicRecover{
  227. Ctx: u.Ctx,
  228. LogID: u.LogID}, u.Ctx,
  229. getParamsTask, handleCheckTask, handleDataTask,
  230. )
  231. }
  232. // @Title query info
  233. // @Description 查询相关信息
  234. // @Param appkey header string false "appkey"
  235. // @Param token header string false "token"
  236. // @Param timestamp header string false "时间戳"
  237. // @Param sign header string false "签名"
  238. // @Param code path string true "code"
  239. // @Success 200 {object} params.param_v1_0.CommonResponse "响应信息"
  240. // @Failure 500 服务器错误
  241. // @router /service/:code [get]
  242. func (u *CenterController) Service() {
  243. startTime := uint64(time.Now().UnixNano())
  244. req := &param_v1_0.ParamQuery{}
  245. cReply := apis.ManagementCheckApiReply{}
  246. ctx := utils.NewContextFromBeegoCtx(u.Ctx)
  247. getParamsTask := func() error {
  248. httper.FillRequireParams(u.Ctx, req, u.LogID)
  249. if req.Sign == "" && req.Token == ""{
  250. return jsonrpc2.NewJsonError(1103, "token/签名为空")
  251. }
  252. if req.Sign != ""{
  253. if req.Timestamp == "" {
  254. return jsonrpc2.NewJsonError(1103, "时间搓为空")
  255. }
  256. if req.Appkey == ""{
  257. return jsonrpc2.NewJsonError(1103, "app key 为空")
  258. }
  259. }
  260. return nil
  261. }
  262. // 验证签名,访问次数等
  263. handleCheckTask := func() error {
  264. var err error
  265. var username string
  266. if req.Token != ""{
  267. _, username, err = utils.ParseToken(req.Token)
  268. if err != nil {
  269. return err
  270. }
  271. }
  272. queryAll := u.Ctx.Input.Context.Request.URL.Query()
  273. m := map[string]string{}
  274. for k, v := range queryAll {
  275. if len(v) == 0 {
  276. m[k] = ""
  277. continue
  278. }
  279. m[k] = v[0]
  280. }
  281. if len(m) == 0 {
  282. return jsonrpc2.NewJsonError(1103, "参数为空")
  283. }
  284. queryAllByte, err := json.Marshal(m)
  285. req.Data = string(queryAllByte)
  286. req.EncryptType = strings.ToUpper(req.EncryptType)
  287. cReply, err = u.handleGetCheckImpl(ctx, u.Ctx, req, startTime, username)
  288. return err
  289. }
  290. // 处理
  291. handleDataTask := func() error {
  292. orderNo := u.Ctx.Input.Context.Request.Header.Get("order_no")
  293. resp := param_v1_0.CommonResponse{
  294. SimpleResp: param_base.SimpleResp{
  295. Code: 0,
  296. Msg: "成功",
  297. },
  298. OrderNo: orderNo,
  299. }
  300. // 只取勾选字段
  301. params, _ := utils.GetRequestParamNew(
  302. cReply.MerchantApiInfo,
  303. cReply.MerchantApiInfo.DecryptParam)
  304. // rpc 调用参数
  305. vReq := apis.CommonReq{}
  306. if err := json.Unmarshal(params, &vReq.Data); err != nil {
  307. }
  308. dutils.FillMerchantApiInfo(&vReq.MerchantApiInfo, &cReply.MerchantApiInfo)
  309. result := NewResult(int64(startTime), cReply.MerchantApiInfo.MinimalTimeConsuming)
  310. go func(result *Result) {
  311. code := u.Ctx.Input.Param(":code")
  312. vReq.Code = code
  313. reply, err := rpc_apis_v1.Service.Query(ctx, &vReq)
  314. accessLog := dutils.NewAccessLogNew(req.Data, reply.IsReuse, reply.LReq, u.Ctx.Input.IP(), &cReply.MerchantApiInfo)
  315. accessLog.OrderNo = resp.OrderNo
  316. if err != nil {
  317. l.Error("rpc",
  318. zap.String("call", code),
  319. zap.String("args", utils.MarshalJsonString(req)),
  320. zap.String("error", err.Error()))
  321. } else {
  322. if reply.ErrCode == 0 {
  323. accessLog.RawResponseParams, _ = utils.GetRawResponseParam(cReply.MerchantApiInfo, &reply.Data)
  324. accessLog.ResponseParams, err = utils.GetResponseParam(cReply.MerchantApiInfo, &reply.Data,req.EncryptType)
  325. if err == nil {
  326. result.Data = accessLog.ResponseParams
  327. }
  328. } else {
  329. err = jsonrpc2.NewJsonError(reply.ErrCode, reply.ErrMsg)
  330. }
  331. }
  332. var respErr error
  333. // 定时任务已经返回
  334. if CheckTimeLock(result) {
  335. respErr = errors.NoRecord
  336. err = errors.ApiTimeOut
  337. } else {
  338. if cReply.MerchantApiInfo.IsRawErrorCode {
  339. respErr = errors.ErrorTransformRaw(err)
  340. } else {
  341. respErr = errors.ErrorTransformRaw(err)
  342. }
  343. result.Chan <- respErr
  344. }
  345. // 写日志并计数
  346. dutils.WriteAccessLogAndCountCode(ctx,
  347. accessLog,
  348. startTime,
  349. cReply.MerchantApiInfo,
  350. err,
  351. respErr)
  352. }(&result)
  353. data, err := GetResult(int64(cReply.MerchantApiInfo.Timeout), &result)
  354. dutils.HttpStateCodeReturn(u.Ctx, cReply.MerchantApiInfo, err)
  355. if err != nil {
  356. return err
  357. } else {
  358. if req.EncryptType == ""{
  359. m := map[string]interface{}{}
  360. if data!= ""{
  361. json.Unmarshal([]byte(data), &m)
  362. }
  363. resp.Data = m
  364. } else {
  365. resp.Data = data
  366. }
  367. httper.JSON(u.Ctx, resp, u.LogID)
  368. return nil
  369. }
  370. }
  371. // 执行以上定义的任务
  372. task.Do(&task.PanicRecover{
  373. Ctx: u.Ctx,
  374. LogID: u.LogID}, u.Ctx,
  375. getParamsTask, handleCheckTask, handleDataTask,
  376. )
  377. }
  378. // @Title query app login
  379. // @Description 获取token
  380. // @Param user query string true "用户名"
  381. // @Param password query string true "密码"
  382. // @Success 200 {object} params.param_v1_0.VehicleLoginResponse "响应信息"
  383. // @Failure 500 服务器错误
  384. // @router /token [get]
  385. func (u *CenterController) VehicleLogin() {
  386. req := &param_v1_0.VehicleLoginRequest{}
  387. ctx := utils.NewContextFromBeegoCtx(u.Ctx)
  388. getParamsTask := func() error {
  389. httper.FillRequireParams(u.Ctx, req, u.LogID)
  390. return nil
  391. }
  392. // 处理
  393. handleDataTask := func() error {
  394. mReq := apis.MerchantAppLoginReq{User: req.User, Password: req.Password}
  395. reply, err := rpc_apis_v1.AuthCheck.MerchantAppLogin(ctx, &mReq)
  396. if err != nil {
  397. l.Error("rpc",
  398. zap.String("call", "rpc_apis_v1.AuthCheck.MerchantAppLogin"),
  399. zap.String("args", utils.MarshalJsonString(req)),
  400. zap.String("error", err.Error()))
  401. return err
  402. }
  403. token, err := utils.ProductToken(reply.MerchantId, req.User)
  404. if err != nil {
  405. return errors.LoginTokenFail
  406. }
  407. expired, _ := config.Conf.Cgi.GdGateway.TokenExpired.Int64()
  408. resp := param_v1_0.VehicleLoginResponse{Token: token,Expire:expired}
  409. resp.Msg = "成功"
  410. httper.JSON(u.Ctx, resp, u.LogID)
  411. return nil
  412. }
  413. // 执行以上定义的任务
  414. task.Do(&task.PanicRecover{
  415. Ctx: u.Ctx,
  416. LogID: u.LogID}, u.Ctx,
  417. getParamsTask, handleDataTask,
  418. )
  419. }