admin.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package services
  2. import (
  3. "errors"
  4. "hongze/hongze_mobile_admin/models/custom"
  5. "hongze/hongze_mobile_admin/models/tables/admin"
  6. "hongze/hongze_mobile_admin/models/tables/admin_record"
  7. "hongze/hongze_mobile_admin/models/tables/h5_admin_session"
  8. "hongze/hongze_mobile_admin/utils"
  9. "strconv"
  10. "time"
  11. )
  12. var ERR_NO_ADMIN_RECORD = errors.New("用户关系没有入库")
  13. var ERR_ADMIN_NOT_BIND = errors.New("用户没有绑定")
  14. //通过openid获取用户信息
  15. func GetAdminUserItemByOpenId(openid string) (item *custom.AdminWx, err error) {
  16. item = &custom.AdminWx{}
  17. //通过openid获取用户关联信息
  18. adminRecord, adminRecordErr := admin_record.GetAdminRecordByOpenId(openid)
  19. if adminRecordErr != nil {
  20. if adminRecordErr.Error() == utils.ErrNoRow() {
  21. err = ERR_NO_ADMIN_RECORD
  22. return
  23. } else {
  24. err = adminRecordErr
  25. return
  26. }
  27. }
  28. //该openid在系统中没有关联关系
  29. if adminRecord == nil {
  30. err = ERR_NO_ADMIN_RECORD
  31. return
  32. }
  33. //该openid没有绑定用户
  34. if adminRecord.UserId <= 0 {
  35. err = ERR_ADMIN_NOT_BIND
  36. //格式化返回用户数据
  37. formatWxUserAndUserRecord(item, adminRecord)
  38. return
  39. }
  40. //获取用户信息
  41. item, adminInfoErr := admin.GetAdminWxById(adminRecord.UserId)
  42. if adminInfoErr != nil {
  43. err = adminInfoErr
  44. //如果是下面这个错误,那么这个可能是用户信息被删除,然后user_record表没有移除该条记录所绑定的user_id信息
  45. if adminInfoErr.Error() == utils.ErrNoRow() {
  46. err = errors.New("用户信息不存在")
  47. }
  48. return
  49. }
  50. //格式化返回用户数据
  51. formatWxUserAndUserRecord(item, adminRecord)
  52. return
  53. }
  54. //根据管理员id和平台id获取用户信息
  55. func GetAdminUserItemByAdminId(adminId, platform int) (item *custom.AdminWx, err error) {
  56. item = &custom.AdminWx{}
  57. //获取用户信息
  58. item, adminErr := admin.GetAdminWxById(adminId)
  59. if adminErr != nil {
  60. err = adminErr
  61. return
  62. }
  63. //格式化返回用户数据
  64. formatWxUser(item, platform)
  65. return
  66. }
  67. //通过用户 关系表记录 和 用户记录 格式化返回 用户数据
  68. func formatWxUserAndUserRecord(adminWx *custom.AdminWx, adminRecord *admin_record.AdminRecord) {
  69. adminWx.OpenId = adminRecord.OpenId
  70. adminWx.UnionId = adminRecord.UnionId
  71. adminWx.NickName = adminRecord.NickName
  72. adminWx.BindAccount = adminRecord.BindAccount
  73. adminWx.Sex = adminRecord.Sex
  74. adminWx.Province = adminRecord.Province
  75. adminWx.City = adminRecord.City
  76. adminWx.Country = adminRecord.Country
  77. adminWx.Headimgurl = adminRecord.Headimgurl
  78. adminWx.AdminId = adminRecord.UserId
  79. //adminWx.Subscribe = adminRecord.Subscribe
  80. //wxUser.RealName = userRecord.RealName
  81. //wxUser.BindAccount = userRecord.BindAccount
  82. }
  83. //通过用户 用户记录 和 来源平台 格式化返回 用户数据
  84. func formatWxUser(adminWx *custom.AdminWx, platform int) {
  85. //根据用户id和平台id获取用户关系
  86. adminRecord, adminRecordErr := admin_record.GetAdminRecordByAdminId(adminWx.AdminId, platform)
  87. if adminRecordErr != nil {
  88. if adminRecordErr.Error() != utils.ErrNoRow() {
  89. return
  90. }
  91. if adminRecordErr.Error() == utils.ErrNoRow() {
  92. return
  93. }
  94. }
  95. //该openid在系统中没有关联关系
  96. if adminRecord == nil {
  97. return
  98. }
  99. formatWxUserAndUserRecord(adminWx, adminRecord)
  100. return
  101. }
  102. //用户绑定
  103. func BindWxUser(openid, userName, password string, registerPlatform int) (adminWx *custom.AdminWx, err error) {
  104. if userName == "" || password == "" {
  105. err = errors.New("账号密码必填")
  106. return
  107. }
  108. //根据账号密码获取管理员信息(校验账号密码是否正常)
  109. adminInfo, err := admin.CheckAdmin(userName, password)
  110. if err != nil {
  111. if err.Error() != utils.ErrNoRow() {
  112. err = errors.New("账号或密码错误")
  113. }
  114. return
  115. }
  116. if adminInfo.Enabled == 0 {
  117. err = errors.New("您的账号已被禁用,如需登录,请联系管理员")
  118. return
  119. }
  120. //获取用户信息
  121. adminWx, err = admin.GetAdminWxById(adminInfo.AdminId)
  122. if err != nil {
  123. return
  124. }
  125. //查询openid的第三方(微信)信息
  126. adminRecord, err := admin_record.GetAdminRecordByOpenId(openid)
  127. if err != nil {
  128. return
  129. }
  130. //如果查询出来的用户是nil,那么需要新增用户
  131. //如果存在该手机号/邮箱,那么需要校验
  132. if adminRecord.UserId > 0 && adminRecord.UserId != adminInfo.AdminId {
  133. err = errors.New("用户已绑定,不允许重复绑定")
  134. return
  135. }
  136. if adminRecord.UserId == 0 {
  137. err = admin_record.BindAdminRecordByOpenid(adminInfo.AdminId, openid)
  138. if err != nil {
  139. return
  140. }
  141. adminRecord.UserId = adminInfo.AdminId
  142. }
  143. //如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复
  144. //if adminRecord.NickName == "" {
  145. // oldUserRecord, err := admin_record.GetAdminThirdRecordByAdminId(adminInfo.AdminId)
  146. // if err == nil && oldUserRecord != nil {
  147. // //如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复
  148. // if oldUserRecord.NickName != "" {
  149. // _ = admin_record.ModifyAdminRecordInfo(adminRecord.OpenId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, adminInfo.AdminId)
  150. // }
  151. // }
  152. //}
  153. //格式化用户数据
  154. formatWxUserAndUserRecord(adminWx, adminRecord)
  155. return
  156. }
  157. //微信登录
  158. func WxLogin(wxPlatform int, wxAccessToken *WxAccessToken, wxUserInfo *WxUserInfo) (token string, adminWx *custom.AdminWx, err error) {
  159. openId := wxAccessToken.Openid
  160. unionId := wxAccessToken.Unionid
  161. if unionId == "" {
  162. unionId = wxUserInfo.Unionid
  163. }
  164. //firstLogin==1,强制绑定手机号或者邮箱
  165. QUERY_WX_USER:
  166. adminWx, adminWxErr := GetAdminUserItemByOpenId(openId)
  167. if adminWxErr == ERR_NO_ADMIN_RECORD { //没有用户openid记录
  168. _, recordErr := AddUserRecord(openId, unionId, wxUserInfo.Nickname, "", wxUserInfo.Province, wxUserInfo.City, wxUserInfo.Country, wxUserInfo.Headimgurl, "", wxPlatform, wxUserInfo.Sex, 0)
  169. //如果插入失败,那么直接将错误信息返回
  170. if recordErr != nil {
  171. err = recordErr
  172. return
  173. }
  174. //插入成功后,需要重新查询该用户,并进入下面的逻辑
  175. goto QUERY_WX_USER
  176. } else if adminWxErr == ERR_ADMIN_NOT_BIND {
  177. //没有用户信息
  178. //wxUser.FirstLogin = 1
  179. } else if adminWxErr != nil {
  180. err = adminWxErr
  181. return
  182. }
  183. adminId := adminWx.AdminId
  184. //获取登录token
  185. tokenItem, tokenErr := h5_admin_session.GetTokenByOpenId(openId)
  186. if tokenErr != nil && tokenErr.Error() != utils.ErrNoRow() {
  187. err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
  188. return
  189. }
  190. if tokenItem == nil || (tokenErr != nil && tokenErr.Error() == utils.ErrNoRow()) {
  191. timeUnix := time.Now().Unix()
  192. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  193. token = utils.MD5(openId) + utils.MD5(timeUnixStr)
  194. //新增session
  195. {
  196. session := &h5_admin_session.H5AdminSession{
  197. OpenId: openId,
  198. AdminId: adminWx.AdminId,
  199. CreatedTime: time.Now(),
  200. LastUpdatedTime: time.Now(),
  201. ExpireTime: time.Now().AddDate(0, 3, 0),
  202. AccessToken: token,
  203. }
  204. session.AccessToken = token
  205. sessionErr := h5_admin_session.AddSession(session)
  206. if err != nil {
  207. err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
  208. return
  209. }
  210. }
  211. } else {
  212. token = tokenItem.AccessToken
  213. //如果联系人编号不为空,且联系人编号与session里面的联系人编号不一致的时候,需要做session变更
  214. if adminId > 0 && tokenItem.AdminId != adminId {
  215. _ = h5_admin_session.UpdateSession(tokenItem.SessionId, adminId, time.Now().AddDate(0, 1, 0))
  216. }
  217. }
  218. return
  219. }
  220. //添加第三方用户(微信)记录
  221. func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (adminRecord *admin_record.AdminRecord, err error) {
  222. find, err := admin_record.GetAdminRecordByOpenId(openId)
  223. if err != nil && err.Error() != utils.ErrNoRow() {
  224. return
  225. }
  226. if find != nil {
  227. adminRecord = find
  228. return
  229. }
  230. adminRecord = &admin_record.AdminRecord{
  231. OpenId: openId, //用户open_id
  232. UnionId: unionId, //用户union_id
  233. Subscribe: subscribe,
  234. NickName: nickName, //用户昵称,最大长度:32
  235. RealName: realName, //用户实际名称,最大长度:32
  236. Sex: sex, //普通用户性别,1为男性,2为女性
  237. Province: province, //普通用户个人资料填写的省份,最大长度:30
  238. City: city, //普通用户个人资料填写的城市,最大长度:30
  239. Country: country, //国家,如中国为CN,最大长度:30
  240. Headimgurl: headimgurl, //用户第三方(微信)头像,最大长度:512
  241. CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
  242. CreatePlatform: platform, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
  243. SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255
  244. }
  245. recordId, err := admin_record.AddAdminRecord(adminRecord)
  246. if err != nil {
  247. return
  248. }
  249. adminRecord.UserRecordId = int(recordId)
  250. return
  251. }