user.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. "context"
  6. "fmt"
  7. "git.getensh.com/common/gopkgs/cache"
  8. "github.com/tidwall/gjson"
  9. "net/http"
  10. "property-applete-gateway/errors"
  11. param_v1 "property-applete-gateway/param/v1"
  12. "property-applete-gateway/pb"
  13. "property-applete-gateway/pb/v1"
  14. "property-applete-gateway/utils"
  15. "time"
  16. "git.getensh.com/common/gopkgs/logger"
  17. "git.getensh.com/common/gopkgs/tasker/httptasker"
  18. "git.getensh.com/common/gopkgs/util"
  19. "github.com/dgrijalva/jwt-go"
  20. "property-applete-gateway/parser"
  21. "git.getensh.com/common/gopkgs/jwtwrapper"
  22. "github.com/gin-gonic/gin"
  23. "go.uber.org/zap"
  24. )
  25. func phonePasswordLogin(req *param_v1.LoginRequest) (param_v1.LoginResponse, error) {
  26. resp := param_v1.LoginResponse{}
  27. rpcReq := &v1.LoginByPhoneRequest{
  28. Phone:req.User,
  29. Password:req.Password,
  30. }
  31. rpcRsp, err := pb.System.LoginByPhone(context.Background(), rpcReq)
  32. if err != nil {
  33. s, _ := json.MarshalToString(req)
  34. logger.Error("func",
  35. zap.String("call", "pb.System.LoginByPhone"),
  36. zap.String("params", s),
  37. zap.String("error", err.Error()))
  38. return resp, err
  39. }
  40. if len(rpcRsp.List) == 0 {
  41. return resp, errors.ErrRecordNotFound
  42. }
  43. // 只有一条记录
  44. if len(rpcRsp.List) == 1 {
  45. now := time.Now()
  46. routers := map[string]bool{}
  47. for _, v := range rpcRsp.List[0].Permissions {
  48. routers[v.Router] = true
  49. }
  50. subject := map[string]interface{}{
  51. "user_name": req.User,
  52. "cid":rpcRsp.List[0].Cid,
  53. "garden_id":rpcRsp.List[0].GardenId,
  54. "is_super_group":rpcRsp.List[0].IsSuperGroup,
  55. "routers":routers,
  56. "garden_name":rpcRsp.List[0].GardenName,
  57. "g_permission_time":rpcRsp.List[0].GlobalPermissionTime,
  58. "u_permission_time":rpcRsp.List[0].UserPermissionTime,
  59. "single_sign_time":fmt.Sprintf("%d", now.Unix()),
  60. }
  61. str, _ := json.MarshalToString(subject)
  62. // 生成token
  63. token, err := jwtwrapper.GenToken(fmt.Sprintf("%d", rpcRsp.List[0].Uid), parser.Conf.Jwt.Issuer, str,
  64. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  65. if err != nil {
  66. logger.Error("func",
  67. zap.String("call", "util.GenJwtToken"),
  68. zap.String("args", fmt.Sprintf("%d", rpcRsp.List[0].Uid)),
  69. zap.String("error", err.Error()))
  70. return resp, errors.SystemError
  71. }
  72. if err = utils.SetSingleSignTime(rpcRsp.List[0].Uid, now.Unix()); err != nil {
  73. return resp, err
  74. }
  75. permissions := []v1.SystemGroupPermissionData{}
  76. for _, v := range rpcRsp.List[0].Permissions {
  77. // 前端控制的权限
  78. //if v.Front {
  79. permissions = append(permissions, *v)
  80. //}
  81. }
  82. resp.Data.Uid = rpcRsp.List[0].Uid
  83. resp.Data.Token = token
  84. resp.Data.User = req.User
  85. resp.Data.Permissions = permissions
  86. resp.Data.IsSuper = rpcRsp.List[0].IsSuperGroup
  87. return resp, nil
  88. }
  89. // 多条记录需要选择小区
  90. loginResp, err := makeLoginByPhoneResponse(rpcRsp, req.User)
  91. if err != nil {
  92. return resp, nil
  93. }
  94. str, _ := json.MarshalToString(rpcRsp)
  95. key := utils.GetKey(utils.SYSTEMPHONELOGINKEY, req.User)
  96. _, err = cache.Redis().SetEx(key, 600, str)
  97. if err != nil {
  98. return resp, errors.RedisError
  99. }
  100. resp.PhoneData = loginResp.Data
  101. return resp, nil
  102. }
  103. // 登录
  104. // @Summary 登录
  105. // @Description 登录
  106. // @Tags 用户
  107. // @Accept json
  108. // @Produce json
  109. // @Param body body v1.LoginBody true "登录信息"
  110. // @Success 200 {object} v1.LoginResponse
  111. // @Failure 500 {object} base.HTTPError
  112. // @Router /api/v1/user/login [post]
  113. func (c *Controller) Login(ctx *gin.Context) {
  114. // 解析参数
  115. req := &param_v1.LoginRequest{}
  116. parseParamTask := func() error {
  117. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.LoginBody)
  118. if err != nil {
  119. logger.Error("func",
  120. zap.String("call", "util.ShouldBind"),
  121. zap.String("error", err.Error()))
  122. return errors.ParamsError
  123. }
  124. if req.Password == "" {
  125. return errors.ParamsError
  126. }
  127. return nil
  128. }
  129. // 业务处理
  130. handleServiceTask := func() error {
  131. // 响应数据
  132. resp := param_v1.LoginResponse{}
  133. // 先尝试手机号登录
  134. if utils.VerifyMobileFormat(req.User) {
  135. if resp, err := phonePasswordLogin(req); err == nil {
  136. ctx.JSON(http.StatusOK, resp)
  137. return nil
  138. }
  139. }
  140. rpcReq := &v1.LoginRequest{
  141. Username: req.User,
  142. Password: req.Password,
  143. }
  144. rpcRsp, err := pb.System.Login(ctx, rpcReq)
  145. if err != nil {
  146. s, _ := json.MarshalToString(req)
  147. logger.Error("func",
  148. zap.String("call", "pb.System.Login"),
  149. zap.String("params", s),
  150. zap.String("error", err.Error()))
  151. return errors.ErrorTransForm(err)
  152. }
  153. routers := map[string]bool{}
  154. for _, v := range rpcRsp.Permissions {
  155. routers[v.Router] = true
  156. }
  157. now := time.Now()
  158. subject := map[string]interface{}{
  159. "user_name": req.User,
  160. "cid":rpcRsp.Cid,
  161. "garden_id":rpcRsp.GardenId,
  162. "is_super_group":rpcRsp.IsSuperGroup,
  163. "routers":routers,
  164. "garden_name":rpcRsp.GardenName,
  165. "g_permission_time":rpcRsp.GlobalPermissionTime,
  166. "u_permission_time":rpcRsp.UserPermissionTime,
  167. "single_sign_time":fmt.Sprintf("%d", now.Unix()),
  168. }
  169. str, _ := json.MarshalToString(subject)
  170. // 生成token
  171. token, err := jwtwrapper.GenToken(fmt.Sprintf("%d", rpcRsp.Uid), parser.Conf.Jwt.Issuer, str,
  172. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  173. if err != nil {
  174. logger.Error("func",
  175. zap.String("call", "util.GenJwtToken"),
  176. zap.String("args", fmt.Sprintf("%d", rpcRsp.Uid)),
  177. zap.String("error", err.Error()))
  178. return errors.SystemError
  179. }
  180. if err = utils.SetSingleSignTime(rpcRsp.Uid, now.Unix()); err != nil {
  181. return err
  182. }
  183. permissions := []v1.SystemGroupPermissionData{}
  184. for _, v := range rpcRsp.Permissions {
  185. // 前端控制的权限
  186. //if v.Front {
  187. permissions = append(permissions, *v)
  188. //}
  189. }
  190. resp.Data.Uid = rpcRsp.Uid
  191. resp.Data.Token = token
  192. resp.Data.User = req.User
  193. resp.Data.Permissions = permissions
  194. resp.Data.IsSuper = rpcRsp.IsSuperGroup
  195. ctx.JSON(http.StatusOK, resp)
  196. return nil
  197. }
  198. // 执行任务
  199. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  200. }
  201. // token
  202. // @Summary 刷新token
  203. // @Description 刷新token
  204. // @Tags 用户
  205. // @Accept json
  206. // @Produce json
  207. // @Param token header string true "token"
  208. // @Success 200 {object} v1.TokenResponse
  209. // @Failure 500 {object} base.HTTPError
  210. // @Router /api/v1/token_refresh [put]
  211. func (c *Controller) TokenRefresh(ctx *gin.Context) {
  212. // 解析参数
  213. req := &param_v1.TokenRequest{}
  214. parseParamTask := func() error {
  215. err := util.ShouldBind(ctx, &req.Header, nil, nil, nil)
  216. if err != nil {
  217. logger.Error("func",
  218. zap.String("call", "util.ShouldBind"),
  219. zap.String("error", err.Error()))
  220. return errors.ParamsError
  221. }
  222. return nil
  223. }
  224. // 业务处理
  225. handleServiceTask := func() error {
  226. tokenObj, err := jwtwrapper.ParseToken(req.Token)
  227. if tokenObj == nil {
  228. return errors.TokenFailedError
  229. }
  230. if err != nil {
  231. switch err.(*jwt.ValidationError).Errors {
  232. case jwt.ValidationErrorExpired:
  233. if tokenObj == nil {
  234. return errors.TokenFailedError
  235. }
  236. if time.Now().Unix() - tokenObj.ExpiresAt > 3600{
  237. return errors.TokenFailedError
  238. }
  239. default:
  240. return errors.TokenFailedError
  241. }
  242. }
  243. uid := tokenObj.Id
  244. subject := tokenObj.Subject
  245. remberPass := gjson.GetBytes([]byte(subject), "rember_password").Bool()
  246. // 生成token
  247. token, err := jwtwrapper.GenToken(uid, parser.Conf.Jwt.Issuer, subject,
  248. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  249. if err != nil {
  250. logger.Error("func",
  251. zap.String("call", "util.GenJwtToken"),
  252. zap.String("args", fmt.Sprintf("%s", uid)),
  253. zap.String("error", err.Error()))
  254. return errors.SystemError
  255. }
  256. refreshTokenTime := time.Duration(24*60*60)*time.Second
  257. if remberPass {
  258. refreshTokenTime = time.Duration(7*24*60*60)*time.Second
  259. }
  260. refreshToken, err := jwtwrapper.GenToken(uid, parser.Conf.Jwt.Issuer, subject,
  261. refreshTokenTime)
  262. if err != nil {
  263. logger.Error("func",
  264. zap.String("call", "util.GenJwtToken"),
  265. zap.String("args", fmt.Sprintf("%s", uid)),
  266. zap.String("error", err.Error()))
  267. return errors.SystemError
  268. }
  269. resp := param_v1.TokenResponse{}
  270. resp.Data = token
  271. resp.RefreshToken = refreshToken
  272. ctx.JSON(http.StatusOK, resp)
  273. return nil
  274. }
  275. // 执行任务
  276. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  277. }
  278. //
  279. // @Summary 获取短信验证码
  280. // @Description 获取短信验证码
  281. // @Tags 用户
  282. // @Accept json
  283. // @Produce json
  284. // @Param phone query string true " "
  285. // @Success 200 {object} v1.GetVcodeResponse
  286. // @Failure 500 {object} base.HTTPError
  287. // @Router /api/v1/user/vcode [get]
  288. func (c *Controller) GetVcode(ctx *gin.Context) {
  289. // 解析参数
  290. req := &param_v1.GetVcodeRequest{}
  291. parseParamTask := func() error {
  292. err := util.ShouldBind(ctx, nil, nil, &req.GetVcodeQuery, nil)
  293. if err != nil {
  294. logger.Error("func",
  295. zap.String("call", "util.ShouldBind"),
  296. zap.String("error", err.Error()))
  297. return errors.ParamsError
  298. }
  299. return nil
  300. }
  301. checkPhoneTask := func() error {
  302. mreq := &v1.CheckPhoneRequest{Phone:req.Phone}
  303. _, err := pb.System.CheckPhone(ctx, mreq)
  304. if err != nil {
  305. return err
  306. }
  307. return nil
  308. }
  309. // 业务处理
  310. handleServiceTask := func() error {
  311. // 响应数据
  312. resp := param_v1.GetVcodeResponse{}
  313. rpcReq := &v1.GetVcodeRequest{
  314. PhoneNumber:req.Phone,
  315. }
  316. rpcRsp, err := pb.Thirdparty.GetVcode(ctx, rpcReq)
  317. if err != nil {
  318. s, _ := json.MarshalToString(req)
  319. logger.Error("func",
  320. zap.String("call", "Thirdparty.GetVcode"),
  321. zap.String("params", s),
  322. zap.String("error", err.Error()))
  323. return errors.ErrorTransForm(err)
  324. }
  325. resp.Data = *rpcRsp
  326. ctx.JSON(http.StatusOK, resp)
  327. return nil
  328. }
  329. // 执行任务
  330. httptasker.Exec(ctx, parseParamTask, checkPhoneTask, handleServiceTask)
  331. }
  332. func makeLoginByPhoneToken(rpcRsp *v1.LoginByPhoneReply, phone string) (token string, err error) {
  333. uid := int64(0)
  334. var subject map[string]interface{}
  335. now := time.Now()
  336. if len(rpcRsp.List) > 1 {
  337. subject = map[string]interface{}{
  338. "phone": phone,
  339. "should_choose_garden":true,
  340. }
  341. uid = 0
  342. } else {
  343. routers := map[string]bool{}
  344. for _, v := range rpcRsp.List[0].Permissions {
  345. routers[v.Router] = true
  346. }
  347. subject = map[string]interface{}{
  348. "user_name": rpcRsp.List[0].UserName,
  349. "cid":rpcRsp.List[0].Cid,
  350. "garden_id":rpcRsp.List[0].GardenId,
  351. "is_super_group":rpcRsp.List[0].IsSuperGroup,
  352. "routers":routers,
  353. "phone":phone,
  354. "garden_name":rpcRsp.List[0].GardenName,
  355. "g_permission_time":rpcRsp.List[0].GlobalPermissionTime,
  356. "u_permission_time":rpcRsp.List[0].UserPermissionTime,
  357. "single_sign_time":fmt.Sprintf("%d", now.Unix()),
  358. }
  359. uid = rpcRsp.List[0].Uid
  360. }
  361. str, _ := json.MarshalToString(subject)
  362. token, err = jwtwrapper.GenToken(fmt.Sprintf("%d", uid), parser.Conf.Jwt.Issuer, str,
  363. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  364. if err != nil {
  365. logger.Error("func",
  366. zap.String("call", "util.GenJwtToken"),
  367. zap.String("args", fmt.Sprintf("%d", rpcRsp.List[0].Uid)),
  368. zap.String("error", err.Error()))
  369. return token, errors.SystemError
  370. }
  371. if len(rpcRsp.List) == 1 {
  372. if err = utils.SetSingleSignTime(rpcRsp.List[0].Uid, now.Unix()); err != nil {
  373. return token, err
  374. }
  375. }
  376. return token, nil
  377. }
  378. func makeLoginByPhoneResponse(rpcRsp *v1.LoginByPhoneReply, phone string) (resp param_v1.LoginByPhoneResponse, err error) {
  379. token, err := makeLoginByPhoneToken(rpcRsp, phone)
  380. if err != nil {
  381. return resp, err
  382. }
  383. for i, _ := range rpcRsp.List {
  384. permissions := []v1.SystemGroupPermissionData{}
  385. for _, v := range rpcRsp.List[i].Permissions {
  386. permissions = append(permissions, *v)
  387. }
  388. item := param_v1.LoginByPhoneItem{
  389. Uid:rpcRsp.List[i].Uid,
  390. GardenName:rpcRsp.List[i].GardenName,
  391. GardenId:rpcRsp.List[i].GardenId,
  392. User:rpcRsp.List[i].UserName,
  393. Permissions:permissions,
  394. IsSuper:rpcRsp.List[i].IsSuperGroup,
  395. }
  396. resp.Data.List = append(resp.Data.List, item)
  397. resp.Data.Token = token
  398. }
  399. return resp, nil
  400. }
  401. //
  402. // @Summary 手机号登录
  403. // @Description 手机号登录
  404. // @Tags 用户
  405. // @Accept json
  406. // @Produce json
  407. // @Param body body v1.LoginByPhoneBody true " "
  408. // @Success 200 {object} v1.LoginByPhoneResponse
  409. // @Failure 500 {object} base.HTTPError
  410. // @Router /api/v1/user/login_by_phone [post]
  411. func (c *Controller) LoginByPhone(ctx *gin.Context) {
  412. // 解析参数
  413. req := &param_v1.LoginByPhoneRequest{}
  414. loginResp := param_v1.LoginByPhoneResponse{}
  415. parseParamTask := func() error {
  416. err := util.ShouldBind(ctx, nil, nil, nil, &req.LoginByPhoneBody)
  417. if err != nil {
  418. logger.Error("func",
  419. zap.String("call", "util.ShouldBind"),
  420. zap.String("error", err.Error()))
  421. return errors.ParamsError
  422. }
  423. return nil
  424. }
  425. // 业务处理
  426. handleServiceTask := func() error {
  427. // 响应数据
  428. rpcReq := &v1.LoginByPhoneRequest{
  429. Phone:req.Phone,
  430. }
  431. rpcRsp, err := pb.System.LoginByPhone(ctx, rpcReq)
  432. if err != nil {
  433. s, _ := json.MarshalToString(req)
  434. logger.Error("func",
  435. zap.String("call", "Thirdparty.GetVcode"),
  436. zap.String("params", s),
  437. zap.String("error", err.Error()))
  438. return errors.ErrorTransForm(err)
  439. }
  440. if len(rpcRsp.List) == 0 {
  441. return errors.ErrRecordNotFound
  442. }
  443. loginResp, err = makeLoginByPhoneResponse(rpcRsp, req.Phone)
  444. if err != nil {
  445. return err
  446. }
  447. str, _ := json.MarshalToString(rpcRsp)
  448. key := utils.GetKey(utils.SYSTEMPHONELOGINKEY, req.Phone)
  449. _, err = cache.Redis().SetEx(key, 600, str)
  450. if err != nil {
  451. return errors.RedisError
  452. }
  453. return nil
  454. }
  455. checkVcodeTask := func() error {
  456. rpcReq := &v1.CheckVcodeRequest{
  457. PhoneNumber:req.Phone,
  458. Vcode:req.Vcode,
  459. }
  460. _, err := pb.Thirdparty.CheckVcode(ctx, rpcReq)
  461. if err != nil {
  462. s, _ := json.MarshalToString(req)
  463. logger.Error("func",
  464. zap.String("call", "pb.Thirdparty.CheckVcode"),
  465. zap.String("params", s),
  466. zap.String("error", err.Error()))
  467. return errors.ErrorTransForm(err)
  468. }
  469. ctx.JSON(http.StatusOK, loginResp)
  470. return nil
  471. }
  472. // 执行任务
  473. httptasker.Exec(ctx, parseParamTask, handleServiceTask, checkVcodeTask)
  474. }
  475. //
  476. // @Summary 手机号登录选择账户
  477. // @Description 手机号登录选择账户
  478. // @Tags 用户
  479. // @Accept json
  480. // @Produce json
  481. // @Param token header string true "token"
  482. // @Param id query int true "账户id"
  483. // @Success 200 {object} v1.ChooseUserResponse
  484. // @Failure 500 {object} base.HTTPError
  485. // @Router /api/v1/user/choose_user [get]
  486. func (c *Controller) ChooseUser(ctx *gin.Context) {
  487. // 解析参数
  488. req := &param_v1.ChooseUserRequest{}
  489. parseParamTask := func() error {
  490. err := util.ShouldBind(ctx, &req.Header, nil, &req.ChooseUserQuery, nil)
  491. if err != nil {
  492. logger.Error("func",
  493. zap.String("call", "util.ShouldBind"),
  494. zap.String("error", err.Error()))
  495. return errors.ParamsError
  496. }
  497. return nil
  498. }
  499. // 业务处理
  500. handleServiceTask := func() error {
  501. tokenInfo, err := utils.GetSubjectValue(ctx)
  502. if err != nil {
  503. return err
  504. }
  505. resp := param_v1.ChooseUserResponse{}
  506. // 获取缓存的用户信息
  507. key := utils.GetKey(utils.SYSTEMPHONELOGINKEY, tokenInfo.Phone)
  508. str, err := cache.Redis().Get(key)
  509. if str == "" {
  510. return errors.SystemError
  511. }
  512. loginByPhoneResp := v1.LoginByPhoneReply{}
  513. json.Unmarshal([]byte(str), &loginByPhoneResp)
  514. var userInfo *v1.LoginReply
  515. // 搜索目标小区
  516. for _, v := range loginByPhoneResp.List {
  517. if v.Uid == req.Id {
  518. userInfo = v
  519. break
  520. }
  521. }
  522. if userInfo == nil {
  523. return errors.SystemError
  524. }
  525. now := time.Now()
  526. // 构造响应数据
  527. routers := map[string]bool{}
  528. for _, v := range userInfo.Permissions {
  529. routers[v.Router] = true
  530. }
  531. subject := map[string]interface{}{
  532. "user_name": userInfo.UserName,
  533. "cid":userInfo.Cid,
  534. "garden_id":userInfo.GardenId,
  535. "is_super_group":userInfo.IsSuperGroup,
  536. "routers":routers,
  537. "garden_name":userInfo.GardenName,
  538. "g_permission_time":userInfo.GlobalPermissionTime,
  539. "u_permission_time":userInfo.UserPermissionTime,
  540. "single_sign_time":fmt.Sprintf("%d", now.Unix()),
  541. }
  542. str, _ = json.MarshalToString(subject)
  543. // 生成token
  544. token, err := jwtwrapper.GenToken(fmt.Sprintf("%d", userInfo.Uid), parser.Conf.Jwt.Issuer, str,
  545. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  546. if err != nil {
  547. logger.Error("func",
  548. zap.String("call", "util.GenJwtToken"),
  549. zap.String("args", fmt.Sprintf("%d", userInfo.Uid)),
  550. zap.String("error", err.Error()))
  551. return errors.SystemError
  552. }
  553. permissions := []v1.SystemGroupPermissionData{}
  554. for _, v := range userInfo.Permissions {
  555. permissions = append(permissions, *v)
  556. }
  557. if err = utils.SetSingleSignTime(userInfo.Uid, now.Unix()); err != nil {
  558. return err
  559. }
  560. resp.Data.Uid = userInfo.Uid
  561. resp.Data.Token = token
  562. resp.Data.User = userInfo.UserName
  563. resp.Data.IsSuper = userInfo.IsSuperGroup
  564. resp.Data.Permissions = permissions
  565. cache.Redis().Del(key)
  566. ctx.JSON(http.StatusOK, resp)
  567. return nil
  568. }
  569. // 执行任务
  570. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  571. }
  572. //
  573. // @Summary 忘记密码重置密码
  574. // @Description 忘记密码重置密码
  575. // @Tags 用户
  576. // @Accept json
  577. // @Produce json
  578. // @Param body body v1.ResetPasswordBody true "信息"
  579. // @Success 200 {object} v1.ResetPasswordResponse
  580. // @Failure 500 {object} base.HTTPError
  581. // @Router /api/v1/user/reset_password [put]
  582. func (c *Controller) ResetPassword(ctx *gin.Context) {
  583. // 解析参数
  584. req := &param_v1.ResetPasswordRequest{}
  585. parseParamTask := func() error {
  586. err := util.ShouldBind(ctx, nil, nil, nil, &req.ResetPasswordBody)
  587. if err != nil {
  588. logger.Error("func",
  589. zap.String("call", "util.ShouldBind"),
  590. zap.String("error", err.Error()))
  591. return errors.ParamsError
  592. }
  593. return nil
  594. }
  595. // 业务处理
  596. handleServiceTask := func() error {
  597. // 响应数据
  598. resp := param_v1.ResetPasswordResponse{}
  599. rpcReq := &v1.ResetPasswordRequest{
  600. Uid:req.Uid,
  601. Phone:req.Phone,
  602. Vcode:req.Vcode,
  603. Password: req.Password,
  604. }
  605. rpcRsp, err := pb.System.ResetPassword(ctx, rpcReq)
  606. if err != nil {
  607. s, _ := json.MarshalToString(req)
  608. logger.Error("func",
  609. zap.String("call", "pb.System.ResetPassword"),
  610. zap.String("params", s),
  611. zap.String("error", err.Error()))
  612. return errors.ErrorTransForm(err)
  613. }
  614. if rpcRsp.List == nil {
  615. rpcRsp.List = make([]*v1.ResetPasswordData, 0)
  616. }
  617. resp.Data = *rpcRsp
  618. ctx.JSON(http.StatusOK, resp)
  619. if len(rpcRsp.List) == 1 {
  620. logReq := OperationLogRequest{
  621. Module:ModuleSelf,
  622. Action:ActionSelfResetPassword,
  623. Origin:nil,
  624. Target:nil,
  625. UserName:"",
  626. Uid:rpcRsp.List[0].Uid,
  627. Cid:0,
  628. GardenId:0,
  629. }
  630. go OperationLogAdd(&logReq)
  631. }
  632. return nil
  633. }
  634. // 执行任务
  635. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  636. }
  637. //
  638. // @Summary 修改密码
  639. // @Description 修改密码
  640. // @Tags 用户
  641. // @Accept json
  642. // @Produce json
  643. // @Param token header string true " "
  644. // @Param body body v1.ChangePasswordBody true "信息"
  645. // @Success 200 {object} v1.ChangePasswordResponse
  646. // @Failure 500 {object} base.HTTPError
  647. // @Router /api/v1/user/change_password [put]
  648. func (c *Controller) ChangePassword(ctx *gin.Context) {
  649. // 解析参数
  650. req := &param_v1.ChangePasswordRequest{}
  651. parseParamTask := func() error {
  652. err := util.ShouldBind(ctx, nil, nil, nil, &req.ChangePasswordBody)
  653. if err != nil {
  654. logger.Error("func",
  655. zap.String("call", "util.ShouldBind"),
  656. zap.String("error", err.Error()))
  657. return errors.ParamsError
  658. }
  659. return nil
  660. }
  661. // 业务处理
  662. handleServiceTask := func() error {
  663. tokenInfo, err := utils.GetSubjectValue(ctx)
  664. if err != nil {
  665. return err
  666. }
  667. // 响应数据
  668. resp := param_v1.ChangePasswordResponse{}
  669. rpcReq := &v1.ChangePasswordRequest{
  670. Uid:tokenInfo.Uid,
  671. OldPassword:req.OldPassword,
  672. NewPassword:req.NewPassword,
  673. }
  674. _, err = pb.System.ChangePassword(ctx, rpcReq)
  675. if err != nil {
  676. s, _ := json.MarshalToString(req)
  677. logger.Error("func",
  678. zap.String("call", "pb.System.ChangePassword"),
  679. zap.String("params", s),
  680. zap.String("error", err.Error()))
  681. return errors.ErrorTransForm(err)
  682. }
  683. ctx.JSON(http.StatusOK, resp)
  684. logReq := OperationLogRequest{
  685. Module:ModuleSelf,
  686. Action:ActionSelfChangePassword,
  687. Origin:nil,
  688. Target:nil,
  689. UserName:tokenInfo.UserName,
  690. Uid:tokenInfo.Uid,
  691. Cid:tokenInfo.Cid,
  692. GardenId:tokenInfo.GardenId,
  693. }
  694. go OperationLogAdd(&logReq)
  695. return nil
  696. }
  697. // 执行任务
  698. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  699. }