user.go 10 KB

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