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