返回顶部
扩大
缩小

Zhang_derek

BBS论坛(十六)

16.登录功能完成

(1)front/forms.py

class SigninForm(BaseForm):
    telephone = StringField(validators=[Regexp(r'1[3578]\d{9}', message='请输入正确格式的手机号码')])
    password = StringField(validators=[Regexp(r'[0-9a-zA-Z_\.]{6,15}', message='请输入正确格式的密码')])
    remember=StringField()

(2)config.py

FRONT_USER_ID = 'FFFF'

(3)front/views.py

class SigninView(views.MethodView):
    def get(self):
        return_to = request.referrer
        if return_to and return_to != request.url and return_to != url_for('front.signup') and safeutils.is_safe_url(
                return_to):
            return render_template('front/signin.html', return_to=return_to)
        else:
            return render_template('front/signin.html')

    def post(self):
        form = SigninForm(request.form)
        if form.validate():
            telephone = form.telephone.data
            password = form.password.data
            remember = form.remember.data
            user = FrontUser.query.filter_by(telephone=telephone).first()
            if user and user.check_password(password):
                session[config.FRONT_USER_ID] = user.id
                if remember:
                    session.permanent = True
                return restful.success()
            else:
                return restful.params_error(message='手机号或密码错误')
        else:
            return restful.params_error(message=form.get_error())

(4)front_signin.js

/**
 * Created by Administrator on 2018/6/9.
 */
$(function(){
    $('#submit-btn').on('click',function(event){
        event.preventDefault();
        var telephone_input=$('input[name=telephone]');
        var password_input=$('input[name=password]');
        var remember_input=$('input[name=remember]');
        var telephone=telephone_input.val();
        var password=password_input.val();
        var remember=remember_input.checked?1:0;

        zlajax.post({
           'url':'/signin/',
           'data':{
               'telephone':telephone,
               'password':password,
               'remember':remember
           },
            'success':function(data){
               if(data['code']==200){
                   var return_to=$('#return-to-span').text();
                   if(return_to){
                       window.location=return_to;
                   }else{
                       window.location='/'
                   }
               }else{
                   zlalert.alertInfo(data['message']);
               }
            }
        });

    });
});

(5)signin.html

 <script src="{{ static('front/js/front_signin.js') }}"></script>

 

posted on 2018-06-09 16:32  zhang_derek  阅读(...)  评论(... 编辑 收藏

导航