广告平台(站长下代理使用)
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.
 
 
 
 
 
 

237 lines
6.6 KiB

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