wechat.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_clpt/models"
  5. "hongze/hongze_clpt/services"
  6. "hongze/hongze_clpt/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. if isWechat == 0 {
  93. items.CreatePlatform = 7
  94. } else {
  95. items.CreatePlatform = 8
  96. }
  97. items.CreateTime = time.Now()
  98. if total == 0 {
  99. _, err = models.AddUserRecord(items)
  100. if err != nil {
  101. br.Msg = "获取用户信息失败"
  102. br.ErrMsg = "添加openid失败,Err:" + err.Error()
  103. return
  104. }
  105. }
  106. timeUnix := time.Now().Unix()
  107. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  108. user, err := models.GetWxUserItemByUserUnionId(unionId)
  109. if err != nil && err.Error() != utils.ErrNoRow() {
  110. br.Msg = "获取用户信息失败"
  111. br.ErrMsg = "获取本地用户信息失败,Err:" + err.Error()
  112. return
  113. }
  114. totalItem, err := models.GetTokenByOpenId(openId)
  115. if err != nil && err.Error() != utils.ErrNoRow() {
  116. br.Msg = "获取用户信息失败"
  117. br.ErrMsg = "查询数量失败,Err:" + err.Error()
  118. return
  119. }
  120. if totalItem == nil {
  121. token = utils.MD5(unionId) + utils.MD5(timeUnixStr)
  122. itemsSession := new(models.CygxClptSession)
  123. itemsSession.UnionId = unionId
  124. itemsSession.OpenId = openId
  125. itemsSession.AccessToken = token
  126. itemsSession.CreatedTime = time.Now()
  127. itemsSession.LastUpdatedTime = time.Now()
  128. itemsSession.ExpireTime = time.Now().AddDate(0, 3, 0)
  129. if user != nil {
  130. itemsSession.UserId = user.UserId
  131. itemsSession.Mobile = user.Mobile
  132. }
  133. err = models.AddCygxClptSession(itemsSession)
  134. if err != nil {
  135. br.Msg = "获取用户信息失败"
  136. br.ErrMsg = "添加Token失败,Err:" + err.Error()
  137. return
  138. }
  139. } else {
  140. token = totalItem.AccessToken
  141. }
  142. if user == nil {
  143. resp.HasPermission = 3
  144. } else {
  145. permissionStr, err := models.GetCompanyPermission(user.CompanyId)
  146. if err != nil {
  147. br.Msg = "获取信息失败"
  148. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  149. return
  150. }
  151. if permissionStr != "" {
  152. resp.Permission = permissionStr
  153. resp.Mobile = user.Mobile
  154. resp.RealName = user.RealName
  155. resp.CompanyName = user.CompanyName
  156. resp.HasPermission = 1
  157. } else {
  158. resp.Mobile = user.Mobile
  159. resp.RealName = user.RealName
  160. resp.HasPermission = 2
  161. }
  162. resp.Headimgurl = user.HeadimgurlRecord
  163. }
  164. if resp.Headimgurl == "" {
  165. resp.Headimgurl = utils.DefaultHeadimgurl
  166. }
  167. resp.Token = token
  168. br.Ret = 200
  169. br.Success = true
  170. br.Msg = "获取成功"
  171. br.Data = resp
  172. }
  173. // @Title 微信登录后绑定手机号接口
  174. // @Description 微信登录后绑定手机号接口
  175. // @Param request body models.WxBindMobileReq true "type json string"
  176. // @Success Ret=200 绑定成功
  177. // @router /bindMobile [post]
  178. func (this *MobileWechatController) WechatBindMobile() {
  179. br := new(models.BaseResponse).Init()
  180. defer func() {
  181. this.Data["json"] = br
  182. this.ServeJSON()
  183. }()
  184. var req models.WxBindMobileReq
  185. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  186. if err != nil {
  187. br.Msg = "参数解析异常!"
  188. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  189. return
  190. }
  191. authorization := this.Token
  192. if authorization == "" {
  193. br.Msg = "参数错误"
  194. br.ErrMsg = "参数错误,authorization不能为空"
  195. return
  196. }
  197. session, err := models.GetSessionByToken(authorization)
  198. if err != nil {
  199. br.Msg = "绑定失败"
  200. br.ErrMsg = "绑定失败,err:" + err.Error()
  201. return
  202. }
  203. openid := session.OpenId
  204. req.Mobile = strings.Trim(req.Mobile, " ")
  205. if req.Mobile == "" {
  206. br.Msg = "参数错误"
  207. br.ErrMsg = "参数错误,手机号为空"
  208. return
  209. }
  210. mobile := req.Mobile
  211. code := req.VCode
  212. if code == "" {
  213. br.Msg = "参数错误"
  214. br.ErrMsg = "Code 为空"
  215. return
  216. }
  217. item, err := models.GetMsgCode(mobile, code)
  218. if err != nil {
  219. if err.Error() == utils.ErrNoRow() {
  220. br.Msg = "验证码错误,请重新输入"
  221. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  222. return
  223. } else {
  224. br.Msg = "验证码错误,请重新输入"
  225. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  226. return
  227. }
  228. }
  229. if item == nil {
  230. br.Msg = "验证码错误,请重新输入"
  231. return
  232. }
  233. user, err := services.BindWxUser(mobile, "86")
  234. if err != nil {
  235. br.Msg = "登录失败"
  236. br.ErrMsg = "获取用户信息失败,GetUserDetailByMobile Err:" + err.Error()
  237. return
  238. }
  239. err = models.BindSessionMobile(mobile, authorization)
  240. if err != nil {
  241. br.Msg = "登录失败"
  242. br.ErrMsg = "获取用户信息失败,BindSessionMobile Err:" + err.Error()
  243. return
  244. }
  245. err = models.BindUserRecordByOpenid(user.UserId, openid, mobile)
  246. if err != nil {
  247. br.Msg = "登录失败"
  248. br.ErrMsg = "获取用户信息失败,BindUserRecordByOpenid Err:" + err.Error()
  249. return
  250. }
  251. br.Ret = 200
  252. br.Success = true
  253. br.Msg = "绑定成功"
  254. }
  255. // @Title 微信获取签名接口
  256. // @Description 微信获取签名接口
  257. // @Param Url query string true "url地址"
  258. // @Success 200 {object} models.WechatSign
  259. // @router /getWxSign [get]
  260. func (this *WechatCommonController) GetWxSign() {
  261. br := new(models.BaseResponse).Init()
  262. defer func() {
  263. this.Data["json"] = br
  264. this.ServeJSON()
  265. }()
  266. getUrl := this.GetString("Url")
  267. accessToken, err := services.GetWxAccessTokenByXzs()
  268. if err != nil {
  269. br.Msg = "获取用户信息失败"
  270. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  271. return
  272. }
  273. if accessToken == "" {
  274. br.Msg = "获取用户信息失败"
  275. br.ErrMsg = "access_token 为空,"
  276. return
  277. }
  278. ticket, err := services.GetWxTicket(accessToken)
  279. if err != nil {
  280. br.Msg = "获取Ticket失败,请联系客服"
  281. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  282. return
  283. }
  284. if ticket == "" {
  285. br.Msg = "获取Ticket失败,请联系客服"
  286. br.ErrMsg = "ticket为空" + ticket
  287. return
  288. }
  289. nonceStr := utils.GetRandStringNoSpecialChar(16)
  290. signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
  291. resp := new(models.WechatSign)
  292. resp.AppId = utils.WxPublicAppId
  293. resp.NonceStr = nonceString
  294. resp.Timestamp = timestamp
  295. resp.Url = getUrl
  296. resp.Signature = signature
  297. br.Ret = 200
  298. br.Success = true
  299. br.Msg = "获取签名成功"
  300. br.Data = resp
  301. }