wechat.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "hongze/hongze_mfyx_gzh/models"
  7. "hongze/hongze_mfyx_gzh/services"
  8. "hongze/hongze_mfyx_gzh/utils"
  9. "strconv"
  10. "time"
  11. )
  12. type WechatController struct {
  13. BaseAuthController
  14. }
  15. type WechatCommonController struct {
  16. BaseCommonController
  17. }
  18. // @Title 微信登录公众号接口
  19. // @Description 微信登录公众号接口
  20. // @Param Code query string true "微信唯一编码code"
  21. // @Success 200 {object} models.UserDetailByUserLogin
  22. // @router /loginByGzh [get]
  23. func (this *WechatCommonController) WechatLoginByGzh() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. code := this.GetString("Code")
  30. if code == "" {
  31. br.Msg = "参数错误"
  32. br.ErrMsg = "Code 为空"
  33. return
  34. }
  35. var token string
  36. item, err := services.WxGetUserOpenIdByCodeGzh(code)
  37. if err != nil {
  38. br.Msg = "获取用户信息失败"
  39. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  40. return
  41. }
  42. if item.Errcode != 0 {
  43. br.Msg = "获取用户信息失败"
  44. br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg + "appid:" + utils.WxPublicAppId + "secret:" + utils.WxPublicAppSecret
  45. return
  46. }
  47. openId := item.Openid
  48. if openId == "" {
  49. br.Msg = "获取用户信息失败"
  50. br.ErrMsg = "获取openid失败,openid:" + item.Openid
  51. return
  52. }
  53. resp := new(models.UserDetailByUserLogin)
  54. accessToken, err := services.GetWxAccessTokenByGzh()
  55. if err != nil {
  56. br.Msg = "获取用户信息失败"
  57. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  58. return
  59. }
  60. if accessToken == "" {
  61. br.Msg = "获取用户信息失败"
  62. br.ErrMsg = "access_token 为空,"
  63. return
  64. }
  65. wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  66. if err != nil {
  67. br.Msg = "获取用户信息失败"
  68. br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
  69. return
  70. }
  71. if wxUserInfo.Errcode != 0 {
  72. userInfoJson, _ := json.Marshal(wxUserInfo)
  73. br.Msg = "登录失败"
  74. br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson)
  75. return
  76. }
  77. unionId := wxUserInfo.Unionid
  78. if unionId == "" {
  79. br.Msg = "获取用户信息失败"
  80. br.ErrMsg = "获取unionid失败,unionid:" + wxUserInfo.Unionid
  81. return
  82. }
  83. mfyxUserRecord, err := models.GetCygxUserRecordByOpenid(openId)
  84. if err != nil && err.Error() != utils.ErrNoRow() {
  85. br.Msg = "获取用户信息失败"
  86. br.ErrMsg = "查询数量失败,Err:" + err.Error()
  87. return
  88. }
  89. user, err := models.GetWxUserItemByUserUnionId(unionId)
  90. if err != nil && err.Error() != utils.ErrNoRow() {
  91. br.Msg = "获取用户信息失败"
  92. br.ErrMsg = "获取本地用户信息失败,Err:" + err.Error()
  93. return
  94. }
  95. items := new(models.CygxMfyxGzhUserRecord)
  96. items.OpenId = openId
  97. items.UnionId = unionId
  98. items.NickName = wxUserInfo.Nickname
  99. items.Sex = wxUserInfo.Sex
  100. items.Province = wxUserInfo.Province
  101. items.City = wxUserInfo.City
  102. items.Country = wxUserInfo.Country
  103. items.Headimgurl = wxUserInfo.Headimgurl
  104. items.CreateTime = time.Now()
  105. items.Subscribe = 1
  106. items.SubscribeTime = time.Now()
  107. if user != nil {
  108. items.CygxUserId = user.UserId
  109. items.CygxBindAccount = user.Mobile
  110. if mfyxUserRecord != nil && mfyxUserRecord.CygxUserId != user.UserId {
  111. err = models.UpdateCygxUserRecordMobile(user.UserId, user.Mobile, unionId)
  112. if err != nil {
  113. br.Msg = "修复用户信息失败"
  114. br.ErrMsg = "修复用户信息失败,Err:" + err.Error()
  115. return
  116. }
  117. }
  118. }
  119. userRecord := &models.UserRecord{
  120. OpenId: openId, //用户open_id
  121. UnionId: unionId, //用户union_id
  122. Subscribe: 0,
  123. NickName: wxUserInfo.Nickname, //用户昵称,最大长度:32
  124. RealName: "", //用户实际名称,最大长度:32
  125. Sex: wxUserInfo.Sex, //普通用户性别,1为男性,2为女性
  126. Province: wxUserInfo.Province, //普通用户个人资料填写的省份,最大长度:30
  127. City: wxUserInfo.City, //普通用户个人资料填写的城市,最大长度:30
  128. Country: wxUserInfo.Country, //国家,如中国为CN,最大长度:30
  129. Headimgurl: wxUserInfo.Headimgurl, //用户第三方(微信)头像,最大长度:512
  130. CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
  131. CreatePlatform: 12, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
  132. SessionKey: wxUserInfo.SessionKey, //微信小程序会话密钥,最大长度:255
  133. }
  134. if user != nil {
  135. userRecord.UserId = user.UserId
  136. }
  137. if mfyxUserRecord == nil {
  138. _, err = models.AddMfyxGzhUserRecord(items)
  139. if err != nil {
  140. br.Msg = "获取用户信息失败"
  141. br.ErrMsg = "添加openid失败,Err:" + err.Error()
  142. return
  143. }
  144. _, err = models.AddUserRecord(userRecord)
  145. if err != nil {
  146. return
  147. }
  148. }
  149. timeUnix := time.Now().Unix()
  150. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  151. totalItem, err := models.GetTokenByOpenId(openId)
  152. if err != nil && err.Error() != utils.ErrNoRow() {
  153. br.Msg = "获取用户信息失败"
  154. br.ErrMsg = "查询数量失败,Err:" + err.Error()
  155. return
  156. }
  157. if totalItem == nil {
  158. token := utils.MD5(unionId) + utils.MD5(timeUnixStr)
  159. itemsSession := new(models.CygxMfyxGzhSession)
  160. itemsSession.UnionId = unionId
  161. itemsSession.OpenId = openId
  162. itemsSession.AccessToken = token
  163. itemsSession.CreatedTime = time.Now()
  164. itemsSession.LastUpdatedTime = time.Now()
  165. itemsSession.ExpireTime = time.Now().AddDate(0, 3, 0)
  166. if user != nil {
  167. itemsSession.UserId = user.UserId
  168. }
  169. err = models.AddCygxXzsSession(itemsSession)
  170. if err != nil {
  171. br.Msg = "获取用户信息失败"
  172. br.ErrMsg = "添加Token失败,Err:" + err.Error()
  173. return
  174. }
  175. } else {
  176. token = totalItem.AccessToken
  177. }
  178. if user == nil {
  179. resp.HasPermission = 3
  180. } else {
  181. permissionStr, err := models.GetCompanyPermission(user.CompanyId)
  182. if err != nil {
  183. br.Msg = "获取信息失败"
  184. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  185. return
  186. }
  187. if permissionStr != "" {
  188. resp.Permission = permissionStr
  189. resp.Mobile = user.Mobile
  190. resp.RealName = user.RealName
  191. resp.CompanyName = user.CompanyName
  192. resp.HasPermission = 1
  193. } else {
  194. resp.Mobile = user.Mobile
  195. resp.RealName = user.RealName
  196. resp.HasPermission = 2
  197. }
  198. resp.Headimgurl = user.HeadimgurlRecord
  199. }
  200. if resp.Headimgurl == "" {
  201. resp.Headimgurl = utils.DefaultHeadimgurl
  202. }
  203. resp.Token = token
  204. br.Ret = 200
  205. br.Success = true
  206. br.Msg = "获取成功"
  207. br.Data = resp
  208. }
  209. // @Title 微信获取签名接口
  210. // @Description 微信获取签名接口
  211. // @Param Url query string true "url地址"
  212. // @Success 200 {object} models.WechatSign
  213. // @router /getWxSign [get]
  214. func (this *WechatCommonController) GetWxSign() {
  215. br := new(models.BaseResponse).Init()
  216. defer func() {
  217. this.Data["json"] = br
  218. this.ServeJSON()
  219. }()
  220. getUrl := this.GetString("Url")
  221. accessToken, err := services.GetWxAccessTokenByGzh()
  222. if err != nil {
  223. br.Msg = "获取用户信息失败"
  224. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  225. return
  226. }
  227. if accessToken == "" {
  228. br.Msg = "获取用户信息失败"
  229. br.ErrMsg = "access_token 为空,"
  230. return
  231. }
  232. ticket, err := services.GetWxTicket(accessToken)
  233. if err != nil {
  234. br.Msg = "获取Ticket失败,请联系客服"
  235. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  236. return
  237. }
  238. if ticket == "" {
  239. br.Msg = "获取Ticket失败,请联系客服"
  240. br.ErrMsg = "ticket为空" + ticket
  241. return
  242. }
  243. nonceStr := utils.GetRandStringNoSpecialChar(16)
  244. signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
  245. resp := new(models.WechatSign)
  246. resp.AppId = utils.WxPublicAppId
  247. resp.NonceStr = nonceString
  248. resp.Timestamp = timestamp
  249. resp.Url = getUrl
  250. resp.Signature = signature
  251. br.Ret = 200
  252. br.Success = true
  253. br.Msg = "获取签名成功"
  254. br.Data = resp
  255. }
  256. // @Title 获取用户详情
  257. // @Description 获取用户详情接口
  258. // @Success 200 {object} models.UserDetailByUserLogin
  259. // @router /user/detail [get]
  260. func (this *WechatController) UserInfo() {
  261. br := new(models.BaseResponse).Init()
  262. defer func() {
  263. this.Data["json"] = br
  264. this.ServeJSON()
  265. }()
  266. user := this.User
  267. if user == nil {
  268. br.Msg = "请登录"
  269. br.ErrMsg = "请登录,用户信息为空"
  270. br.Ret = 408
  271. return
  272. }
  273. uid := user.UserId
  274. var hasPermission int
  275. detail := new(models.UserDetail)
  276. if uid > 0 {
  277. var err error
  278. detail, err = models.GetUserDetailByUserId(uid)
  279. if err != nil && err.Error() != utils.ErrNoRow() {
  280. br.Msg = "获取信息失败"
  281. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  282. return
  283. }
  284. if detail == nil {
  285. br.Msg = "获取信息失败"
  286. br.ErrMsg = "获取信息失败,Err:用户不存在"
  287. return
  288. }
  289. userRecord, _ := models.GetUserRecordByUserId(uid, utils.WxPlatform)
  290. if userRecord != nil {
  291. detail.NickName = userRecord.NickName
  292. if detail.Headimgurl == "" {
  293. detail.Headimgurl = userRecord.Headimgurl
  294. }
  295. }
  296. if user.CompanyId > 1 {
  297. companyItem, err := models.GetCompanyDetailById(user.CompanyId)
  298. if err != nil && err.Error() != utils.ErrNoRow() {
  299. br.Msg = "获取信息失败"
  300. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  301. return
  302. }
  303. if companyItem != nil && companyItem.CompanyId > 1 {
  304. detail.CompanyName = companyItem.CompanyName
  305. detail.SellerName = companyItem.SellerName
  306. detail.SellerMobile = companyItem.Mobile
  307. // 获取用户所在公司剩余的点
  308. companyPointsNum, err := models.GetCompanyPoints(user.CompanyId)
  309. if err != nil && err.Error() != utils.ErrNoRow() {
  310. br.Msg = "获取信息失败"
  311. br.ErrMsg = "获取用户所在公司剩余的点失败,Err:" + err.Error()
  312. return
  313. }
  314. detail.CompanyPointsNum = companyPointsNum
  315. userYanxunaDetail, err := models.GetCygxUserYanxuanPermissionDetailByUserId(user.UserId)
  316. if err != nil && err.Error() != utils.ErrNoRow() {
  317. br.Msg = "获取信息失败"
  318. br.ErrMsg = "获取信息失败,GetCygxUserYanxuanPermissionDetailByUserId Err:" + err.Error()
  319. return
  320. }
  321. if userYanxunaDetail != nil {
  322. detail.PermissionStatus = userYanxunaDetail.Status
  323. detail.StartDate = userYanxunaDetail.StartDate
  324. detail.EndDate = userYanxunaDetail.EndDate
  325. detail.PermissionName = utils.CHART_PERMISSION_NAME_MF_YANXUAN
  326. } else {
  327. //查询研选的权限状态
  328. var condition string
  329. var pars []interface{}
  330. condition += " AND company_id = ? AND status IN ('正式','试用') AND chart_permission_id = ? ORDER BY company_report_permission_id DESC LIMIT 1 "
  331. pars = append(pars, user.CompanyId, utils.CHART_PERMISSION_ID_YANXUAN)
  332. companyReportPermissionDetail, err := models.GetCompanyReportPermissionDetailByCondition(condition, pars)
  333. if err != nil && err.Error() != utils.ErrNoRow() {
  334. br.Msg = "获取信息失败"
  335. br.ErrMsg = "获取用户所在公司剩余的点失败,Err:" + err.Error()
  336. return
  337. }
  338. companyProduct, err := models.GetCompanyProductDetail(user.CompanyId, 2)
  339. if err != nil {
  340. br.Msg = "获取信息失败"
  341. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  342. return
  343. }
  344. if companyReportPermissionDetail == nil || companyProduct.IsSuspend == 1 {
  345. hasPermission = 1
  346. } else {
  347. detail.PermissionStatus = companyReportPermissionDetail.Status
  348. detail.StartDate = companyReportPermissionDetail.StartDate
  349. detail.EndDate = companyReportPermissionDetail.EndDate
  350. }
  351. }
  352. } else {
  353. hasPermission = 1
  354. }
  355. } else {
  356. //判断是否已经申请过
  357. applyCount, err := models.GetApplyRecordCount(uid)
  358. if err != nil && err.Error() != utils.ErrNoRow() {
  359. br.Msg = "获取信息失败"
  360. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  361. return
  362. }
  363. if applyCount > 0 {
  364. hasPermission = 3
  365. } else {
  366. hasPermission = 2
  367. }
  368. detail.CompanyName = detail.Note
  369. }
  370. detail.HasPermission = hasPermission
  371. } else {
  372. ur, _ := models.GetCygxUserRecordByOpenid(user.OpenId)
  373. if ur != nil {
  374. detail.NickName = ur.NickName
  375. detail.Email = ur.CygxBindAccount
  376. detail.Mobile = ur.CygxBindAccount
  377. detail.NickName = ur.NickName
  378. detail.Headimgurl = ur.Headimgurl
  379. }
  380. hasPermission = 2
  381. detail.HasPermission = hasPermission
  382. }
  383. if detail.Headimgurl == "" {
  384. detail.Headimgurl = utils.DefaultHeadimgurl
  385. }
  386. br.Ret = 200
  387. br.Success = true
  388. br.Msg = "获取成功"
  389. br.Data = detail
  390. }
  391. // @Title 微信获取签名接口
  392. // @Description 微信获取签名接口
  393. // @Param Url query string true "url地址"
  394. // @Success 200 {object} models.WechatSign
  395. // @router /notify [get,post]
  396. func (this *WechatCommonController) Notify() {
  397. echostr := this.GetString("echostr")
  398. method := this.Ctx.Input.Method()
  399. //添加请求日志记录
  400. itemApiLog := new(models.CygxThreeApiLog)
  401. itemApiLog.CreateTime = time.Now()
  402. itemApiLog.Source = 1
  403. itemApiLog.Url = this.Ctx.Input.URI()
  404. itemApiLog.Body = ""
  405. if method == "POST" {
  406. body := this.Ctx.Input.RequestBody
  407. utils.FileLog.Info("wechat echostr:" + string(body))
  408. itemApiLog.Body = string(body)
  409. }
  410. go models.AddCygxThreeApiLog(itemApiLog)
  411. if method == "POST" {
  412. body := this.Ctx.Input.RequestBody
  413. item := new(models.Notify)
  414. err := xml.Unmarshal(body, &item)
  415. if err != nil {
  416. utils.FileLog.Info("xml.Unmarshal:" + err.Error())
  417. }
  418. contactMsg := "感谢关注【买方研选】机构投资者研究共享平台\n\r\n\r\n点击下方【登录】按钮,即可获取模版消息推送! "
  419. var openId, returnResult string
  420. if item.MsgType != "" {
  421. openId = item.FromUserName
  422. }
  423. xmlTpl := `<xml>
  424. <ToUserName><![CDATA[%s]]></ToUserName>
  425. <FromUserName><![CDATA[%s]]></FromUserName>
  426. <CreateTime>%s</CreateTime>
  427. <MsgType><![CDATA[text]]></MsgType>
  428. <Content><![CDATA[%s]]></Content>
  429. </xml>`
  430. createTime := strconv.FormatInt(time.Now().Unix(), 10)
  431. xmlTpl = fmt.Sprintf(xmlTpl, openId, utils.WxId, createTime, contactMsg)
  432. total, err := models.GetCygxUserRecordCount(openId)
  433. if err != nil {
  434. utils.FileLog.Info("GetCygxUserRecordCount:" + err.Error())
  435. }
  436. var unionId string
  437. if total == 0 {
  438. accessToken, err := services.GetWxAccessTokenByGzh()
  439. if err != nil {
  440. utils.FileLog.Info("accessToken:" + err.Error())
  441. }
  442. if accessToken == "" {
  443. utils.FileLog.Info("access_token 为空 openId:" + openId)
  444. }
  445. wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  446. if err != nil {
  447. utils.FileLog.Info("accessToken:" + err.Error())
  448. return
  449. }
  450. if wxUserInfo.Errcode != 0 {
  451. userInfoJson, _ := json.Marshal(wxUserInfo)
  452. utils.FileLog.Info("获取用户信息失败,err:" + string(userInfoJson))
  453. }
  454. unionId = wxUserInfo.Unionid
  455. if unionId != "" {
  456. services.AddCygxUserRecord(wxUserInfo)
  457. }
  458. } else {
  459. userRecordDetail, err := models.GetCygxUserRecordByOpenid(openId)
  460. if err != nil {
  461. utils.FileLog.Info("GetCygxUserRecordByOpenid:" + err.Error())
  462. return
  463. }
  464. unionId = userRecordDetail.UnionId
  465. }
  466. if unionId == "" {
  467. utils.FileLog.Info("获取unionid失败,openId:" + openId)
  468. return
  469. }
  470. wxUser, err := models.GetUserRecordByUnionId(unionId)
  471. if err != nil {
  472. utils.FileLog.Info("GetUserDetailBuOpenid:" + err.Error())
  473. }
  474. if wxUser == nil {
  475. utils.FileLog.Info("用户不存在openId:" + openId)
  476. return
  477. }
  478. if item.MsgType == "event" {
  479. switch item.Event {
  480. case "subscribe":
  481. fmt.Println("关注")
  482. go models.UserSubscribe(1, wxUser.UserId)
  483. go models.CygxUserSubscribe(1, unionId)
  484. break
  485. case "unsubscribe":
  486. fmt.Println("取消关注")
  487. go models.UserSubscribe(0, wxUser.UserId)
  488. go models.CygxUserSubscribe(0, unionId)
  489. break
  490. case "CLICK":
  491. returnResult = xmlTpl
  492. break
  493. default:
  494. utils.FileLog.Info("wechat notify event:" + item.Event)
  495. }
  496. this.Ctx.WriteString(xmlTpl)
  497. } else if item.MsgType == "text" {
  498. returnResult = xmlTpl
  499. this.Ctx.WriteString(returnResult)
  500. } else {
  501. returnResult = xmlTpl
  502. }
  503. this.Ctx.WriteString(returnResult)
  504. } else {
  505. this.Ctx.WriteString(echostr)
  506. }
  507. }
  508. //type DataOpenidArr struct {
  509. // Data OpenidArr `json:"data"`
  510. //}
  511. //
  512. //type OpenidArr struct {
  513. // Openid []string `json:"openid"`
  514. //}
  515. //
  516. //func init() {
  517. // access_token := "87_vfOIH2qwWI6cYG584J-SQklN5r5_HelQeLToq-ZNbb4LOl3SnxKSIH6qMfbljvOI9tTWTv0e9t2Gq41-u_WJhYyFzm7rH7bhRYaMhqaU713Wx9qhPfpbTb506GYXMLcAFAODC"
  518. // url := "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + access_token
  519. // method := "GET"
  520. // client := &http.Client{}
  521. // req, err := http.NewRequest(method, url, nil)
  522. //
  523. // if err != nil {
  524. // fmt.Println(err)
  525. // return
  526. // }
  527. // res, err := client.Do(req)
  528. // if err != nil {
  529. // fmt.Println(err)
  530. // return
  531. // }
  532. // defer res.Body.Close()
  533. //
  534. // body, err := ioutil.ReadAll(res.Body)
  535. // if err != nil {
  536. // fmt.Println(err)
  537. // return
  538. // }
  539. // item := new(DataOpenidArr)
  540. // err = json.Unmarshal(body, &item)
  541. // fmt.Println(len(item.Data.Openid))
  542. //
  543. // //var condition string
  544. // //var pars []interface{}
  545. // //
  546. // //condition = ` AND open_id IN (` + utils.GetOrmInReplace(len(item.Data.Openid)) + `) `
  547. // //pars = append(pars, item.Data.Openid)
  548. // //
  549. // //listUserRecord, err := models.GetUserRecordList(condition, pars)
  550. // //if err != nil {
  551. // // fmt.Println(err)
  552. // // return
  553. // //}
  554. // accessToken, err := services.GetWxAccessTokenByGzh()
  555. // for k, v := range item.Data.Openid {
  556. // //if k > 1 {
  557. // // return
  558. // //}
  559. //
  560. // fmt.Println(k, "___", v)
  561. // openId := v
  562. //
  563. // wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  564. // if err != nil {
  565. // utils.FileLog.Info("accessToken:" + err.Error())
  566. // return
  567. // }
  568. // SubscribeTime := time.Unix(int64(wxUserInfo.SubscribeTime), 0).Format(utils.FormatDateTime)
  569. // fmt.Println(SubscribeTime)
  570. // if wxUserInfo.Errcode != 0 {
  571. // userInfoJson, _ := json.Marshal(wxUserInfo)
  572. // fmt.Println("获取用户信息失败,err:" + string(userInfoJson))
  573. // }
  574. // unionId := wxUserInfo.Unionid
  575. // wxUser, err := models.GetUserRecordByUnionId(unionId)
  576. // if err != nil {
  577. // utils.FileLog.Info("GetUserDetailBuOpenid:" + err.Error())
  578. // }
  579. // if wxUser == nil {
  580. // continue
  581. // }
  582. // if wxUser.UserId == 0 {
  583. // continue
  584. // }
  585. //
  586. // items := new(models.CygxMfyxGzhUserRecord)
  587. // items.OpenId = openId
  588. // items.UnionId = unionId
  589. // items.NickName = wxUserInfo.Nickname
  590. // items.Sex = wxUserInfo.Sex
  591. // items.Province = wxUserInfo.Province
  592. // items.City = wxUserInfo.City
  593. // items.Country = wxUserInfo.Country
  594. // items.Headimgurl = wxUserInfo.Headimgurl
  595. // items.CreateTime = time.Now()
  596. // items.Subscribe = 1
  597. // items.SubscribeTime = time.Unix(int64(wxUserInfo.SubscribeTime), 0)
  598. // items.CygxUserId = wxUser.UserId
  599. // items.CygxBindAccount = wxUser.BindAccount
  600. //
  601. // total, err := models.GetCygxUserRecordCount(openId)
  602. // if total == 0 {
  603. // _, err = models.AddMfyxGzhUserRecord(items)
  604. // }
  605. //
  606. // userRecord := &models.UserRecord{
  607. // OpenId: openId, //用户open_id
  608. // UnionId: unionId, //用户union_id
  609. // Subscribe: 1,
  610. // NickName: wxUserInfo.Nickname, //用户昵称,最大长度:32
  611. // RealName: "", //用户实际名称,最大长度:32
  612. // Sex: wxUserInfo.Sex, //普通用户性别,1为男性,2为女性
  613. // Province: wxUserInfo.Province, //普通用户个人资料填写的省份,最大长度:30
  614. // City: wxUserInfo.City, //普通用户个人资料填写的城市,最大长度:30
  615. // Country: wxUserInfo.Country, //国家,如中国为CN,最大长度:30
  616. // Headimgurl: wxUserInfo.Headimgurl, //用户第三方(微信)头像,最大长度:512
  617. // CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
  618. // CreatePlatform: 12, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
  619. // SessionKey: wxUserInfo.SessionKey, //微信小程序会话密钥,最大长度:255
  620. // }
  621. // userRecord.UserId = wxUser.UserId
  622. // totalUserRecord, err := models.GetUserRecordCount(openId)
  623. // if totalUserRecord == 0 {
  624. // _, err = models.AddUserRecord(userRecord)
  625. // }
  626. //
  627. // fmt.Println("关注")
  628. // models.UserSubscribeInit(1, wxUser.UserId, SubscribeTime)
  629. // //go models.CygxUserSubscribe(1, unionId)
  630. // }
  631. //
  632. // fmt.Println("end")
  633. //}