码迷,mamicode.com
首页 > 其他好文 > 详细

yb课堂之用户注册登陆模块《六》

时间:2020-07-16 21:18:17      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:center   json   sele   parse   https   md5   map   ESS   setname   

用户注册功能接口开发

  • 注册接口开发
  • MD5加密工具类封装

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.ybclass.online_ybclass.mapper.UserMapper">
    <!--根据手机号查询用户信息-->
    <select id="findByPhone" resultType="User">
        SELECT * FROM user WHERE phone=#{phone}
    </select>
    <insert id="save" parameterType="User">
        INSERT INTO user (name,pwd,head_img,phone,create_time)
        values (#{name,jdbcType=VARCHAR},#{pwd,jdbcType=VARCHAR},#{headImg,jdbcType=VARCHAR},
        #{phone,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP})
    </insert>
</mapper>

UserMapper.java

package net.ybclass.online_ybclass.mapper;

import net.ybclass.online_ybclass.domain.User;
import org.apache.ibatis.annotations.Param;

public interface UserMapper {
int save(User user);
User findByPhone(@Param("phone") String phone);
}

UserService.java

package net.ybclass.online_ybclass.service;

import net.ybclass.online_ybclass.domain.User;
import java.util.Map;

public interface UserService {
    /**
     * 新增用户
     * @param userInfo
     * @return
     */
    int save(Map<String,String> userInfo);

    /**
     * 根据手机号查询用户
     * @param phone
     * @return
     */
    User findByPhone(String phone);
}

UserServiceImpl.java

package net.ybclass.online_ybclass.service.impl;

import net.ybclass.online_ybclass.domain.User;
import net.ybclass.online_ybclass.mapper.UserMapper;
import net.ybclass.online_ybclass.service.UserService;
import net.ybclass.online_ybclass.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;
    @Override
    public int save(Map<String, String> userInfo) {
        User user=parseToUser(userInfo);
        if (user!=null){
            return userMapper.save(user);
        }else {
            return -1;
        }
    }

    /**
     * 解析map中的User对象
     * @param userInfo
     * @return
     */
    private User parseToUser(Map<String, String> userInfo) {
        if (userInfo.containsKey("phone")&&userInfo.containsKey("pwd")&&userInfo.containsKey("name")){
            User user =new User();
            user.setName(userInfo.get("name"));
            user.setHeadImg(getRandomImg());
            user.setPhone(userInfo.get("phone"));
            user.setCreateTime(new Date());
            String pwd=userInfo.get("pwd");
            //MD5加密
            user.setPwd(CommonUtils.MD5(pwd));
            return user;
        }
        return null;
    }

    /**
     * 放在CDN上的随机头像
     */
    private static final String [] headImg = {
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/12.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/11.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/13.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/14.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/15.jpeg"
    };

    /**
     * 获取随机头像
     * @return
     */
    private String getRandomImg(){
        int size=headImg.length;
        Random random=new Random();
        int index=random.nextInt(size);
        return  headImg[index];
    }
    @Override
    public User findByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }
}

CommonUtils.java

package net.ybclass.online_ybclass.utils;

public class CommonUtils {
    /**
     * MD5加密工具类
     * @param data
     * @return
     */
    public static String MD5(String data) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            byte[] array = md.digest(data.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte bt : array) {
                sb.append(Integer.toHexString((bt & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString().toUpperCase();
        } catch (Exception ex) {
        }
        return null;
    }
}

UserController.java

package net.ybclass.online_ybclass.controller;

import net.ybclass.online_ybclass.service.UserService;
import net.ybclass.online_ybclass.utils.JsonData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/v1/pri/user")
public class UserController {
    @Autowired(required = false)
    private UserService userService;
    @PostMapping("register")
    public JsonData register(@RequestBody Map<String,String> userInfo){
        return userService.save(userInfo)==1?JsonData.buildSuccess():JsonData.buildError("注册失败,请重试");
    }
    @PostMapping("find_phone")
    public JsonData findByPhone(@Param("phone") String phone){
        return JsonData.buildSuccess(userService.findByPhone(phone));
    }
}

yb课堂之用户注册登陆模块《六》

标签:center   json   sele   parse   https   md5   map   ESS   setname   

原文地址:https://www.cnblogs.com/chenyanbin/p/13323908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!