login_api_controller.go 11KB

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