广告平台(站长下代理使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hdl_login.go 6.1 KiB

3 months ago
2 months ago
2 months ago
3 months ago
2 months ago
3 months ago
2 months ago
2 months ago
3 months ago
2 months ago
3 months ago
2 months ago
3 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package hdl
  2. import (
  3. "applet/app/e"
  4. "applet/app/lib/validate"
  5. "applet/app/md"
  6. "applet/app/svc"
  7. "applet/app/utils"
  8. db "code.fnuoos.com/zhimeng/model.git/src"
  9. "code.fnuoos.com/zhimeng/model.git/src/implement"
  10. model2 "code.fnuoos.com/zhimeng/model.git/src/model"
  11. implement2 "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  12. "code.fnuoos.com/zhimeng/model.git/src/super/model"
  13. "fmt"
  14. "github.com/gin-gonic/gin"
  15. "time"
  16. )
  17. // Login 登陆
  18. // @Summary 登陆
  19. // @Tags ADMIN
  20. // @Description 登入
  21. // @Accept json
  22. // @Produce json
  23. // @Param req body md.LoginReq true "用户名密码"
  24. // @Success 200 {object} md.LoginResponse "token"
  25. // @Failure 400 {object} md.Response "具体错误"
  26. // @Router /api/login [post]
  27. func Login(c *gin.Context) {
  28. var req md.LoginReq
  29. err := c.ShouldBindJSON(&req)
  30. if err != nil {
  31. err = validate.HandleValidateErr(err)
  32. err1 := err.(e.E)
  33. e.OutErr(c, err1.Code, err1.Error())
  34. return
  35. }
  36. engine := db.DBs[svc.GetMasterId(c)]
  37. adminDb := implement.NewAgentDb(engine)
  38. admin, err := adminDb.GetAgentByUsername(req.UserName)
  39. if err != nil {
  40. e.OutErr(c, e.ERR_DB_ORM, err)
  41. return
  42. }
  43. if admin == nil {
  44. e.OutErr(c, e.ERR_NO_DATA, "账号不存在!")
  45. return
  46. }
  47. if utils.Md5(req.PassWord) != admin.Password {
  48. e.OutErr(c, e.ERR_INVALID_ARGS, "密码错误")
  49. return
  50. }
  51. ip := utils.GetIP(c.Request)
  52. key := fmt.Sprintf(md.JwtTokenKey, ip, utils.AnyToString(admin.Id))
  53. token, err := svc.HandleLoginToken(key, admin)
  54. if err != nil {
  55. e.OutErr(c, e.ERR, err.Error())
  56. return
  57. }
  58. e.OutSuc(c, md.LoginResponse{
  59. Token: token,
  60. }, nil)
  61. return
  62. }
  63. // LoginPhone 手机号登陆
  64. // @Summary 手机号登陆
  65. // @Tags ADMIN
  66. // @Description 手机号登入
  67. // @Accept json
  68. // @Produce json
  69. // @Param req body md.LoginPhoneReq true "用户名、验证码"
  70. // @Success 200 {object} md.LoginResponse "token"
  71. // @Failure 400 {object} md.Response "具体错误"
  72. // @Router /api/login/phone [post]
  73. func LoginPhone(c *gin.Context) {
  74. var req md.LoginPhoneReq
  75. err := c.ShouldBindJSON(&req)
  76. if err != nil {
  77. err = validate.HandleValidateErr(err)
  78. err1 := err.(e.E)
  79. e.OutErr(c, err1.Code, err1.Error())
  80. return
  81. }
  82. engine := db.DBs[svc.GetMasterId(c)]
  83. check, err := svc.SmsCheck(c, req.UserName, req.Captcha)
  84. if check == false {
  85. e.OutErr(c, 400, e.NewErr(400, err.Error()))
  86. return
  87. }
  88. adminDb := implement.NewAgentDb(engine)
  89. admin, err := adminDb.GetAgentByUsername(req.UserName)
  90. if err != nil {
  91. e.OutErr(c, e.ERR_DB_ORM, err)
  92. return
  93. }
  94. if admin == nil {
  95. e.OutErr(c, e.ERR_NO_DATA, "账号不存在!")
  96. return
  97. }
  98. ip := utils.GetIP(c.Request)
  99. key := fmt.Sprintf(md.JwtTokenKey, ip, utils.AnyToString(admin.Id))
  100. token, err := svc.HandleLoginToken(key, admin)
  101. if err != nil {
  102. e.OutErr(c, e.ERR, err.Error())
  103. return
  104. }
  105. e.OutSuc(c, md.LoginResponse{
  106. Token: token,
  107. }, nil)
  108. return
  109. }
  110. // Register 手机号注册
  111. // @Summary 手机号注册
  112. // @Tags ADMIN
  113. // @Description 手机号注册
  114. // @Accept json
  115. // @Produce json
  116. // @Param req body md.LoginReq true "用户名、验证码"
  117. // @Success 200 {object} md.LoginResponse "token"
  118. // @Failure 400 {object} md.Response "具体错误"
  119. // @Router /api/login/register [post]
  120. func Register(c *gin.Context) {
  121. var req md.RegisterReq
  122. err := c.ShouldBindJSON(&req)
  123. if err != nil {
  124. err = validate.HandleValidateErr(err)
  125. err1 := err.(e.E)
  126. e.OutErr(c, err1.Code, err1.Error())
  127. return
  128. }
  129. masterId := svc.GetMasterId(c)
  130. engine := db.DBs[svc.GetMasterId(c)]
  131. check, err := svc.SmsCheck(c, req.UserName, req.Captcha)
  132. if check == false {
  133. e.OutErr(c, 400, e.NewErr(400, err.Error()))
  134. return
  135. }
  136. now := time.Now()
  137. //1、判断当前账号是否已存在
  138. agentDb := implement.NewAgentDb(engine)
  139. agent, err := agentDb.GetAgentByUsername(req.UserName)
  140. if err != nil {
  141. e.OutErr(c, e.ERR_DB_ORM, err)
  142. return
  143. }
  144. if agent != nil {
  145. e.OutErr(c, e.ERR_NO_DATA, "账号已存在!")
  146. return
  147. }
  148. //2、生成 agent_list 记录
  149. agentListDb := implement2.NewAgentListDb(db.Db)
  150. agentId := utils.GenerateUniqueRandomNumbers(8)
  151. agentList := model.AgentList{
  152. Uuid: utils.StrToInt(masterId),
  153. AgentId: utils.StrToInt(agentId),
  154. Kind: 1,
  155. CompanyName: "",
  156. CompanyAbbreviation: "",
  157. UnifiedSocialCreditCode: "",
  158. CertificateType: 1,
  159. BusinessLicenseImgUrl: "",
  160. LegalRepresentative: "",
  161. CountryRegionId: 1,
  162. CountryRegion: "",
  163. RegisteredAddressProvinceId: 0,
  164. RegisteredAddressCityId: 0,
  165. RegisteredAddressCountyId: 0,
  166. RegisteredAddress: "",
  167. BusinessLicenseAddress: "",
  168. CertificateValidity: "",
  169. State: 0,
  170. CreateAt: now.Format("2006-01-02 15:04:05"),
  171. UpdateAt: now.Format("2006-01-02 15:04:05"),
  172. }
  173. insertAffected, err := agentListDb.AgentListInsert(&agentList)
  174. if err != nil {
  175. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  176. return
  177. }
  178. if insertAffected <= 0 {
  179. e.OutErr(c, e.ERR_DB_ORM, "生成 记录失败")
  180. return
  181. }
  182. //3、新增 agent 记录
  183. agentModel := model2.Agent{
  184. AgentId: utils.StrToInt(agentId),
  185. Username: req.UserName,
  186. Password: utils.Md5(req.PassWord),
  187. State: 1,
  188. IsSuperAdministrator: 1,
  189. Memo: "",
  190. CreateAt: now.Format("2006-01-02 15:04:05"),
  191. UpdateAt: now.Format("2006-01-02 15:04:05"),
  192. }
  193. insertAffected, err = agentDb.AgentInsert(&agentModel)
  194. if err != nil {
  195. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  196. return
  197. }
  198. if insertAffected <= 0 {
  199. e.OutErr(c, e.ERR_DB_ORM, "新增 记录失败")
  200. return
  201. }
  202. ip := utils.GetIP(c.Request)
  203. key := fmt.Sprintf(md.JwtTokenKey, ip, utils.AnyToString(agentModel.Id))
  204. token, err := svc.HandleLoginToken(key, &agentModel)
  205. if err != nil {
  206. e.OutErr(c, e.ERR, err.Error())
  207. return
  208. }
  209. e.OutSuc(c, md.LoginResponse{
  210. Token: token,
  211. }, nil)
  212. return
  213. }