user.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. package user
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/gin-gonic/gin"
  6. "github.com/silenceper/wechat/v2/miniprogram/auth"
  7. admin2 "hongze/hongze_yb/models/tables/admin"
  8. "hongze/hongze_yb/models/tables/rddp/session"
  9. "hongze/hongze_yb/models/tables/user_record"
  10. "hongze/hongze_yb/models/tables/wx_user"
  11. "hongze/hongze_yb/models/tables/wx_user_log"
  12. "hongze/hongze_yb/utils"
  13. "strconv"
  14. "time"
  15. )
  16. var ERR_NO_USER_RECORD = errors.New("用户关系没有入库")
  17. var ERR_USER_NOT_BIND = errors.New("用户没有绑定")
  18. type UserInfo struct {
  19. wx_user.WxUser
  20. RecordInfo *user_record.UserRecord
  21. }
  22. // GetWxUserItemByOpenId 通过openid获取用户信息
  23. func GetWxUserItemByOpenId(openid string) (userInfo UserInfo, err error) {
  24. //通过openid获取用户关联信息
  25. userRecord, userRecordErr := user_record.GetByOpenID(openid)
  26. if userRecordErr != nil {
  27. if userRecordErr == utils.ErrNoRow {
  28. err = ERR_NO_USER_RECORD
  29. return
  30. } else {
  31. err = userRecordErr
  32. return
  33. }
  34. }
  35. //该openid在系统中没有关联关系
  36. if userRecord == nil {
  37. err = ERR_NO_USER_RECORD
  38. return
  39. }
  40. //该openid没有绑定用户
  41. if userRecord.UserID <= 0 {
  42. err = ERR_USER_NOT_BIND
  43. item := new(wx_user.WxUser)
  44. //格式化返回用户数据
  45. userInfo = formatWxUserAndUserRecord(item, userRecord)
  46. return
  47. }
  48. //获取用户信息
  49. item, wxUserErr := wx_user.GetByUserId(userRecord.UserID)
  50. if wxUserErr != nil {
  51. err = wxUserErr
  52. //如果是找不到数据,那么可能是该用户被删除了,但是user_record没有删除对应的关系
  53. if wxUserErr == utils.ErrNoRow {
  54. //用户被删除了,但是user_record没有删除对应的关系,那么去解除绑定
  55. userUnbindErr := user_record.UnBindUserRecordByOpenid(openid)
  56. if userUnbindErr != nil {
  57. err = userUnbindErr
  58. return
  59. }
  60. //返回状态为 用户未绑定 逻辑代码
  61. err = ERR_USER_NOT_BIND
  62. item := new(wx_user.WxUser)
  63. //格式化返回用户数据
  64. userInfo = formatWxUserAndUserRecord(item, userRecord)
  65. return
  66. }
  67. return
  68. }
  69. //格式化返回用户数据
  70. userInfo = formatWxUserAndUserRecord(item, userRecord)
  71. return
  72. }
  73. // GetWxUserItemByUserId 根据用户id和平台id获取用户信息
  74. func GetWxUserItemByUserId(userId, platform int) (userInfo UserInfo, err error) {
  75. //获取用户信息
  76. wxUser, err := wx_user.GetByUserId(userId)
  77. if err != nil {
  78. return
  79. }
  80. //格式化返回用户数据
  81. userInfo = formatWxUser(wxUser, platform)
  82. return
  83. }
  84. // GetWxUserItemByEmail 根据用户邮箱和平台id获取用户信息
  85. func GetWxUserItemByEmail(email string, platform int) (userInfo UserInfo, err error) {
  86. //获取用户信息
  87. wxUser, err := wx_user.GetByEmail(email)
  88. if err != nil {
  89. return
  90. }
  91. //格式化返回用户数据
  92. userInfo = formatWxUser(wxUser, platform)
  93. return
  94. }
  95. // GetWxUserItemByMobile 根据用户手机号和平台id获取用户信息
  96. func GetWxUserItemByMobile(mobile string, platform int) (userInfo UserInfo, err error) {
  97. //获取用户信息
  98. wxUser, err := wx_user.GetByMobile(mobile)
  99. if err != nil {
  100. return
  101. }
  102. //格式化返回用户数据
  103. userInfo = formatWxUser(wxUser, platform)
  104. return
  105. }
  106. // GetWxUserItemByUnionId 根据用户unionid和平台id获取用户信息
  107. func GetWxUserItemByUnionId(unionId string, platform int) (userInfo UserInfo, err error) {
  108. // 获取用户信息
  109. userRecord, userRecordErr := user_record.GetByUnionID(unionId, platform)
  110. if userRecordErr != nil {
  111. if userRecordErr == utils.ErrNoRow {
  112. err = ERR_NO_USER_RECORD
  113. return
  114. } else {
  115. err = userRecordErr
  116. return
  117. }
  118. }
  119. // 该union在系统中没有关联关系
  120. if userRecord == nil {
  121. err = ERR_NO_USER_RECORD
  122. return
  123. }
  124. // 该openid没有绑定用户
  125. if userRecord.UserID <= 0 {
  126. err = ERR_USER_NOT_BIND
  127. item := new(wx_user.WxUser)
  128. //格式化返回用户数据
  129. userInfo = formatWxUserAndUserRecord(item, userRecord)
  130. return
  131. }
  132. item, wxUserErr := wx_user.GetByUserId(userRecord.UserID)
  133. if wxUserErr != nil {
  134. err = wxUserErr
  135. // 如果是找不到数据,那么可能是该用户被删除了,但是user_record没有删除对应的关系
  136. if wxUserErr == utils.ErrNoRow {
  137. // 用户被删除了,但是user_record没有删除对应的关系,那么去解除绑定
  138. userUnbindErr := user_record.UnBindUserRecordByUnionId(unionId, platform)
  139. if userUnbindErr != nil {
  140. err = userUnbindErr
  141. return
  142. }
  143. // 返回状态为用户未绑定
  144. err = ERR_USER_NOT_BIND
  145. item := new(wx_user.WxUser)
  146. // 格式化返回用户数据
  147. userInfo = formatWxUserAndUserRecord(item, userRecord)
  148. return
  149. }
  150. return
  151. }
  152. // 格式化返回用户数据
  153. userInfo = formatWxUserAndUserRecord(item, userRecord)
  154. return
  155. }
  156. // formatWxUserAndUserRecord 通过用户 关系表记录 和 用户记录 格式化返回 用户数据
  157. func formatWxUserAndUserRecord(wxUser *wx_user.WxUser, userRecord *user_record.UserRecord) (userInfo UserInfo) {
  158. wxUser.OpenID = userRecord.OpenID
  159. wxUser.UnionID = userRecord.UnionID
  160. wxUser.NickName = userRecord.NickName
  161. //wxUser.RealName = userRecord.RealName
  162. //wxUser.BindAccount = userRecord.BindAccount
  163. wxUser.Headimgurl = userRecord.Headimgurl
  164. wxUserJson, _ := json.Marshal(wxUser)
  165. _ = json.Unmarshal(wxUserJson, &userInfo)
  166. userInfo.RecordInfo = userRecord
  167. return
  168. }
  169. // formatWxUser 通过用户 用户记录 和 来源平台 格式化返回 用户数据
  170. func formatWxUser(wxUser *wx_user.WxUser, platform int) (userInfo UserInfo) {
  171. //根据用户id和平台id获取用户关系
  172. userRecord, userRecordErr := user_record.GetByUserId(int(wxUser.UserID), platform)
  173. if userRecordErr != nil {
  174. if userRecordErr != utils.ErrNoRow {
  175. return
  176. }
  177. }
  178. //该openid在系统中没有关联关系
  179. if userRecord == nil {
  180. wxUserJson, _ := json.Marshal(wxUser)
  181. _ = json.Unmarshal(wxUserJson, &userInfo)
  182. return
  183. } else {
  184. userInfo = formatWxUserAndUserRecord(wxUser, userRecord)
  185. }
  186. return
  187. }
  188. // WxLogin 微信登录
  189. func WxLogin(wxPlatform int, wxSession auth.ResCode2Session) (token string, userId int, isBind bool, err error) {
  190. openId := wxSession.OpenID
  191. unionId := wxSession.UnionID
  192. sessionKey := wxSession.SessionKey
  193. needUpdateSessionKey := true //是否更新sessionKey
  194. QUERY_WX_USER:
  195. wxUser, wxUserErr := GetWxUserItemByOpenId(openId)
  196. if wxUserErr == ERR_NO_USER_RECORD { //没有用户openid记录
  197. _, recordErr := AddUserRecord(openId, unionId, "", "", "", "", "", "", sessionKey, wxPlatform, 0, 0)
  198. //如果插入失败,那么直接将错误信息返回
  199. if recordErr != nil {
  200. err = recordErr
  201. return
  202. }
  203. needUpdateSessionKey = false //因为是新增用户微信信息,所以不需要更新sessionKey的数据了
  204. //插入成功后,需要重新查询该用户,并进入下面的逻辑
  205. goto QUERY_WX_USER
  206. } else if wxUserErr == ERR_USER_NOT_BIND {
  207. // 未绑定则去查询是否为弘则研究公众号用户,有相应的手机号邮箱信息则自动绑定
  208. platformUser, platformErr := GetWxUserItemByUnionId(unionId, 1)
  209. if platformErr == nil {
  210. // 当公众号用户存在时
  211. if platformUser.Mobile != "" || platformUser.Email != "" {
  212. // 有手机号或邮箱则绑定信息则自动绑定并新增wx_user
  213. countryCode := 0
  214. if platformUser.CountryCode != "" {
  215. countryCode, _ = strconv.Atoi(platformUser.CountryCode)
  216. }
  217. _, _, tempErr, errMsg := BindWxUser(openId, platformUser.Mobile, platformUser.Email, "", 3, countryCode, 1)
  218. if tempErr != nil {
  219. err = errors.New("自动绑定公众号用户失败" + errMsg)
  220. return
  221. }
  222. isBind = true
  223. }
  224. }
  225. } else if wxUserErr != nil {
  226. err = wxUserErr
  227. return
  228. }
  229. //更新微信用户的sessionKey
  230. if needUpdateSessionKey {
  231. _ = user_record.ModifySessionKeyByOpenid(openId, sessionKey)
  232. }
  233. // 如果已经登录注册绑定的情况下/或者首次登录且为弘则研究公众号用户
  234. if wxUserErr == nil {
  235. userId = int(wxUser.UserID)
  236. // 如果账户有绑定了手机号或者邮箱,那么标记为已绑定
  237. if wxUser.Mobile != "" || wxUser.Email != "" {
  238. isBind = true
  239. }
  240. }
  241. //获取登录token
  242. tokenItem, tokenErr := session.GetTokenByOpenId(openId)
  243. if tokenErr != nil && tokenErr != utils.ErrNoRow {
  244. err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
  245. return
  246. }
  247. if tokenErr != nil && tokenErr == utils.ErrNoRow {
  248. timeUnix := time.Now().Unix()
  249. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  250. token = utils.MD5(openId) + utils.MD5(timeUnixStr)
  251. //新增session
  252. {
  253. sessionItem := &session.Session{
  254. OpenID: openId,
  255. UserID: int64(userId),
  256. CreatedTime: time.Now(),
  257. LastUpdatedTime: time.Now(),
  258. ExpireTime: time.Now().AddDate(0, 3, 0),
  259. AccessToken: token,
  260. }
  261. sessionErr := sessionItem.Create()
  262. if err != nil {
  263. err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
  264. return
  265. }
  266. }
  267. } else {
  268. token = tokenItem.AccessToken
  269. //如果联系人编号不为空,且联系人编号与session里面的联系人编号不一致的时候,需要做session变更
  270. if userId > 0 && tokenItem.UserID != int64(userId) {
  271. _ = tokenItem.UpdateSession(int64(userId), time.Now().AddDate(0, 1, 0))
  272. }
  273. }
  274. //新增登录日志
  275. {
  276. loginLog := &wx_user_log.WxUserLog{
  277. UserID: userId,
  278. OpenID: openId,
  279. UnionID: unionId,
  280. CreateTime: time.Now(),
  281. Handle: "yb_login",
  282. Remark: token,
  283. }
  284. go loginLog.Create()
  285. }
  286. return
  287. }
  288. // AddUserRecord 添加第三方用户(微信)记录
  289. func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (userRecordInfo *user_record.UserRecord, err error) {
  290. find, err := user_record.GetByOpenID(openId)
  291. if err == nil {
  292. userRecordInfo = find
  293. return
  294. }
  295. if err != utils.ErrNoRow {
  296. return
  297. }
  298. userRecordInfo = &user_record.UserRecord{
  299. OpenID: openId, //用户open_id
  300. UnionID: unionId, //用户union_id
  301. Subscribe: int8(subscribe),
  302. NickName: nickName, //用户昵称,最大长度:32
  303. RealName: realName, //用户实际名称,最大长度:32
  304. Sex: int64(sex), //普通用户性别,1为男性,2为女性
  305. Province: province, //普通用户个人资料填写的省份,最大长度:30
  306. City: city, //普通用户个人资料填写的城市,最大长度:30
  307. Country: country, //国家,如中国为CN,最大长度:30
  308. Headimgurl: headimgurl, //用户第三方(微信)头像,最大长度:512
  309. CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
  310. CreatePlatform: int8(platform), //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
  311. SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255
  312. }
  313. err = userRecordInfo.Create()
  314. return
  315. }
  316. // GetInfoByClaims 从Claims中获取用户信息
  317. func GetInfoByClaims(c *gin.Context) (userInfo UserInfo) {
  318. //获取jwt数据失败
  319. claims, _ := c.Get("userInfo")
  320. userInfo = claims.(UserInfo)
  321. return
  322. }
  323. // GetAdminByUserId 判断当前用户是否为内部人员
  324. func GetAdminByUserInfo(userInfo UserInfo) (ok bool, adminInfo *admin2.Admin, err error) {
  325. mobile := userInfo.Mobile
  326. if mobile == "" {
  327. err = errors.New("用户手机号信息有误")
  328. return
  329. }
  330. adminInfo, err = admin2.GetAdminByMobile(mobile)
  331. if err != nil {
  332. if err == utils.ErrNoRow {
  333. err = nil
  334. return
  335. }
  336. return
  337. }
  338. ok = true
  339. return
  340. }