广告平台(媒体使用)
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 7.4 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
1 month ago
2 months ago
1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.NewMediumDb(engine)
  38. admin, err := adminDb.GetMediumByUsername(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.NewMediumDb(engine)
  89. admin, err := adminDb.GetMediumByUsername(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/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. engine := db.DBs[svc.GetMasterId(c)]
  130. //check, err := svc.SmsCheck(c, req.UserName, req.Captcha)
  131. //if check == false {
  132. // e.OutErr(c, 400, e.NewErr(400, err.Error()))
  133. // return
  134. //}
  135. now := time.Now()
  136. //1、判断当前账号是否已存在
  137. mediumDb := implement.NewMediumDb(engine)
  138. medium, err := mediumDb.GetMediumByUsername(req.UserName)
  139. if err != nil {
  140. e.OutErr(c, e.ERR_DB_ORM, err)
  141. return
  142. }
  143. if medium != nil {
  144. e.OutErr(c, e.ERR_NO_DATA, "账号已存在!")
  145. return
  146. }
  147. //2、生成 medium_list 记录
  148. mediumListDb := implement2.NewMediumListDb(db.Db)
  149. mediumId := utils.GenerateUniqueRandomNumbers(8)
  150. mediumList := model.MediumList{
  151. Uuid: utils.StrToInt(c.GetString("mid")),
  152. MediumId: utils.StrToInt(mediumId),
  153. Kind: 1,
  154. CompanyName: "",
  155. SettlementType: 1,
  156. CompanyAbbreviation: "",
  157. UnifiedSocialCreditCode: "",
  158. BusinessLicenseImgUrl: "",
  159. LegalRepresentative: "",
  160. CountryRegionId: 1,
  161. CountryRegion: "",
  162. RegisteredAddressProvinceId: 0,
  163. RegisteredAddressCityId: 0,
  164. RegisteredAddressCountyId: 0,
  165. RegisteredAddress: "",
  166. BusinessLicenseAddress: "",
  167. CertificateValidity: "",
  168. State: 0,
  169. CreateAt: now.Format("2006-01-02 15:04:05"),
  170. UpdateAt: now.Format("2006-01-02 15:04:05"),
  171. }
  172. insertAffected, err := mediumListDb.MediumListInsert(&mediumList)
  173. if err != nil {
  174. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  175. return
  176. }
  177. if insertAffected <= 0 {
  178. e.OutErr(c, e.ERR_DB_ORM, "生成 medium_list 记录失败")
  179. return
  180. }
  181. //3、新增 medium 记录
  182. mediumModel := model2.Medium{
  183. MediumId: utils.StrToInt(mediumId),
  184. Username: req.UserName,
  185. Password: utils.Md5(req.PassWord),
  186. State: 1,
  187. IsSuperAdministrator: 1,
  188. Memo: "",
  189. CreateAt: now.Format("2006-01-02 15:04:05"),
  190. UpdateAt: now.Format("2006-01-02 15:04:05"),
  191. }
  192. insertAffected, err = mediumDb.MediumInsert(&mediumModel)
  193. if err != nil {
  194. return
  195. }
  196. if insertAffected <= 0 {
  197. e.OutErr(c, e.ERR_DB_ORM, "新增 medium 记录失败")
  198. return
  199. }
  200. //4、新增 MediumDivisionStrategy 记录
  201. MediumDivisionStrategyModel := model.MediumDivisionStrategy{
  202. MediumId: utils.StrToInt(mediumId),
  203. Uuid: utils.StrToInt(c.GetString("mid")),
  204. CreateAt: now.Format("2006-01-02 15:04:05"),
  205. UpdateAt: now.Format("2006-01-02 15:04:05"),
  206. }
  207. insertAffected, err = db.Db.Insert(&MediumDivisionStrategyModel)
  208. if err != nil {
  209. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  210. return
  211. }
  212. if insertAffected <= 0 {
  213. e.OutErr(c, e.ERR_DB_ORM, "新增 记录失败")
  214. return
  215. }
  216. //5、判断是否为渠道代理邀请
  217. if req.AgentId != "" {
  218. agentDb := implement.NewAgentDb(engine)
  219. agent, err1 := agentDb.GetAgent(utils.StrToInt(req.AgentId))
  220. if err1 != nil {
  221. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  222. return
  223. }
  224. if agent != nil {
  225. //5.1 新增agent_with_medium记录
  226. agentWithMediumDb := implement2.NewAgentWithMediumDb(engine)
  227. insertAffected, err = agentWithMediumDb.AgentWithMediumInsert(&model.AgentWithMedium{
  228. AgentId: utils.StrToInt(req.AgentId),
  229. MediumId: utils.StrToInt(mediumId),
  230. CreateAt: now.Format("2006-01-02 15:04:05"),
  231. UpdateAt: now.Format("2006-01-02 15:04:05"),
  232. })
  233. if err != nil {
  234. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  235. return
  236. }
  237. if insertAffected <= 0 {
  238. e.OutErr(c, e.ERR_DB_ORM, "新增 agent_with_medium 记录失败")
  239. return
  240. }
  241. }
  242. }
  243. ip := utils.GetIP(c.Request)
  244. key := fmt.Sprintf(md.JwtTokenKey, ip, utils.AnyToString(mediumModel.Id))
  245. token, err := svc.HandleLoginToken(key, &mediumModel)
  246. if err != nil {
  247. e.OutErr(c, e.ERR, err.Error())
  248. return
  249. }
  250. e.OutSuc(c, md.LoginResponse{
  251. Token: token,
  252. }, nil)
  253. return
  254. }