user.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_api/models"
  6. "hongze/hongze_api/utils"
  7. "strconv"
  8. "time"
  9. )
  10. var ERR_NO_USER_RECORD = errors.New("用户关系没有入库")
  11. var ERR_USER_NOT_BIND = errors.New("用户没有绑定")
  12. //通过openid获取用户信息
  13. func GetWxUserItemByOpenId(openid string) (item *models.WxUserItem, err error) {
  14. //通过openid获取用户关联信息
  15. userRecord, userRecordErr := models.GetUserRecordByOpenId(openid)
  16. if userRecordErr != nil {
  17. if userRecordErr.Error() == utils.ErrNoRow() {
  18. err = ERR_NO_USER_RECORD
  19. return
  20. } else {
  21. err = userRecordErr
  22. return
  23. }
  24. }
  25. //该openid在系统中没有关联关系
  26. if userRecord == nil {
  27. err = ERR_NO_USER_RECORD
  28. return
  29. }
  30. //该openid没有绑定用户
  31. if userRecord.UserId <= 0 {
  32. err = ERR_USER_NOT_BIND
  33. item = new(models.WxUserItem)
  34. //格式化返回用户数据
  35. formatWxUserAndUserRecord(item, userRecord)
  36. return
  37. }
  38. //获取用户信息
  39. item, wxUserErr := models.GetWxUserItemByUserId(userRecord.UserId)
  40. if wxUserErr != nil {
  41. err = wxUserErr
  42. //如果是找不到数据,那么可能是该用户被删除了,但是user_record没有删除对应的关系
  43. if wxUserErr.Error() == utils.ErrNoRow() {
  44. //用户被删除了,但是user_record没有删除对应的关系,那么去解除绑定
  45. userUnbindErr := models.UnBindUserRecordByOpenid(openid)
  46. if userUnbindErr != nil {
  47. err = userUnbindErr
  48. return
  49. }
  50. //返回状态为 用户未绑定 逻辑代码
  51. err = ERR_USER_NOT_BIND
  52. item = new(models.WxUserItem)
  53. //格式化返回用户数据
  54. formatWxUserAndUserRecord(item, userRecord)
  55. return
  56. }
  57. return
  58. }
  59. //格式化返回用户数据
  60. formatWxUserAndUserRecord(item, userRecord)
  61. return
  62. }
  63. //根据用户id和平台id获取用户信息
  64. func GetWxUserItemByUserId(userId, platform int) (wxUserItem *models.WxUserItem, err error) {
  65. //获取用户信息
  66. wxUserItem, wxUserErr := models.GetWxUserItemByUserId(userId)
  67. if wxUserErr != nil {
  68. err = wxUserErr
  69. return
  70. }
  71. //格式化返回用户数据
  72. formatWxUser(wxUserItem, platform)
  73. return
  74. }
  75. //根据用户邮箱和平台id获取用户信息
  76. func GetWxUserItemByEmail(email string, platform int) (wxUserItem *models.WxUserItem, err error) {
  77. //获取用户信息
  78. wxUserItem, wxUserErr := models.GetWxUserItemByEmail(email)
  79. if wxUserErr != nil {
  80. err = wxUserErr
  81. return
  82. }
  83. //格式化返回用户数据
  84. formatWxUser(wxUserItem, platform)
  85. return
  86. }
  87. //根据用户手机号和平台id获取用户信息
  88. func GetWxUserItemByMobile(mobile string, platform int) (wxUserItem *models.WxUserItem, err error) {
  89. //获取用户信息
  90. wxUserItem, wxUserErr := models.GetWxUserItemByMobile(mobile)
  91. if wxUserErr != nil {
  92. err = wxUserErr
  93. return
  94. }
  95. //格式化返回用户数据
  96. formatWxUser(wxUserItem, platform)
  97. return
  98. }
  99. //根据用户unionid和平台id获取用户信息
  100. func GetWxUserItemByUnionId(unionId string, platform int) (wxUserItem *models.WxUserItem, err error) {
  101. //获取用户信息
  102. wxUserItem, wxUserErr := models.GetWxUserItemByUnionid(unionId)
  103. if wxUserErr != nil {
  104. err = wxUserErr
  105. return
  106. }
  107. //格式化返回用户数据
  108. formatWxUser(wxUserItem, platform)
  109. return
  110. }
  111. //通过用户 关系表记录 和 用户记录 格式化返回 用户数据
  112. func formatWxUserAndUserRecord(wxUser *models.WxUserItem, userRecord *models.UserRecord) {
  113. wxUser.OpenId = userRecord.OpenId
  114. wxUser.UnionId = userRecord.UnionId
  115. wxUser.NickName = userRecord.NickName
  116. //wxUser.RealName = userRecord.RealName
  117. //wxUser.BindAccount = userRecord.BindAccount
  118. wxUser.Headimgurl = userRecord.Headimgurl
  119. }
  120. //通过用户 用户记录 和 来源平台 格式化返回 用户数据
  121. func formatWxUser(wxUser *models.WxUserItem, platform int) {
  122. //根据用户id和平台id获取用户关系
  123. userRecord, userRecordErr := models.GetUserRecordByUserId(wxUser.UserId, platform)
  124. if userRecordErr != nil {
  125. if userRecordErr.Error() != utils.ErrNoRow() {
  126. return
  127. }
  128. if userRecordErr.Error() == utils.ErrNoRow() {
  129. return
  130. }
  131. }
  132. //该openid在系统中没有关联关系
  133. if userRecord == nil {
  134. return
  135. }
  136. wxUser.OpenId = userRecord.OpenId
  137. wxUser.UnionId = userRecord.UnionId
  138. wxUser.NickName = userRecord.NickName
  139. //wxUser.RealName = userRecord.RealName
  140. //wxUser.BindAccount = userRecord.BindAccount
  141. wxUser.Headimgurl = userRecord.Headimgurl
  142. return
  143. }
  144. //用户绑定
  145. func BindWxUser(openid, mobile, email string, areaNum, registerPlatform int) (wxUser *models.WxUserItem, err error) {
  146. source := 1 //绑定来源,1:微信端,2:pc网页端,3:查研观向小程序,4:每日咨询
  147. if mobile == "" && email == "" {
  148. err = errors.New("手机号或邮箱必填一个")
  149. return
  150. }
  151. var bindAccount string
  152. //根据手机号获取用户信息
  153. if mobile != "" {
  154. tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile)
  155. if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
  156. err = wxUserErr
  157. return
  158. }
  159. wxUser = tmpWxUser
  160. bindAccount = mobile
  161. }
  162. //根据邮箱获取用户信息
  163. if wxUser == nil && email != "" {
  164. tmpWxUser, wxUserErr := models.GetWxUserItemByEmail(email)
  165. if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
  166. err = wxUserErr
  167. return
  168. }
  169. wxUser = tmpWxUser
  170. bindAccount = email
  171. }
  172. //查询openid的第三方(微信)信息
  173. userRecord, err := models.GetUserRecordByOpenId(openid)
  174. if err != nil {
  175. return
  176. }
  177. var userId int
  178. //如果查询出来的用户是nil,那么需要新增用户
  179. if wxUser == nil {
  180. key := "bind_wx_user:mobile:" + mobile + ":email:" + email
  181. isHas := utils.Rc.IsExist(key)
  182. if isHas {
  183. err = errors.New("多次提交,请关闭页面重新进入")
  184. return
  185. }
  186. utils.Rc.SetNX(key, "ok", time.Second*300)
  187. user := &models.WxUser{
  188. CompanyId: 1,
  189. CreatedTime: time.Now(),
  190. FirstLogin: 1,
  191. Enabled: 1,
  192. RegisterPlatform: registerPlatform, //账号注册来源,注册平台,1:微信端,2:PC网页端
  193. RegisterTime: time.Now(),
  194. Mobile: mobile,
  195. Email: email,
  196. IsRegister: 1,
  197. Source: source,
  198. CountryCode: areaNum,
  199. OutboundMobile: mobile,
  200. OutboundCountryCode: areaNum,
  201. }
  202. tmpUserId, addUserErr := models.AddWxUser(user)
  203. //添加完成,清除缓存
  204. _ = utils.Rc.Delete(key)
  205. if addUserErr != nil {
  206. err = addUserErr
  207. return
  208. }
  209. user.UserId = int(tmpUserId)
  210. userId = int(tmpUserId)
  211. wxUser, err = models.GetWxUserItemByUserId(userId)
  212. } else {
  213. userId = wxUser.UserId
  214. }
  215. //如果存在该手机号/邮箱,那么需要校验
  216. if userRecord.UserId > 0 && userRecord.UserId != userId {
  217. err = errors.New(fmt.Sprint("用户已绑定其他账户,已绑定的用户编号:", userRecord.UserId, ",不允许重复绑定"))
  218. return
  219. }
  220. if userRecord.UserId == 0 {
  221. err = models.BindUserRecordByOpenid(userId, openid, bindAccount)
  222. if err != nil {
  223. return
  224. }
  225. userRecord.UserId = userId
  226. }
  227. //如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复
  228. if userRecord.NickName == "" {
  229. oldUserRecord, err := models.GetUserThirdRecordByUserId(userId)
  230. if err == nil && oldUserRecord != nil {
  231. //如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复
  232. if oldUserRecord.NickName != "" {
  233. _ = models.ModifyUserRecordInfo(userRecord.OpenId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, userId)
  234. }
  235. }
  236. }
  237. //如果该用户 绑定注册状态 字段处于 未注册 的情况下,那么去修改该数据
  238. if wxUser.IsRegister == 0 {
  239. err = models.ModifyWxUserRegisterStatus(userId, 1, source, time.Now())
  240. if err != nil {
  241. return
  242. }
  243. }
  244. //格式化用户数据
  245. formatWxUserAndUserRecord(wxUser, userRecord)
  246. return
  247. }
  248. //微信登录
  249. func WxLogin(wxPlatform int, code string, wxAccessToken *WxAccessToken, wxUserInfo *WxUserInfo) (token string, userId, firstLogin, permission int, err error) {
  250. openId := wxAccessToken.Openid
  251. unionId := wxAccessToken.Unionid
  252. if unionId == "" {
  253. unionId = wxUserInfo.Unionid
  254. }
  255. //firstLogin==1,强制绑定手机号或者邮箱
  256. firstLogin = 1
  257. QUERY_WX_USER:
  258. wxUser, wxUserErr := GetWxUserItemByOpenId(openId)
  259. if wxUserErr == ERR_NO_USER_RECORD { //没有用户openid记录
  260. _, recordErr := AddUserRecord(openId, unionId, wxUserInfo.Nickname, "", wxUserInfo.Province, wxUserInfo.City, wxUserInfo.Country, wxUserInfo.Headimgurl, "", wxPlatform, wxUserInfo.Sex, 0)
  261. //如果插入失败,那么直接将错误信息返回
  262. if recordErr != nil {
  263. err = recordErr
  264. return
  265. }
  266. //插入成功后,需要重新查询该用户,并进入下面的逻辑
  267. goto QUERY_WX_USER
  268. } else if wxUserErr == ERR_USER_NOT_BIND {
  269. //没有用户信息
  270. //wxUser.FirstLogin = 1
  271. } else if wxUserErr != nil {
  272. err = wxUserErr
  273. return
  274. }
  275. //如果已经登录注册绑定的情况下
  276. if wxUser != nil && wxUserErr == nil {
  277. //获取用户权限
  278. firstLogin = wxUser.FirstLogin
  279. userId = wxUser.UserId
  280. permission, permissionErr := CheckUserPermission(userId)
  281. if permissionErr != nil {
  282. //记录日志
  283. utils.FileLog.Info("userId:%s,err:%s", strconv.Itoa(userId), err)
  284. }
  285. if wxUserInfo != nil {
  286. go models.ModifyUserRecordInfo(openId, wxUserInfo.Nickname, wxUserInfo.Headimgurl, wxUserInfo.City, wxUserInfo.Province, wxUserInfo.Country, wxUserInfo.Sex, userId)
  287. }
  288. {
  289. codeLog := new(models.WxUserCode)
  290. codeLog.WxCode = code
  291. codeLog.UserId = userId
  292. codeLog.Code = 0
  293. codeLog.FirstLogin = firstLogin
  294. codeLog.Authorization = token
  295. codeLog.UserPermission = permission
  296. codeLog.CreateTime = time.Now()
  297. models.AddWxUserCode(codeLog)
  298. }
  299. if wxUser.Mobile == "" && wxUser.Email == "" {
  300. firstLogin = 1
  301. } else {
  302. firstLogin = 0
  303. }
  304. }
  305. //获取登录token
  306. tokenItem, tokenErr := models.GetTokenByOpenId(openId)
  307. if tokenErr != nil && tokenErr.Error() != utils.ErrNoRow() {
  308. err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
  309. return
  310. }
  311. if tokenItem == nil || (tokenErr != nil && tokenErr.Error() == utils.ErrNoRow()) {
  312. timeUnix := time.Now().Unix()
  313. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  314. token = utils.MD5(openId) + utils.MD5(timeUnixStr)
  315. //新增session
  316. {
  317. session := new(models.Session)
  318. session.OpenId = openId
  319. session.UserId = userId
  320. session.CreatedTime = time.Now()
  321. session.LastUpdatedTime = time.Now()
  322. session.ExpireTime = time.Now().AddDate(0, 3, 0)
  323. session.AccessToken = token
  324. sessionErr := models.AddSession(session)
  325. if err != nil {
  326. err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
  327. return
  328. }
  329. }
  330. } else {
  331. token = tokenItem.AccessToken
  332. //如果联系人编号不为空,且联系人编号与session里面的联系人编号不一致的时候,需要做session变更
  333. if userId > 0 && tokenItem.UserId != userId {
  334. _ = models.UpdateSession(tokenItem.SessionId, userId, time.Now().AddDate(0, 1, 0))
  335. }
  336. }
  337. //新增登录日志
  338. {
  339. loginLog := new(models.WxUserLog)
  340. loginLog.UserId = userId
  341. loginLog.OpenId = openId
  342. loginLog.UnionId = unionId
  343. loginLog.CreateTime = time.Now()
  344. loginLog.Handle = "wechat_login"
  345. loginLog.Remark = token
  346. go models.AddWxUserLog(loginLog)
  347. }
  348. return
  349. }
  350. //添加第三方用户(微信)记录
  351. func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (userRecord *models.UserRecord, err error) {
  352. find, err := models.GetUserRecordByOpenId(openId)
  353. if err != nil && err.Error() != utils.ErrNoRow() {
  354. return
  355. }
  356. if find != nil {
  357. userRecord = find
  358. return
  359. }
  360. userRecord = &models.UserRecord{
  361. OpenId: openId, //用户open_id
  362. UnionId: unionId, //用户union_id
  363. Subscribe: subscribe,
  364. NickName: nickName, //用户昵称,最大长度:32
  365. RealName: realName, //用户实际名称,最大长度:32
  366. Sex: sex, //普通用户性别,1为男性,2为女性
  367. Province: province, //普通用户个人资料填写的省份,最大长度:30
  368. City: city, //普通用户个人资料填写的城市,最大长度:30
  369. Country: country, //国家,如中国为CN,最大长度:30
  370. Headimgurl: headimgurl, //用户第三方(微信)头像,最大长度:512
  371. CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
  372. CreatePlatform: platform, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
  373. SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255
  374. }
  375. recordId, err := models.AddUserRecord(userRecord)
  376. if err != nil {
  377. return
  378. }
  379. userRecord.UserRecordId = int(recordId)
  380. return
  381. }