没有跳转和登陆失败提示​

没有跳转和登陆失败提示​

package com.imooc.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.reader.entity.Member;
import com.imooc.reader.mapper.MemberMapper;
import com.imooc.reader.service.MemberService;
import com.imooc.reader.service.exception.MemberException;
import com.imooc.reader.utils.Md5Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberMapper memberMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Member createMember(String username, String password, String nickname) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        List<Member> members = memberMapper.selectList(wrapper);
        if(members.size() > 0 ){
            throw new MemberException("用户已存在");
        }
        Member member = new Member();
        member.setUsername(username);
        member.setNickname(nickname);
        member.setCreateTime(new Date());
        int salt = new Random().nextInt(1000) + 1000;
        member.setSalt(salt);
        String md5 = Md5Utils.md5Digest(password, salt);
        member.setPassword(md5);
        memberMapper.insert(member);
        return member;
    }

    @Override
    public Member checkLogin(String username, String password) {
        QueryWrapper<Member> wrapper = new QueryWrapper();
        wrapper.eq("username", username);
        Member member = memberMapper.selectOne(wrapper);
        if(member == null){
            throw new MemberException("用户不存在");
        }
        String md5 = Md5Utils.md5Digest(password, member.getSalt());
        if(!md5.equals(member.getPassword())){
            throw new MemberException("您输入的密码有误");
        }
        return member;
    }
}
package com.imooc.reader.controller;

import com.imooc.reader.entity.Member;
import com.imooc.reader.service.MemberService;
import com.imooc.reader.utils.ResponseUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/member")
public class MemberController {
    @Resource
    private MemberService memberService;
    @PostMapping("/registe")
    public ResponseUtils registe(String username, String password , String nickname , String vc , HttpServletRequest request){
        String verifyCode = (String)request.getSession().getAttribute("kaptchaVerifyCode");
        ResponseUtils resp ;
        if(vc == null || verifyCode == null || !vc.equalsIgnoreCase(verifyCode)){
            resp = new ResponseUtils("VerifyCodeError", "验证码错误");
        }else{
            resp = new ResponseUtils();
            //验证码比对成功后进行用户注册
            try {
                Member member = memberService.createMember(username, password, nickname);
                resp = new ResponseUtils();
            }catch (Exception e){
                e.printStackTrace();
                resp = new ResponseUtils(e.getClass().getSimpleName(), e.getMessage());
            }
        }
        return resp;
    }
    @PostMapping("/check_login")
    public ResponseUtils checkLogin(String username , String password, String vc , HttpServletRequest request){
        String verifyCode = (String)request.getSession().getAttribute("kaptchaVerifyCode");
        ResponseUtils resp ;
        if(vc == null || verifyCode == null || !vc.equalsIgnoreCase(verifyCode)){
            resp = new ResponseUtils("VerifyCodeError", "验证码错误");
        }else{
            resp = new ResponseUtils();
            //验证码比对成功后进行用户登录查询
            try {
                Member member = memberService.checkLogin(username, password);
                member.setPassword(null);
                member.setSalt(null);
                resp = new ResponseUtils().put("member" , member);
            }catch (Exception e){
                e.printStackTrace();
                resp = new ResponseUtils(e.getClass().getSimpleName(), e.getMessage());
            }
        }
        return resp;
    }
}
package com.imooc.reader.service;

import com.imooc.reader.entity.Member;

public interface MemberService {
    public Member createMember(String username,String password,String nickname);
    public Member checkLogin(String username,String password);

}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0,user-scalable=no">
    <title>慕课书评网</title>
    <!-- 引入样式文件 -->
    <link rel="stylesheet"
          href="/assets/vant/index.css"/>

    <!-- 引入 Vue 和 Vant 的 JS 文件 -->
    <script src="/assets/vue/vue.global.js"></script>
    <script src="/assets/vant/vant.min.js"></script>
    <script src="/assets/axios/axios.js"></script>
</head>
<body>
<div id="app">
    <van-nav-bar right-text="会员注册" @click-right="directRegister">
        <template #left>
            <a href="/" style="padding-top: 10px">
                <img src="/images/logo2.png" style="width: 80px">
            </a>
        </template>
    </van-nav-bar>
    <h3 style="padding-left:32px">会员登录</h3>
    <van-form ref="loginForm">
        <van-cell-group inset>
            <van-field
                    v-model="username"
                    name="用户名"
                    label="用户名"
                    autocomplete="off"
                    placeholder="请输入用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]"
            />
        </van-cell-group>
        <van-cell-group inset>
            <van-field
                    v-model="password"
                    type="password"
                    name="密码"
                    label="密码"
                    placeholder="请输入密码"
                    :rules="[{ required: true, message: '请填写密码' }]"
            />
        </van-cell-group>
        <van-cell-group inset>
            <van-row>
                <van-col span="16">
                    <van-field
                            v-model="verifyCode"
                            name="verifyCode"
                            label="验证码"
                            placeholder="请输入验证码"
                            autocomplete="off"
                            :rules="[{ required: true, message: '请填写验证码' }]"
                    />
                </van-col>
                <van-col span="8">
                    <img id="imgVerifyCode" src="/api/verify_code"
                         style="width: 100%;height:44px;cursor: pointer" @click="reloadVerifyCode">
                </van-col>
            </van-row>
        </van-cell-group>
        <div style="margin: 16px;">
            <van-button round block type="primary" @click="onSubmit('loginForm')">
                登录
            </van-button>
        </div>
    </van-form>
</div>
<script>

    const Main = {
        data() {
            return {
                username: "",
                password: "",
                verifyCode: ""
            }
        }
        , methods: {
            onSubmit: function (formName) {
                const form = this.$refs[formName];
                const objApp = this;
                form.validate().then(function(){
                    //这里组织表单验证后登录代码
                    const params = new URLSearchParams();
                    params.append("username", objApp.username);
                    params.append("password", objApp.password);
                    params.append("vc", objApp.verifyCode);
                    axios.post("/api/member/check_login" , params)
                        .then(function(response){
                            const json = response.data;
                            if(json.code == "0"){
                                sessionStorage.mid = json.data.member.memberId;
                                window.location = "/index.html";
                            }else{
                                objApp.reloadVerifyCode();
                                vant.Dialog.alert({
                                    title : "登录失败",
                                    message : json.message
                                })
                            }
                        })
                }).catch(function(e){console.debug("表单验证失败",e)});
            }
            , directRegister: function () {
                window.location.href = '/register.html';
            }
            , reloadVerifyCode: function () {
                document.getElementById("imgVerifyCode").src = "/api/verify_code?ts=" + new Date().getTime();
            }
        }
        , mounted() {

        }
    };
    const app = Vue.createApp(Main);
    app.use(vant);
    app.use(vant.Lazyload);
    app.mount("#app");
</script>
</body>
</html>

https://img1.sycdn.imooc.com//climg/641426dd094f13e906050685.jpg

https://img1.sycdn.imooc.com//climg/641426e409020c4105760639.jpg



正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小蓝 2023-03-17 16:53:34

同学你好,老师没有理解同学问题的意思,建议同学详细描述一下问题。从同学提供的截图中,用户imooc_12345在数据库中确实不存在的,现在的提示方式是正常的。

祝学习愉快~

  • 提问者 rudtjd #1

    这个是截图视频的演示画面,我的页面没反应。

    https://img1.sycdn.imooc.com//climg/64142acd096b23b118210830.jpg

    2023-03-17 16:54:38
  • 好帮手慕小蓝 回复 提问者 rudtjd #2

    同学你好,老师使用同学的代码配合课程源码,是可以正常登录跳转的。

    建议同学先使用课程源码尝试一下,排除一下数据库的问题。如果使用课程源码可以登录跳转,再换成同学的代码,在MemberController的checkLogin方法中打断点,使用debug启动项目,查看一下运行流程。通常在运行流程里可以看出哪里出现了错误。

    祝学习愉快~

    2023-03-17 17:07:03
  • 提问者 rudtjd 回复 好帮手慕小蓝 #3

    源码能正常跳转,数据库没问题。

    https://img1.sycdn.imooc.com//climg/641555ae091cee7d19200886.jpg

    https://img1.sycdn.imooc.com//climg/641555b60905a1b906460107.jpg

    搜过网上的办法但是没解决,麻烦老师看一下

    2023-03-18 14:10:17
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师