orginfo_api_controller.go 8.5KB

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