Web项目中常用状态码 项目中的使用

在web项目中,通常前端需要我们返回一些状态码,表示请求的响应。而在实际的项目中,一般使用英文替代状态码,有两个好处:

  1. 方便其他开发人员识别;
  2. 统一管理,便于后期维护。

常用状态码

# coding:utf-8

class RET:
    OK                  = "0"
    DBERR               = "4001"
    NODATA              = "4002"
    DATAEXIST           = "4003"
    DATAERR             = "4004"
    SESSIONERR          = "4101"
    LOGINERR            = "4102"
    PARAMERR            = "4103"
    USERERR             = "4104"
    ROLEERR             = "4105"
    PWDERR              = "4106"
    CPWDERR             = "4107"
    MOBILEERR           = "4108"
    REQERR              = "4201"
    IPERR               = "4202"
    THIRDERR            = "4301"
    IOERR               = "4302"
    SERVERERR           = "4500"
    UNKOWNERR           = "4501"
    NECESSARYPARAMERR   = "4502"
    SMSCODERR           = "4503"
    ALLOWERR            = "4504"


error_map = {
    RET.OK                    : u"成功",
    RET.DBERR                 : u"数据库查询错误",
    RET.NODATA                : u"无数据",
    RET.DATAEXIST             : u"数据已存在",
    RET.DATAERR               : u"数据错误",
    RET.SESSIONERR            : u"用户未登录",
    RET.LOGINERR              : u"用户登录失败",
    RET.PARAMERR              : u"参数错误",
    RET.USERERR               : u"用户不存在或未激活",
    RET.ROLEERR               : u"用户身份错误",
    RET.PWDERR                : u"密码错误",
    RET.CPWDERR               : u"密码不一致",
    RET.MOBILEERR             : u"手机号错误",
    RET.REQERR                : u"非法请求或请求次数受限",
    RET.IPERR                 : u"IP受限",
    RET.THIRDERR              : u"第三方系统错误",
    RET.IOERR                 : u"文件读写错误",
    RET.SERVERERR             : u"内部错误",
    RET.UNKOWNERR             : u"未知错误",
    RET.NECESSARYPARAMERR     : u"缺少必传参数",
    RET.SMSCODERR             : u"短信验证码有误",
    RET.ALLOWERR              : u"未勾选协议",
}



# coding:utf-8

class RETCODE:
    OK                  = "0"
    IMAGECODEERR        = "4001"
    THROTTLINGERR       = "4002"
    NECESSARYPARAMERR   = "4003"
    USERERR             = "4004"
    PWDERR              = "4005"
    CPWDERR             = "4006"
    MOBILEERR           = "4007"
    SMSCODERR           = "4008"
    ALLOWERR            = "4009"
    SESSIONERR          = "4101"
    DBERR               = "5000"
    EMAILERR            = "5001"
    TELERR              = "5002"
    NODATAERR           = "5003"
    NEWPWDERR           = "5004"


    REQERR              = "4201"
    IPERR               = "4202"
    THIRDERR            = "4301"
    IOERR               = "4302"
    SERVERERR           = "4500"
    UNKOWNERR           = "4501"


err_msg = {
    RETCODE.OK                 : u"成功",
    RETCODE.IMAGECODEERR       : u"图形验证码错误",
    RETCODE.THROTTLINGERR      : u"访问过于频繁",
    RETCODE.NECESSARYPARAMERR  : u"缺少必传参数",
    RETCODE.USERERR            : u"用户名错误",
    RETCODE.PWDERR             : u"密码错误",
    RETCODE.CPWDERR            : u"密码不一致",
    RETCODE.MOBILEERR          : u"手机号错误",
    RETCODE.SMSCODERR          : u"短信验证码有误",
    RETCODE.ALLOWERR           : u"未勾选协议",
    RETCODE.SESSIONERR         : u"用户未登录",
    RETCODE.DBERR              : u"数据错误",
    RETCODE.EMAILERR           : u"邮箱错误",
    RETCODE.TELERR             : u"固定电话错误",
    RETCODE.NODATAERR          : u"无数据",
    RETCODE.NEWPWDERR          : u"新密码数据",
}

项目中使用

在项目中,状态码会被封装成一个文件,通过导入方式调用状态码。

from django.views import View
from django import http
import json
# 调用状态码
from meiduo_mall.utils.response_code import RETCODE


class OrderCommitView(View):
    def post(self, request):
    
    # 响应请求
       return http.JsonResponse({"code": RETCODE.OK, "errmsg": "请求成功"})
发布了53 篇原创文章 · 获赞 69 · 访问量 1954
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览