Go 后台接口书写
设置路由
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package healthinterface import ( "Ganzhou/config" "Ganzhou/gzq/healthinterface/routers/gzhealthrinterface" "Ganzhou/pkg/log" "Ganzhou/pkg/middleware" "github.com/gin-gonic/gin" "net/http" ) func LoadRouter() { gin.SetMode(config.AppMode) router := gin.Default() //pprof.Register(router) //检测性能 router.Use(middleware.Cors()) gzhealthrinterface.LoadInfaceRouter(router) srv := &http.Server{ Addr: ":" + config.AppPort, Handler: router, } err := srv.ListenAndServe() if err != nil { log.Fatal("listen: %s\n", err) } log.Info("Server exiting") }
设置地址和方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package gzhealthrinterface import ( "Ganzhou/gzq/healthinterface/routers/gzhealthrinterface/handler" "github.com/gin-gonic/gin" ) // 加载接口路由 func LoadInfaceRouter(app *gin.Engine) { gzif := app.Group(`/api`) gzif.GET("/getResidentArchive", handler.GetResidentArchive) //获取居民档案 gzif.GET("/getFamilyDoctorContract", handler.GetFamilyDoctorContract) //获取z家庭医生签约信息 gzif.GET("/getChronicDisease", handler.GetChronicDisease) //获取居民慢性病信息 gzif.GET("/getTheAged", handler.GetTheAged) //获取老年人管理信息 gzif.GET("/getTheChildren", handler.GetTheChildren) //获取儿童管理信息 gzif.GET("/getThePregnantWoman", handler.GetThePregnantWoman) //获取孕产妇管理信息 gzif.GET("/getComdatabyDays", handler.GetComdatabyDays) //获取公务系统统计数据 gzif.POST("/addContractServRec", handler.AddContractServRec) //his就诊添加履约记录 }
书写方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package handler import ( "Ganzhou/common" im "Ganzhou/gzq/healthinterface/model" bm "Ganzhou/model" "Ganzhou/pkg/gutil" "github.com/gin-gonic/gin" "strconv" "time" ) // 获取居民档案 func GetResidentArchive(c *gin.Context) { //patName := c.Query("patName") idCode := c.Query("idCode") if idCode == "" { im.ResFail(c, "idCode不可为空") return } id_no := common.EncryptionIdentity(idCode) data, err := bm.GetArchivesDataBYIdno(id_no) if err != nil { im.ResFail(c, err.Error()) } aDate := &im.Archive{} im.ArchiveRdata(&data, aDate) im.ResSuccess(c, aDate) return }
设置放回json
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package model import ( "Ganzhou/pkg/log" "fmt" "github.com/gin-gonic/gin" "net/http" ) // 响应成功 func ResSuccess(c *gin.Context, data interface{}) { c.JSON(http.StatusOK, &resplace{Code: 1, Msg: "OK", Data: data, Ret: true}) c.Abort() } // 响应失败 func ResFail(c *gin.Context, msg string) { log.RespFail(fmt.Sprintf("%s", msg)) //输出日志 c.JSON(http.StatusOK, &resplace{Code: 2, Msg: msg, Ret: false}) c.Abort() } //旧的resp对象 type resplace struct { Ret bool `json:"ret"` Msg string `json:"msg"` Code int `json:"code"` Data interface{} `json:"data"` }
post请求参数的接受
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
func AddContractServRec(c *gin.Context) { var data im.RecServRec if err := c.ShouldBind(&data); err != nil { im.ResFail(c, err.Error()) return } if data.LocalContractId == 0 { im.ResFail(c, "LocalContractId不可为空") return } layput := "2006-01-02" _, ber := time.Parse(layput, data.ServiceTime) if ber != nil { im.ResFail(c, ber.Error()) return } _, ber = time.Parse(layput, data.NextServiceTime) if ber != nil { im.ResFail(c, ber.Error()) return } err := im.AddServRec(data) if err == nil { im.ResSuccess(c, "添加成功!") } else { im.ResFail(c, err.Error()) } return }