• 6-----BBS论坛


    BBS论坛(六)

    6.1.优化json数据的返回

    (1)新建utils/restful.py

    # utils/restful.py
    
    from flask import jsonify
    
    class HttpCode(object):
        ok = 200
        unautherror = 401
        paramserror = 400
        servererror = 500
    
    def restful_result(code,message,data):
        return jsonify({"code":code,"message":message,"data":data or {}})
    
    def success(message="",data=None):
        return restful_result(code=HttpCode.ok,message=message,data=data)
    
    def unauth_error(message=""):
        return restful_result(code=HttpCode.unautherror,message=message,data=None)
    
    def params_error(message=""):
        return restful_result(code=HttpCode.paramserror,message=message,data=None)
    
    def server_error(message=""):
        return restful_result(code=HttpCode.servererror,message=message or "服务器内部错误",data=None)

    (2)cms/views.py

    class ResetPwdView(views.MethodView):
        .
        .
        .
        .
                if user.check_password(oldpwd):
                    user.password = newpwd
                    db.session.commit()
                    return restful.success()
                else:
                    return restful.params_error("旧密码错误")
            else:
                return restful.params_error(form.get_error())

    6.2.sweetalert优化修改密码结果反馈

     (1)把sweetalert文件夹放到static/common下面

    (2)在cms/cms_base.html中引用

      <link rel="stylesheet" href="{{ static("common/sweetalert/sweetalert.css") }}">
        <script src="{{ static("common/sweetalert/sweetalert.min.js") }}"></script>
        <script src="{{ static("common/sweetalert/zlalert.js") }}"></script>

    (3)cms/js/resetpwd.js

    /**
     * Created by derek on 2018/6/3.
     */
    
    $(function () {
        $("#submit").click(function (event) {
            //阻止按钮的提交表单的事件
            event.preventDefault();
            //分别获取三个标签
            var oldpwdE = $("input[name=oldpwd]");
            var newpwdE = $("input[name=newpwd]");
            var newpwd2E = $("input[name=newpwd2]");
    
            var oldpwd = oldpwdE.val();
            var newpwd = newpwdE.val();
            var newpwd2 = newpwd2E.val();
    
            //1.要在模板的meta标签中渲染一个csrf-token
            //2.在ajax请求的头部汇总设置X-CSRFtoken
            zlajax.post({
                'url':'/cms/resetpwd',
                'data':{
                    'oldpwd':oldpwd,
                    'newpwd':newpwd,
                    'newpwd2':newpwd2
                },
                'success':function (data) {
                    if (data['code'] == 200){
                        zlalert.alertSuccessToast("恭喜!密码修改成功!");
                        oldpwd.val('');
                        newpwd.val('');
                        newpwd2.val('');
                    }else {
                        var message = data['message'];
                        zlalert.alertInfo(message);
                    }
                },
                'fail':function (error) {
                    zlalert.alertNetworkError(error);
                }
            });
        });
    });

    修改密码弹出的信息

     旧密码输错的提示信息

    end。。。

  • 相关阅读:
    PyQT_Group
    单例模式演示-1-39-07
    RSqlBuilder
    RExcel
    RJson
    NodeJs开发目录
    NodeJs事件驱动
    NodeJs实用工具util
    NodeJs之global,process
    NodeJs两个简单调试技巧
  • 原文地址:https://www.cnblogs.com/edeny/p/10020864.html
Copyright © 2020-2023  润新知