user.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. "github.com/dgrijalva/jwt-go"
  6. "github.com/jaryhe/gopkgs/logger"
  7. "github.com/jaryhe/gopkgs/tasker/httptasker"
  8. "github.com/jaryhe/gopkgs/util"
  9. "google.golang.org/grpc/status"
  10. "net/http"
  11. "smart-supplier-management-gateway/consts"
  12. "smart-supplier-management-gateway/errors"
  13. param_v1 "smart-supplier-management-gateway/param/v1"
  14. "smart-supplier-management-gateway/pb"
  15. "smart-supplier-management-gateway/pb/v1"
  16. "strconv"
  17. "time"
  18. "fmt"
  19. "github.com/gin-gonic/gin"
  20. "go.uber.org/zap"
  21. "smart-supplier-management-gateway/utils"
  22. "github.com/jaryhe/gopkgs/jwtwrapper"
  23. "smart-supplier-management-gateway/parser"
  24. )
  25. // 注册
  26. // @Summary 注册
  27. // @Description 注册
  28. // @Tags user
  29. // @Accept json
  30. // @Produce json
  31. // @Param body body v1.RegisterBody true "登录信息"
  32. // @Success 200 {object} v1.RegisterResponse
  33. // @Failure 500 {object} base.HTTPError
  34. // @Router /api/v1/user/register [post]
  35. func (c *Controller) Register(ctx *gin.Context) {
  36. // 解析参数
  37. req := &param_v1.RegisterRequest{}
  38. parseParamTask := func() error {
  39. err := util.ShouldBind(ctx, nil, nil, nil, &req.RegisterBody)
  40. if err != nil {
  41. logger.Error("func",
  42. zap.String("call", "util.ShouldBind"),
  43. zap.String("error", err.Error()))
  44. return errors.ParamsError
  45. }
  46. return nil
  47. }
  48. // 业务处理
  49. handleServiceTask := func() error {
  50. // 响应数据
  51. vcode, _ := strconv.Atoi(req.Verify)
  52. resp := param_v1.RegisterResponse{}
  53. rpcReq := &v1.RegisterRequest{
  54. }
  55. utils.StructCopy(rpcReq, &req.RegisterBody, "")
  56. rpcReq.Vcode = uint32(vcode)
  57. rpcRsp, err := pb.Supplier.Register(ctx, rpcReq)
  58. if err != nil {
  59. s, _ := json.MarshalToString(req)
  60. logger.Error("func",
  61. zap.String("call", "Register"),
  62. zap.String("params", s),
  63. zap.String("error", err.Error()))
  64. return err
  65. }
  66. resp.Data = *rpcRsp
  67. ctx.JSON(http.StatusOK, resp)
  68. return nil
  69. }
  70. // 执行任务
  71. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  72. }
  73. // 登录
  74. // @Summary 登录
  75. // @Description 登录
  76. // @Tags user
  77. // @Accept json
  78. // @Produce json
  79. // @Param body body v1.LoginBody true "登录信息"
  80. // @Success 200 {object} v1.LoginResponse
  81. // @Failure 500 {object} base.HTTPError
  82. // @Router /api/v1/user/login [post]
  83. func (c *Controller) Login(ctx *gin.Context) {
  84. // 解析参数
  85. req := &param_v1.LoginRequest{}
  86. loginUid := int64(0)
  87. parseParamTask := func() error {
  88. err := util.ShouldBind(ctx, nil, nil, nil, &req.LoginBody)
  89. if err != nil {
  90. logger.Error("func",
  91. zap.String("call", "util.ShouldBind"),
  92. zap.String("error", err.Error()))
  93. return errors.ParamsError
  94. }
  95. return nil
  96. }
  97. // 业务处理
  98. handleServiceTask := func() error {
  99. if !utils.CaptchaVerify(ctx, req.Verify, req.Captcha) {
  100. return errors.VerifyError
  101. }
  102. // 响应数据
  103. resp := param_v1.LoginResponse{}
  104. rpcReq := &v1.LoginRequest{
  105. User:req.User,
  106. Password:req.Password,
  107. }
  108. rpcRsp, err := pb.Supplier.Login(ctx, rpcReq)
  109. if err != nil {
  110. s, _ := json.MarshalToString(req)
  111. logger.Error("func",
  112. zap.String("call", "Login"),
  113. zap.String("params", s),
  114. zap.String("error", err.Error()))
  115. return errors.ErrorTransForm(err)
  116. }
  117. loginUid = rpcRsp.Uid
  118. subJect := map[string]interface{}{
  119. "user_name":req.User,
  120. }
  121. str,_ := json.MarshalToString(subJect)
  122. // 生成token
  123. token, err := jwtwrapper.GenToken(fmt.Sprintf("%d", rpcRsp.Uid), parser.Conf.Jwt.Issuer, str,
  124. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  125. if err != nil {
  126. logger.Error("func",
  127. zap.String("call", "util.GenJwtToken"),
  128. zap.String("args", fmt.Sprintf("%d", rpcRsp.Uid)),
  129. zap.String("error", err.Error()))
  130. return errors.SystemError
  131. }
  132. refreshToken, err := jwtwrapper.GenToken(fmt.Sprintf("%d", rpcRsp.Uid), parser.Conf.Jwt.Issuer, str,
  133. time.Duration(7*24*60*60)*time.Second)
  134. if err != nil {
  135. logger.Error("func",
  136. zap.String("call", "util.GenJwtToken"),
  137. zap.String("args", fmt.Sprintf("%d", rpcRsp.Uid)),
  138. zap.String("error", err.Error()))
  139. return errors.SystemError
  140. }
  141. resp.Data.Uid = rpcRsp.Uid
  142. resp.Data.Token = token
  143. resp.Data.RefreshToken = refreshToken
  144. ctx.JSON(http.StatusOK, resp)
  145. return nil
  146. }
  147. // 执行任务
  148. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  149. utils.LogWrite("登录", loginUid, req.User, "", err, 0)
  150. }
  151. // 根据社会码获取注册状态
  152. // @Summary 根据社会码获取注册状态
  153. // @Description 根据社会码获取注册状态
  154. // @Tags user
  155. // @Accept json
  156. // @Produce json
  157. // @Param social_code query string true " "
  158. // @Success 200 {object} v1.ProviderApproveStatusResponse
  159. // @Failure 500 {object} base.HTTPError
  160. // @Router /api/v1/user/status [get]
  161. func (c *Controller) ProviderApproveStatus(ctx *gin.Context) {
  162. // 解析参数
  163. req := &param_v1.ProviderApproveStatusRequest{}
  164. parseParamTask := func() error {
  165. err := util.ShouldBind(ctx, nil, nil, &req.ProviderApproveStatusQuery, nil)
  166. if err != nil {
  167. logger.Error("func",
  168. zap.String("call", "util.ShouldBind"),
  169. zap.String("error", err.Error()))
  170. return errors.ParamsError
  171. }
  172. return nil
  173. }
  174. // 业务处理
  175. handleServiceTask := func() error {
  176. // 响应数据
  177. resp := param_v1.ProviderApproveStatusResponse{}
  178. rpcReq := &v1.ProviderApproveStatusRequest{
  179. SocialCode:req.SocialCode,
  180. }
  181. rpcRsp, err := pb.Supplier.ProviderApproveStatus(ctx, rpcReq)
  182. if err != nil {
  183. s, _ := json.MarshalToString(req)
  184. logger.Error("func",
  185. zap.String("call", "ProviderApproveStatus"),
  186. zap.String("params", s),
  187. zap.String("error", err.Error()))
  188. return err
  189. }
  190. resp.Data = *rpcRsp
  191. ctx.JSON(http.StatusOK, resp)
  192. return nil
  193. }
  194. // 执行任务
  195. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  196. }
  197. // token
  198. // @Summary 刷新token
  199. // @Description 刷新token
  200. // @Tags user
  201. // @Accept json
  202. // @Produce json
  203. // @Param token header string true "token"
  204. // @Success 200 {object} v1.TokenResponse
  205. // @Failure 500 {object} base.HTTPError
  206. // @Router /api/v1/token_refresh [put]
  207. func (c *Controller) TokenRefresh(ctx *gin.Context) {
  208. // 解析参数
  209. req := &param_v1.TokenRequest{}
  210. parseParamTask := func() error {
  211. err := util.ShouldBind(ctx, &req.Header, nil, nil, nil)
  212. if err != nil {
  213. logger.Error("func",
  214. zap.String("call", "util.ShouldBind"),
  215. zap.String("error", err.Error()))
  216. return errors.ParamsError
  217. }
  218. return nil
  219. }
  220. // 业务处理
  221. handleServiceTask := func() error {
  222. tokenObj, err := jwtwrapper.ParseToken(req.Token)
  223. if tokenObj == nil {
  224. return errors.TokenFailedError
  225. }
  226. if err != nil {
  227. switch err.(*jwt.ValidationError).Errors {
  228. case jwt.ValidationErrorExpired:
  229. return errors.TokenFailedError
  230. default:
  231. return errors.TokenFailedError
  232. }
  233. }
  234. uid := tokenObj.Id
  235. // 生成token
  236. token, err := jwtwrapper.GenToken(fmt.Sprintf("%s", uid), parser.Conf.Jwt.Issuer, tokenObj.Subject,
  237. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  238. if err != nil {
  239. logger.Error("func",
  240. zap.String("call", "util.GenJwtToken"),
  241. zap.String("args", fmt.Sprintf("%d", uid)),
  242. zap.String("error", err.Error()))
  243. return errors.SystemError
  244. }
  245. resp := param_v1.TokenResponse{}
  246. resp.Data = token
  247. ctx.JSON(http.StatusOK, resp)
  248. return nil
  249. }
  250. // 执行任务
  251. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  252. }
  253. // 公示
  254. // @Summary 公示
  255. // @Description 公示
  256. // @Tags publicity
  257. // @Accept json
  258. // @Produce json
  259. // @Param provider query string false "供应商名称"
  260. // @Param device_code query int false "设备类型"
  261. // @Success 200 {object} v1.PublicityResponse
  262. // @Failure 500 {object} base.HTTPError
  263. // @Router /api/v1/publicity [get]
  264. func (c *Controller) Publicity(ctx *gin.Context) {
  265. // 解析参数
  266. req := &param_v1.PublicityRequest{}
  267. parseParamTask := func() error {
  268. err := util.ShouldBind(ctx, nil, nil, &req.PublicityQuery, nil)
  269. if err != nil {
  270. logger.Error("func",
  271. zap.String("call", "util.ShouldBind"),
  272. zap.String("error", err.Error()))
  273. return errors.ParamsError
  274. }
  275. return nil
  276. }
  277. // 业务处理
  278. handleServiceTask := func() error {
  279. // 响应数据
  280. resp := param_v1.PublicityResponse{}
  281. rpcReq := &v1.PublicityRequest{
  282. DeviceCode:int32(req.DeviceCode),
  283. Provider:req.Provider,
  284. }
  285. rpcRsp, err := pb.Supplier.Publicity(ctx, rpcReq)
  286. if err != nil {
  287. s, _ := json.MarshalToString(req)
  288. logger.Error("func",
  289. zap.String("call", "pb.Supplier.Publicity"),
  290. zap.String("params", s),
  291. zap.String("error", err.Error()))
  292. return err
  293. }
  294. resp.Data.List = make([]param_v1.PublicityItem, len(rpcRsp.List))
  295. for i, v := range rpcRsp.List {
  296. resp.Data.List[i].Name = v.Name
  297. resp.Data.List[i].SocialCode = v.SocialCode
  298. }
  299. ctx.JSON(http.StatusOK, resp)
  300. return nil
  301. }
  302. // 执行任务
  303. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  304. }
  305. // 获取短信验证码
  306. // @Summary 获取短信验证码
  307. // @Description 获取短信验证码
  308. // @Tags vcode
  309. // @Accept json
  310. // @Produce json
  311. // @Param phone query string true "phone"
  312. // @Param verify query bool false "verify"
  313. // @Success 200 {object} v1.VcodeResponse
  314. // @Failure 500 {object} base.HTTPError
  315. // @Router /api/v1/vcode [get]
  316. func (c *Controller) Vcode(ctx *gin.Context) {
  317. // 解析参数
  318. req := &param_v1.VcodeRequest{}
  319. parseParamTask := func() error {
  320. err := util.ShouldBind(ctx, nil, nil, &req.VcodeQuery, nil)
  321. if err != nil {
  322. logger.Error("func",
  323. zap.String("call", "util.ShouldBind"),
  324. zap.String("error", err.Error()))
  325. return errors.ParamsError
  326. }
  327. return nil
  328. }
  329. // 业务处理
  330. handleServiceTask := func() error {
  331. if req.Verify {
  332. mreq := &v1.UserInfoRequest{Phone:req.Phone}
  333. _, err := pb.Supplier.UserInfo(ctx, mreq)
  334. if err != nil {
  335. return err
  336. }
  337. }
  338. // 响应数据
  339. resp := param_v1.VcodeResponse{}
  340. rpcReq := &v1.GetVcodeRequest{
  341. PhoneNumber:req.Phone,
  342. }
  343. rpcRsp, err := pb.Thirdparty.GetVcode(ctx, rpcReq)
  344. if err != nil {
  345. s, _ := json.MarshalToString(req)
  346. logger.Error("func",
  347. zap.String("call", "pb.Thirdparty.GetVcode"),
  348. zap.String("params", s),
  349. zap.String("error", err.Error()))
  350. return err
  351. }
  352. resp.Data = *rpcRsp
  353. ctx.JSON(http.StatusOK, resp)
  354. return nil
  355. }
  356. // 执行任务
  357. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  358. }
  359. /*
  360. // 找回密码校验手机和图片验证码
  361. // @Summary 找回密码校验手机和图片验证码
  362. // @Description 找回密码校验手机和图片验证码
  363. // @Tags user
  364. // @Accept json
  365. // @Produce json
  366. // @Param body body v1.FindPasswdVerifyBody true "登录信息"
  367. // @Success 200 {object} v1.LoginResponse
  368. // @Failure 500 {object} base.HTTPError
  369. // @Router /api/v1/user/find_passwd_verify [post]
  370. func (c *Controller) FindPasswdVerify(ctx *gin.Context) {
  371. // 解析参数
  372. req := &param_v1.FindPasswdVerifyRequest{}
  373. parseParamTask := func() error {
  374. err := util.ShouldBind(ctx, nil, nil, nil, &req.FindPasswdVerifyBody)
  375. if err != nil {
  376. logger.Error("func",
  377. zap.String("call", "util.ShouldBind"),
  378. zap.String("error", err.Error()))
  379. return errors.ParamsError
  380. }
  381. return nil
  382. }
  383. // 业务处理
  384. handleServiceTask := func() error {
  385. if !utils.CaptchaVerify(ctx, req.Verify, req.Captcha) {
  386. return errors.ParamsError
  387. }
  388. // 响应数据
  389. resp := param_v1.FindPasswdVerifyResponse{}
  390. rpcReq := &v1.CheckVcodeRequest{
  391. PhoneNumber:req.Phone,
  392. Vcode:req.Vcode,
  393. }
  394. _, err := pb.Thirdparty.CheckVcode(ctx, rpcReq)
  395. if err != nil {
  396. s, _ := json.MarshalToString(req)
  397. logger.Error("func",
  398. zap.String("call", "pb.Thirdparty.CheckVcode"),
  399. zap.String("params", s),
  400. zap.String("error", err.Error()))
  401. return errors.ErrorTransForm(err)
  402. }
  403. ctx.JSON(http.StatusOK, resp)
  404. return nil
  405. }
  406. // 执行任务
  407. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  408. }
  409. */
  410. // 找回密码设置密码
  411. // @Summary 找回密码设置密码
  412. // @Description 找回密码设置密码
  413. // @Tags user
  414. // @Accept json
  415. // @Produce json
  416. // @Param body body v1.SetPasswdBody true "登录信息"
  417. // @Success 200 {object} v1.SetPasswdResponse
  418. // @Failure 500 {object} base.HTTPError
  419. // @Router /api/v1/user/set_passwd [put]
  420. func (c *Controller) SetPasswd(ctx *gin.Context) {
  421. // 解析参数
  422. req := &param_v1.SetPasswdRequest{}
  423. parseParamTask := func() error {
  424. err := util.ShouldBind(ctx, nil, nil, nil, &req.SetPasswdBody)
  425. if err != nil {
  426. logger.Error("func",
  427. zap.String("call", "util.ShouldBind"),
  428. zap.String("error", err.Error()))
  429. return errors.ParamsError
  430. }
  431. return nil
  432. }
  433. handleVcodeVerifyTask := func() error {
  434. // 响应数据
  435. rpcReq := &v1.CheckVcodeRequest{
  436. PhoneNumber:req.Phone,
  437. Vcode:uint32(req.Vcode),
  438. }
  439. _, err := pb.Thirdparty.CheckVcode(ctx, rpcReq)
  440. if err != nil {
  441. s, _ := json.MarshalToString(req)
  442. logger.Error("func",
  443. zap.String("call", "pb.Thirdparty.CheckVcode"),
  444. zap.String("params", s),
  445. zap.String("error", err.Error()))
  446. return errors.ErrorTransForm(err)
  447. }
  448. return nil
  449. }
  450. // 业务处理
  451. handleServiceTask := func() error {
  452. // 响应数据
  453. resp := param_v1.SetPasswdResponse{}
  454. rpcReq := &v1.SetPasswdRequest {
  455. Passwd:req.Passwd,
  456. Phone:req.Phone,
  457. }
  458. _, err := pb.Supplier.SetPasswd(ctx, rpcReq)
  459. if err != nil {
  460. s, _ := json.MarshalToString(req)
  461. logger.Error("func",
  462. zap.String("call", "pb.Supplier.SetPasswd"),
  463. zap.String("params", s),
  464. zap.String("error", err.Error()))
  465. return errors.ErrorTransForm(err)
  466. }
  467. ctx.JSON(http.StatusOK, resp)
  468. return nil
  469. }
  470. // 执行任务
  471. err := httptasker.Exec(ctx, parseParamTask, handleVcodeVerifyTask, handleServiceTask)
  472. s, _ := json.MarshalToString(req)
  473. utils.LogWrite("找回密码", 0, req.Phone, s, err, 0)
  474. }
  475. // 修改密码
  476. // @Summary 修改密码
  477. // @Description 修改密码
  478. // @Tags user
  479. // @Accept json
  480. // @Produce json
  481. // @Param token header string true " "
  482. // @Param body body v1.ChangePasswdBody true " "
  483. // @Success 200 {object} v1.ChangePasswdResponse
  484. // @Failure 500 {object} base.HTTPError
  485. // @Router /api/v1/user/change_passwd [put]
  486. func (c *Controller) ChangePasswd(ctx *gin.Context) {
  487. // 解析参数
  488. var loginUid int64
  489. var userName string
  490. req := &param_v1.ChangePasswdRequest{}
  491. parseParamTask := func() error {
  492. err := util.ShouldBind(ctx, nil, nil, nil, &req.ChangePasswdBody)
  493. if err != nil {
  494. logger.Error("func",
  495. zap.String("call", "util.ShouldBind"),
  496. zap.String("error", err.Error()))
  497. return errors.ParamsError
  498. }
  499. userName, loginUid, err = utils.GetTokenInfo(ctx)
  500. return nil
  501. }
  502. // 业务处理
  503. handleServiceTask := func() error {
  504. // 响应数据
  505. _, loginUid,_ := utils.GetTokenInfo(ctx)
  506. resp := param_v1.ChangePasswdResponse{}
  507. rpcReq := &v1.ChangePasswdRequest {
  508. Old:req.Old,
  509. New:req.New,
  510. Uid:loginUid,
  511. }
  512. _, err := pb.Supplier.ChangePasswd(ctx, rpcReq)
  513. if err != nil {
  514. s, _ := json.MarshalToString(req)
  515. logger.Error("func",
  516. zap.String("call", "pb.Supplier.ChangePasswd"),
  517. zap.String("params", s),
  518. zap.String("error", err.Error()))
  519. return errors.ErrorTransForm(err)
  520. }
  521. ctx.JSON(http.StatusOK, resp)
  522. return nil
  523. }
  524. // 执行任务
  525. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  526. req.New = "****"
  527. req.Old = "****"
  528. s, _ := json.MarshalToString(req)
  529. utils.LogWrite("修改密码", loginUid, userName, s, err, 0)
  530. }
  531. // 操作日志列表
  532. // @Summary 操作日志列表
  533. // @Description 操作日志列表
  534. // @Tags log
  535. // @Accept json
  536. // @Produce json
  537. // @Param token header string true " "
  538. // @Param page query string true " "
  539. // @Param start query int64 false " "
  540. // @Param end query int64 false " "
  541. // @Success 200 {object} v1.LogListResponse
  542. // @Failure 500 {object} base.HTTPError
  543. // @Router /api/v1/log/list [get]
  544. func (c *Controller) LogList(ctx *gin.Context) {
  545. // 解析参数
  546. req := &param_v1.LogListRequest{}
  547. parseParamTask := func() error {
  548. err := util.ShouldBind(ctx, nil, nil, &req.LogListQuery, 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. // 响应数据
  560. if req.Page < 1 {
  561. req.Page = 1
  562. }
  563. resp := param_v1.LogListResponse{}
  564. rpcReq := &v1.LogListRequest {
  565. Type:consts.LogTypeProvider,
  566. Page:req.Page,
  567. Start:req.Start,
  568. End:req.End,
  569. }
  570. reply, err := pb.OperationLog.LogList(ctx, rpcReq)
  571. if err != nil {
  572. s, _ := json.MarshalToString(req)
  573. logger.Error("func",
  574. zap.String("call", "pb.OperationLog.LogList"),
  575. zap.String("params", s),
  576. zap.String("error", err.Error()))
  577. return errors.ErrorTransForm(err)
  578. }
  579. resp.Data = *reply
  580. if resp.Data.List == nil {
  581. resp.Data.List = make([]*v1.LogInfo, 0)
  582. }
  583. ctx.JSON(http.StatusOK, resp)
  584. return nil
  585. }
  586. // 执行任务
  587. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  588. }
  589. // 获取图片验证码id
  590. // @Summary 获取图片验证码id
  591. // @Description 获取图片验证码id
  592. // @Tags user
  593. // @Accept json
  594. // @Produce json
  595. // @Success 200 {object} v1.CaptchaIdResponse
  596. // @Failure 500 {object} base.HTTPError
  597. // @Router /api/v1/user/captcha_id [get]
  598. func (c *Controller) CaptchaId(ctx *gin.Context) {
  599. // 解析参数
  600. parseParamTask := func() error {
  601. return nil
  602. }
  603. // 业务处理
  604. handleServiceTask := func() error {
  605. // 响应数据
  606. resp := param_v1.CaptchaIdResponse{}
  607. id := utils.Captcha(ctx, 4)
  608. resp.Data.CaptureId= id
  609. ctx.JSON(http.StatusOK, resp)
  610. return nil
  611. }
  612. // 执行任务
  613. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  614. }
  615. // 获取图片验证码图片
  616. // @Summary 获取图片验证码图片
  617. // @Description 获取图片验证码图片
  618. // @Tags user
  619. // @Accept json
  620. // @Produce json
  621. // @Param captcha_id path string true " "
  622. // @Failure 500 {object} base.HTTPError
  623. // @Router /api/v1/user/captcha_png/{captcha_id} [get]
  624. func (c *Controller) CaptchaPng(ctx *gin.Context) {
  625. // 解析参数
  626. req := &param_v1.CaptchaPngRequest{}
  627. parseParamTask := func() error {
  628. err := util.ShouldBind(ctx, nil, &req.CaptchaPngPath, nil, nil)
  629. if err != nil {
  630. logger.Error("func",
  631. zap.String("call", "util.ShouldBind"),
  632. zap.String("error", err.Error()))
  633. return errors.ParamsError
  634. }
  635. return nil
  636. }
  637. // 业务处理
  638. handleServiceTask := func() error {
  639. // 响应数据
  640. utils.Serve(ctx.Writer, ctx.Request, req.CaptchaId, ".png", "zh", false, utils.CaptchaWidth, utils.CaptchaHight, "")
  641. return nil
  642. }
  643. // 执行任务
  644. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  645. }
  646. //
  647. // @Summary 发送验证邮件
  648. // @Description 发送验证邮件
  649. // @Tags user
  650. // @Accept json
  651. // @Produce json
  652. // @Param token header string true " "
  653. // @Param body body v1.MailPrepareBody true " "
  654. // @Success 200 {object} v1.MailPrepareResponse
  655. // @Failure 500 {object} base.HTTPError
  656. // @Router /api/v1/user/verify_mail [put]
  657. func (c *Controller) MailPrepare(ctx *gin.Context) {
  658. // 解析参数
  659. var loginUid int64
  660. var userName string
  661. req := &param_v1.MailPrepareRequest{}
  662. parseParamTask := func() error {
  663. err := util.ShouldBind(ctx, nil, nil, nil, &req.MailPrepareBody)
  664. if err != nil {
  665. logger.Error("func",
  666. zap.String("call", "util.ShouldBind"),
  667. zap.String("error", err.Error()))
  668. return errors.ParamsError
  669. }
  670. if !utils.VerifyEmailFormat(req.Email) {
  671. return status.Error(10003, "邮箱格式错误")
  672. }
  673. userName, loginUid, err = utils.GetTokenInfo(ctx)
  674. if err != nil {
  675. return err
  676. }
  677. return nil
  678. }
  679. // 业务处理
  680. handleServiceTask := func() error {
  681. // 响应数据
  682. rpcReq := &v1.EmailPrepareRequest {
  683. Id:loginUid,
  684. Password:req.Password,
  685. Email:req.Email,
  686. }
  687. _, err := pb.Supplier.EmailPrepare(ctx, rpcReq)
  688. if err != nil {
  689. s, _ := json.MarshalToString(req)
  690. logger.Error("func",
  691. zap.String("call", "pb.Supplier.MailPrepare"),
  692. zap.String("params", s),
  693. zap.String("error", err.Error()))
  694. return errors.ErrorTransForm(err)
  695. }
  696. //ctx.JSON(http.StatusOK, resp)
  697. return nil
  698. }
  699. sendMailTask := func() error {
  700. subject := map[string]interface{}{
  701. "email": req.Email,
  702. "project_id": 0,
  703. "user_name":userName,
  704. }
  705. str, _ := json.MarshalToString(subject)
  706. // 生成token
  707. token, err := jwtwrapper.GenToken(fmt.Sprintf("%d", loginUid), parser.Conf.Jwt.Issuer, str,
  708. time.Duration(180)*time.Second)
  709. resp := param_v1.MailPrepareResponse{}
  710. rpcReq := &v1.SendEmailRequest {
  711. Email:req.Email,
  712. Content:fmt.Sprintf("浏览器中打开以下链接验证邮箱:\n%s?token=%s", parser.Conf.SupplierMailCheckUrl, token),
  713. }
  714. _, err = pb.Thirdparty.SendEmail(ctx, rpcReq)
  715. if err != nil {
  716. s, _ := json.MarshalToString(req)
  717. logger.Error("func",
  718. zap.String("call", "pb.Thirdparty.SendEmail"),
  719. zap.String("params", s),
  720. zap.String("error", err.Error()))
  721. return errors.ErrorTransForm(err)
  722. }
  723. ctx.JSON(http.StatusOK, resp)
  724. return nil
  725. }
  726. // 执行任务
  727. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask, sendMailTask)
  728. s, _ := json.MarshalToString(req)
  729. utils.LogWrite("发送验证邮件", loginUid, userName, s, err, 0)
  730. }
  731. //
  732. // @Summary 修改邮箱
  733. // @Description 修改邮箱
  734. // @Tags user
  735. // @Accept json
  736. // @Produce json
  737. // @Param token query string true " "
  738. // @Success 200 {object} v1.MailUpdateResponse
  739. // @Failure 500 {object} base.HTTPError
  740. // @Router /api/v1/user/mail [get]
  741. func (c *Controller) MailUpdate(ctx *gin.Context) {
  742. // 解析参数
  743. var loginUid int64
  744. var userName string
  745. //var projectId int64
  746. var email string
  747. req := &param_v1.MailUpdateRequest{}
  748. parseParamTask := func() error {
  749. err := util.ShouldBind(ctx, nil, nil, &req.MailUpdateQuery, nil)
  750. if err != nil {
  751. logger.Error("func",
  752. zap.String("call", "util.ShouldBind"),
  753. zap.String("error", err.Error()))
  754. return errors.ParamsError
  755. }
  756. loginUid, _, userName, email, err = utils.EmailTokenVeriy(req.Token)
  757. if err != nil {
  758. return err
  759. }
  760. return nil
  761. }
  762. // 业务处理
  763. var pid int64
  764. handleServiceTask := func() error {
  765. resp := param_v1.MailUpdateResponse{
  766. }
  767. rpcReq := &v1.EmailUpdateRequest {
  768. Id:loginUid,
  769. Email:email,
  770. }
  771. _, err := pb.Supplier.EmailUpdate(ctx, rpcReq)
  772. if err != nil {
  773. s, _ := json.MarshalToString(req)
  774. logger.Error("func",
  775. zap.String("call", "pb.Supplier.EmailUpdate"),
  776. zap.String("params", s),
  777. zap.String("error", err.Error()))
  778. return errors.ErrorTransForm(err)
  779. }
  780. resp.Message = "邮箱设置成功"
  781. ctx.JSON(http.StatusOK, resp)
  782. return nil
  783. }
  784. // 执行任务
  785. err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  786. utils.LogWrite("修改邮箱", loginUid, userName, email, err, pid)
  787. }
  788. //
  789. // @Summary 修改电话号码
  790. // @Description 修改电话号码
  791. // @Tags user
  792. // @Accept json
  793. // @Produce json
  794. // @Param token header string true " "
  795. // @Param body body v1.PhoneUpdateBody true " "
  796. // @Success 200 {object} v1.PhoneUpdateResponse
  797. // @Failure 500 {object} base.HTTPError
  798. // @Router /api/v1/user/phone [put]
  799. func (c *Controller) PhoneUpdate(ctx *gin.Context) {
  800. // 解析参数
  801. var loginUid int64
  802. var userName string
  803. req := &param_v1.PhoneUpdateRequest{}
  804. parseParamTask := func() error {
  805. err := util.ShouldBind(ctx, nil, nil, nil, &req.PhoneUpdateBody)
  806. if err != nil {
  807. logger.Error("func",
  808. zap.String("call", "util.ShouldBind"),
  809. zap.String("error", err.Error()))
  810. return errors.ParamsError
  811. }
  812. userName, loginUid, err = utils.GetTokenInfo(ctx)
  813. if err != nil {
  814. return err
  815. }
  816. return nil
  817. }
  818. var pid int64
  819. vcodeCheckTask := func() error {
  820. rpcReq := &v1.CheckVcodeRequest {
  821. Vcode:req.Vcode,
  822. PhoneNumber:req.Phone,
  823. }
  824. _, err := pb.Thirdparty.CheckVcode(ctx, rpcReq)
  825. if err != nil {
  826. s, _ := json.MarshalToString(req)
  827. logger.Error("func",
  828. zap.String("call", "pb.Thirdparty.CheckVcode"),
  829. zap.String("params", s),
  830. zap.String("error", err.Error()))
  831. return errors.ErrorTransForm(err)
  832. }
  833. return nil
  834. }
  835. // 业务处理
  836. handleServiceTask := func() error {
  837. resp := &param_v1.PhoneUpdateResponse{}
  838. rpcReq := &v1.PhoneUpdateRequest {
  839. Id:loginUid,
  840. Phone:req.Phone,
  841. }
  842. _, err := pb.Supplier.PhoneUpdate(ctx, rpcReq)
  843. if err != nil {
  844. s, _ := json.MarshalToString(req)
  845. logger.Error("func",
  846. zap.String("call", "pb.Supplier.PhoneUpdate"),
  847. zap.String("params", s),
  848. zap.String("error", err.Error()))
  849. return errors.ErrorTransForm(err)
  850. }
  851. ctx.JSON(http.StatusOK, resp)
  852. return nil
  853. }
  854. // 执行任务
  855. err := httptasker.Exec(ctx, parseParamTask, vcodeCheckTask, handleServiceTask)
  856. s, _ := json.MarshalToString(req)
  857. utils.LogWrite("修改电话号码", loginUid, userName, s, err, pid)
  858. }
  859. //
  860. // @Summary 获取电话和邮箱
  861. // @Description 获取电话和邮箱
  862. // @Tags 用户
  863. // @Accept json
  864. // @Produce json
  865. // @Param token header string true " "
  866. // @Success 200 {object} v1.UserInfoResponse
  867. // @Failure 500 {object} base.HTTPError
  868. // @Router /api/v1/user/info [get]
  869. func (c *Controller) UserInfo(ctx *gin.Context) {
  870. // 解析参数
  871. req := &param_v1.UserInfoRequest{}
  872. var loginUid int64
  873. //var userName string
  874. parseParamTask := func() error {
  875. err := util.ShouldBind(ctx, nil, nil, nil, nil)
  876. if err != nil {
  877. logger.Error("func",
  878. zap.String("call", "util.ShouldBind"),
  879. zap.String("error", err.Error()))
  880. return errors.ParamsError
  881. }
  882. _, loginUid, err = utils.GetTokenInfo(ctx)
  883. if err != nil {
  884. return err
  885. }
  886. return nil
  887. }
  888. // 业务处理
  889. handleServiceTask := func() error {
  890. // 响应数据
  891. resp := param_v1.UserInfoResponse{}
  892. rpcReq := &v1.UserInfoRequest{
  893. Id:loginUid,
  894. }
  895. rpcRsp, err := pb.Supplier.UserInfo(ctx, rpcReq)
  896. if err != nil {
  897. s, _ := json.MarshalToString(req)
  898. logger.Error("func",
  899. zap.String("call", "pb.Supplier.UserInfo"),
  900. zap.String("params", s),
  901. zap.String("error", err.Error()))
  902. return errors.ErrorTransForm(err)
  903. }
  904. resp.Data = *rpcRsp
  905. ctx.JSON(http.StatusOK, resp)
  906. return nil
  907. }
  908. // 执行任务
  909. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  910. }
  911. //
  912. // @Summary 供应商基本信息
  913. // @Description 供应商基本信息
  914. // @Tags 用户
  915. // @Accept json
  916. // @Produce json
  917. // @Param token header string true " "
  918. // @Success 200 {object} v1.ProviderInfoResponse
  919. // @Failure 500 {object} base.HTTPError
  920. // @Router /api/v1/user/provider_info [get]
  921. func (c *Controller) ProviderInfo(ctx *gin.Context) {
  922. // 解析参数
  923. req := &param_v1.ProviderInfoRequest{}
  924. var loginUid int64
  925. //var userName string
  926. parseParamTask := func() error {
  927. err := util.ShouldBind(ctx, nil, nil, nil, nil)
  928. if err != nil {
  929. logger.Error("func",
  930. zap.String("call", "util.ShouldBind"),
  931. zap.String("error", err.Error()))
  932. return errors.ParamsError
  933. }
  934. _, loginUid, err = utils.GetTokenInfo(ctx)
  935. if err != nil {
  936. return err
  937. }
  938. return nil
  939. }
  940. // 业务处理
  941. handleServiceTask := func() error {
  942. // 响应数据
  943. resp := param_v1.ProviderInfoResponse{}
  944. rpcReq := &v1.ProviderInfoRequest{
  945. Id:loginUid,
  946. }
  947. rpcRsp, err := pb.Supplier.ProviderInfo(ctx, rpcReq)
  948. if err != nil {
  949. s, _ := json.MarshalToString(req)
  950. logger.Error("func",
  951. zap.String("call", "pb.Supplier.ProviderInfo"),
  952. zap.String("params", s),
  953. zap.String("error", err.Error()))
  954. return errors.ErrorTransForm(err)
  955. }
  956. resp.Data = *rpcRsp
  957. ctx.JSON(http.StatusOK, resp)
  958. return nil
  959. }
  960. // 执行任务
  961. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  962. }