1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package v1
- import (
- "github.com/dgrijalva/jwt-go"
- "github.com/jaryhe/gopkgs/logger"
- "github.com/jaryhe/gopkgs/tasker/httptasker"
- "github.com/jaryhe/gopkgs/util"
- "google.golang.org/grpc/status"
- "net/http"
- "smart-supplier-management-gateway/consts"
- "smart-supplier-management-gateway/errors"
- param_v1 "smart-supplier-management-gateway/param/v1"
- "smart-supplier-management-gateway/pb"
- "smart-supplier-management-gateway/pb/v1"
- "strconv"
- "time"
- "fmt"
- "github.com/gin-gonic/gin"
- "go.uber.org/zap"
- "smart-supplier-management-gateway/utils"
- "github.com/jaryhe/gopkgs/jwtwrapper"
- "smart-supplier-management-gateway/parser"
- )
- // 注册
- // @Summary 注册
- // @Description 注册
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param body body v1.RegisterBody true "登录信息"
- // @Success 200 {object} v1.RegisterResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/register [post]
- func (c *Controller) Register(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.RegisterRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, &req.RegisterBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- vcode, _ := strconv.Atoi(req.Verify)
- resp := param_v1.RegisterResponse{}
- rpcReq := &v1.RegisterRequest{
- }
- utils.StructCopy(rpcReq, &req.RegisterBody, "")
- rpcReq.Vcode = uint32(vcode)
- rpcRsp, err := pb.Supplier.Register(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "Register"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return err
- }
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // 登录
- // @Summary 登录
- // @Description 登录
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param body body v1.LoginBody true "登录信息"
- // @Success 200 {object} v1.LoginResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/login [post]
- func (c *Controller) Login(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.LoginRequest{}
- loginUid := int64(0)
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, &req.LoginBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- if !utils.CaptchaVerify(ctx, req.Verify, req.Captcha) {
- return errors.VerifyError
- }
- // 响应数据
- resp := param_v1.LoginResponse{}
- rpcReq := &v1.LoginRequest{
- User:req.User,
- Password:req.Password,
- }
- rpcRsp, err := pb.Supplier.Login(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "Login"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- loginUid = rpcRsp.Uid
- subJect := map[string]interface{}{
- "user_name":req.User,
- }
- str,_ := json.MarshalToString(subJect)
- // 生成token
- token, err := jwtwrapper.GenToken(fmt.Sprintf("%d", rpcRsp.Uid), parser.Conf.Jwt.Issuer, str,
- time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.GenJwtToken"),
- zap.String("args", fmt.Sprintf("%d", rpcRsp.Uid)),
- zap.String("error", err.Error()))
- return errors.SystemError
- }
- refreshToken, err := jwtwrapper.GenToken(fmt.Sprintf("%d", rpcRsp.Uid), parser.Conf.Jwt.Issuer, str,
- time.Duration(7*24*60*60)*time.Second)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.GenJwtToken"),
- zap.String("args", fmt.Sprintf("%d", rpcRsp.Uid)),
- zap.String("error", err.Error()))
- return errors.SystemError
- }
- resp.Data.Uid = rpcRsp.Uid
- resp.Data.Token = token
- resp.Data.RefreshToken = refreshToken
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- utils.LogWrite("登录", loginUid, req.User, "", err, 0)
- }
- // 根据社会码获取注册状态
- // @Summary 根据社会码获取注册状态
- // @Description 根据社会码获取注册状态
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param social_code query string true " "
- // @Success 200 {object} v1.ProviderApproveStatusResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/status [get]
- func (c *Controller) ProviderApproveStatus(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.ProviderApproveStatusRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, &req.ProviderApproveStatusQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.ProviderApproveStatusResponse{}
- rpcReq := &v1.ProviderApproveStatusRequest{
- SocialCode:req.SocialCode,
- }
- rpcRsp, err := pb.Supplier.ProviderApproveStatus(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "ProviderApproveStatus"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return err
- }
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // token
- // @Summary 刷新token
- // @Description 刷新token
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Success 200 {object} v1.TokenResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/token_refresh [put]
- func (c *Controller) TokenRefresh(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.TokenRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- tokenObj, err := jwtwrapper.ParseToken(req.Token)
- if tokenObj == nil {
- return errors.TokenFailedError
- }
- if err != nil {
- switch err.(*jwt.ValidationError).Errors {
- case jwt.ValidationErrorExpired:
- return errors.TokenFailedError
- default:
- return errors.TokenFailedError
- }
- }
- uid := tokenObj.Id
- // 生成token
- token, err := jwtwrapper.GenToken(fmt.Sprintf("%s", uid), parser.Conf.Jwt.Issuer, tokenObj.Subject,
- time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.GenJwtToken"),
- zap.String("args", fmt.Sprintf("%d", uid)),
- zap.String("error", err.Error()))
- return errors.SystemError
- }
- resp := param_v1.TokenResponse{}
- resp.Data = token
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // 公示
- // @Summary 公示
- // @Description 公示
- // @Tags publicity
- // @Accept json
- // @Produce json
- // @Param provider query string false "供应商名称"
- // @Param device_code query int false "设备类型"
- // @Success 200 {object} v1.PublicityResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/publicity [get]
- func (c *Controller) Publicity(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.PublicityRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, &req.PublicityQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.PublicityResponse{}
- rpcReq := &v1.PublicityRequest{
- DeviceCode:int32(req.DeviceCode),
- Provider:req.Provider,
- }
- rpcRsp, err := pb.Supplier.Publicity(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Supplier.Publicity"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return err
- }
- resp.Data.List = make([]param_v1.PublicityItem, len(rpcRsp.List))
- for i, v := range rpcRsp.List {
- resp.Data.List[i].Name = v.Name
- resp.Data.List[i].SocialCode = v.SocialCode
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // 获取短信验证码
- // @Summary 获取短信验证码
- // @Description 获取短信验证码
- // @Tags vcode
- // @Accept json
- // @Produce json
- // @Param phone query string true "phone"
- // @Param verify query bool false "verify"
- // @Success 200 {object} v1.VcodeResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/vcode [get]
- func (c *Controller) Vcode(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.VcodeRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, &req.VcodeQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- if req.Verify {
- mreq := &v1.UserInfoRequest{Phone:req.Phone}
- _, err := pb.Supplier.UserInfo(ctx, mreq)
- if err != nil {
- return err
- }
- }
- // 响应数据
- resp := param_v1.VcodeResponse{}
- rpcReq := &v1.GetVcodeRequest{
- PhoneNumber:req.Phone,
- }
- rpcRsp, err := pb.Thirdparty.GetVcode(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Thirdparty.GetVcode"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return err
- }
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- /*
- // 找回密码校验手机和图片验证码
- // @Summary 找回密码校验手机和图片验证码
- // @Description 找回密码校验手机和图片验证码
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param body body v1.FindPasswdVerifyBody true "登录信息"
- // @Success 200 {object} v1.LoginResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/find_passwd_verify [post]
- func (c *Controller) FindPasswdVerify(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.FindPasswdVerifyRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, &req.FindPasswdVerifyBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- if !utils.CaptchaVerify(ctx, req.Verify, req.Captcha) {
- return errors.ParamsError
- }
- // 响应数据
- resp := param_v1.FindPasswdVerifyResponse{}
- rpcReq := &v1.CheckVcodeRequest{
- PhoneNumber:req.Phone,
- Vcode:req.Vcode,
- }
- _, err := pb.Thirdparty.CheckVcode(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Thirdparty.CheckVcode"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- */
- // 找回密码设置密码
- // @Summary 找回密码设置密码
- // @Description 找回密码设置密码
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param body body v1.SetPasswdBody true "登录信息"
- // @Success 200 {object} v1.SetPasswdResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/set_passwd [put]
- func (c *Controller) SetPasswd(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.SetPasswdRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, &req.SetPasswdBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- handleVcodeVerifyTask := func() error {
- // 响应数据
- rpcReq := &v1.CheckVcodeRequest{
- PhoneNumber:req.Phone,
- Vcode:uint32(req.Vcode),
- }
- _, err := pb.Thirdparty.CheckVcode(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Thirdparty.CheckVcode"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.SetPasswdResponse{}
- rpcReq := &v1.SetPasswdRequest {
- Passwd:req.Passwd,
- Phone:req.Phone,
- }
- _, err := pb.Supplier.SetPasswd(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Supplier.SetPasswd"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- err := httptasker.Exec(ctx, parseParamTask, handleVcodeVerifyTask, handleServiceTask)
- s, _ := json.MarshalToString(req)
- utils.LogWrite("找回密码", 0, req.Phone, s, err, 0)
- }
- // 修改密码
- // @Summary 修改密码
- // @Description 修改密码
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param body body v1.ChangePasswdBody true " "
- // @Success 200 {object} v1.ChangePasswdResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/change_passwd [put]
- func (c *Controller) ChangePasswd(ctx *gin.Context) {
- // 解析参数
- var loginUid int64
- var userName string
- req := ¶m_v1.ChangePasswdRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, &req.ChangePasswdBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- userName, loginUid, err = utils.GetTokenInfo(ctx)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- _, loginUid,_ := utils.GetTokenInfo(ctx)
- resp := param_v1.ChangePasswdResponse{}
- rpcReq := &v1.ChangePasswdRequest {
- Old:req.Old,
- New:req.New,
- Uid:loginUid,
- }
- _, err := pb.Supplier.ChangePasswd(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Supplier.ChangePasswd"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- req.New = "****"
- req.Old = "****"
- s, _ := json.MarshalToString(req)
- utils.LogWrite("修改密码", loginUid, userName, s, err, 0)
- }
- // 操作日志列表
- // @Summary 操作日志列表
- // @Description 操作日志列表
- // @Tags log
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param page query string true " "
- // @Param start query int64 false " "
- // @Param end query int64 false " "
- // @Success 200 {object} v1.LogListResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/log/list [get]
- func (c *Controller) LogList(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.LogListRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, &req.LogListQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- if req.Page < 1 {
- req.Page = 1
- }
- resp := param_v1.LogListResponse{}
- rpcReq := &v1.LogListRequest {
- Type:consts.LogTypeProvider,
- Page:req.Page,
- Start:req.Start,
- End:req.End,
- }
- reply, err := pb.OperationLog.LogList(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.OperationLog.LogList"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- resp.Data = *reply
- if resp.Data.List == nil {
- resp.Data.List = make([]*v1.LogInfo, 0)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // 获取图片验证码id
- // @Summary 获取图片验证码id
- // @Description 获取图片验证码id
- // @Tags user
- // @Accept json
- // @Produce json
- // @Success 200 {object} v1.CaptchaIdResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/captcha_id [get]
- func (c *Controller) CaptchaId(ctx *gin.Context) {
- // 解析参数
- parseParamTask := func() error {
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.CaptchaIdResponse{}
- id := utils.Captcha(ctx, 4)
- resp.Data.CaptureId= id
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // 获取图片验证码图片
- // @Summary 获取图片验证码图片
- // @Description 获取图片验证码图片
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param captcha_id path string true " "
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/captcha_png/{captcha_id} [get]
- func (c *Controller) CaptchaPng(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.CaptchaPngRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, &req.CaptchaPngPath, nil, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- utils.Serve(ctx.Writer, ctx.Request, req.CaptchaId, ".png", "zh", false, utils.CaptchaWidth, utils.CaptchaHight, "")
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- //
- // @Summary 发送验证邮件
- // @Description 发送验证邮件
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param body body v1.MailPrepareBody true " "
- // @Success 200 {object} v1.MailPrepareResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/verify_mail [put]
- func (c *Controller) MailPrepare(ctx *gin.Context) {
- // 解析参数
- var loginUid int64
- var userName string
- req := ¶m_v1.MailPrepareRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, &req.MailPrepareBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- if !utils.VerifyEmailFormat(req.Email) {
- return status.Error(10003, "邮箱格式错误")
- }
- userName, loginUid, err = utils.GetTokenInfo(ctx)
- if err != nil {
- return err
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- rpcReq := &v1.EmailPrepareRequest {
- Id:loginUid,
- Password:req.Password,
- Email:req.Email,
- }
- _, err := pb.Supplier.EmailPrepare(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Supplier.MailPrepare"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- //ctx.JSON(http.StatusOK, resp)
- return nil
- }
- sendMailTask := func() error {
- subject := map[string]interface{}{
- "email": req.Email,
- "project_id": 0,
- "user_name":userName,
- }
- str, _ := json.MarshalToString(subject)
- // 生成token
- token, err := jwtwrapper.GenToken(fmt.Sprintf("%d", loginUid), parser.Conf.Jwt.Issuer, str,
- time.Duration(180)*time.Second)
- resp := param_v1.MailPrepareResponse{}
- rpcReq := &v1.SendEmailRequest {
- Email:req.Email,
- Content:fmt.Sprintf("浏览器中打开以下链接验证邮箱:\n%s?token=%s", parser.Conf.SupplierMailCheckUrl, token),
- }
- _, err = pb.Thirdparty.SendEmail(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Thirdparty.SendEmail"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- err := httptasker.Exec(ctx, parseParamTask, handleServiceTask, sendMailTask)
- s, _ := json.MarshalToString(req)
- utils.LogWrite("发送验证邮件", loginUid, userName, s, err, 0)
- }
- //
- // @Summary 修改邮箱
- // @Description 修改邮箱
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param token query string true " "
- // @Success 200 {object} v1.MailUpdateResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/mail [get]
- func (c *Controller) MailUpdate(ctx *gin.Context) {
- // 解析参数
- var loginUid int64
- var userName string
- //var projectId int64
- var email string
- req := ¶m_v1.MailUpdateRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, &req.MailUpdateQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- loginUid, _, userName, email, err = utils.EmailTokenVeriy(req.Token)
- if err != nil {
- return err
- }
- return nil
- }
- // 业务处理
- var pid int64
- handleServiceTask := func() error {
- resp := param_v1.MailUpdateResponse{
- }
- rpcReq := &v1.EmailUpdateRequest {
- Id:loginUid,
- Email:email,
- }
- _, err := pb.Supplier.EmailUpdate(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Supplier.EmailUpdate"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- resp.Message = "邮箱设置成功"
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- utils.LogWrite("修改邮箱", loginUid, userName, email, err, pid)
- }
- //
- // @Summary 修改电话号码
- // @Description 修改电话号码
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param body body v1.PhoneUpdateBody true " "
- // @Success 200 {object} v1.PhoneUpdateResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/phone [put]
- func (c *Controller) PhoneUpdate(ctx *gin.Context) {
- // 解析参数
- var loginUid int64
- var userName string
- req := ¶m_v1.PhoneUpdateRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, &req.PhoneUpdateBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- userName, loginUid, err = utils.GetTokenInfo(ctx)
- if err != nil {
- return err
- }
- return nil
- }
- var pid int64
- vcodeCheckTask := func() error {
- rpcReq := &v1.CheckVcodeRequest {
- Vcode:req.Vcode,
- PhoneNumber:req.Phone,
- }
- _, err := pb.Thirdparty.CheckVcode(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Thirdparty.CheckVcode"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- resp := ¶m_v1.PhoneUpdateResponse{}
- rpcReq := &v1.PhoneUpdateRequest {
- Id:loginUid,
- Phone:req.Phone,
- }
- _, err := pb.Supplier.PhoneUpdate(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Supplier.PhoneUpdate"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- err := httptasker.Exec(ctx, parseParamTask, vcodeCheckTask, handleServiceTask)
- s, _ := json.MarshalToString(req)
- utils.LogWrite("修改电话号码", loginUid, userName, s, err, pid)
- }
- //
- // @Summary 获取电话和邮箱
- // @Description 获取电话和邮箱
- // @Tags 用户
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Success 200 {object} v1.UserInfoResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/info [get]
- func (c *Controller) UserInfo(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.UserInfoRequest{}
- var loginUid int64
- //var userName string
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- _, loginUid, err = utils.GetTokenInfo(ctx)
- if err != nil {
- return err
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.UserInfoResponse{}
- rpcReq := &v1.UserInfoRequest{
- Id:loginUid,
- }
- rpcRsp, err := pb.Supplier.UserInfo(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Supplier.UserInfo"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- //
- // @Summary 供应商基本信息
- // @Description 供应商基本信息
- // @Tags 用户
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Success 200 {object} v1.ProviderInfoResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/provider_info [get]
- func (c *Controller) ProviderInfo(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.ProviderInfoRequest{}
- var loginUid int64
- //var userName string
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, nil, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- _, loginUid, err = utils.GetTokenInfo(ctx)
- if err != nil {
- return err
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.ProviderInfoResponse{}
- rpcReq := &v1.ProviderInfoRequest{
- Id:loginUid,
- }
- rpcRsp, err := pb.Supplier.ProviderInfo(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Supplier.ProviderInfo"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
|