forked from free-lancers/electricity_bill_calc_service
		
	feat(response):增加新的用于表示用户未获得授权的响应方法。
This commit is contained in:
		| @@ -31,7 +31,7 @@ func listMaintenanceFees(c *gin.Context) { | |||||||
| 	result := response.NewResult(c) | 	result := response.NewResult(c) | ||||||
| 	userSession, err := _retreiveSession(c) | 	userSession, err := _retreiveSession(c) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | 		result.Unauthorized(err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	requestPark := c.DefaultQuery("park", "") | 	requestPark := c.DefaultQuery("park", "") | ||||||
| @@ -42,7 +42,7 @@ func listMaintenanceFees(c *gin.Context) { | |||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 		if !sure { | 		if !sure { | ||||||
| 			result.NotAccept("不能访问不属于自己的园区。") | 			result.Unauthorized("不能访问不属于自己的园区。") | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 		fees, err := service.MaintenanceFeeService.ListMaintenanceFees([]string{requestPark}) | 		fees, err := service.MaintenanceFeeService.ListMaintenanceFees([]string{requestPark}) | ||||||
| @@ -79,7 +79,7 @@ func createMaintenanceFeeRecord(c *gin.Context) { | |||||||
| 	c.BindJSON(formData) | 	c.BindJSON(formData) | ||||||
| 	userSession, err := _retreiveSession(c) | 	userSession, err := _retreiveSession(c) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | 		result.Unauthorized(err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	sure, err := service.ParkService.EnsurePark(userSession.Uid, formData.ParkId) | 	sure, err := service.ParkService.EnsurePark(userSession.Uid, formData.ParkId) | ||||||
| @@ -88,7 +88,7 @@ func createMaintenanceFeeRecord(c *gin.Context) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	if !sure { | 	if !sure { | ||||||
| 		result.NotAccept("不能访问不属于自己的园区。") | 		result.Unauthorized("不能访问不属于自己的园区。") | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	newMaintenanceFee := &model.MaintenanceFee{} | 	newMaintenanceFee := &model.MaintenanceFee{} | ||||||
|   | |||||||
| @@ -37,7 +37,7 @@ func listAllParksUnderSessionUser(c *gin.Context) { | |||||||
| 	result := response.NewResult(c) | 	result := response.NewResult(c) | ||||||
| 	userSession, err := _retreiveSession(c) | 	userSession, err := _retreiveSession(c) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | 		result.Unauthorized(err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	parks, err := service.ParkService.ListAllParkBelongsTo(userSession.Uid) | 	parks, err := service.ParkService.ListAllParkBelongsTo(userSession.Uid) | ||||||
| @@ -76,7 +76,7 @@ func createNewPark(c *gin.Context) { | |||||||
| 	result := response.NewResult(c) | 	result := response.NewResult(c) | ||||||
| 	userSession, err := _retreiveSession(c) | 	userSession, err := _retreiveSession(c) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | 		result.Unauthorized(err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	formData := new(_ParkInfoFormData) | 	formData := new(_ParkInfoFormData) | ||||||
| @@ -100,7 +100,7 @@ func modifyPark(c *gin.Context) { | |||||||
| 	result := response.NewResult(c) | 	result := response.NewResult(c) | ||||||
| 	userSession, err := _retreiveSession(c) | 	userSession, err := _retreiveSession(c) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | 		result.Unauthorized(err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	requestParkId := c.Param("pid") | 	requestParkId := c.Param("pid") | ||||||
| @@ -112,7 +112,7 @@ func modifyPark(c *gin.Context) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	if userSession.Uid != park.UserId { | 	if userSession.Uid != park.UserId { | ||||||
| 		result.NotAccept("不能修改不属于自己的园区。") | 		result.Unauthorized("不能修改不属于自己的园区。") | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	copier.Copy(park, formData) | 	copier.Copy(park, formData) | ||||||
| @@ -130,7 +130,7 @@ func fetchParkDetail(c *gin.Context) { | |||||||
| 	result := response.NewResult(c) | 	result := response.NewResult(c) | ||||||
| 	userSession, err := _retreiveSession(c) | 	userSession, err := _retreiveSession(c) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | 		result.Unauthorized(err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	requestParkId := c.Param("pid") | 	requestParkId := c.Param("pid") | ||||||
| @@ -140,7 +140,7 @@ func fetchParkDetail(c *gin.Context) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	if userSession.Uid != park.UserId { | 	if userSession.Uid != park.UserId { | ||||||
| 		result.NotAccept("不能访问不属于自己的园区。") | 		result.Unauthorized("不能访问不属于自己的园区。") | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	result.Json(http.StatusOK, "已经获取到指定园区的信息。", gin.H{"park": park}) | 	result.Json(http.StatusOK, "已经获取到指定园区的信息。", gin.H{"park": park}) | ||||||
| @@ -154,7 +154,7 @@ func changeParkEnableState(c *gin.Context) { | |||||||
| 	result := response.NewResult(c) | 	result := response.NewResult(c) | ||||||
| 	userSession, err := _retreiveSession(c) | 	userSession, err := _retreiveSession(c) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | 		result.Unauthorized(err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	requestParkId := c.Param("pid") | 	requestParkId := c.Param("pid") | ||||||
| @@ -172,7 +172,7 @@ func deleteSpecificPark(c *gin.Context) { | |||||||
| 	result := response.NewResult(c) | 	result := response.NewResult(c) | ||||||
| 	userSession, err := _retreiveSession(c) | 	userSession, err := _retreiveSession(c) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | 		result.Unauthorized(err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	requestParkId := c.Param("pid") | 	requestParkId := c.Param("pid") | ||||||
|   | |||||||
| @@ -42,6 +42,14 @@ func (r *Result) Error(code int, msg string) { | |||||||
| 	r.Ctx.JSON(http.StatusOK, res) | 	r.Ctx.JSON(http.StatusOK, res) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // 用户未获得授权)响应 | ||||||
|  | func (r *Result) Unauthorized(msg string) { | ||||||
|  | 	res := BaseResponse{} | ||||||
|  | 	res.Code = http.StatusUnauthorized | ||||||
|  | 	res.Message = msg | ||||||
|  | 	r.Ctx.JSON(http.StatusOK, res) | ||||||
|  | } | ||||||
|  |  | ||||||
| // 简易操作成功信息 | // 简易操作成功信息 | ||||||
| func (r *Result) Success(msg string) { | func (r *Result) Success(msg string) { | ||||||
| 	res := BaseResponse{} | 	res := BaseResponse{} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user