user.go 29 KB

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