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) | ||||
| 	userSession, err := _retreiveSession(c) | ||||
| 	if err != nil { | ||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | ||||
| 		result.Unauthorized(err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	requestPark := c.DefaultQuery("park", "") | ||||
| @@ -42,7 +42,7 @@ func listMaintenanceFees(c *gin.Context) { | ||||
| 			return | ||||
| 		} | ||||
| 		if !sure { | ||||
| 			result.NotAccept("不能访问不属于自己的园区。") | ||||
| 			result.Unauthorized("不能访问不属于自己的园区。") | ||||
| 			return | ||||
| 		} | ||||
| 		fees, err := service.MaintenanceFeeService.ListMaintenanceFees([]string{requestPark}) | ||||
| @@ -79,7 +79,7 @@ func createMaintenanceFeeRecord(c *gin.Context) { | ||||
| 	c.BindJSON(formData) | ||||
| 	userSession, err := _retreiveSession(c) | ||||
| 	if err != nil { | ||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | ||||
| 		result.Unauthorized(err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	sure, err := service.ParkService.EnsurePark(userSession.Uid, formData.ParkId) | ||||
| @@ -88,7 +88,7 @@ func createMaintenanceFeeRecord(c *gin.Context) { | ||||
| 		return | ||||
| 	} | ||||
| 	if !sure { | ||||
| 		result.NotAccept("不能访问不属于自己的园区。") | ||||
| 		result.Unauthorized("不能访问不属于自己的园区。") | ||||
| 		return | ||||
| 	} | ||||
| 	newMaintenanceFee := &model.MaintenanceFee{} | ||||
|   | ||||
| @@ -37,7 +37,7 @@ func listAllParksUnderSessionUser(c *gin.Context) { | ||||
| 	result := response.NewResult(c) | ||||
| 	userSession, err := _retreiveSession(c) | ||||
| 	if err != nil { | ||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | ||||
| 		result.Unauthorized(err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	parks, err := service.ParkService.ListAllParkBelongsTo(userSession.Uid) | ||||
| @@ -76,7 +76,7 @@ func createNewPark(c *gin.Context) { | ||||
| 	result := response.NewResult(c) | ||||
| 	userSession, err := _retreiveSession(c) | ||||
| 	if err != nil { | ||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | ||||
| 		result.Unauthorized(err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	formData := new(_ParkInfoFormData) | ||||
| @@ -100,7 +100,7 @@ func modifyPark(c *gin.Context) { | ||||
| 	result := response.NewResult(c) | ||||
| 	userSession, err := _retreiveSession(c) | ||||
| 	if err != nil { | ||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | ||||
| 		result.Unauthorized(err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	requestParkId := c.Param("pid") | ||||
| @@ -112,7 +112,7 @@ func modifyPark(c *gin.Context) { | ||||
| 		return | ||||
| 	} | ||||
| 	if userSession.Uid != park.UserId { | ||||
| 		result.NotAccept("不能修改不属于自己的园区。") | ||||
| 		result.Unauthorized("不能修改不属于自己的园区。") | ||||
| 		return | ||||
| 	} | ||||
| 	copier.Copy(park, formData) | ||||
| @@ -130,7 +130,7 @@ func fetchParkDetail(c *gin.Context) { | ||||
| 	result := response.NewResult(c) | ||||
| 	userSession, err := _retreiveSession(c) | ||||
| 	if err != nil { | ||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | ||||
| 		result.Unauthorized(err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	requestParkId := c.Param("pid") | ||||
| @@ -140,7 +140,7 @@ func fetchParkDetail(c *gin.Context) { | ||||
| 		return | ||||
| 	} | ||||
| 	if userSession.Uid != park.UserId { | ||||
| 		result.NotAccept("不能访问不属于自己的园区。") | ||||
| 		result.Unauthorized("不能访问不属于自己的园区。") | ||||
| 		return | ||||
| 	} | ||||
| 	result.Json(http.StatusOK, "已经获取到指定园区的信息。", gin.H{"park": park}) | ||||
| @@ -154,7 +154,7 @@ func changeParkEnableState(c *gin.Context) { | ||||
| 	result := response.NewResult(c) | ||||
| 	userSession, err := _retreiveSession(c) | ||||
| 	if err != nil { | ||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | ||||
| 		result.Unauthorized(err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	requestParkId := c.Param("pid") | ||||
| @@ -172,7 +172,7 @@ func deleteSpecificPark(c *gin.Context) { | ||||
| 	result := response.NewResult(c) | ||||
| 	userSession, err := _retreiveSession(c) | ||||
| 	if err != nil { | ||||
| 		result.Failure(http.StatusInternalServerError, err.Error()) | ||||
| 		result.Unauthorized(err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	requestParkId := c.Param("pid") | ||||
|   | ||||
| @@ -42,6 +42,14 @@ func (r *Result) Error(code int, msg string) { | ||||
| 	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) { | ||||
| 	res := BaseResponse{} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user