123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- package controllers
- import (
- "encoding/json"
- "fmt"
- "github.com/medivhzhan/weapp/v2"
- "hongze/hongze_mfyx/models"
- "hongze/hongze_mfyx/models/order"
- "hongze/hongze_mfyx/services"
- "hongze/hongze_mfyx/utils"
- "strconv"
- "time"
- )
- type WechatController struct {
- BaseAuthController
- }
- type WechatCommonController struct {
- BaseCommonController
- }
- // @Title 微信登录接口
- // @Description 微信登录接口
- // @Param Code query string true "微信唯一编码code"
- // @Success 200 {object} models.WxLoginResp
- // @router /login [get]
- func (this *WechatCommonController) WechatLogin() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- code := this.GetString("Code")
- if code == "" {
- br.Msg = "参数错误"
- br.ErrMsg = "Code 为空"
- return
- }
- wxInfo, err := weapp.Login(utils.WxAppId, utils.WxAppSecret, code)
- if err != nil {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
- return
- }
- if err = wxInfo.GetResponseError(); err != nil {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "获取用户信息失败,code:" + strconv.Itoa(wxInfo.ErrCode) + ",msg:" + wxInfo.ErrMSG
- return
- }
- wxUserInfo := new(services.WxUserInfo)
- wxUserInfo.Unionid = wxInfo.UnionID
- wxUserInfo.Openid = wxInfo.OpenID
- wxUserInfo.Errcode = wxInfo.ErrCode
- wxUserInfo.Errmsg = wxInfo.ErrMSG
- wxUserInfo.SessionKey = wxInfo.SessionKey
- token, userId, firstLogin, _, err := services.WxLogin(code, wxInfo.OpenID, wxInfo.UnionID, wxUserInfo)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "微信登录失败"
- br.ErrMsg = "微信登录失败,err:" + err.Error()
- return
- }
- if token == "" {
- br.Msg = "微信登录失败"
- br.ErrMsg = "token:" + token + "" + code + " " + wxInfo.OpenID + " " + wxInfo.UnionID
- return
- }
- //新增登录日志
- {
- loginLog := new(models.WxUserLog)
- loginLog.UserId = userId
- loginLog.OpenId = wxInfo.OpenID
- loginLog.UnionId = wxInfo.UnionID
- loginLog.CreateTime = time.Now()
- loginLog.Handle = "wechat_login_mfyx"
- loginLog.Remark = token
- go models.AddWxUserLog(loginLog)
- }
- {
- codeLog := new(models.WxUserCode)
- codeLog.WxCode = code
- codeLog.UserId = userId
- codeLog.Code = 0
- codeLog.FirstLogin = firstLogin
- codeLog.Authorization = token
- codeLog.UserPermission = 1
- codeLog.CreateTime = time.Now()
- models.AddWxUserCode(codeLog)
- }
- resp := new(models.WxLoginResp)
- resp.UserId = userId
- resp.FirstLogin = firstLogin
- resp.Authorization = token
- br.Ret = 200
- br.Success = true
- br.Msg = "登录成功"
- br.Data = resp
- }
- // @Title 小程序获取用户信息
- // @Description 小程序获取用户信息接口(需要登录)
- // @Param request body models.WxGetUserInfoReq true "type json string"
- // @Success 200 {object} models.WxGetUserInfoResp
- // @router /getUserInfo [post]
- func (this *WechatController) GetUserInfo() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.WxGetUserInfoReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.RawData == "" || req.EncryptedData == "" || req.Signature == "" || req.Iv == "" {
- br.Msg = "参数错误"
- return
- }
- user := this.User
- if user == nil {
- br.Msg = "请登陆"
- br.Ret = 408
- return
- }
- userId := user.UserId
- sessionKey := user.SessionKey
- fmt.Println("sessionKey:", sessionKey)
- fmt.Println(sessionKey, req.RawData, req.EncryptedData, req.Signature, req.Iv)
- userInfo, err := weapp.DecryptUserInfo(sessionKey, req.RawData, req.EncryptedData, req.Signature, req.Iv)
- fmt.Println("weapp.DecryptUserInfo ", err)
- if err != nil {
- br.Msg = "解析用户信息失败"
- br.ErrMsg = "解析用户信息失败,DecryptUserInfo Err:" + err.Error()
- return
- }
- //修改用户微信信息
- err = models.ModifyUserRecordByDetail(userInfo.OpenID, userInfo.UnionID, userInfo.Nickname, userInfo.Avatar, userInfo.City, userInfo.Province, userInfo.Country, userInfo.Gender, userId)
- if err != nil {
- br.Msg = "授权失败"
- br.ErrMsg = "授权失败,修改用户信息失败:" + err.Error()
- return
- }
- var token string
- tokenItem, err := models.GetTokenByOpenId(userInfo.OpenID)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "授权失败"
- br.ErrMsg = "授权失败,获取token失败:" + err.Error()
- return
- }
- if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
- timeUnix := time.Now().Unix()
- timeUnixStr := strconv.FormatInt(timeUnix, 10)
- token = utils.MD5(userInfo.OpenID) + utils.MD5(timeUnixStr)
- //新增session
- {
- session := new(models.CygxSession)
- session.OpenId = userInfo.OpenID
- session.UnionId = userInfo.UnionID
- session.UserId = userId
- session.CreatedTime = time.Now()
- session.LastUpdatedTime = time.Now()
- session.ExpireTime = time.Now().AddDate(0, 3, 0)
- session.AccessToken = token
- err = models.AddSession(session)
- if err != nil {
- br.Msg = "授权失败"
- br.ErrMsg = "授权失败,新增用户session信息失败:" + err.Error()
- return
- }
- }
- } else {
- token = tokenItem.AccessToken
- }
- resp := new(models.WxGetUserInfoResp)
- resp.Authorization = token
- br.Msg = "获取成功!"
- br.Ret = 200
- br.Success = true
- }
- // @Title 小程序获取用户绑定手机号
- // @Description 小程序获取用户绑定手机号接口(需要登录)
- // @Param request body models.WxGetPhoneNumberReq true "type json string"
- // @Success 200 {object} models.WxGetPhoneNumberResp
- // @router /getPhoneNumber [post]
- func (this *WechatController) GetPhoneNumber() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.WxGetPhoneNumberReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EncryptedData == "" || req.Iv == "" {
- br.Msg = "参数错误"
- return
- }
- user := this.User
- if user == nil {
- br.Msg = "请登陆"
- br.Ret = 408
- return
- }
- sessionKey := user.SessionKey
- wxMobile, err := weapp.DecryptMobile(sessionKey, req.EncryptedData, req.Iv)
- if err != nil {
- br.Msg = "解析用户手机号信息失败"
- br.ErrMsg = "解析用户手机号信息失败,Err:" + err.Error()
- return
- }
- err = models.ModifyUsersMobile(user.UserId, wxMobile.PurePhoneNumber)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- resp := new(models.WxGetPhoneNumberResp)
- resp.PhoneNumber = wxMobile.PhoneNumber
- resp.PurePhoneNumber = wxMobile.PurePhoneNumber
- resp.CountryCode = wxMobile.CountryCode
- br.Msg = "获取成功!"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- }
- // @Title 获取短信验证码
- // @Description 获取短信验证码接口
- // @Param Mobile query string true "手机号码"
- // @Param AreaNum query string true "地区编码"
- // @Success Ret=200 获取成功
- // @router /getSmsCode [get]
- func (this *WechatController) GetSmsCode() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- mobile := this.GetString("Mobile")
- if mobile == "" {
- br.Msg = "请输入手机号"
- return
- }
- areaNum := this.GetString("AreaNum")
- msgCode := utils.GetRandDigit(4)
- var result bool
- if areaNum == "86" || areaNum == "" || areaNum == "0" {
- result = services.SendSmsCode(mobile, msgCode)
- } else {
- result = services.SendSmsCodeGj(mobile, msgCode, areaNum)
- }
- //发送成功
- if result {
- item := new(models.MsgCode)
- item.OpenId = ""
- item.Code = msgCode
- item.Mobile = mobile
- item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
- item.Enabled = 1
- item.CreatedTime = time.Now()
- err := models.AddMsgCode(item)
- if err != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "发送失败,Err:" + err.Error()
- return
- }
- br.Msg = "发送成功"
- } else {
- br.Msg = "发送失败"
- }
- br.Ret = 200
- br.Success = true
- }
- // @Title 获取邮件验证码
- // @Description 获取邮件验证码接口
- // @Param Email query string true "邮箱"
- // @Success Ret=200 获取成功
- // @router /getEmailCode [get]
- func (this *WechatController) GetEmailCode() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- email := this.GetString("Email")
- if email == "" {
- br.Msg = "请输入邮箱地址"
- return
- }
- if !utils.ValidateEmailFormatat(email) {
- br.Msg = "邮箱格式错误,请重新输入"
- return
- }
- msgCode := utils.GetRandDigit(4)
- content := "尊敬的用户:</br>您好,感谢您使用买方研选,您正在进行邮箱验证,本次请求的验证码为:" + msgCode + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>买方研选团队 </br>2019年05月11日"
- title := "买方研选登陆验证"
- //发送邮件
- result, err := utils.SendEmailByHz(title, content, email)
- if err != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "发送失败,Err:" + err.Error()
- return
- }
- if result {
- item := new(models.MsgCode)
- item.OpenId = ""
- item.Code = msgCode
- item.Mobile = email
- item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
- item.Enabled = 1
- item.CreatedTime = time.Now()
- err := models.AddMsgCode(item)
- if err != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "发送失败,Err:" + err.Error()
- return
- }
- br.Msg = "发送成功"
- } else {
- br.Msg = "发送失败"
- }
- br.Ret = 200
- br.Success = true
- }
- // @Title 获取短信验证码(无需token)
- // @Description 获取短信验证码接口(无需token)
- // @Param Mobile query string true "手机号码"
- // @Param AreaNum query string true "地区编码"
- // @Success Ret=200 获取成功
- // @router /getSmsCodePublic [get]
- func (this *WechatCommonController) GetSmsCode() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- mobile := this.GetString("Mobile")
- if mobile == "" {
- br.Msg = "请输入手机号"
- return
- }
- areaNum := this.GetString("AreaNum")
- msgCode := utils.GetRandDigit(4)
- var result bool
- if areaNum == "86" || areaNum == "" || areaNum == "0" {
- result = services.SendSmsCode(mobile, msgCode)
- } else {
- result = services.SendSmsCodeGj(mobile, msgCode, areaNum)
- }
- //发送成功
- if result {
- item := new(models.MsgCode)
- item.OpenId = ""
- item.Code = msgCode
- item.Mobile = mobile
- item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
- item.Enabled = 1
- item.CreatedTime = time.Now()
- err := models.AddMsgCode(item)
- if err != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "发送失败,Err:" + err.Error()
- return
- }
- br.Msg = "发送成功"
- } else {
- br.Msg = "发送失败"
- }
- br.Ret = 200
- br.Success = true
- }
- // @Title 微信获取用户绑定的手机号
- // @Description 微信获取用户绑定的手机号接口
- // @Param Code query string true "微信唯一编码code"
- // @Param InviteShareCode query string false "销售账号邀请码"
- // @Success 200 {object} models.WxLoginResp
- // @router /getuserphonenumber [get]
- func (this *WechatController) Getuserphonenumber() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录"
- br.Ret = 408
- return
- }
- code := this.GetString("Code")
- if code == "" {
- br.Msg = "参数错误"
- br.ErrMsg = "Code 为空"
- return
- }
- inviteShareCode := this.GetString("InviteShareCode")
- wxitem, err := services.Getuserphonenumber(code)
- if err != nil {
- br.Msg = "微信登录失败"
- br.ErrMsg = "微信登录失败,err:" + err.Error()
- return
- }
- var mobile string
- var countryCode string
- openId := this.User.OpenId
- mobile = wxitem.PhoneInfo.PhoneNumber
- countryCode = wxitem.PhoneInfo.CountryCode
- if mobile == "" {
- br.Msg = "微信登录失败"
- br.ErrMsg = "微信登录失败,手机号为空"
- return
- }
- user, err = services.BindWxUser(openId, mobile, "", countryCode, inviteShareCode)
- if err != nil {
- br.Msg = "登录失败"
- br.ErrMsg = "绑定手机号失败:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "登录成功"
- //br.Data = resp
- }
- // @Title 微信付款回调接口
- // @Description 微信付款回调接口
- // @Success 200 {object} models.WxLoginResp
- // @router /wxpay/notify [post]
- func (this *WechatCommonController) WxpayNotify() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- body := this.Ctx.Input.RequestBody
- resp := new(order.WechatPayNotifyUrlJson)
- //utils.FileLog.Info("wechat notify:" + string(body))
- item := new(models.CygxShanghaiCompanyLog)
- item.CreateTime = time.Now()
- //item.Body = jsonCompany
- item.Result = string(body)
- go models.AddCygxShanghaiCompanyLog(item)
- itemNotify := services.WxDecodeNotify(body)
- if itemNotify == nil {
- resp.Code = "FAIL"
- resp.Message = "失败"
- br.Ret = 400
- return
- }
- if itemNotify.OutTradeNo != "" {
- go services.HandleOrderByWechatPay(itemNotify)
- item.Url = itemNotify.OutTradeNo // 记录一下同一笔订单微信推送了几次
- go models.AddCygxShanghaiCompanyLog(item)
- }
- resp.Code = "SUCCESS"
- br.Data = resp
- br.Ret = 200
- br.Success = true
- }
- // @Title 微信退款回调接口
- // @Description 微信退款回调接口
- // @Success 200 {object} models.WxLoginResp
- // @router /wxpay/refunds/notify [post]
- func (this *WechatCommonController) WxpayRefundsNotify() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- body := this.Ctx.Input.RequestBody
- resp := new(order.WechatPayNotifyUrlJson)
- //utils.FileLog.Info("wechat notify:" + string(body))
- item := new(models.CygxShanghaiCompanyLog)
- item.CreateTime = time.Now()
- //item.Body = jsonCompany
- item.Result = string(body)
- itemNotify := services.WxDecodeNotify(body)
- if itemNotify == nil {
- resp.Code = "FAIL"
- resp.Message = "失败"
- br.Ret = 400
- return
- }
- if itemNotify.OutTradeNo != "" {
- go services.HandleOrderRefundByWechatPay(itemNotify)
- outTradeNo := itemNotify.OutTradeNo
- orderDetail, err := order.GetCygxOrderDetailByOutTradeNo(outTradeNo)
- if err != nil {
- return
- }
- //修改过状态的不再二次处理
- if orderDetail.OrderStatus != 3 {
- go models.AddCygxShanghaiCompanyLog(item)
- }
- }
- br.Data = resp
- br.Ret = 200
- br.Success = true
- }
|