login_api_controller.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package mobile_api_controllers
  2. import (
  3. "XT_New/enums"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "XT_New/models"
  10. "XT_New/service"
  11. "XT_New/utils"
  12. "github.com/astaxie/beego"
  13. )
  14. type LoginAPIController struct {
  15. MobileBaseAPIController
  16. }
  17. func (this *LoginAPIController) LoginByCs() {
  18. mobile := this.GetString("mobile")
  19. pwd := this.GetString("password")
  20. if len(mobile) == 0 || len(pwd) == 0 || utils.CellPhoneRegexp().MatchString(mobile) == false {
  21. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  22. return
  23. }
  24. ip := utils.GetIP(this.Ctx.Request)
  25. ssoDomain := beego.AppConfig.String("sso_domain")
  26. api := ssoDomain + "/m/login/pwd"
  27. values := make(url.Values)
  28. values.Set("mobile", mobile)
  29. values.Set("password", pwd)
  30. values.Set("app_type", "3")
  31. values.Set("ip", ip)
  32. resp, requestErr := http.PostForm(api, values)
  33. if requestErr != nil {
  34. utils.ErrorLog("请求SSO登录接口失败: %v", requestErr)
  35. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  36. return
  37. }
  38. defer resp.Body.Close()
  39. body, ioErr := ioutil.ReadAll(resp.Body)
  40. if ioErr != nil {
  41. utils.ErrorLog("SSO登录接口返回数据读取失败: %v", ioErr)
  42. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  43. return
  44. }
  45. var respJSON map[string]interface{}
  46. utils.InfoLog(string(body))
  47. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  48. utils.ErrorLog("SSO登录接口返回数据解析JSON失败: %v", err)
  49. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  50. return
  51. }
  52. if respJSON["state"].(float64) != 1 {
  53. msg := respJSON["msg"].(string)
  54. utils.ErrorLog("SSO登录接口请求失败: %v", msg)
  55. if int(respJSON["code"].(float64)) == 609 {
  56. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  57. return
  58. }
  59. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  60. return
  61. } else {
  62. utils.SuccessLog("SSO登录成功")
  63. // 下面这几段 Map=>JSON=>Struct 的流程可能会造成速度很慢
  64. userJSON := respJSON["data"].(map[string]interface{})["admin"].(map[string]interface{})
  65. userJSONBytes, _ := json.Marshal(userJSON)
  66. var adminUser models.AdminUser
  67. if err := json.Unmarshal(userJSONBytes, &adminUser); err != nil {
  68. utils.ErrorLog("解析管理员失败:%v", err)
  69. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  70. return
  71. }
  72. orgJSON := respJSON["data"].(map[string]interface{})["org"].(map[string]interface{})
  73. orgJSONBytes, _ := json.Marshal(orgJSON)
  74. var org models.Org
  75. if err := json.Unmarshal(orgJSONBytes, &org); err != nil {
  76. utils.ErrorLog("解析机构失败:%v", err)
  77. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  78. return
  79. }
  80. appJSON := respJSON["data"].(map[string]interface{})["app"].(map[string]interface{})
  81. appJSONBytes, _ := json.Marshal(appJSON)
  82. var app models.OrgApp
  83. if err := json.Unmarshal(appJSONBytes, &app); err != nil {
  84. utils.ErrorLog("解析应用失败:%v", err)
  85. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  86. return
  87. }
  88. appRoleJSON := respJSON["data"].(map[string]interface{})["app_role"].(map[string]interface{})
  89. appRoleJSONBytes, _ := json.Marshal(appRoleJSON)
  90. var appRole models.App_Role
  91. if err := json.Unmarshal(appRoleJSONBytes, &appRole); err != nil {
  92. utils.ErrorLog("解析AppRole失败:%v", err)
  93. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  94. return
  95. }
  96. subscibeJSON := respJSON["data"].(map[string]interface{})["subscibe"].(map[string]interface{})
  97. subscibeJSONBytes, _ := json.Marshal(subscibeJSON)
  98. var subscibe models.ServeSubscibe
  99. if err := json.Unmarshal(subscibeJSONBytes, &subscibe); err != nil {
  100. utils.ErrorLog("解析Subscibe失败:%v", err)
  101. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  102. return
  103. }
  104. service.GetOrgSubscibeState(&subscibe)
  105. templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
  106. mobileAdminUserInfo := &MobileAdminUserInfo{
  107. AdminUser: &adminUser,
  108. Org: &org,
  109. App: &app,
  110. AppRole: &appRole,
  111. Subscibe: &subscibe,
  112. TemplateInfo: &templateInfo,
  113. }
  114. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  115. // configList, _ := service.GetConfigList(org.Id)
  116. // var FiledList []*models.FiledConfig
  117. // FiledList, _ = service.FindFiledByOrgId(org.Id)
  118. // if len(FiledList) == 0 {
  119. // err := service.BatchInsertFiledConfig(org.Id)
  120. // if err == nil {
  121. // FiledList, _ = service.FindFiledByOrgId(org.Id)
  122. // } else {
  123. // utils.ErrorLog("字段批量插入失败:%v", err)
  124. // }
  125. // }
  126. this.ServeSuccessJSON(map[string]interface{}{
  127. "user": map[string]interface{}{
  128. "id": adminUser.Id,
  129. "mobile": adminUser.Mobile,
  130. "user_name": appRole.UserName,
  131. "avatar": appRole.Avatar,
  132. "intro": appRole.Intro,
  133. "user_type": appRole.UserType,
  134. "user_title": appRole.UserTitle,
  135. },
  136. "org": map[string]interface{}{
  137. "id": org.Id,
  138. "org_name": org.OrgName,
  139. "org_short_name": org.OrgShortName,
  140. "org_intro": org.OrgIntroduction,
  141. "org_logo": org.OrgLogo,
  142. "province": org.Province,
  143. "city": org.City,
  144. "district": org.District,
  145. "address": org.Address,
  146. },
  147. "subscibe": map[string]interface{}{
  148. "id": subscibe.ID,
  149. "period_start": subscibe.PeriodStart,
  150. "period_end": subscibe.PeriodEnd,
  151. "state": subscibe.State,
  152. }, "template_info": map[string]interface{}{
  153. "id": templateInfo.ID,
  154. "org_id": templateInfo.OrgId,
  155. "template_id": templateInfo.TemplateId,
  156. },
  157. // "config_list": configList,
  158. // "filed_list": FiledList,
  159. })
  160. }
  161. }
  162. // /m/api/login/pwd [post] LoginByPwd
  163. // @param mobile:string
  164. // @param password:string
  165. func (this *LoginAPIController) LoginByPwd() {
  166. mobile := this.GetString("mobile")
  167. pwd := this.GetString("password")
  168. if len(mobile) == 0 || len(pwd) == 0 || utils.CellPhoneRegexp().MatchString(mobile) == false {
  169. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  170. return
  171. }
  172. ip := utils.GetIP(this.Ctx.Request)
  173. ssoDomain := beego.AppConfig.String("sso_domain")
  174. api := ssoDomain + "/m/login/pwd"
  175. values := make(url.Values)
  176. values.Set("mobile", mobile)
  177. values.Set("password", pwd)
  178. values.Set("app_type", "3")
  179. values.Set("ip", ip)
  180. resp, requestErr := http.PostForm(api, values)
  181. if requestErr != nil {
  182. utils.ErrorLog("请求SSO登录接口失败: %v", requestErr)
  183. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  184. return
  185. }
  186. defer resp.Body.Close()
  187. body, ioErr := ioutil.ReadAll(resp.Body)
  188. if ioErr != nil {
  189. utils.ErrorLog("SSO登录接口返回数据读取失败: %v", ioErr)
  190. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  191. return
  192. }
  193. var respJSON map[string]interface{}
  194. utils.InfoLog(string(body))
  195. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  196. utils.ErrorLog("SSO登录接口返回数据解析JSON失败: %v", err)
  197. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  198. return
  199. }
  200. if respJSON["state"].(float64) != 1 {
  201. msg := respJSON["msg"].(string)
  202. utils.ErrorLog("SSO登录接口请求失败: %v", msg)
  203. if int(respJSON["code"].(float64)) == 609 {
  204. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  205. return
  206. }
  207. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  208. return
  209. } else {
  210. utils.SuccessLog("SSO登录成功")
  211. // 下面这几段 Map=>JSON=>Struct 的流程可能会造成速度很慢
  212. userJSON := respJSON["data"].(map[string]interface{})["admin"].(map[string]interface{})
  213. userJSONBytes, _ := json.Marshal(userJSON)
  214. var adminUser models.AdminUser
  215. if err := json.Unmarshal(userJSONBytes, &adminUser); err != nil {
  216. utils.ErrorLog("解析管理员失败:%v", err)
  217. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  218. return
  219. }
  220. orgJSON := respJSON["data"].(map[string]interface{})["org"].(map[string]interface{})
  221. orgJSONBytes, _ := json.Marshal(orgJSON)
  222. var org models.Org
  223. if err := json.Unmarshal(orgJSONBytes, &org); err != nil {
  224. utils.ErrorLog("解析机构失败:%v", err)
  225. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  226. return
  227. }
  228. appJSON := respJSON["data"].(map[string]interface{})["app"].(map[string]interface{})
  229. appJSONBytes, _ := json.Marshal(appJSON)
  230. var app models.OrgApp
  231. if err := json.Unmarshal(appJSONBytes, &app); err != nil {
  232. utils.ErrorLog("解析应用失败:%v", err)
  233. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  234. return
  235. }
  236. appRoleJSON := respJSON["data"].(map[string]interface{})["app_role"].(map[string]interface{})
  237. appRoleJSONBytes, _ := json.Marshal(appRoleJSON)
  238. var appRole models.App_Role
  239. if err := json.Unmarshal(appRoleJSONBytes, &appRole); err != nil {
  240. utils.ErrorLog("解析AppRole失败:%v", err)
  241. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  242. return
  243. }
  244. subscibeJSON := respJSON["data"].(map[string]interface{})["subscibe"].(map[string]interface{})
  245. subscibeJSONBytes, _ := json.Marshal(subscibeJSON)
  246. var subscibe models.ServeSubscibe
  247. if err := json.Unmarshal(subscibeJSONBytes, &subscibe); err != nil {
  248. utils.ErrorLog("解析Subscibe失败:%v", err)
  249. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  250. return
  251. }
  252. service.GetOrgSubscibeState(&subscibe)
  253. templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
  254. mobileAdminUserInfo := &MobileAdminUserInfo{
  255. AdminUser: &adminUser,
  256. Org: &org,
  257. App: &app,
  258. AppRole: &appRole,
  259. Subscibe: &subscibe,
  260. TemplateInfo: &templateInfo,
  261. }
  262. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  263. //= utils.GenerateLoginToken(mobile)
  264. token := utils.GenerateLoginToken(mobile)
  265. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  266. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  267. fmt.Println(token)
  268. fmt.Println(this.Ctx.GetCookie("token_cookie"))
  269. configList, _ := service.GetConfigList(org.Id)
  270. var FiledList []*models.FiledConfig
  271. FiledList, _ = service.FindFiledByOrgId(org.Id)
  272. if len(FiledList) == 0 {
  273. err := service.BatchInsertFiledConfig(org.Id)
  274. if err == nil {
  275. FiledList, _ = service.FindFiledByOrgId(org.Id)
  276. } else {
  277. utils.ErrorLog("字段批量插入失败:%v", err)
  278. }
  279. }
  280. this.ServeSuccessJSON(map[string]interface{}{
  281. "user": map[string]interface{}{
  282. "id": adminUser.Id,
  283. "mobile": adminUser.Mobile,
  284. "user_name": appRole.UserName,
  285. "avatar": appRole.Avatar,
  286. "intro": appRole.Intro,
  287. "user_type": appRole.UserType,
  288. "user_title": appRole.UserTitle,
  289. },
  290. "org": map[string]interface{}{
  291. "id": org.Id,
  292. "org_name": org.OrgName,
  293. "org_short_name": org.OrgShortName,
  294. "org_intro": org.OrgIntroduction,
  295. "org_logo": org.OrgLogo,
  296. "province": org.Province,
  297. "city": org.City,
  298. "district": org.District,
  299. "address": org.Address,
  300. },
  301. "subscibe": map[string]interface{}{
  302. "id": subscibe.ID,
  303. "period_start": subscibe.PeriodStart,
  304. "period_end": subscibe.PeriodEnd,
  305. "state": subscibe.State,
  306. }, "template_info": map[string]interface{}{
  307. "id": templateInfo.ID,
  308. "org_id": templateInfo.OrgId,
  309. "template_id": templateInfo.TemplateId,
  310. },
  311. "config_list": configList,
  312. "filed_list": FiledList,
  313. })
  314. }
  315. }
  316. // /m/api/login/code [post] LoginByCode
  317. func (this *LoginAPIController) LoginByCode() {
  318. }