admin.go 10 KB

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