12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160 |
- // 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-enterprise-management-gateway/consts"
- "smart-enterprise-management-gateway/errors"
- param_v1 "smart-enterprise-management-gateway/param/v1"
- "smart-enterprise-management-gateway/pb"
- "smart-enterprise-management-gateway/pb/v1"
- "strconv"
- "time"
- "fmt"
- "github.com/gin-gonic/gin"
- "go.uber.org/zap"
- "smart-enterprise-management-gateway/utils"
- "github.com/jaryhe/gopkgs/jwtwrapper"
- "smart-enterprise-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.Enterprise.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 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.Enterprise.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 user
- // @Accept json
- // @Produce json
- // @Param social_code query string true " "
- // @Success 200 {object} v1.CompanyApproveStatusResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/status [get]
- func (c *Controller) CompanyApproveStatus(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.CompanyApproveStatusRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, nil, nil, &req.CompanyApproveStatusQuery, 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.CompanyApproveStatusResponse{}
- rpcReq := &v1.CompanyApproveStatusRequest{
- SocialCode:req.SocialCode,
- }
- rpcRsp, err := pb.Enterprise.CompanyApproveStatus(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "CompanyApproveStatus"),
- 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{}
- 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.Enterprise.Login(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Enterprise.Login"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- 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
- }
- // 执行任务
- 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 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.Enterprise.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
- }
- // 业务处理
- handleVcodeTask := func() error {
- // 响应数据
- 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)
- }
- return nil
- }
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.SetPasswdResponse{}
- rpcReq := &v1.SetPasswdRequest {
- Passwd:req.Passwd,
- Phone:req.Phone,
- }
- _, err := pb.Enterprise.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
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleVcodeTask, handleServiceTask)
- }
- // 修改密码
- // @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
- }
- loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- loginUid, _, _ := utils.GetJwtIdFromCtx(ctx)
- resp := param_v1.ChangePasswdResponse{}
- rpcReq := &v1.ChangePasswdRequest {
- Old:req.Old,
- New:req.New,
- Uid:loginUid,
- }
- _, err := pb.Enterprise.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) {
- // 解析参数
- //var loginUid int64
- //var userName string
- 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
- }
- //loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- if req.Page < 1 {
- req.Page = 1
- }
- resp := param_v1.LogListResponse{}
- rpcReq := &v1.LogListRequest {
- Type:consts.LogTypeCompany,
- 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 前台
- // @Accept json
- // @Produce json
- // @Success 200 {object} v1.MapResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/map [get]
- func (c *Controller) Map(ctx *gin.Context) {
- // 业务处理
- handleServiceTask := func() error {
- /*
- if !utils.CaptchaVerify(ctx, req.Verify, req.Captcha) {
- //return errors.ParamsError
- }*/
- // 响应数据
- resp := param_v1.MapResponse{}
- resp.Data.Url = parser.Conf.MapUrl
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, 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, "邮箱格式错误")
- }
- loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- loginUid, _, _ := utils.GetJwtIdFromCtx(ctx)
- rpcReq := &v1.EmailPrepareRequest {
- Id:loginUid,
- Password:req.Password,
- Email:req.Email,
- }
- _, err := pb.Enterprise.EmailPrepare(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Enterprise.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.EnterpriseMailCheckUrl, 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.Enterprise.EmailUpdate(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Enterprise.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
- }
- loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
- 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.Enterprise.PhoneUpdate(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Enterprise.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.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.UserInfoResponse{}
- rpcReq := &v1.UserInfoRequest{
- Id:loginUid,
- }
- rpcRsp, err := pb.Enterprise.UserInfo(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Enterprise.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)
- }
- // 获取临时token
- // @Summary 获取临时token
- // @Description 获取临时token
- // @Tags user
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param project_id query string true "项目id"
- // @Success 200 {object} v1.TmpTokenResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/tmp_token [get]
- func (c *Controller) TmpToken(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.TmpTokenRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, &req.TmpTokenQuery, 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.TmpTokenResponse{}
- subject := map[string]interface{}{
- "tmp_token":true,
- "project_id":req.ProjectId,
- }
- subjectStr, _ := json.MarshalToString(subject)
- // 生成token
- token, err := jwtwrapper.GenToken(fmt.Sprintf("%d", req.ProjectId), parser.Conf.Jwt.Issuer, subjectStr,
- time.Duration(120)*time.Second)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.TmpToken"),
- zap.String("args", fmt.Sprintf("%d", req.ProjectId)),
- zap.String("error", err.Error()))
- return errors.SystemError
- }
- resp.Data.Token = token
- 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.CompanyInfoResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/company_info [get]
- func (c *Controller) CompanyInfo(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.CompanyInfoRequest{}
- 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.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.CompanyInfoResponse{}
- rpcReq := &v1.CompanyInfoRequest{
- Id:loginUid,
- }
- rpcRsp, err := pb.Enterprise.CompanyInfo(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Enterprise.CompanyInfo"),
- 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)
- }
|