user.go 12 KB

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