user.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "fmt"
  6. "net/http"
  7. "property-household-gateway/errors"
  8. param_v1 "property-household-gateway/param/v1"
  9. "property-household-gateway/pb"
  10. "property-household-gateway/pb/v1"
  11. "property-household-gateway/utils"
  12. "strings"
  13. "time"
  14. "git.getensh.com/common/gopkgs/logger"
  15. "git.getensh.com/common/gopkgs/tasker/httptasker"
  16. "git.getensh.com/common/gopkgs/util"
  17. "github.com/dgrijalva/jwt-go"
  18. "property-household-gateway/parser"
  19. "git.getensh.com/common/gopkgs/jwtwrapper"
  20. "github.com/gin-gonic/gin"
  21. "go.uber.org/zap"
  22. )
  23. func TokenTest() {
  24. subject := map[string]interface{}{
  25. "nick_name": "",
  26. "open_id": "",
  27. "real_name": "",
  28. "phone": "",
  29. }
  30. str, _ := json.MarshalToString(subject)
  31. token, _ := jwtwrapper.GenToken(fmt.Sprintf("%d", 1), parser.Conf.Jwt.Issuer, str,
  32. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  33. fmt.Printf("token:%s\n", token)
  34. }
  35. // 登录
  36. // @Summary 登录
  37. // @Description 登录
  38. // @Tags 用户
  39. // @Accept json
  40. // @Produce json
  41. // @Param body body v1.LoginBody true "登录信息"
  42. // @Success 200 {object} v1.LoginResponse
  43. // @Failure 500 {object} base.HTTPError
  44. // @Router /api/v1/user/login [post]
  45. func (c *Controller) Login(ctx *gin.Context) {
  46. // 解析参数
  47. req := &param_v1.LoginRequest{}
  48. parseParamTask := func() error {
  49. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.LoginBody)
  50. if err != nil {
  51. logger.Error("func",
  52. zap.String("call", "util.ShouldBind"),
  53. zap.String("error", err.Error()))
  54. return errors.ParamsError
  55. }
  56. return nil
  57. }
  58. // 业务处理
  59. handleServiceTask := func() error {
  60. // 响应数据
  61. resp := param_v1.LoginResponse{}
  62. rpcReq := &v1.LoginRequest{
  63. Code: req.Code,
  64. PhoneEncrypt: req.PhoneEncrypt,
  65. PhoneIv: req.PhoneIv,
  66. UserEncrypt: req.UserEncrypt,
  67. UserIv: req.UserIv,
  68. }
  69. rpcRsp, err := pb.Household.Login(ctx, rpcReq)
  70. if err != nil {
  71. s, _ := json.MarshalToString(req)
  72. logger.Error("func",
  73. zap.String("call", "pb.Household.Login"),
  74. zap.String("params", s),
  75. zap.String("error", err.Error()))
  76. return errors.ErrorTransForm(err)
  77. }
  78. subject := map[string]interface{}{
  79. "nick_name": rpcRsp.NickName,
  80. "open_id": rpcRsp.OpenId,
  81. "real_name": rpcRsp.RealName,
  82. "phone": rpcRsp.Phone,
  83. "openim_id": rpcRsp.OpenimId,
  84. "user_icon": rpcRsp.Avatar,
  85. "id_number": rpcRsp.IdNumber,
  86. }
  87. str, _ := json.MarshalToString(subject)
  88. // 生成token
  89. token, err := jwtwrapper.GenToken(fmt.Sprintf("%d", rpcRsp.Uid), parser.Conf.Jwt.Issuer, str,
  90. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  91. if err != nil {
  92. logger.Error("func",
  93. zap.String("call", "util.GenJwtToken"),
  94. zap.String("args", fmt.Sprintf("%d", rpcRsp.Uid)),
  95. zap.String("error", err.Error()))
  96. return errors.SystemError
  97. }
  98. resp.Data.Uid = rpcRsp.Uid
  99. resp.Data.Token = token
  100. resp.Data.User = rpcRsp.RealName
  101. resp.Data.OpenId = rpcRsp.OpenId
  102. resp.Data.Avatar = rpcRsp.Avatar
  103. resp.Data.NickName = rpcRsp.NickName
  104. resp.Data.OpenimId = rpcRsp.OpenimId
  105. resp.Data.Phone = rpcRsp.Phone
  106. if resp.Data.User == "" {
  107. resp.Data.User = rpcRsp.Phone
  108. }
  109. ctx.JSON(http.StatusOK, resp)
  110. return nil
  111. }
  112. // 执行任务
  113. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  114. }
  115. //
  116. // @Summary 获取openim token
  117. // @Description 获取openim_token
  118. // @Tags openim
  119. // @Accept json
  120. // @Produce json
  121. // @Param token header string true "token"
  122. // @Param openim_id query string true "openim_id"
  123. // @Success 200 {object} v1.OpenImTokenResponse
  124. // @Failure 500 {object} base.HTTPError
  125. // @Router /api/v1/user/openim_token [get]
  126. func (c *Controller) OpenImToken(ctx *gin.Context) {
  127. // 解析参数
  128. req := &param_v1.OpenImTokenRequest{}
  129. parseParamTask := func() error {
  130. err := util.ShouldBind(ctx, &req.Header, nil, &req.OpenImTokenQuery, nil)
  131. if err != nil {
  132. logger.Error("func",
  133. zap.String("call", "util.ShouldBind"),
  134. zap.String("error", err.Error()))
  135. return errors.ParamsError
  136. }
  137. return nil
  138. }
  139. // 业务处理
  140. handleServiceTask := func() error {
  141. // 响应数据
  142. resp := param_v1.OpenImTokenResponse{}
  143. rpcReq := &v1.OpenImTokenRequest{
  144. Platform: 5,
  145. OpenimId: req.OpenimId,
  146. }
  147. rpcRsp, err := pb.Thirdparty.OpenImToken(ctx, rpcReq)
  148. if err != nil {
  149. s, _ := json.MarshalToString(req)
  150. logger.Error("func",
  151. zap.String("call", "pb.Thirdparty.OpenImToken"),
  152. zap.String("params", s),
  153. zap.String("error", err.Error()))
  154. return errors.ErrorTransForm(err)
  155. }
  156. resp.Data = *rpcRsp
  157. ctx.JSON(http.StatusOK, resp)
  158. return nil
  159. }
  160. // 执行任务
  161. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  162. }
  163. //
  164. // @Summary 批量获取openim 用户信息
  165. // @Description 批量获取openim_用户信息
  166. // @Tags openim
  167. // @Accept json
  168. // @Produce json
  169. // @Param token header string true "token"
  170. // @Param openim_ids query string true "用户openim_id,以逗号分割"
  171. // @Success 200 {object} v1.OpenImUsersInfoResponse
  172. // @Failure 500 {object} base.HTTPError
  173. // @Router /api/v1/user/openim_users_info [get]
  174. func (c *Controller) OpenImUsersInfo(ctx *gin.Context) {
  175. // 解析参数
  176. req := &param_v1.OpenImUsersInfoRequest{}
  177. parseParamTask := func() error {
  178. err := util.ShouldBind(ctx, &req.Header, nil, &req.OpenImUsersInfoQuery, nil)
  179. if err != nil {
  180. logger.Error("func",
  181. zap.String("call", "util.ShouldBind"),
  182. zap.String("error", err.Error()))
  183. return errors.ParamsError
  184. }
  185. return nil
  186. }
  187. // 业务处理
  188. handleServiceTask := func() error {
  189. // 响应数据
  190. resp := param_v1.OpenImUsersInfoResponse{}
  191. array := strings.Split(req.OpenimIds, ",")
  192. rpcReq := &v1.OpenImUserInfoRequest{
  193. PlatformId: 5,
  194. OpenimIds: array,
  195. }
  196. rpcRsp, err := pb.Thirdparty.OpenImUserInfo(ctx, rpcReq)
  197. if err != nil {
  198. s, _ := json.MarshalToString(req)
  199. logger.Error("func",
  200. zap.String("call", "pb.Thirdparty.OpenImUserInfo"),
  201. zap.String("params", s),
  202. zap.String("error", err.Error()))
  203. return errors.ErrorTransForm(err)
  204. }
  205. if len(rpcRsp.List) == 0 {
  206. rpcRsp.List = make([]*v1.OpenImUserData, 0)
  207. }
  208. resp.Data = *rpcRsp
  209. ctx.JSON(http.StatusOK, resp)
  210. return nil
  211. }
  212. // 执行任务
  213. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  214. }
  215. // token
  216. // @Summary 刷新token
  217. // @Description 刷新token
  218. // @Tags 用户
  219. // @Accept json
  220. // @Produce json
  221. // @Param token header string true "token"
  222. // @Success 200 {object} v1.TokenResponse
  223. // @Failure 500 {object} base.HTTPError
  224. // @Router /api/v1/token_refresh [put]
  225. func (c *Controller) TokenRefresh(ctx *gin.Context) {
  226. // 解析参数
  227. req := &param_v1.TokenRequest{}
  228. parseParamTask := func() error {
  229. err := util.ShouldBind(ctx, &req.Header, nil, nil, nil)
  230. if err != nil {
  231. logger.Error("func",
  232. zap.String("call", "util.ShouldBind"),
  233. zap.String("error", err.Error()))
  234. return errors.ParamsError
  235. }
  236. return nil
  237. }
  238. // 业务处理
  239. handleServiceTask := func() error {
  240. tokenObj, err := jwtwrapper.ParseToken(req.Token)
  241. if tokenObj == nil {
  242. return errors.TokenFailedError
  243. }
  244. if err != nil {
  245. switch err.(*jwt.ValidationError).Errors {
  246. case jwt.ValidationErrorExpired:
  247. if tokenObj == nil {
  248. return errors.TokenFailedError
  249. }
  250. if time.Now().Unix()-tokenObj.ExpiresAt > 600 {
  251. return errors.TokenExpiredError
  252. }
  253. default:
  254. return errors.TokenFailedError
  255. }
  256. }
  257. uid := tokenObj.Id
  258. subject := tokenObj.Subject
  259. //remberPass := gjson.GetBytes([]byte(subject), "rember_password").Bool()
  260. // 生成token
  261. token, err := jwtwrapper.GenToken(uid, parser.Conf.Jwt.Issuer, subject,
  262. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  263. if err != nil {
  264. logger.Error("func",
  265. zap.String("call", "util.GenJwtToken"),
  266. zap.String("args", fmt.Sprintf("%s", uid)),
  267. zap.String("error", err.Error()))
  268. return errors.SystemError
  269. }
  270. /*
  271. refreshTokenTime := time.Duration(24*60*60) * time.Second
  272. if remberPass {
  273. refreshTokenTime = time.Duration(7*24*60*60) * time.Second
  274. }
  275. refreshToken, err := jwtwrapper.GenToken(uid, parser.Conf.Jwt.Issuer, subject,
  276. refreshTokenTime)*/
  277. if err != nil {
  278. logger.Error("func",
  279. zap.String("call", "util.GenJwtToken"),
  280. zap.String("args", fmt.Sprintf("%s", uid)),
  281. zap.String("error", err.Error()))
  282. return errors.SystemError
  283. }
  284. resp := param_v1.TokenResponse{}
  285. resp.Data = token
  286. resp.RefreshToken = token
  287. ctx.JSON(http.StatusOK, resp)
  288. return nil
  289. }
  290. // 执行任务
  291. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  292. }
  293. //
  294. // @Summary 实名认证
  295. // @Description 实名认证
  296. // @Tags 用户
  297. // @Accept json
  298. // @Produce json
  299. // @Param token header string true "token"
  300. // @Param body body v1.RealNameAuthBody true "信息"
  301. // @Success 200 {object} v1.RealNameAuthResponse
  302. // @Failure 500 {object} base.HTTPError
  303. // @Router /api/v1/user/real_name_auth [put]
  304. func (c *Controller) RealNameAuth(ctx *gin.Context) {
  305. // 解析参数
  306. req := &param_v1.RealNameAuthRequest{}
  307. parseParamTask := func() error {
  308. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.RealNameAuthBody)
  309. if err != nil {
  310. logger.Error("func",
  311. zap.String("call", "util.ShouldBind"),
  312. zap.String("error", err.Error()))
  313. return errors.ParamsError
  314. }
  315. return nil
  316. }
  317. // 业务处理
  318. handleServiceTask := func() error {
  319. tokenInfo, err := utils.GetJwtTokenInfo(ctx)
  320. if err != nil {
  321. return err
  322. }
  323. // 响应数据
  324. resp := param_v1.RealNameAuthResponse{}
  325. rpcReq := &v1.RealNameAuthRequest{
  326. Uid: tokenInfo.Uid,
  327. IdNumber: req.IdNumber,
  328. IdType: req.IdType,
  329. RealName: req.RealName,
  330. }
  331. _, err = pb.Household.RealNameAuth(ctx, rpcReq)
  332. if err != nil {
  333. s, _ := json.MarshalToString(req)
  334. logger.Error("func",
  335. zap.String("call", "pb.Household.RealNameAuth"),
  336. zap.String("params", s),
  337. zap.String("error", err.Error()))
  338. return errors.ErrorTransForm(err)
  339. }
  340. subject := map[string]interface{}{
  341. "nick_name": tokenInfo.NickName,
  342. "open_id": tokenInfo.OpenId,
  343. "real_name": tokenInfo.RealName,
  344. "phone": tokenInfo.Phone,
  345. "openim_id": tokenInfo.OpenimId,
  346. "user_icon": tokenInfo.UserIcon,
  347. "id_number": tokenInfo.IdNumber,
  348. }
  349. if req.RealName != "" && req.RealName != tokenInfo.RealName {
  350. subject["real_name"] = req.RealName
  351. }
  352. if req.IdNumber != "" && req.IdNumber != tokenInfo.IdNumber {
  353. subject["id_number"] = req.IdNumber
  354. }
  355. str, _ := json.MarshalToString(subject)
  356. // 生成token
  357. token, err := jwtwrapper.GenToken(fmt.Sprintf("%d", tokenInfo.Uid), parser.Conf.Jwt.Issuer, str,
  358. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  359. if err != nil {
  360. logger.Error("func",
  361. zap.String("call", "util.GenJwtToken"),
  362. zap.String("args", fmt.Sprintf("%d", tokenInfo.Uid)),
  363. zap.String("error", err.Error()))
  364. return errors.SystemError
  365. }
  366. resp.Data.Token = token
  367. ctx.JSON(http.StatusOK, resp)
  368. return nil
  369. }
  370. // 执行任务
  371. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  372. }
  373. //
  374. // @Summary 修改昵称
  375. // @Description 修改昵称
  376. // @Tags 用户
  377. // @Accept json
  378. // @Produce json
  379. // @Param token header string true "token"
  380. // @Param body body v1.UserUpdateNickNameBody true "信息"
  381. // @Success 200 {object} v1.UserUpdateNickNameResponse
  382. // @Failure 500 {object} base.HTTPError
  383. // @Router /api/v1/user/nick_name [put]
  384. func (c *Controller) UserUpdateNickName(ctx *gin.Context) {
  385. // 解析参数
  386. req := &param_v1.UserUpdateNickNameRequest{}
  387. parseParamTask := func() error {
  388. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.UserUpdateNickNameBody)
  389. if err != nil {
  390. logger.Error("func",
  391. zap.String("call", "util.ShouldBind"),
  392. zap.String("error", err.Error()))
  393. return errors.ParamsError
  394. }
  395. return nil
  396. }
  397. // 业务处理
  398. handleServiceTask := func() error {
  399. tokenInfo, err := utils.GetJwtTokenInfo(ctx)
  400. if err != nil {
  401. return err
  402. }
  403. // 响应数据
  404. resp := param_v1.UserUpdateNickNameResponse{}
  405. rpcReq := &v1.UserUpdateNickNameRequest{
  406. Uid: tokenInfo.Uid,
  407. NickName: req.NickName,
  408. }
  409. _, err = pb.Household.UserUpdateNickName(ctx, rpcReq)
  410. if err != nil {
  411. s, _ := json.MarshalToString(req)
  412. logger.Error("func",
  413. zap.String("call", "pb.Household.UserUpdateNickName"),
  414. zap.String("params", s),
  415. zap.String("error", err.Error()))
  416. return errors.ErrorTransForm(err)
  417. }
  418. ctx.JSON(http.StatusOK, resp)
  419. return nil
  420. }
  421. // 执行任务
  422. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  423. }
  424. //
  425. // @Summary 获取用户信息
  426. // @Description 获取用户信息
  427. // @Tags 用户
  428. // @Accept json
  429. // @Produce json
  430. // @Param token header string true "token"
  431. // @Success 200 {object} v1.UserInfoResponse
  432. // @Failure 500 {object} base.HTTPError
  433. // @Router /api/v1/user/info [get]
  434. func (c *Controller) UserInfo(ctx *gin.Context) {
  435. // 解析参数
  436. req := &param_v1.UserInfoRequest{}
  437. parseParamTask := func() error {
  438. err := util.ShouldBind(ctx, &req.Header, nil, nil, nil)
  439. if err != nil {
  440. logger.Error("func",
  441. zap.String("call", "util.ShouldBind"),
  442. zap.String("error", err.Error()))
  443. return errors.ParamsError
  444. }
  445. return nil
  446. }
  447. // 业务处理
  448. handleServiceTask := func() error {
  449. tokenInfo, err := utils.GetJwtTokenInfo(ctx)
  450. if err != nil {
  451. return err
  452. }
  453. // 响应数据
  454. resp := param_v1.UserInfoResponse{}
  455. rpcReq := &v1.UserInfoRequest{
  456. Uid: tokenInfo.Uid,
  457. }
  458. rpcRsp, err := pb.Household.UserInfo(ctx, rpcReq)
  459. if err != nil {
  460. s, _ := json.MarshalToString(req)
  461. logger.Error("func",
  462. zap.String("call", "pb.Household.UserInfo"),
  463. zap.String("params", s),
  464. zap.String("error", err.Error()))
  465. return errors.ErrorTransForm(err)
  466. }
  467. resp.Data = *rpcRsp
  468. ctx.JSON(http.StatusOK, resp)
  469. return nil
  470. }
  471. // 执行任务
  472. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  473. }
  474. //
  475. // @Summary 用户的房屋
  476. // @Description 用户的房屋
  477. // @Tags 用户
  478. // @Accept json
  479. // @Produce json
  480. // @Param token header string true "token"
  481. // @Param for_rent query bool false "true:返回可发布租房的房屋 false:返回用户的所有房屋"
  482. // @Success 200 {object} v1.HouseholdHousesResponse
  483. // @Failure 500 {object} base.HTTPError
  484. // @Router /api/v1/user/house [get]
  485. func (c *Controller) HouseholdHouses(ctx *gin.Context) {
  486. // 解析参数
  487. req := &param_v1.HouseholdHousesRequest{}
  488. parseParamTask := func() error {
  489. err := util.ShouldBind(ctx, &req.Header, nil, &req.HouseholdHousesQuery, nil)
  490. if err != nil {
  491. logger.Error("func",
  492. zap.String("call", "util.ShouldBind"),
  493. zap.String("error", err.Error()))
  494. return errors.ParamsError
  495. }
  496. return nil
  497. }
  498. // 业务处理
  499. handleServiceTask := func() error {
  500. tokenInfo, err := utils.GetJwtTokenInfo(ctx)
  501. if err != nil {
  502. return err
  503. }
  504. // 响应数据
  505. resp := param_v1.HouseholdHousesResponse{}
  506. rpcReq := &v1.HouseholdHousesRequest{
  507. Uid: tokenInfo.Uid,
  508. ForRent: req.ForRent,
  509. //BaseConf:
  510. //SpecialConf
  511. }
  512. rpcRsp, err := pb.Household.HouseholdHouses(ctx, rpcReq)
  513. if err != nil {
  514. s, _ := json.MarshalToString(req)
  515. logger.Error("func",
  516. zap.String("call", "pb.Household.HouseholdHouses"),
  517. zap.String("params", s),
  518. zap.String("error", err.Error()))
  519. return errors.ErrorTransForm(err)
  520. }
  521. if rpcRsp.List == nil {
  522. rpcRsp.List = make([]*v1.HouseholdHouseInfo, 0)
  523. }
  524. resp.Data = *rpcRsp
  525. ctx.JSON(http.StatusOK, resp)
  526. return nil
  527. }
  528. // 执行任务
  529. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  530. }
  531. //
  532. // @Summary 用户房屋下的车位(仅业主可查看)
  533. // @Description 用户房屋下的车位(仅业主可查看)
  534. // @Tags 用户
  535. // @Accept json
  536. // @Produce json
  537. // @Param token header string true "token"
  538. // @Param house_id query int true "房屋id"
  539. // @Param garden_id query int true "小区id"
  540. // @Param user_type query int true "住户类型1 业主 2 家人 3 租户"
  541. // @Success 200 {object} v1.HouseholdSpaceResponse
  542. // @Failure 500 {object} base.HTTPError
  543. // @Router /api/v1/user/space [get]
  544. func (c *Controller) HouseholdSpace(ctx *gin.Context) {
  545. // 解析参数
  546. req := &param_v1.HouseholdSpaceRequest{}
  547. parseParamTask := func() error {
  548. err := util.ShouldBind(ctx, &req.Header, nil, &req.HouseholdSpaceQuery, nil)
  549. if err != nil {
  550. logger.Error("func",
  551. zap.String("call", "util.ShouldBind"),
  552. zap.String("error", err.Error()))
  553. return errors.ParamsError
  554. }
  555. return nil
  556. }
  557. // 业务处理
  558. handleServiceTask := func() error {
  559. //tokenInfo, err := utils.GetJwtTokenInfo(ctx)
  560. //if err != nil {
  561. // return err
  562. //}
  563. // 响应数据
  564. resp := param_v1.HouseholdSpaceResponse{}
  565. rpcReq := &v1.ParkSpaceListRequest{
  566. GardenId: req.GardenId,
  567. PageSize: -1,
  568. Page: -1,
  569. HouseId: req.HouseId,
  570. }
  571. // 非业主不能查看
  572. if req.UserType != 1 {
  573. resp.Data = v1.ParkSpaceListReply{}
  574. resp.Data.List = make([]*v1.ParkSpaceItem, 0)
  575. ctx.JSON(http.StatusOK, resp)
  576. return nil
  577. }
  578. rpcRsp, err := pb.Garden.ParkSpaceList(ctx, rpcReq)
  579. if err != nil {
  580. s, _ := json.MarshalToString(req)
  581. logger.Error("func",
  582. zap.String("call", "pb.Garden.ParkSpaceList"),
  583. zap.String("params", s),
  584. zap.String("error", err.Error()))
  585. return errors.ErrorTransForm(err)
  586. }
  587. if rpcRsp.List == nil {
  588. rpcRsp.List = make([]*v1.ParkSpaceItem, 0)
  589. }
  590. resp.Data = *rpcRsp
  591. ctx.JSON(http.StatusOK, resp)
  592. return nil
  593. }
  594. // 执行任务
  595. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  596. }
  597. //
  598. // @Summary 用户小区下的车辆
  599. // @Description 用户小区下的车辆
  600. // @Tags 用户
  601. // @Accept json
  602. // @Produce json
  603. // @Param token header string true "token"
  604. // @Param garden_id query int true "小区id"
  605. // @Success 200 {object} v1.HouseholdVehicleResponse
  606. // @Failure 500 {object} base.HTTPError
  607. // @Router /api/v1/user/vehicle [get]
  608. func (c *Controller) HouseholdVehicle(ctx *gin.Context) {
  609. // 解析参数
  610. req := &param_v1.HouseholdVehicleRequest{}
  611. parseParamTask := func() error {
  612. err := util.ShouldBind(ctx, &req.Header, nil, &req.HouseholdVehicleQuery, nil)
  613. if err != nil {
  614. logger.Error("func",
  615. zap.String("call", "util.ShouldBind"),
  616. zap.String("error", err.Error()))
  617. return errors.ParamsError
  618. }
  619. return nil
  620. }
  621. // 业务处理
  622. handleServiceTask := func() error {
  623. tokenInfo, err := utils.GetJwtTokenInfo(ctx)
  624. if err != nil {
  625. return err
  626. }
  627. //响应数据
  628. resp := param_v1.HouseholdVehicleResponse{}
  629. rpcReq := &v1.VehicleListRequest{
  630. GardenId: req.GardenId,
  631. PageSize: -1,
  632. Page: -1,
  633. HouseholdUid: tokenInfo.Uid,
  634. }
  635. rpcRsp, err := pb.Garden.VehicleList(ctx, rpcReq)
  636. if err != nil {
  637. s, _ := json.MarshalToString(req)
  638. logger.Error("func",
  639. zap.String("call", "pb.Garden.VehicleList"),
  640. zap.String("params", s),
  641. zap.String("error", err.Error()))
  642. return errors.ErrorTransForm(err)
  643. }
  644. if rpcRsp.List == nil {
  645. rpcRsp.List = make([]*v1.VehicleItem, 0)
  646. }
  647. resp.Data = *rpcRsp
  648. ctx.JSON(http.StatusOK, resp)
  649. return nil
  650. }
  651. // 执行任务
  652. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  653. }