广告平台(媒体使用)
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

215 righe
6.1 KiB

  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. CompanyAbbreviation: "",
  156. UnifiedSocialCreditCode: "",
  157. BusinessLicenseImgUrl: "",
  158. LegalRepresentative: "",
  159. CountryRegionId: 1,
  160. CountryRegion: "",
  161. RegisteredAddressProvinceId: 0,
  162. RegisteredAddressCityId: 0,
  163. RegisteredAddressCountyId: 0,
  164. RegisteredAddress: "",
  165. BusinessLicenseAddress: "",
  166. CertificateValidity: "",
  167. State: 0,
  168. CreateAt: now.Format("2006-01-02 15:04:05"),
  169. UpdateAt: now.Format("2006-01-02 15:04:05"),
  170. }
  171. insertAffected, err := mediumListDb.MediumListInsert(&mediumList)
  172. if err != nil {
  173. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  174. return
  175. }
  176. if insertAffected <= 0 {
  177. e.OutErr(c, e.ERR_DB_ORM, "生成 medium_list 记录失败")
  178. return
  179. }
  180. //3、新增 medium 记录
  181. mediumModel := model2.Medium{
  182. MediumId: utils.StrToInt(mediumId),
  183. Username: req.UserName,
  184. Password: utils.Md5(req.PassWord),
  185. State: 1,
  186. IsSuperAdministrator: 1,
  187. Memo: "",
  188. CreateAt: now.Format("2006-01-02 15:04:05"),
  189. UpdateAt: now.Format("2006-01-02 15:04:05"),
  190. }
  191. insertAffected, err = mediumDb.MediumInsert(&mediumModel)
  192. if err != nil {
  193. return
  194. }
  195. if insertAffected <= 0 {
  196. e.OutErr(c, e.ERR_DB_ORM, "新增 medium 记录失败")
  197. return
  198. }
  199. ip := utils.GetIP(c.Request)
  200. key := fmt.Sprintf(md.JwtTokenKey, ip, utils.AnyToString(mediumModel.Id))
  201. token, err := svc.HandleLoginToken(key, &mediumModel)
  202. if err != nil {
  203. e.OutErr(c, e.ERR, err.Error())
  204. return
  205. }
  206. e.OutSuc(c, md.LoginResponse{
  207. Token: token,
  208. }, nil)
  209. return
  210. }