admin.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package system
  2. import (
  3. "eta/eta_forum_admin/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. type LoginReq struct {
  10. Username string `description:"账号"`
  11. Password string `description:"密码"`
  12. IsRemember bool `description:"是否属于受信设备"`
  13. }
  14. type LoginResp struct {
  15. Authorization string
  16. AdminName string `description:"系统用户名称"`
  17. RealName string `description:"系统用户姓名"`
  18. RoleName string `description:"角色名称"`
  19. RoleTypeCode string `description:"角色类型编码"`
  20. SysRoleTypeCode string `description:"角色类型编码"`
  21. AdminId int `description:"系统用户id"`
  22. ProductName string `description:"产品名称:admin,ficc,权益"`
  23. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,4:ficc销售组长"`
  24. }
  25. type Admin struct {
  26. AdminId int `orm:"column(admin_id);pk" description:"系统用户id"`
  27. AdminName string `description:"系统用户名称"`
  28. AdminAvatar string `description:"用户头像"`
  29. RealName string `description:"系统用户姓名"`
  30. Password string `json:"-"`
  31. LastUpdatedPasswordTime string `json:"-"`
  32. Enabled int
  33. Email string `description:"系统用户邮箱"`
  34. LastLoginTime string
  35. CreatedTime time.Time
  36. LastUpdatedTime string
  37. Role string `description:"系统用户角色"`
  38. Mobile string `description:"手机号"`
  39. RoleType int `description:"角色类型:1需要录入指标,0:不需要"`
  40. RoleId int `description:"角色ID"`
  41. RoleName string `description:"角色名称"`
  42. RoleTypeCode string `description:"角色类型编码"`
  43. DepartmentId int `description:"部门id"`
  44. DepartmentName string `description:"部门名称"`
  45. GroupId int `description:"分组id"`
  46. GroupName string `description:"分组名称"`
  47. Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,3:超级管理员,4:ficc销售组长"`
  48. Position string `description:"职位"`
  49. DisableTime time.Time `description:"禁用时间"`
  50. ChartPermission int8 `description:"图表权限id"`
  51. OpenId string `description:"弘则部门公众号的openid"`
  52. UnionId string `description:"微信公众平台唯一标识"`
  53. EdbPermission int8 `description:"指标库操作权限,0:只能操作 自己的,1:所有指标可操作"`
  54. MysteelChemicalPermission int8 `description:"钢联化工指标操作权限,0:只能操作 自己的,1:所有指标可操作"`
  55. PredictEdbPermission int8 `description:"预测指标库操作权限,0:只能操作 自己的,1:所有预测指标可操作"`
  56. Province string `description:"省"`
  57. ProvinceCode string `description:"省编码"`
  58. City string `description:"市"`
  59. CityCode string `description:"市编码"`
  60. EmployeeId string `description:"员工工号(钉钉/每刻报销)"`
  61. TelAreaCode string `description:"手机区号"`
  62. IsLdap int `description:"是否为域用户:0-系统账户;1-域用户"`
  63. BusinessCode string `description:"商家编码"`
  64. }
  65. // Update 更新用户基础信息
  66. func (item *Admin) Update(cols []string) (err error) {
  67. o := orm.NewOrm()
  68. _, err = o.Update(item, cols...)
  69. return
  70. }
  71. func CheckSysUser(userName, password string) (item *Admin, err error) {
  72. sql := ` SELECT a.*,b.role_type_code FROM admin AS a
  73. INNER JOIN sys_role AS b ON a.role_id=b.role_id WHERE a.admin_name=? AND a.password=? LIMIT 1`
  74. o := orm.NewOrm()
  75. err = o.Raw(sql, userName, password).QueryRow(&item)
  76. return
  77. }
  78. func GetSysUserById(sysUserId int) (item *Admin, err error) {
  79. sql := `SELECT * FROM admin WHERE admin_id=? LIMIT 1`
  80. o := orm.NewOrm()
  81. err = o.Raw(sql, sysUserId).QueryRow(&item)
  82. return
  83. }
  84. func ModifyPwd(sysUserId int, newPwd string) (err error) {
  85. sql := `UPDATE admin SET password=?,last_updated_time=NOW() WHERE admin_id=? `
  86. o := orm.NewOrm()
  87. _, err = o.Raw(sql, newPwd, sysUserId).Exec()
  88. return
  89. }
  90. type ModifyPwdReq struct {
  91. OldPwd string `description:"旧密码"`
  92. NewPwd string `description:"新密码"`
  93. }
  94. // AdminWxUserRelation 管理员-微信用户关联
  95. type AdminWxUserRelation struct {
  96. AdminId int `json:"admin_id"`
  97. AdminName string `json:"admin_name"`
  98. RealName string `json:"real_name"`
  99. UserId int `json:"user_id"`
  100. }
  101. // GetAdminListByIdList 根据用户id列表获取系统用户列表
  102. func GetAdminListByIdList(idList []int) (items []*Admin, err error) {
  103. lenNum := len(idList)
  104. if lenNum <= 0 {
  105. return
  106. }
  107. sql := `SELECT * FROM admin WHERE admin_id in (` + utils.GetOrmInReplace(lenNum) + `) and enabled=1 `
  108. o := orm.NewOrm()
  109. _, err = o.Raw(sql, idList).QueryRows(&items)
  110. return
  111. }
  112. // GetAdminListByIdList 根据用户id列表获取系统用户列表
  113. func GetAdminListByIdListWithoutEnable(idList []int) (items []*Admin, err error) {
  114. lenNum := len(idList)
  115. if lenNum <= 0 {
  116. return
  117. }
  118. sql := `SELECT * FROM admin WHERE admin_id in (` + utils.GetOrmInReplace(lenNum) + `) `
  119. o := orm.NewOrm()
  120. _, err = o.Raw(sql, idList).QueryRows(&items)
  121. return
  122. }
  123. func (item *Admin) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  124. o := orm.NewOrm()
  125. sql := fmt.Sprintf(`SELECT COUNT(1) FROM admin WHERE 1=1 %s`, condition)
  126. err = o.Raw(sql, pars).QueryRow(&count)
  127. return
  128. }
  129. func (item *Admin) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Admin, err error) {
  130. o := orm.NewOrm()
  131. fields := strings.Join(fieldArr, ",")
  132. if len(fieldArr) == 0 {
  133. fields = `*`
  134. }
  135. order := `ORDER BY created_time DESC`
  136. if orderRule != "" {
  137. order = ` ORDER BY ` + orderRule
  138. }
  139. sql := fmt.Sprintf(`SELECT %s FROM admin WHERE 1=1 %s %s`, fields, condition, order)
  140. _, err = o.Raw(sql, pars).QueryRows(&items)
  141. return
  142. }
  143. // GetSysUserByMobile 手机号获取用户
  144. func GetSysUserByMobile(mobile string) (item *Admin, err error) {
  145. o := orm.NewOrm()
  146. sql := `SELECT
  147. a.*, b.role_type_code
  148. FROM
  149. admin AS a
  150. INNER JOIN sys_role AS b ON a.role_id = b.role_id
  151. WHERE
  152. a.mobile = ?
  153. LIMIT 1`
  154. err = o.Raw(sql, mobile).QueryRow(&item)
  155. return
  156. }
  157. // GetSysUserByEmail 邮箱获取用户
  158. func GetSysUserByEmail(email string) (item *Admin, err error) {
  159. o := orm.NewOrm()
  160. sql := `SELECT
  161. a.*, b.role_type_code
  162. FROM
  163. admin AS a
  164. INNER JOIN sys_role AS b ON a.role_id = b.role_id
  165. WHERE
  166. a.email = ?
  167. LIMIT 1`
  168. err = o.Raw(sql, email).QueryRow(&item)
  169. return
  170. }
  171. // GetSysUserByAdminName 账号获取用户
  172. func GetSysUserByAdminName(adminName string) (item *Admin, err error) {
  173. o := orm.NewOrm()
  174. sql := `SELECT
  175. a.*, b.role_type_code
  176. FROM
  177. admin AS a
  178. INNER JOIN sys_role AS b ON a.role_id = b.role_id
  179. WHERE
  180. a.admin_name = ?
  181. LIMIT 1`
  182. err = o.Raw(sql, adminName).QueryRow(&item)
  183. return
  184. }
  185. // GetUserUnexpiredSysSession 获取用户未过期的Token
  186. func GetUserUnexpiredSysSession(userName, expiredTime string) (item *SysSession, err error) {
  187. o := orm.NewOrm()
  188. sql := `SELECT
  189. *
  190. FROM
  191. sys_session
  192. WHERE
  193. user_name = ? AND expired_time > ?
  194. ORDER BY expired_time DESC
  195. LIMIT 1`
  196. err = o.Raw(sql, userName, expiredTime).QueryRow(&item)
  197. return
  198. }
  199. type AuthCodeLoginReq struct {
  200. AuthCode string
  201. }
  202. type BusinessAdmin struct {
  203. AdminId string `description:"系统用户id"`
  204. RealName string `description:"用户真实名称"`
  205. ChildrenList []*BusinessAdmin `description:"用户列表"`
  206. }
  207. type BusinessAdminResp struct {
  208. List []*BusinessAdmin `description:"用户列表"`
  209. }