二维码的生成
来源:互联网 发布:mac微信怎么设置声音 编辑:程序博客网 时间:2024/06/05 23:24
二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型。
前几天公司要做二维码登录,这个任务就交给了我,刚开始用websocket来做,但是因为部署到多台服务器上,但是websocket的session无法存到redis中,后来改成了长连接方式,话不多说上代码
/**
* 生成二维码图片
* return
* uuid
* qrCodeImg
*/
@RequestMapping("/code/GetQrCode")
@ResponseBody
public Map<String, Object> GetQrCode(HttpServletRequest request) {
Map<String, Object> resultmap = new HashMap<String, Object>();
try {
String Urlkey = MyProperties.getKey("Urlkey");
String Pathkey = MyProperties.getKey("Pathkey");
String uuid = Tools.get32UUID();
String ip = "www.verygrow.com";
// ip = InetAddress.getLocalHost().getHostAddress();
String content = "http://" +ip+"/code/phonescan?uuid="+uuid;
RedisUtil.set(uuid, uuid,40000);
RedisUtil.set("scantimeout"+uuid, uuid,40000); //当二维码生成是进入扫码倒计时
String imgName = uuid + "_" + (int) (new Date().getTime() / 1000) + ".png";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String imgPath = Pathkey+sdf.format(new Date())+"/"+ imgName;
String qrCodeImg = Urlkey+sdf.format(new Date())+"/"+ imgName;
QRCodeUtil handler = new QRCodeUtil();
handler.encoderQRCode(content, imgPath, "png", null);
// String qrCodeImg = Urlkey + imgName;
resultmap.put("imgPath", imgPath);
resultmap.put("uuid", uuid);
resultmap.put("qrCodeImg", qrCodeImg);//图片路径
resultmap.put("status", 0);
} catch (Exception e) {
e.printStackTrace();
resultmap.put("status", 1);
resultmap.put("errmsg", "出错"+e);
}
return resultmap;
}
这是生成二维码图片,我们的图片上传到专门的服务器上,当二维码生成时候,有一个扫码长连接已经触发,他会一直检验二维码是否被扫
/**
* 扫码长链接
* @param request
* @return
*/
@RequestMapping("/code/ScanLongConnectionCheck")
@ResponseBody
public Map<String, Object> ScanLongConnectionCheck(HttpServletRequest request) {
Map<String, Object> resultmap = new HashMap<String, Object>();
try {
long inTime = new Date().getTime();
Boolean bool = true;
while (bool) {
try {
Thread.sleep(500); //每0.5秒轮询一次
} catch (InterruptedException e) {
e.printStackTrace();
}
String uuid = request.getParameter("uuid");
//检测是否扫码
if(RedisUtil.exists("uuid"+uuid)){
bool = false;
resultmap.put("status", 0);
}else{
if(new Date().getTime() - inTime > LONG_TIME_WAIT){
bool = false;
resultmap.put("status", 1);
resultmap.put("errmsg", "二维码已失效");
}
}
}
} catch (Exception e) {
e.printStackTrace();
resultmap.put("status", 1);
resultmap.put("errmsg", "出错"+e);
}
return resultmap;
}
然后扫描二维码
/**
* 扫码
* @param request
* @return
*/
@RequestMapping("/code/phonescan")
@ResponseBody
public Map<String, Object> phonescan(HttpServletRequest request) {
Map<String, Object> resultmap = new HashMap<String, Object>();
String info = request.getParameter("info");
String uuid="";
try {
info=AESUtil.decrypt(info,"v7l27ffb9xamti4igqa308ju");
JSONObject jsonObject = JSONObject.fromObject(info);
uuid=jsonObject.getString("uuid");
if(RedisUtil.exists(uuid)){
if(!RedisUtil.exists("scantimeout"+uuid)){
resultmap.put("status", 1);
resultmap.put("errmsg", "二维码已失效");
return resultmap;
}else{
String phone = jsonObject.getString("phone");
RedisUtil.set("uuid"+uuid, phone);
RedisUtil.set("loginscantimeout"+uuid,phone,40000); //当扫码成功后出发登录倒计时
RedisUtil.del(uuid);
}
}else{
resultmap.put("status",1);
resultmap.put("errmsg","二维码已失效");
return resultmap;
}
} catch (Exception e) {
e.printStackTrace();
resultmap.put("status", 1);
resultmap.put("errmsg", "出错"+e);
}
resultmap.put("status",0);
resultmap.put("loginUrl","/code/phoneLogin?uuid="+uuid);
return resultmap;
}
当扫码成功时,会触发登录长连接,检验是否登录
/**
* 登录长链接
* @param request
* @return
*/
@RequestMapping("/code/LoginLongConnectionCheck")
@ResponseBody
public Map<String, Object> LoginLongConnectionCheck(HttpServletRequest request,HttpServletResponse response) {
Map<String, Object> resultmap = new HashMap<String, Object>();
try {
long inTime = new Date().getTime();
Boolean bool = true;
while (bool) {
try {
Thread.sleep(500); //每0.5秒轮询一次
} catch (InterruptedException e) {
e.printStackTrace();
}
String uuid = request.getParameter("uuid");
//检测是否登陆
//System.out.println("检测是否登录"+RedisUtil.exists(request.getParameter("uuid")+"phone"));
if(RedisUtil.exists("phone"+uuid)){
String phone = RedisUtil.get("phone"+uuid);
Sauser sauser = new Sauser();
sauser.setPhone(phone);
Map<String,Object> info = saleLoginService.salecheckLogin(sauser);
if(info == null){
resultmap.put("status", 1);
resultmap.put("msg", "没有此账号");
return resultmap;
}
if("03".equals(info.get("status"))){
resultmap.put("status", 1);
resultmap.put("msg", "账号已被冻结");
return resultmap;
}
//更新最后登录时间和登录次数
saleUserService.SaleupdateLoginnum(info.get("uid").toString(),Integer.parseInt(info.get("loginnum").toString()));
//查最高的lev,sid,aid
List<Map<String,Object>> area = saleLoginService.queryRegionmarketRole(phone);
JSONObject jsonObject = new JSONObject();
//查询市场身份
// Market market = saleLoginService.selectByPrimaryKey(info.get("uid").toString());
// jsonObject.put("market", market);
jsonObject.put("user", info);
jsonObject.put("area", area);
jsonObject.put("type", "00");
//查询角色
List<Map<String,Object>> rList = saleLoginService.queryMultipleRole(phone);
jsonObject.put("rlist", rList);
//存redis
String UUID = UUIDGenerator.generate();
String requestHeader = request.getHeader("user-agent");
if(Tools.isMobileDevice(requestHeader)){
//手机端登录
//失效设置7天
redisService.set( "APPUSER"+UUID, jsonObject.toString(), 604800000);
}else{
//WEB端登录
CookieOperation.addCookie(response, "authId", UUID, 0);
CookieOperation.addCookie(response, "name", info.get("uname").toString(), 0);
redisService.set( "USER"+UUID, jsonObject.toString(), 36000);
}
RedisUtil.del("phone"+uuid);
resultmap.put("info", info);
resultmap.put("authId",UUID);
resultmap.put("status", 0);
bool = false;
}else{
if(new Date().getTime() - inTime > LONG_TIME_WAIT){
bool = false;
resultmap.put("status", 1);
resultmap.put("errmsg", "登录超时");
RedisUtil.del("uuid"+uuid);
}
}
}
} catch (Exception e) {
e.printStackTrace();
resultmap.put("status", 1);
resultmap.put("errmsg", "出错"+e);
}
return resultmap;
}
当用户点登陆时代码
/**
* 登录
* @param uuid
* @param phone
**/
@RequestMapping("/code/phoneLogin")
@ResponseBody
public Map<String, Object> phoneLogin(HttpServletRequest request,HttpServletResponse response) {
Map<String, Object> resultmap = new HashMap<String, Object>();
try {
String uuid = request.getParameter("uuid");
if(!RedisUtil.exists("loginscantimeout"+uuid)){
resultmap.put("status", 1);
RedisUtil.del("uuid"+uuid);
resultmap.put("errmsg", "登陆超时");
return resultmap;
}
if(RedisUtil.exists("uuid"+uuid)){
String phone = RedisUtil.get("uuid"+uuid);
RedisUtil.set("phone"+uuid, phone);
RedisUtil.del("uuid"+uuid);
resultmap.put("status", 0);
}else{
resultmap.put("status", 1);
resultmap.put("errmsg", "二维码信息有误");
}
} catch (Exception e) {
e.printStackTrace();
resultmap.put("status", 1);
resultmap.put("errmsg", "出错"+e);
}
return resultmap;
}
}
这是我第一次写博客,对于一个实习生来说,我觉得做出来二维码登录实在不易
- googlechart的二维码生成
- iOS二维码的生成
- 二维码的生成
- 二维码生成的记载
- 二维码的服务端生成
- 二维码的生成
- 二维码的扫描、生成
- 生成二维码的工具
- 二维码的生成
- 二维码的生成
- 二维码的生成
- 原生二维码的生成
- 二维码的生成
- 二维码的生成
- 二维码的生成
- 二维码的生成/扫描
- 生成文字的二维码
- IOS 二维码的生成
- 欢迎使用CSDN-markdown编辑器
- 除了贵州那台世界最大的射电望远镜,你还需要了解这些
- 雷诺-日产联盟与微软合力研发下一代智能网联车载系统
- 两位顶级专家加入HoloLens团队,微软又要推出什么样的黑科技
- C++内存配置和释放过程
- 二维码的生成
- 内部类(基础)
- 005_跨平台开发_MUI_数字角标(badges)
- Python与机器学习(二)
- Image Formation Pipeline --- 从2D到3D(一)
- 安卓图片二次采样的实现(http://cantellow.iteye.com/blog/838473)
- Windows 10装机量破4亿,3个月增加5000万台
- mysql centos操作系统优化
- Google计划于2017年发行Pixel 3 Laptop,所用系统为最新Andromeda OS