wechat.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "github.com/medivhzhan/weapp/v2"
  7. adminResp "hongze/hongze_mobile_admin/models/response/admin"
  8. "hongze/hongze_mobile_admin/models/response/wechat"
  9. "hongze/hongze_mobile_admin/services"
  10. "hongze/hongze_mobile_admin/utils"
  11. "strconv"
  12. "time"
  13. )
  14. type WeChatCommon struct {
  15. BaseNotAuth
  16. }
  17. // WeChatLogin
  18. // @Title 微信登录接口
  19. // @Description 微信登录接口
  20. // @Param Code query string true "微信唯一编码code"
  21. // @Success 200 {object} wechat.WxLoginResp
  22. // @router /login [get]
  23. func (c *WeChatCommon) WeChatLogin() {
  24. code := c.GetString("Code")
  25. fmt.Println("code:", code)
  26. utils.FileLog.Info("WechatLogin code:%s", code)
  27. item, err := services.WxGetUserOpenIdByCode(code)
  28. if err != nil {
  29. c.FailWithMessage(fmt.Sprintf("%v", err), fmt.Sprintf("%v", err))
  30. return
  31. }
  32. if item.Errcode != 0 {
  33. c.FailWithMessage("获取用户信息失败", "获取access_token 失败 errCode:"+strconv.Itoa(item.Errcode)+" ;errMsg:"+item.Errmsg)
  34. }
  35. openId := item.Openid
  36. if openId == "" {
  37. c.FailWithMessage("获取用户信息失败", "获取openid失败,openid:"+item.Openid)
  38. }
  39. accessToken, err := services.WxGetAccessToken()
  40. if err != nil {
  41. c.FailWithMessage("获取用户信息失败", "获取access_token失败,err:"+err.Error())
  42. }
  43. //获取用户信息
  44. wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  45. if err != nil {
  46. c.FailWithMessage("获取用户信息失败", "获取微信用户信息失败,Err:"+err.Error())
  47. }
  48. if wxUserInfo.Errcode != 0 {
  49. userInfoJson, _ := json.Marshal(wxUserInfo)
  50. c.FailWithMessage("登录失败", "获取用户信息失败,err:"+string(userInfoJson))
  51. return
  52. }
  53. token, adminWx, err, errMsg := services.WxLogin(utils.WxPlatform, item, wxUserInfo)
  54. if err != nil {
  55. c.FailWithMessage(errMsg, "微信登录失败,err:"+err.Error())
  56. return
  57. }
  58. userInfo := adminResp.LoginResp{
  59. RealName: adminWx.RealName,
  60. AdminName: adminWx.AdminName,
  61. RoleName: adminWx.RoleName,
  62. RoleTypeCode: adminWx.RoleTypeCode,
  63. AdminId: adminWx.AdminId,
  64. DepartmentName: adminWx.DepartmentName,
  65. GroupName: adminWx.GroupName,
  66. }
  67. userInfo.AdminId = adminWx.AdminId
  68. var productName string
  69. productId := services.GetProductId(adminWx.RoleTypeCode)
  70. if productId == 1 {
  71. productName = utils.COMPANY_PRODUCT_FICC_NAME
  72. } else if productId == 2 {
  73. productName = utils.COMPANY_PRODUCT_RAI_NAME
  74. } else {
  75. productName = "admin"
  76. }
  77. userInfo.ProductName = productName
  78. userInfo.Authority = adminWx.Authority
  79. resp := wechat.WxLoginResp{
  80. AdminId: adminWx.AdminId,
  81. Code: 0,
  82. Authorization: token,
  83. Headimgurl: adminWx.Headimgurl,
  84. RealName: adminWx.RealName,
  85. UserInfo: userInfo,
  86. }
  87. //登录日志
  88. {
  89. returnResult, err := json.Marshal(resp)
  90. if err != nil {
  91. utils.FileLog.Info(c.Ctx.Input.URI() + " Err:%s" + err.Error())
  92. }
  93. utils.FileLog.Info(c.Ctx.Input.URI()+" code: %s , return data: %s", code, string(returnResult))
  94. }
  95. c.OkDetailed(resp, "登录成功")
  96. }
  97. // WxAppLogin
  98. // @Title 微信登录接口
  99. // @Description 微信登录接口
  100. // @Param Code query string true "微信唯一编码code"
  101. // @Success 200 {object} wechat.WxLoginResp
  102. // @router /wxapp/login [get]
  103. func (c *WeChatCommon) WxAppLogin() {
  104. code := c.GetString("Code")
  105. if code == "" {
  106. c.FailWithMessage("参数错误", "Code 为空")
  107. return
  108. }
  109. wxInfo, err := weapp.Login(utils.WxAppId2, utils.WxAppSecret2, code)
  110. if err != nil {
  111. c.FailWithMessage("获取用户信息失败", "获取用户信息失败,Err:"+err.Error())
  112. return
  113. }
  114. if err = wxInfo.GetResponseError(); err != nil {
  115. c.FailWithMessage("获取用户信息失败", "获取用户信息失败,code:"+strconv.Itoa(wxInfo.ErrCode)+",msg:"+wxInfo.ErrMSG)
  116. return
  117. }
  118. item := &services.WxAccessToken{
  119. //AccessToken :wxInfo.
  120. //ExpiresIn :wxInfo.,
  121. //RefreshToken : wxInfo,
  122. Openid: wxInfo.OpenID,
  123. Unionid: wxInfo.UnionID,
  124. //Scope :wxInfo,
  125. Errcode: wxInfo.ErrCode,
  126. Errmsg: wxInfo.ErrMSG,
  127. }
  128. wxUserInfo := new(services.WxUserInfo)
  129. wxUserInfo.Unionid = wxInfo.UnionID
  130. wxUserInfo.Openid = wxInfo.OpenID
  131. wxUserInfo.Unionid = wxInfo.UnionID
  132. wxUserInfo.Errcode = wxInfo.ErrCode
  133. wxUserInfo.Errmsg = wxInfo.ErrMSG
  134. wxUserInfo.SessionKey = wxInfo.SessionKey
  135. fmt.Println("openId", wxInfo.OpenID)
  136. fmt.Println("unionId", wxInfo.UnionID)
  137. token, adminWx, err, errMsg := services.WxLogin(utils.WxPlatform2, item, wxUserInfo)
  138. if err != nil {
  139. c.FailWithMessage(errMsg, "微信登录失败,err:"+err.Error())
  140. return
  141. }
  142. userInfo := adminResp.LoginResp{
  143. RealName: adminWx.RealName,
  144. AdminName: adminWx.AdminName,
  145. RoleName: adminWx.RoleName,
  146. RoleTypeCode: adminWx.RoleTypeCode,
  147. AdminId: adminWx.AdminId,
  148. DepartmentName: adminWx.DepartmentName,
  149. GroupName: adminWx.GroupName,
  150. }
  151. userInfo.AdminId = adminWx.AdminId
  152. var productName string
  153. productId := services.GetProductId(adminWx.RoleTypeCode)
  154. if productId == 1 {
  155. productName = utils.COMPANY_PRODUCT_FICC_NAME
  156. } else if productId == 2 {
  157. productName = utils.COMPANY_PRODUCT_RAI_NAME
  158. } else {
  159. productName = "admin"
  160. }
  161. userInfo.ProductName = productName
  162. userInfo.Authority = adminWx.Authority
  163. resp := wechat.WxLoginResp{
  164. AdminId: adminWx.AdminId,
  165. Code: 0,
  166. Authorization: token,
  167. Headimgurl: adminWx.Headimgurl,
  168. RealName: adminWx.RealName,
  169. UserInfo: userInfo,
  170. }
  171. //登录日志
  172. {
  173. returnResult, err := json.Marshal(resp)
  174. if err != nil {
  175. utils.FileLog.Info(c.Ctx.Input.URI() + " Err:%s" + err.Error())
  176. }
  177. utils.FileLog.Info(c.Ctx.Input.URI()+" code: %s , return data: %s", code, string(returnResult))
  178. }
  179. c.OkDetailed(resp, "登录成功")
  180. }
  181. // @Title 小程序获取用户信息
  182. // @Description 小程序获取用户信息接口(需要登录)
  183. // @Param request body models.WxGetUserInfoReq true "type json string"
  184. // @Success 200 {object} models.WxGetUserInfoResp
  185. // @router /getUserInfo [post]
  186. //func (c *WechatController) GetUserInfo() {
  187. // br := new(models.BaseResponse).Init()
  188. // defer func() {
  189. // c.Data["json"] = br
  190. // c.ServeJSON()
  191. // }()
  192. // var req models.WxGetUserInfoReq
  193. // err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  194. // if err != nil {
  195. // br.Msg = "参数解析异常!"
  196. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  197. // return
  198. // }
  199. // if req.RawData == "" || req.EncryptedData == "" || req.Signature == "" || req.Iv == "" {
  200. // br.Msg = "参数错误"
  201. // return
  202. // }
  203. // user := c.User
  204. // if user == nil {
  205. // br.Msg = "请登陆"
  206. // br.Ret = 408
  207. // return
  208. // }
  209. // userId := user.UserId
  210. // sessionKey := user.SessionKey
  211. // fmt.Println("sessionKey:", sessionKey)
  212. // fmt.Println(sessionKey, req.RawData, req.EncryptedData, req.Signature, req.Iv)
  213. // userInfo, err := weapp.DecryptUserInfo(sessionKey, req.RawData, req.EncryptedData, req.Signature, req.Iv)
  214. // fmt.Println("weapp.DecryptUserInfo ", err)
  215. //
  216. // if err != nil {
  217. // br.Msg = "解析用户信息失败"
  218. // br.ErrMsg = "解析用户信息失败,DecryptUserInfo Err:" + err.Error()
  219. // return
  220. // }
  221. // //修改用户微信信息
  222. // err = models.ModifyUserRecordByDetail(userInfo.OpenID, userInfo.UnionID, userInfo.Nickname, userInfo.Avatar, userInfo.City, userInfo.Province, userInfo.Country, userInfo.Gender, userId)
  223. // if err != nil {
  224. // br.Msg = "授权失败"
  225. // br.ErrMsg = "授权失败,修改用户信息失败:" + err.Error()
  226. // return
  227. // }
  228. // var token string
  229. // tokenItem, err := models.GetTokenByOpenId(userInfo.OpenID)
  230. // if err != nil && err.Error() != utils.ErrNoRow() {
  231. // br.Msg = "授权失败"
  232. // br.ErrMsg = "授权失败,获取token失败:" + err.Error()
  233. // return
  234. // }
  235. //
  236. // if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  237. // timeUnix := time.Now().Unix()
  238. // timeUnixStr := strconv.FormatInt(timeUnix, 10)
  239. // token = utils.MD5(userInfo.OpenID) + utils.MD5(timeUnixStr)
  240. // //新增session
  241. // {
  242. // session := new(models.CygxSession)
  243. // session.OpenId = userInfo.OpenID
  244. // session.UnionId = userInfo.UnionID
  245. // session.UserId = userId
  246. // session.CreatedTime = time.Now()
  247. // session.LastUpdatedTime = time.Now()
  248. // session.ExpireTime = time.Now().AddDate(0, 3, 0)
  249. // session.AccessToken = token
  250. // err = models.AddSession(session)
  251. // if err != nil {
  252. // br.Msg = "授权失败"
  253. // br.ErrMsg = "授权失败,新增用户session信息失败:" + err.Error()
  254. // return
  255. // }
  256. // }
  257. // } else {
  258. // token = tokenItem.AccessToken
  259. // }
  260. // resp := new(models.WxGetUserInfoResp)
  261. // resp.Authorization = token
  262. // br.Msg = "获取成功!"
  263. // br.Ret = 200
  264. // br.Success = true
  265. //}
  266. // AdminWeChatLogin
  267. // @Title 内部员工公众号(弘则部门)微信绑定接口
  268. // @Description 内部员工公众号(弘则部门)微信绑定接口
  269. // @Param Code query string true "微信唯一编码code"
  270. // @Success 200 {object} wechat.WxLoginResp
  271. // @router /admin/login [get]
  272. func (c *WeChatCommon) AdminWeChatLogin() {
  273. code := c.GetString("Code")
  274. fmt.Println("code:", code)
  275. utils.FileLog.Info("WechatLogin code:%s", code)
  276. item, err := services.WxGetAdminOpenIdByCode(code)
  277. if err != nil {
  278. c.FailWithMessage(fmt.Sprintf("%v", err), fmt.Sprintf("%v", err))
  279. return
  280. }
  281. if item.Errcode != 0 {
  282. if item.Errcode == 40163 {
  283. c.FailWithCodeUsed("获取用户信息失败", "获取access_token 失败 errCode:"+strconv.Itoa(item.Errcode)+" ;errMsg:"+item.Errmsg)
  284. return
  285. }
  286. c.FailWithMessage("获取用户信息失败", "获取access_token 失败 errCode:"+strconv.Itoa(item.Errcode)+" ;errMsg:"+item.Errmsg)
  287. return
  288. }
  289. openId := item.Openid
  290. if openId == "" {
  291. c.FailWithMessage("获取用户信息失败", "获取openid失败,openid:"+item.Openid)
  292. return
  293. }
  294. accessToken, err := services.WxGetRedisAccessToken(utils.AdminWxAppId, utils.AdminWxAppSecret)
  295. if err != nil {
  296. c.FailWithMessage("获取用户信息失败", "获取access_token失败,err:"+err.Error())
  297. }
  298. //获取用户信息
  299. wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  300. if err != nil {
  301. c.FailWithMessage("获取用户信息失败", "获取微信用户信息失败,Err:"+err.Error())
  302. return
  303. }
  304. userInfoJson, _ := json.Marshal(wxUserInfo)
  305. utils.FileLog.Info("services.WxGetUserInfo:%s", string(userInfoJson))
  306. if wxUserInfo.Errcode != 0 {
  307. c.FailWithMessage("登录失败", "获取用户信息失败,err:"+string(userInfoJson))
  308. return
  309. }
  310. /*item := &services.WxAccessToken{
  311. AccessToken: "",
  312. ExpiresIn: 0,
  313. RefreshToken: "",
  314. Openid: "openid1231243124343",
  315. Unionid: "unionid123123123",
  316. Scope: "",
  317. Errcode: 0,
  318. Errmsg: "",
  319. }
  320. wxUserInfo := &services.WxUserInfo{
  321. Openid: "openid1231243124343",
  322. Nickname: "",
  323. Sex: 0,
  324. Language: "",
  325. City: "",
  326. Province: "",
  327. Country: "",
  328. Headimgurl: "",
  329. SubscribeTime: 0,
  330. Unionid: "unionid123123123",
  331. Remark: "",
  332. Groupid: 0,
  333. SubscribeScene: "",
  334. SessionKey: "",
  335. Errcode: 0,
  336. Errmsg: "",
  337. }*/
  338. resp, err, errMsg := services.AdminWxLogin(item, wxUserInfo)
  339. if err != nil {
  340. c.FailWithMessage(errMsg, "微信登录失败,err:"+err.Error())
  341. return
  342. }
  343. //登录日志
  344. {
  345. returnResult, err := json.Marshal(resp)
  346. if err != nil {
  347. utils.FileLog.Info(c.Ctx.Input.URI() + " Err:%s" + err.Error())
  348. }
  349. utils.FileLog.Info(c.Ctx.Input.URI()+" code: %s , return data: %s", code, string(returnResult))
  350. }
  351. c.OkDetailed(resp, "登录成功")
  352. }
  353. // @Title 微信获取签名接口
  354. // @Description 微信获取签名接口
  355. // @Param Url query string true "url地址"
  356. // @Success 200 {object} models.WechatSign
  357. // @router /notify [get,post]
  358. func (c *WeChatCommon) Notify() {
  359. echostr := c.GetString("echostr")
  360. method := c.Ctx.Input.Method()
  361. if method == "POST" {
  362. body := c.Ctx.Input.RequestBody
  363. utils.FileLog.Info("wechat notify:" + string(body))
  364. item := new(Notify)
  365. err := xml.Unmarshal(body, &item)
  366. if err != nil {
  367. utils.FileLog.Info("xml.Unmarshal:" + err.Error())
  368. }
  369. contactMsg := "感谢关注弘则内部。\n\n此公众号仅供内部员工使用,用于内部员工接收消息通知"
  370. var openId, returnResult string
  371. if item.MsgType != "" {
  372. openId = item.FromUserName
  373. }
  374. xmlTpl := `<xml>
  375. <ToUserName><![CDATA[%s]]></ToUserName>
  376. <FromUserName><![CDATA[%s]]></FromUserName>
  377. <CreateTime>%s</CreateTime>
  378. <MsgType><![CDATA[text]]></MsgType>
  379. <Content><![CDATA[%s]]></Content>
  380. </xml>`
  381. createTime := strconv.FormatInt(time.Now().Unix(), 10)
  382. xmlTpl = fmt.Sprintf(xmlTpl, openId, utils.WxId, createTime, contactMsg)
  383. if item.MsgType == "event" {
  384. switch item.Event {
  385. case "subscribe":
  386. fmt.Println("关注")
  387. // go models.UserSubscribe(1, openId)
  388. break
  389. case "unsubscribe":
  390. fmt.Println("取消关注")
  391. // go models.UserSubscribe(0, openId)
  392. break
  393. case "CLICK":
  394. returnResult = xmlTpl
  395. break
  396. default:
  397. utils.FileLog.Info("wechat notify event:" + item.Event)
  398. }
  399. c.Ctx.WriteString(xmlTpl)
  400. } else {
  401. returnResult = xmlTpl
  402. }
  403. c.Ctx.WriteString(returnResult)
  404. } else {
  405. c.Ctx.WriteString(echostr)
  406. }
  407. }
  408. type Notify struct {
  409. ToUserName string `xml:"ToUserName"`
  410. FromUserName string `xml:"FromUserName"`
  411. CreateTime int `xml:"CreateTime"`
  412. MsgType string `xml:"MsgType"`
  413. Event string `xml:"Event"`
  414. EventKey string `xml:"EventKey"`
  415. }