orginfo_api_controller.go 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_New/utils"
  7. "encoding/json"
  8. "reflect"
  9. "time"
  10. "github.com/astaxie/beego"
  11. )
  12. type OrgInfoApiController struct {
  13. BaseAuthAPIController
  14. }
  15. func OrgInfoApiRegistRouters() {
  16. beego.Router("/api/orginfo/getinfo", &OrgInfoApiController{}, "get:GetOrgInfo")
  17. beego.Router("/api/orginfo/savegallery", &OrgInfoApiController{}, "post:SaveOrgGallery")
  18. beego.Router("/api/orginfo/deletegallery", &OrgInfoApiController{}, "delete:DeleteOrgGallery")
  19. beego.Router("/api/orginfo/edit", &OrgInfoApiController{}, "post:EditOrgInfo")
  20. }
  21. func (c *OrgInfoApiController) GetOrgInfo() {
  22. adminUserInfo := c.GetAdminUserInfo()
  23. orgInfo := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  24. provinces, _ := service.GetDistrictsByUpid(0)
  25. var citys []*models.District
  26. var districts []*models.District
  27. if orgInfo.Province > 0 {
  28. citys, _ = service.GetDistrictsByUpid(orgInfo.Province)
  29. }
  30. if orgInfo.City > 0 {
  31. districts, _ = service.GetDistrictsByUpid(orgInfo.City)
  32. }
  33. orgtypes, _ := service.GetOrgTypes()
  34. illness, _ := service.GetIllnessList()
  35. c.ServeSuccessJSON(map[string]interface{}{
  36. "orginfo": orgInfo,
  37. "provinces": provinces,
  38. "citys": citys,
  39. "districts": districts,
  40. "orgtypes": orgtypes,
  41. "illness": illness,
  42. })
  43. return
  44. }
  45. func (c *OrgInfoApiController) EditOrgInfo() {
  46. adminUserInfo := c.GetAdminUserInfo()
  47. if !adminUserInfo.AdminUser.IsSuperAdmin {
  48. c.ServeFailJsonSend(enums.ErrorCodePermissionDenied, "权限不足")
  49. return
  50. }
  51. dataBody := make(map[string]interface{}, 0)
  52. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  53. if err != nil {
  54. utils.ErrorLog(err.Error())
  55. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  56. return
  57. }
  58. orgInfo := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  59. org := *orgInfo
  60. if dataBody["org_name"] == nil || reflect.TypeOf(dataBody["org_name"]).String() != "string" {
  61. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:机构名称")
  62. return
  63. }
  64. orgName, _ := dataBody["org_name"].(string)
  65. if len(orgName) == 0 {
  66. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "机构名称不能为空")
  67. return
  68. }
  69. org.OrgName = orgName
  70. if dataBody["contact_name"] == nil || reflect.TypeOf(dataBody["contact_name"]).String() != "string" {
  71. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:联系人姓名")
  72. return
  73. }
  74. contactName, _ := dataBody["contact_name"].(string)
  75. if len(contactName) == 0 {
  76. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "联系人姓名不能为空")
  77. return
  78. }
  79. org.ContactName = contactName
  80. // if dataBody["org_short_name"] == nil || reflect.TypeOf(dataBody["org_short_name"]).String() != "string" {
  81. // c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:商家简称")
  82. // return
  83. // }
  84. // orgShortName, _ := dataBody["org_short_name"].(string)
  85. // if len(orgShortName) == 0 {
  86. // c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "商家简称不能为空")
  87. // return
  88. // }
  89. org.OrgShortName = orgName
  90. if dataBody["org_introduction"] == nil || reflect.TypeOf(dataBody["org_introduction"]).String() != "string" {
  91. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:机构介绍")
  92. return
  93. }
  94. orgIntroduction, _ := dataBody["org_introduction"].(string)
  95. if len(orgIntroduction) == 0 {
  96. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "机构介绍不能为空")
  97. return
  98. }
  99. org.OrgIntroduction = orgIntroduction
  100. if dataBody["org_logo"] == nil || reflect.TypeOf(dataBody["org_logo"]).String() != "string" {
  101. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:机构头像")
  102. return
  103. }
  104. orgLogo, _ := dataBody["org_logo"].(string)
  105. if len(orgLogo) == 0 {
  106. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "机构头像不能为空")
  107. return
  108. }
  109. org.OrgLogo = orgLogo
  110. if dataBody["province"] != nil || reflect.TypeOf(dataBody["province"]).String() == "float64" {
  111. province := int64(dataBody["province"].(float64))
  112. org.Province = province
  113. }
  114. if dataBody["city"] != nil || reflect.TypeOf(dataBody["city"]).String() == "float64" {
  115. city := int64(dataBody["city"].(float64))
  116. org.City = city
  117. }
  118. if dataBody["district"] != nil || reflect.TypeOf(dataBody["district"]).String() == "float64" {
  119. district := int64(dataBody["district"].(float64))
  120. org.District = district
  121. }
  122. if dataBody["address"] != nil || reflect.TypeOf(dataBody["address"]).String() == "string" {
  123. address, _ := dataBody["address"].(string)
  124. org.Address = address
  125. }
  126. if dataBody["org_type"] != nil || reflect.TypeOf(dataBody["org_type"]).String() == "float64" {
  127. orgType := int64(dataBody["org_type"].(float64))
  128. org.OrgType = orgType
  129. }
  130. if dataBody["telephone"] != nil || reflect.TypeOf(dataBody["telephone"]).String() == "string" {
  131. telephone, _ := dataBody["telephone"].(string)
  132. org.Telephone = telephone
  133. }
  134. if dataBody["operating_state"] != nil || reflect.TypeOf(dataBody["operating_state"]).String() == "float64" {
  135. operatingState := int64(dataBody["operating_state"].(float64))
  136. org.OperatingState = operatingState
  137. }
  138. if dataBody["business_week"] != nil || reflect.TypeOf(dataBody["business_week"]).String() == "string" {
  139. businessWeek, _ := dataBody["business_week"].(string)
  140. org.BusinessWeek = businessWeek
  141. }
  142. if dataBody["business_time"] != nil || reflect.TypeOf(dataBody["business_time"]).String() == "string" {
  143. businessTime, _ := dataBody["business_time"].(string)
  144. org.BusinessTime = businessTime
  145. }
  146. if dataBody["illness"] != nil || reflect.TypeOf(dataBody["illness"]).String() == "string" {
  147. illness, _ := dataBody["illness"].(string)
  148. org.Illness = illness
  149. }
  150. timeNow := time.Now().Unix()
  151. org.ModifyTime = timeNow
  152. org.OrgGallery = nil
  153. err = service.UpdateOrgInfo(&org)
  154. if err != nil {
  155. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  156. return
  157. }
  158. orgInfo.OrgName = orgName
  159. orgInfo.OrgShortName = orgName
  160. orgInfo.OrgIntroduction = orgIntroduction
  161. orgInfo.OrgLogo = orgLogo
  162. orgInfo.Province = org.Province
  163. orgInfo.District = org.District
  164. orgInfo.City = org.City
  165. orgInfo.OrgType = org.OrgType
  166. orgInfo.Telephone = org.Telephone
  167. orgInfo.OperatingState = org.OperatingState
  168. orgInfo.BusinessWeek = org.BusinessWeek
  169. orgInfo.BusinessTime = org.BusinessTime
  170. orgInfo.Illness = org.Illness
  171. orgInfo.ContactName = org.ContactName
  172. c.ServeSuccessJSON(map[string]interface{}{
  173. "msg": "ok",
  174. })
  175. return
  176. }
  177. func (c *OrgInfoApiController) SaveOrgGallery() {
  178. adminUserInfo := c.GetAdminUserInfo()
  179. dataBody := make(map[string]interface{}, 0)
  180. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  181. if err != nil {
  182. utils.ErrorLog(err.Error())
  183. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  184. return
  185. }
  186. if dataBody["type"] == nil || reflect.TypeOf(dataBody["type"]).String() != "float64" {
  187. utils.ErrorLog("type")
  188. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  189. return
  190. }
  191. galleryType := int64(dataBody["type"].(float64))
  192. if galleryType != 1 && galleryType != 2 {
  193. utils.ErrorLog("galleryType != 1&&2")
  194. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  195. return
  196. }
  197. if dataBody["url"] == nil || reflect.TypeOf(dataBody["url"]).String() != "string" {
  198. utils.ErrorLog("url")
  199. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  200. return
  201. }
  202. url, _ := dataBody["url"].(string)
  203. if len(url) == 0 {
  204. utils.ErrorLog("len(url) == 0")
  205. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  206. return
  207. }
  208. timeNow := time.Now().Unix()
  209. var gallery models.OrgGallery
  210. gallery.Type = galleryType
  211. gallery.Url = url
  212. gallery.OrgId = adminUserInfo.CurrentOrgId
  213. gallery.Status = 1
  214. gallery.Ctime = timeNow
  215. gallery.Mtime = timeNow
  216. err = service.CreateOrgGalleryItem(&gallery)
  217. if err != nil {
  218. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  219. return
  220. }
  221. orgInfo := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  222. orgInfo.OrgGallery = append(orgInfo.OrgGallery, &gallery)
  223. c.ServeSuccessJSON(map[string]interface{}{
  224. "msg": "ok",
  225. })
  226. return
  227. }
  228. func (c *OrgInfoApiController) DeleteOrgGallery() {
  229. adminUserInfo := c.GetAdminUserInfo()
  230. id, _ := c.GetInt64("id", 0)
  231. if id <= 0 {
  232. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  233. return
  234. }
  235. err := service.DeleteOrgGalleryItem(id)
  236. if err != nil {
  237. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  238. return
  239. }
  240. orgInfo := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  241. for index, item := range orgInfo.OrgGallery {
  242. if item.ID == id {
  243. orgInfo.OrgGallery = append(orgInfo.OrgGallery[:index], orgInfo.OrgGallery[index+1:]...)
  244. }
  245. }
  246. c.ServeSuccessJSON(map[string]interface{}{
  247. "msg": "ok",
  248. })
  249. return
  250. }