分销商城(微商城)接口项目

orginfo_api_controller.go 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "time"
  6. "wsc-go/enums"
  7. "wsc-go/models"
  8. "wsc-go/service"
  9. "wsc-go/utils"
  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. c.ServeSuccessJSON(map[string]interface{}{
  172. "msg": "ok",
  173. })
  174. return
  175. }
  176. func (c *OrgInfoApiController) SaveOrgGallery() {
  177. adminUserInfo := c.GetAdminUserInfo()
  178. dataBody := make(map[string]interface{}, 0)
  179. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  180. if err != nil {
  181. utils.ErrorLog(err.Error())
  182. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  183. return
  184. }
  185. if dataBody["type"] == nil || reflect.TypeOf(dataBody["type"]).String() != "float64" {
  186. utils.ErrorLog("type")
  187. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  188. return
  189. }
  190. galleryType := int64(dataBody["type"].(float64))
  191. if galleryType != 1 && galleryType != 2 {
  192. utils.ErrorLog("galleryType != 1&&2")
  193. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  194. return
  195. }
  196. if dataBody["url"] == nil || reflect.TypeOf(dataBody["url"]).String() != "string" {
  197. utils.ErrorLog("url")
  198. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  199. return
  200. }
  201. url, _ := dataBody["url"].(string)
  202. if len(url) == 0 {
  203. utils.ErrorLog("len(url) == 0")
  204. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  205. return
  206. }
  207. timeNow := time.Now().Unix()
  208. var gallery models.OrgGallery
  209. gallery.Type = galleryType
  210. gallery.Url = url
  211. gallery.OrgId = adminUserInfo.CurrentOrgId
  212. gallery.Status = 1
  213. gallery.Ctime = timeNow
  214. gallery.Mtime = timeNow
  215. err = service.CreateOrgGalleryItem(&gallery)
  216. if err != nil {
  217. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  218. return
  219. }
  220. orgInfo := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  221. orgInfo.OrgGallery = append(orgInfo.OrgGallery, &gallery)
  222. c.ServeSuccessJSON(map[string]interface{}{
  223. "msg": "ok",
  224. })
  225. return
  226. }
  227. func (c *OrgInfoApiController) DeleteOrgGallery() {
  228. adminUserInfo := c.GetAdminUserInfo()
  229. id, _ := c.GetInt64("id", 0)
  230. if id <= 0 {
  231. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  232. return
  233. }
  234. err := service.DeleteOrgGalleryItem(id)
  235. if err != nil {
  236. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  237. return
  238. }
  239. orgInfo := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  240. for index, item := range orgInfo.OrgGallery {
  241. if item.ID == id {
  242. orgInfo.OrgGallery = append(orgInfo.OrgGallery[:index], orgInfo.OrgGallery[index+1:]...)
  243. }
  244. }
  245. c.ServeSuccessJSON(map[string]interface{}{
  246. "msg": "ok",
  247. })
  248. return
  249. }