wechat.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_web_mfyx/models"
  5. "hongze/hongze_web_mfyx/services"
  6. "hongze/hongze_web_mfyx/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type WechatController struct {
  12. BaseAuthController
  13. }
  14. type WechatCommonController struct {
  15. BaseCommonController
  16. }
  17. type MobileWechatController struct {
  18. BaseAuthMobileController
  19. }
  20. // @Title 微信登录小助手接口
  21. // @Description 微信登录小助手接口
  22. // @Param Code query string true "微信唯一编码code"
  23. // @Param IsWechat query int true "是否微信内置浏览器打开 1是 ,0否 ,不传默认为0 "
  24. // @Success 200 {object} models.UserDetailByUserLogin
  25. // @router /login [get]
  26. func (this *WechatCommonController) WechatLogin() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. code := this.GetString("Code")
  33. isWechat, _ := this.GetInt("IsWechat")
  34. if code == "" {
  35. br.Msg = "参数错误"
  36. br.ErrMsg = "Code 为空"
  37. return
  38. }
  39. var token string
  40. resp := new(models.UserDetailByUserLogin)
  41. var WxAccessToken *services.WxAccessToken
  42. var errWx error
  43. if isWechat == 0 {
  44. WxAccessToken, errWx = services.GetWxAccessOpenToken(code)
  45. } else {
  46. WxAccessToken, errWx = services.WxGetUserOpenIdByCode(code)
  47. }
  48. if errWx != nil {
  49. br.Msg = "获取用户信息失败"
  50. br.ErrMsg = "获取access_token失败,err:" + errWx.Error()
  51. return
  52. }
  53. if WxAccessToken.AccessToken == "" {
  54. br.Msg = "获取用户信息失败"
  55. br.ErrMsg = "access_token 为空,"
  56. return
  57. }
  58. openId := WxAccessToken.Openid
  59. wxUserInfo, err := services.WxGetUserInfo(openId, WxAccessToken.AccessToken)
  60. if err != nil {
  61. br.Msg = "获取用户信息失败"
  62. br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
  63. return
  64. }
  65. if wxUserInfo.Errcode != 0 {
  66. userInfoJson, _ := json.Marshal(wxUserInfo)
  67. br.Msg = "登录失败"
  68. br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson)
  69. return
  70. }
  71. unionId := wxUserInfo.Unionid
  72. if unionId == "" {
  73. br.Msg = "获取用户信息失败"
  74. br.ErrMsg = "获取unionid失败,unionid:" + wxUserInfo.Unionid
  75. return
  76. }
  77. total, err := models.GetCygxUserRecordCount(openId)
  78. if err != nil {
  79. br.Msg = "获取用户信息失败"
  80. br.ErrMsg = "查询数量失败,Err:" + err.Error()
  81. return
  82. }
  83. items := new(models.UserRecord)
  84. items.OpenId = openId
  85. items.UnionId = unionId
  86. items.NickName = wxUserInfo.Nickname
  87. items.Sex = wxUserInfo.Sex
  88. items.Province = wxUserInfo.Province
  89. items.City = wxUserInfo.City
  90. items.Country = wxUserInfo.Country
  91. items.Headimgurl = wxUserInfo.Headimgurl
  92. items.CreatePlatform = utils.WxPlatform
  93. items.CreateTime = time.Now()
  94. if total == 0 {
  95. _, err = models.AddUserRecord(items)
  96. if err != nil {
  97. br.Msg = "获取用户信息失败"
  98. br.ErrMsg = "添加openid失败,Err:" + err.Error()
  99. return
  100. }
  101. }
  102. timeUnix := time.Now().Unix()
  103. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  104. user, err := models.GetWxUserItemByUserUnionId(unionId)
  105. if err != nil && err.Error() != utils.ErrNoRow() {
  106. br.Msg = "获取用户信息失败"
  107. br.ErrMsg = "获取本地用户信息失败,Err:" + err.Error()
  108. return
  109. }
  110. totalItem, err := models.GetTokenByOpenId(openId)
  111. if err != nil && err.Error() != utils.ErrNoRow() {
  112. br.Msg = "获取用户信息失败"
  113. br.ErrMsg = "查询数量失败,Err:" + err.Error()
  114. return
  115. }
  116. if totalItem == nil {
  117. token = utils.MD5(unionId) + utils.MD5(timeUnixStr)
  118. itemsSession := new(models.CygxMfyxWebSession)
  119. itemsSession.UnionId = unionId
  120. itemsSession.OpenId = openId
  121. itemsSession.AccessToken = token
  122. itemsSession.CreatedTime = time.Now()
  123. itemsSession.LastUpdatedTime = time.Now()
  124. itemsSession.ExpireTime = time.Now().AddDate(0, 0, 30)
  125. if user != nil {
  126. itemsSession.UserId = user.UserId
  127. itemsSession.Mobile = user.Mobile
  128. }
  129. err = services.HandleCygxMfyxWebSession(itemsSession)
  130. if err != nil {
  131. br.Msg = "获取用户信息失败"
  132. br.ErrMsg = "添加Token失败,Err:" + err.Error()
  133. return
  134. }
  135. } else {
  136. token = totalItem.AccessToken
  137. }
  138. if user == nil {
  139. resp.HasPermission = 3
  140. } else {
  141. permissionStr, err := models.GetCompanyPermission(user.CompanyId)
  142. if err != nil {
  143. br.Msg = "获取信息失败"
  144. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  145. return
  146. }
  147. if permissionStr != "" {
  148. resp.Permission = permissionStr
  149. resp.Mobile = user.Mobile
  150. resp.RealName = user.RealName
  151. resp.CompanyName = user.CompanyName
  152. resp.HasPermission = 1
  153. } else {
  154. resp.Mobile = user.Mobile
  155. resp.RealName = user.RealName
  156. resp.HasPermission = 2
  157. }
  158. resp.Headimgurl = user.HeadimgurlRecord
  159. }
  160. if resp.Headimgurl == "" {
  161. resp.Headimgurl = utils.DefaultHeadimgurl
  162. }
  163. resp.Token = token
  164. br.Ret = 200
  165. br.Success = true
  166. br.Msg = "获取成功"
  167. br.Data = resp
  168. }
  169. // @Title 微信登录后绑定手机号接口
  170. // @Description 微信登录后绑定手机号接口
  171. // @Param request body models.WxBindMobileReq true "type json string"
  172. // @Success Ret=200 绑定成功
  173. // @router /bindMobile [post]
  174. func (this *MobileWechatController) WechatBindMobile() {
  175. br := new(models.BaseResponse).Init()
  176. defer func() {
  177. this.Data["json"] = br
  178. this.ServeJSON()
  179. }()
  180. var req models.WxBindMobileReq
  181. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  182. if err != nil {
  183. br.Msg = "参数解析异常!"
  184. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  185. return
  186. }
  187. authorization := this.Token
  188. if authorization == "" {
  189. br.Msg = "参数错误"
  190. br.ErrMsg = "参数错误,authorization不能为空"
  191. return
  192. }
  193. session, err := models.GetSessionByToken(authorization)
  194. if err != nil {
  195. br.Msg = "绑定失败"
  196. br.ErrMsg = "绑定失败,err:" + err.Error()
  197. return
  198. }
  199. openid := session.OpenId
  200. req.Mobile = strings.Trim(req.Mobile, " ")
  201. if req.Mobile == "" {
  202. br.Msg = "参数错误"
  203. br.ErrMsg = "参数错误,手机号为空"
  204. return
  205. }
  206. mobile := req.Mobile
  207. code := req.VCode
  208. if code == "" {
  209. br.Msg = "参数错误"
  210. br.ErrMsg = "Code 为空"
  211. return
  212. }
  213. item, err := models.GetMsgCode(mobile, code)
  214. if err != nil {
  215. if err.Error() == utils.ErrNoRow() {
  216. br.Msg = "验证码错误,请重新输入"
  217. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  218. return
  219. } else {
  220. br.Msg = "验证码错误,请重新输入"
  221. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  222. return
  223. }
  224. }
  225. if item == nil {
  226. br.Msg = "验证码错误,请重新输入"
  227. return
  228. }
  229. user, err := services.BindWxUser(mobile, "86", "")
  230. if err != nil {
  231. br.Msg = "登录失败"
  232. br.ErrMsg = "获取用户信息失败,GetUserDetailByMobile Err:" + err.Error()
  233. return
  234. }
  235. err = models.BindSessionMobile(mobile, authorization)
  236. if err != nil {
  237. br.Msg = "登录失败"
  238. br.ErrMsg = "获取用户信息失败,BindSessionMobile Err:" + err.Error()
  239. return
  240. }
  241. err = models.BindUserRecordByOpenid(user.UserId, openid, mobile)
  242. if err != nil {
  243. br.Msg = "登录失败"
  244. br.ErrMsg = "获取用户信息失败,BindUserRecordByOpenid Err:" + err.Error()
  245. return
  246. }
  247. br.Ret = 200
  248. br.Success = true
  249. br.Msg = "绑定成功"
  250. }
  251. // @Title 微信获取签名接口
  252. // @Description 微信获取签名接口
  253. // @Param Url query string true "url地址"
  254. // @Success 200 {object} models.WechatSign
  255. // @router /getWxSign [get]
  256. func (this *WechatCommonController) GetWxSign() {
  257. br := new(models.BaseResponse).Init()
  258. defer func() {
  259. this.Data["json"] = br
  260. this.ServeJSON()
  261. }()
  262. getUrl := this.GetString("Url")
  263. accessToken, err := services.GetWxAccessTokenByMfyx()
  264. if err != nil {
  265. br.Msg = "获取用户信息失败"
  266. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  267. return
  268. }
  269. if accessToken == "" {
  270. br.Msg = "获取用户信息失败"
  271. br.ErrMsg = "access_token 为空,"
  272. return
  273. }
  274. ticket, err := services.GetWxTicket(accessToken)
  275. if err != nil {
  276. br.Msg = "获取Ticket失败,请联系客服"
  277. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  278. return
  279. }
  280. if ticket == "" {
  281. br.Msg = "获取Ticket失败,请联系客服"
  282. br.ErrMsg = "ticket为空" + ticket
  283. return
  284. }
  285. nonceStr := utils.GetRandStringNoSpecialChar(16)
  286. signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
  287. resp := new(models.WechatSign)
  288. resp.AppId = utils.WxPublicAppId
  289. resp.NonceStr = nonceString
  290. resp.Timestamp = timestamp
  291. resp.Url = getUrl
  292. resp.Signature = signature
  293. br.Ret = 200
  294. br.Success = true
  295. br.Msg = "获取签名成功"
  296. br.Data = resp
  297. }