wechat.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "eta/eta_mini_api/models"
  6. "eta/eta_mini_api/models/request"
  7. "eta/eta_mini_api/models/response"
  8. "eta/eta_mini_api/services/alarm_msg"
  9. "eta/eta_mini_api/services/wechat"
  10. "eta/eta_mini_api/services/wx_app"
  11. "eta/eta_mini_api/utils"
  12. "fmt"
  13. "strconv"
  14. "time"
  15. )
  16. type WechatController struct {
  17. BaseCommonController
  18. }
  19. // @Title 微信获取签名接口
  20. // @Description 微信获取签名接口
  21. // @Param Url query string true "url地址"
  22. // @Success 200 {object} models.WechatSign
  23. // @router /notify [get,post]
  24. func (this *WechatController) Notify() {
  25. echostr := this.GetString("echostr")
  26. method := this.Ctx.Input.Method()
  27. type Notify struct {
  28. ToUserName string `xml:"ToUserName"`
  29. FromUserName string `xml:"FromUserName"`
  30. CreateTime int `xml:"CreateTime"`
  31. MsgType string `xml:"MsgType"`
  32. Event string `xml:"Event"`
  33. EventKey string `xml:"EventKey"`
  34. Content string `xml:"Content"`
  35. }
  36. if method == "POST" {
  37. body := this.Ctx.Input.RequestBody
  38. utils.FileLog.Info("wechat notify:" + string(body))
  39. item := new(Notify)
  40. err := xml.Unmarshal(body, &item)
  41. if err != nil {
  42. utils.FileLog.Info("xml.Unmarshal:" + err.Error())
  43. }
  44. contactMsg := "感谢关注东吴期货研究所\r\n公司地址:上海市黄浦区西藏南路1208号东吴证券大厦19楼\r\n\r\n业务合作:\r\n电话:021-6312 3065\r\n邮箱:lvan@dwqh88.com\r\n邮编:200001"
  45. var openId, returnResult string
  46. if item.MsgType != "" {
  47. openId = item.FromUserName
  48. }
  49. xmlTpl := `<xml>
  50. <ToUserName><![CDATA[%s]]></ToUserName>
  51. <FromUserName><![CDATA[%s]]></FromUserName>
  52. <CreateTime>%s</CreateTime>
  53. <MsgType><![CDATA[text]]></MsgType>
  54. <Content><![CDATA[%s]]></Content>
  55. </xml>`
  56. createTime := strconv.FormatInt(time.Now().Unix(), 10)
  57. WxId := "gh_5dc508325c6f" // 弘则投研公众号原始id
  58. xmlTpl = fmt.Sprintf(xmlTpl, openId, WxId, createTime, contactMsg)
  59. if item.MsgType == "event" {
  60. switch item.Event {
  61. case "subscribe":
  62. fmt.Println("关注")
  63. go subscribe(openId)
  64. case "unsubscribe":
  65. fmt.Println("取消关注")
  66. go unsubscribe(openId)
  67. case "CLICK":
  68. returnResult = xmlTpl
  69. default:
  70. utils.FileLog.Info("wechat notify event:" + item.Event)
  71. }
  72. this.Ctx.WriteString(xmlTpl)
  73. } else {
  74. returnResult = xmlTpl
  75. }
  76. this.Ctx.WriteString(returnResult)
  77. } else {
  78. this.Ctx.WriteString(echostr)
  79. }
  80. }
  81. // subscribe 关注后的处理逻辑
  82. func subscribe(openId string) {
  83. userRecord, err := models.GetUserRecordByOpenId(openId)
  84. if err != nil && err.Error() != utils.ErrNoRow() {
  85. fmt.Println("通过openid获取user_record记录失败,err:" + err.Error())
  86. return
  87. }
  88. err = nil
  89. // openId已存在
  90. if userRecord != nil {
  91. if userRecord.UserId > 0 && userRecord.UnionId != "" { //已经绑定了的话,那么就去修改用户状态
  92. models.UserSubscribe(1, openId)
  93. } else {
  94. // 没有绑定的话,那么校验下unionid,然后再去修改
  95. unionId := userRecord.UnionId
  96. if unionId == `` {
  97. wxUserItem, err := wechat.GetUserInfo(openId)
  98. if err != nil {
  99. fmt.Println("获取用户信息失败,err:" + err.Error())
  100. return
  101. }
  102. if wxUserItem.UnionID != `` {
  103. unionId = wxUserItem.UnionID
  104. }
  105. }
  106. userRecord.UnionId = unionId
  107. userRecord.Subscribe = 1
  108. userRecord.SubscribeTime = time.Now()
  109. err = userRecord.Update([]string{"union_id", "subscribe", "subscribe_time"})
  110. if err != nil {
  111. fmt.Println("关注后,通过openid更新user_record异常,err:", err)
  112. return
  113. }
  114. user, err := models.GetUserByUnionId(unionId)
  115. if err != nil {
  116. fmt.Println("关注后,通过unionid获取user记录失败,err:" + err.Error())
  117. return
  118. }
  119. if user != nil {
  120. user.IsSubscribed = true
  121. user.ModifyTime = time.Now()
  122. err = user.Update([]string{"modify_time", "is_subscribed"})
  123. if err != nil {
  124. fmt.Println("关注后,修改绑定用户状态异常,err:", err)
  125. return
  126. }
  127. userRecord.UserId = user.UserId
  128. userRecord.Update([]string{"user_id"})
  129. }
  130. }
  131. return
  132. }
  133. // 没有记录,那么需要获取下unionid
  134. wxUserItem, err := wechat.GetUserInfo(openId)
  135. if err != nil {
  136. fmt.Println("获取用户信息失败,err:" + err.Error())
  137. return
  138. }
  139. newUserRecord := &models.UserRecord{
  140. OpenId: openId,
  141. UnionId: wxUserItem.UnionID,
  142. Subscribe: 1,
  143. SubscribeTime: time.Now(),
  144. NickName: wxUserItem.Nickname,
  145. Sex: int(wxUserItem.Sex),
  146. Province: wxUserItem.Province,
  147. City: wxUserItem.City,
  148. Country: wxUserItem.Country,
  149. Headimgurl: wxUserItem.Headimgurl,
  150. CreateTime: time.Now(),
  151. }
  152. insertId, err := newUserRecord.Insert()
  153. newUserRecord.UserRecordId = int(insertId)
  154. if err != nil {
  155. fmt.Println("关注后,添加user_record信息失败,err:" + err.Error())
  156. return
  157. }
  158. // 检测用户是否绑定小程序,如果绑定修改用户状态
  159. user, err := models.GetUserByUnionId(newUserRecord.UnionId)
  160. if err != nil {
  161. fmt.Println("修改绑定用户状态异常,ERR:", err)
  162. return
  163. }
  164. if user != nil {
  165. user.IsRegistered = true
  166. user.ModifyTime = time.Now()
  167. err = user.Update([]string{"is_registered", "modify_time"})
  168. if err != nil {
  169. fmt.Println("关注后,修改绑定用户状态异常,ERR:", err)
  170. return
  171. }
  172. newUserRecord.UserId = user.UserId
  173. newUserRecord.Update([]string{"user_id"})
  174. }
  175. }
  176. // unsubscribe 取消关注后的处理逻辑
  177. func unsubscribe(openId string) {
  178. userRecord, err := models.GetUserRecordByOpenId(openId)
  179. if err != nil {
  180. return
  181. }
  182. userRecord.Subscribe = 0
  183. err = userRecord.Update([]string{"subscribe"})
  184. if err != nil {
  185. return
  186. }
  187. if userRecord.UnionId != "" {
  188. user, err := models.GetUserByUnionId(userRecord.UnionId)
  189. if err != nil {
  190. return
  191. }
  192. if user != nil {
  193. user.IsSubscribed = false
  194. user.ModifyTime = time.Now()
  195. err = user.Update([]string{"is_subscribed", "modify_time"})
  196. if err != nil {
  197. return
  198. }
  199. }
  200. }
  201. }
  202. // @Title 微信登录
  203. // @Description 微信登录
  204. // @Param request body models.LoginReq true "type json string"
  205. // @Success 200 {object} models.LoginResp
  206. // @router /login [post]
  207. func (this *WechatController) Login() {
  208. br := new(models.BaseResponse).Init()
  209. defer func() {
  210. if err := recover(); err != nil {
  211. fmt.Println(err)
  212. }
  213. this.Data["json"] = br
  214. this.ServeJSON()
  215. }()
  216. var req request.WeChatLoginReq
  217. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  218. if err != nil {
  219. br.Msg = "参数解析失败"
  220. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  221. return
  222. }
  223. if req.Code == "" {
  224. br.Msg = "授权码不存在"
  225. return
  226. }
  227. userInfo, err := wx_app.GetSession(req.Code)
  228. if err != nil {
  229. br.Msg = "登录失败,请重新尝试"
  230. br.ErrMsg = "用户信息获取失败,系统错误,Err:" + err.Error()
  231. return
  232. }
  233. session, err := models.GetWxSessionByOpenId(userInfo.OpenID)
  234. if err != nil && err.Error() != utils.ErrNoRow() {
  235. br.Msg = "登录失败,请重新尝试"
  236. br.ErrMsg = "用户信息获取失败,系统错误,Err:" + err.Error()
  237. return
  238. }
  239. if session == nil {
  240. session = &models.WxSession{
  241. OpenId: userInfo.OpenID,
  242. UnionId: userInfo.UnionID,
  243. CreateTime: time.Now(),
  244. }
  245. insertId, er := session.Insert()
  246. session.WxSessionId = int(insertId)
  247. if er != nil {
  248. br.Msg = "用户登录失败"
  249. br.ErrMsg = "用户登录获取失败,系统错误,Err:" + er.Error()
  250. return
  251. }
  252. }
  253. var token string
  254. timeUnix := time.Now().Unix()
  255. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  256. token = utils.MD5(session.OpenId) + utils.MD5(timeUnixStr)
  257. session.AccessToken = token
  258. session.LastUpdateTime = time.Now()
  259. err = session.Update([]string{"access_token", "last_update_time"})
  260. if err != nil {
  261. br.Msg = "微信登录失败"
  262. br.ErrMsg = "微信登录失败,更新用户信息失败:" + err.Error()
  263. return
  264. }
  265. token = session.AccessToken
  266. resp := new(response.WeChatLoginResp)
  267. resp.Authorization = token
  268. br.Data = resp
  269. br.Msg = "登录成功"
  270. br.Success = true
  271. br.Ret = 200
  272. }
  273. // @Title 公众号绑定
  274. // @Description 公众号绑定
  275. // @Param request body request.WeChatLoginReq true "type json string"
  276. // @Success 200 {object} models.LoginResp
  277. // @router /subscribe [post]
  278. func (this *WechatController) Subscribe() {
  279. br := new(models.BaseResponse).Init()
  280. defer func() {
  281. if br.Ret != 200 {
  282. b, _ := json.Marshal(br)
  283. alarm_msg.SendAlarmMsg(string(b), 1)
  284. }
  285. this.Data["json"] = br
  286. this.ServeJSON()
  287. }()
  288. var req request.WeChatLoginReq
  289. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  290. if err != nil {
  291. br.Msg = "参数解析失败"
  292. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  293. return
  294. }
  295. if req.Code == "" {
  296. br.Msg = "授权码不存在"
  297. return
  298. }
  299. info, err := wechat.GetWxUserInfo(req.Code)
  300. if err != nil {
  301. br.Msg = "获取失败"
  302. br.ErrMsg = "获取失败,Err:" + err.Error()
  303. return
  304. }
  305. if info.ErrCode != 0 {
  306. br.Msg = "获取失败"
  307. br.ErrMsg = "获取失败,Err:" + info.ErrMsg
  308. return
  309. }
  310. userRecord, err := models.GetUserRecordByOpenId(info.OpenId)
  311. if err != nil && err.Error() != utils.ErrNoRow() {
  312. br.Msg = "获取用户关注记录失败"
  313. br.ErrMsg = "获取用户关注记录失败,Err:" + err.Error()
  314. return
  315. }
  316. // 如果不存在就新增一条记录
  317. if userRecord == nil {
  318. userRecord := &models.UserRecord{
  319. OpenId: info.OpenId,
  320. }
  321. insertId, err := userRecord.Insert()
  322. if err != nil {
  323. br.Msg = "新增失败"
  324. br.ErrMsg = "新增失败,Err:" + err.Error()
  325. return
  326. }
  327. userRecord.UserRecordId = int(insertId)
  328. }
  329. if userRecord.UnionId == "" {
  330. wxInfo, er := wechat.GetUserInfo(userRecord.OpenId)
  331. if er != nil {
  332. br.Msg = "获取失败"
  333. br.ErrMsg = "获取失败,Err:" + er.Error()
  334. return
  335. }
  336. userRecord.UnionId = wxInfo.UnionID
  337. er = userRecord.Update([]string{"union_id"})
  338. if er != nil {
  339. br.Msg = "获取失败"
  340. br.ErrMsg = "获取失败,Err:" + er.Error()
  341. return
  342. }
  343. }
  344. user, err := models.GetUserByUnionId(userRecord.UnionId)
  345. if err != nil && err.Error() != utils.ErrNoRow() {
  346. br.Msg = "获取用户信息失败"
  347. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  348. return
  349. }
  350. if user != nil {
  351. user.IsSubscribed = true
  352. err := user.Update([]string{"is_subscribed"})
  353. if err != nil {
  354. br.Msg = "更新用户信息失败"
  355. br.ErrMsg = "更新用户信息失败,Err:" + err.Error()
  356. return
  357. }
  358. userRecord.UserId = user.UserId
  359. userRecord.Update([]string{"user_id"})
  360. }
  361. br.Msg = "获取成功"
  362. br.Success = true
  363. br.Ret = 200
  364. }