admin.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package admin
  2. import (
  3. //_ "github.com/go-sql-driver/mysql"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_mobile_admin/models/custom"
  6. "hongze/hongze_mobile_admin/utils"
  7. "time"
  8. )
  9. type Admin struct {
  10. AdminId int `orm:"column(admin_id);pk" json:"admin_id"`
  11. AdminName string `orm:"column(admin_name);" json:"admin_name"`
  12. AdminAvatar string `description:"用户头像"`
  13. RealName string `orm:"column(real_name);" json:"real_name"`
  14. Password string `orm:"column(password);" json:"password"`
  15. LastUpdatedPasswordTime string `orm:"column(last_updated_password_time);" json:"last_updated_password_time"`
  16. Enabled int `orm:"column(enabled);" json:"enabled"` // 1:有效,0:禁用
  17. Email string `orm:"column(email);" json:"email"`
  18. LastLoginTime string `orm:"column(last_login_time);" json:"last_login_time"` // 最近登陆时间
  19. CreatedTime string `orm:"column(created_time);" json:"created_time"` // 创建时间
  20. LastUpdatedTime string `orm:"column(last_updated_time);" json:"last_updated_time"`
  21. Role string `orm:"column(role);" json:"role"` // 用户角色
  22. Mobile string `orm:"column(mobile);" json:"mobile"` // 手机号
  23. RoleType int `orm:"column(role_type);" json:"role_type"` // 角色类型:1需要录入指标,0:不需要
  24. RoleId int `orm:"column(role_id);" json:"role_id"` // 角色id
  25. RoleName string `orm:"column(role_name);" json:"role_name"` // 角色名称
  26. RoleTypeCode string `orm:"column(role_type_code);" json:"role_type_code"` // 角色编码
  27. DepartmentId int `orm:"column(department_id);" json:"department_id"` // 部门id
  28. DepartmentName string `orm:"column(department_name);" json:"department_name"` // 部门名称
  29. GroupId int `orm:"column(group_id);" json:"group_id"` // 分组id
  30. GroupName string `orm:"column(group_name);" json:"group_name"` // 分组名称
  31. Authority int `orm:"column(authority);" json:"authority"` // 管理权限,0:无,1:部门负责人,2:小组负责人,3:超级管理员
  32. Position string `orm:"column(position);" json:"position"` // 职位
  33. OpenId string `orm:"column(open_id);" json:"open_id"` //弘则部门公众号的openid
  34. UnionId string `orm:"column(union_id);" json:"union_id"` //微信公众平台唯一标识
  35. }
  36. // 账号密码校验
  37. func CheckAdmin(userName, password string) (item *Admin, err error) {
  38. sql := ` SELECT a.*,b.role_type_code FROM admin AS a
  39. INNER JOIN sys_role AS b ON a.role_id=b.role_id WHERE a.admin_name=? AND a.password=? LIMIT 1`
  40. o := orm.NewOrm()
  41. err = o.Raw(sql, userName, password).QueryRow(&item)
  42. return
  43. }
  44. // 根据管理员id获取管理员信息
  45. func GetAdminById(adminId int) (item *Admin, err error) {
  46. sql := `SELECT * FROM admin WHERE admin_id=? LIMIT 1`
  47. o := orm.NewOrm()
  48. err = o.Raw(sql, adminId).QueryRow(&item)
  49. return
  50. }
  51. // 根据权限code获取系统用户列表
  52. func GetAdminListByRoleCode(roleTypeCode string) (items []*Admin, err error) {
  53. sql := `SELECT * FROM admin WHERE role_type_code=? and enabled=1 `
  54. o := orm.NewOrm()
  55. _, err = o.Raw(sql, roleTypeCode).QueryRows(&items)
  56. return
  57. }
  58. // 根据权限id获取系统用户列表
  59. func GetAdminListByRoleId(roleId string) (items []*Admin, err error) {
  60. sql := `SELECT * FROM admin WHERE role_id=? and enabled=1 `
  61. o := orm.NewOrm()
  62. _, err = o.Raw(sql, roleId).QueryRows(&items)
  63. return
  64. }
  65. // 根据用户id字符串获取系统用户列表
  66. func GetAdminListByIds(ids string) (items []*Admin, err error) {
  67. sql := `SELECT * FROM admin WHERE admin_id in (` + ids + `) and enabled=1 `
  68. o := orm.NewOrm()
  69. _, err = o.Raw(sql).QueryRows(&items)
  70. return
  71. }
  72. // 根据管理员id获取管理员信息(包含微信、第三方信息)
  73. func GetAdminWxById(adminId int) (item *custom.AdminWx, err error) {
  74. sql := `SELECT * FROM admin WHERE admin_id=? LIMIT 1`
  75. o := orm.NewOrm()
  76. err = o.Raw(sql, adminId).QueryRow(&item)
  77. return
  78. }
  79. func GetSysuserList(condition string, pars []interface{}, startSize, pageSize int) (items []*AdminItem, err error) {
  80. o := orm.NewOrm()
  81. sql := `SELECT * FROM admin WHERE 1=1 `
  82. if condition != "" {
  83. sql += condition
  84. }
  85. sql += `ORDER BY created_time DESC LIMIT ?,?`
  86. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  87. return
  88. }
  89. // AdminItem 管理后台的adminInfo,没有使用json格式化,路演这边需要用到,没办法
  90. type AdminItem struct {
  91. AdminId int `description:"系统用户id"`
  92. AdminName string `description:"系统用户名称"`
  93. RealName string `description:"系统用户姓名"`
  94. Password string
  95. LastUpdatedPasswordTime string `json:"-"`
  96. Enabled int `description:"1:有效,0:禁用"`
  97. Email string `description:"系统用户邮箱"`
  98. LastLoginTime string
  99. CreatedTime time.Time
  100. LastUpdatedTime string
  101. Role string `description:"系统用户角色"`
  102. Mobile string `description:"手机号"`
  103. RoleType int `description:"角色类型:1需要录入指标,0:不需要"`
  104. RoleId int `description:"角色id"`
  105. RoleName string `description:"角色名称"`
  106. RoleTypeCode string `description:"角色编码"`
  107. DepartmentId int `description:"部门id"`
  108. DepartmentName string `json:"-" description:"部门名称"`
  109. TeamId int `description:"三级id"`
  110. GroupId int `description:"分组id"`
  111. GroupName string `json:"-" description:"分组名称"`
  112. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,4:ficc销售组长"`
  113. Position string `description:"职位"`
  114. DepartmentGroup string `description:"部门分组"`
  115. LabelVal int `description:"标签:1:超级管理员,2:管理员,3:部门经理,4:组长,5:ficc销售主管"`
  116. }
  117. type SmsCodeReq struct {
  118. Mobile string
  119. Token string
  120. AreaNum string
  121. }
  122. type BindMobileReq struct {
  123. Mobile string
  124. Token string
  125. Code string
  126. }
  127. type WxLoginResp struct {
  128. BindToken string
  129. BindFlag bool
  130. }
  131. type WxSmsResp struct {
  132. SmsFlag bool
  133. BindFlag bool
  134. }
  135. // GetAdminWxByAdminOpenId 根据openId 获取管理员信息
  136. func GetAdminWxByAdminOpenId(openId string) (item *Admin, err error) {
  137. sql := `SELECT * FROM admin WHERE open_id=? LIMIT 1`
  138. o := orm.NewOrm()
  139. err = o.Raw(sql, openId).QueryRow(&item)
  140. return
  141. }
  142. // GetAdminByMobile 根据手机号 获取管理员信息
  143. func GetAdminByMobile(mobile string) (item *Admin, err error) {
  144. sql := `SELECT * FROM admin WHERE mobile=? LIMIT 1`
  145. o := orm.NewOrm()
  146. err = o.Raw(sql, mobile).QueryRow(&item)
  147. return
  148. }
  149. // UpdateAdminOpenIdUnionId 更新openId
  150. func UpdateAdminOpenIdUnionId(adminId int, openId, unionId string) (err error) {
  151. o := orm.NewOrm()
  152. sql := `UPDATE admin
  153. SET
  154. last_updated_time = NOW(),open_id=?,union_id=? WHERE admin_id = ? `
  155. _, err = o.Raw(sql, openId, unionId, adminId).Exec()
  156. return
  157. }
  158. type OpenIdList struct {
  159. OpenId string
  160. AdminId int
  161. }
  162. // GetOpenIdListByMobile 根据手机号获取用户的openid列表
  163. func GetOpenIdListByMobile(mobile, openIdStr string) (items []*OpenIdList, err error) {
  164. sql := `SELECT admin_id, open_id FROM admin
  165. WHERE open_id != "" and mobile=? `
  166. if openIdStr != "" {
  167. sql += ` AND open_id in (` + openIdStr + `) `
  168. }
  169. _, err = orm.NewOrm().Raw(sql, mobile).QueryRows(&items)
  170. return
  171. }
  172. func GetAdminWxByIdS(adminIdStr string) (items []*custom.AdminWx, err error) {
  173. sql := `SELECT * FROM admin WHERE admin_id IN(` + adminIdStr + `)`
  174. o := orm.NewOrm()
  175. _, err = o.Raw(sql).QueryRows(&items)
  176. return
  177. }
  178. // GetSysuserRaiList 获取权益销售
  179. func GetSysuserRaiList() (items []*AdminItem, err error) {
  180. o := orm.NewOrm()
  181. sql := `SELECT
  182. real_name,
  183. mobile,
  184. group_name
  185. FROM
  186. admin
  187. WHERE
  188. role_type_code IN ('rai_group','rai_seller')
  189. AND group_id NOT IN ( 19, 10, 17 )
  190. AND enabled = 1
  191. OR real_name IN ( '沈涛', '张传星' ) ` // 先写死,看情况要不要改
  192. _, err = o.Raw(sql).QueryRows(&items)
  193. return
  194. }
  195. // GetAdminListByIdList 根据用户id列表获取系统用户列表
  196. func GetAdminListByIdListWithoutEnable(idList []int) (items []*Admin, err error) {
  197. lenNum := len(idList)
  198. if lenNum <= 0 {
  199. return
  200. }
  201. sql := `SELECT * FROM admin WHERE admin_id in (` + utils.GetOrmInReplace(lenNum) + `) `
  202. o := orm.NewOrm()
  203. _, err = o.Raw(sql, idList).QueryRows(&items)
  204. return
  205. }
  206. // GetSysUserByAdminName 账号获取用户
  207. func GetSysUserByAdminName(adminName string) (item *Admin, err error) {
  208. o := orm.NewOrm()
  209. sql := `SELECT
  210. a.*, b.role_type_code
  211. FROM
  212. admin AS a
  213. INNER JOIN sys_role AS b ON a.role_id = b.role_id
  214. WHERE
  215. a.admin_name = ?
  216. LIMIT 1`
  217. err = o.Raw(sql, adminName).QueryRow(&item)
  218. return
  219. }