分享码

Raod 3 years ago
parent c0aa455761
commit 7259593066

@ -53,4 +53,9 @@ public class ReportShareDto extends GaeaBaseDTO implements Serializable {
@ApiModelProperty(value = "0--未删除 1--已删除 DIC_NAME=DELETE_FLAG") @ApiModelProperty(value = "0--未删除 1--已删除 DIC_NAME=DELETE_FLAG")
private Integer deleteFlag; private Integer deleteFlag;
/** 分享码 */
private String sharePassword;
private boolean sharePasswordFlag = false;
} }

@ -1,11 +1,11 @@
package com.anjiplus.template.gaea.business.modules.reportshare.dao.entity; package com.anjiplus.template.gaea.business.modules.reportshare.dao.entity;
import lombok.Data;
import io.swagger.annotations.ApiModelProperty;
import com.anji.plus.gaea.curd.entity.GaeaBaseEntity; import com.anji.plus.gaea.curd.entity.GaeaBaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import javax.validation.constraints.*; import lombok.Data;
import java.util.Date; import java.util.Date;
/** /**
* @description entity * @description entity
@ -39,5 +39,12 @@ public class ReportShare extends GaeaBaseEntity {
/** 0--未删除 1--已删除 DIC_NAME=DELETE_FLAG */ /** 0--未删除 1--已删除 DIC_NAME=DELETE_FLAG */
private Integer deleteFlag; private Integer deleteFlag;
/** 分享码 */
@TableField(exist = false)
private String sharePassword;
@TableField(exist = false)
private boolean sharePasswordFlag;
} }

@ -13,9 +13,11 @@ import com.anjiplus.template.gaea.business.modules.reportshare.dao.entity.Report
import com.anjiplus.template.gaea.business.modules.reportshare.service.ReportShareService; import com.anjiplus.template.gaea.business.modules.reportshare.service.ReportShareService;
import com.anjiplus.template.gaea.business.util.DateUtil; import com.anjiplus.template.gaea.business.util.DateUtil;
import com.anjiplus.template.gaea.business.util.JwtUtil; import com.anjiplus.template.gaea.business.util.JwtUtil;
import com.anjiplus.template.gaea.business.util.MD5Util;
import com.anjiplus.template.gaea.business.util.UuidUtil; import com.anjiplus.template.gaea.business.util.UuidUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -51,12 +53,18 @@ public class ReportShareServiceImpl implements ReportShareService {
@Override @Override
public ReportShareDto insertShare(ReportShareDto dto) { public ReportShareDto insertShare(ReportShareDto dto) {
//设置分享码
if (dto.isSharePasswordFlag()) {
dto.setSharePassword(UuidUtil.getRandomPwd(4));
}
ReportShareDto reportShareDto = new ReportShareDto(); ReportShareDto reportShareDto = new ReportShareDto();
ReportShare entity = new ReportShare(); ReportShare entity = new ReportShare();
BeanUtils.copyProperties(dto, entity); BeanUtils.copyProperties(dto, entity);
insert(entity); insert(entity);
//将分享链接返回 //将分享链接返回
reportShareDto.setShareUrl(entity.getShareUrl()); reportShareDto.setShareUrl(entity.getShareUrl());
reportShareDto.setSharePassword(dto.getSharePassword());
return reportShareDto; return reportShareDto;
} }
@ -69,6 +77,12 @@ public class ReportShareServiceImpl implements ReportShareService {
if (null == reportShare) { if (null == reportShare) {
throw BusinessExceptionBuilder.build(ResponseCode.REPORT_SHARE_LINK_INVALID); throw BusinessExceptionBuilder.build(ResponseCode.REPORT_SHARE_LINK_INVALID);
} }
//解析jwt token获取密码
String password = JwtUtil.getPassword(reportShare.getShareToken());
if (StringUtils.isNotBlank(password)) {
//md5加密返回
reportShare.setSharePassword(MD5Util.encrypt(password));
}
return reportShare; return reportShare;
} }
@ -101,7 +115,8 @@ public class ReportShareServiceImpl implements ReportShareService {
} else { } else {
entity.setShareUrl(entity.getShareUrl() + SHARE_FLAG + shareCode); entity.setShareUrl(entity.getShareUrl() + SHARE_FLAG + shareCode);
} }
entity.setShareValidTime(DateUtil.getFutureDateTmdHms(entity.getShareValidType())); entity.setShareValidTime(DateUtil.getFutureDateTmdHms(entity.getShareValidType()));
entity.setShareToken(JwtUtil.createToken(entity.getReportCode(), shareCode, entity.getShareValidTime())); entity.setShareToken(JwtUtil.createToken(entity.getReportCode(), shareCode, entity.getSharePassword(), entity.getShareValidTime()));
} }
} }

@ -7,6 +7,7 @@ import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm; import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim; import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT; import com.auth0.jwt.interfaces.DecodedJWT;
import org.apache.commons.lang3.StringUtils;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
@ -19,11 +20,16 @@ public class JwtUtil {
private static final String JWT_SECRET = "aj-report"; private static final String JWT_SECRET = "aj-report";
public static String createToken(String reportCode, String shareCode, Date expires) { public static String createToken(String reportCode, String shareCode, Date expires) {
return createToken(reportCode, shareCode, null, expires);
}
public static String createToken(String reportCode, String shareCode, String password, Date expires) {
String token = JWT.create() String token = JWT.create()
.withIssuedAt(new Date()) .withIssuedAt(new Date())
.withExpiresAt(expires) .withExpiresAt(expires)
.withClaim("reportCode", reportCode) .withClaim("reportCode", reportCode)
.withClaim("shareCode", shareCode) .withClaim("shareCode", shareCode)
.withClaim("sharePassword", password)
.sign(Algorithm.HMAC256(JWT_SECRET)); .sign(Algorithm.HMAC256(JWT_SECRET));
return token; return token;
} }
@ -55,4 +61,15 @@ public class JwtUtil {
return claim.asString(); return claim.asString();
} }
public static String getPassword(String token) {
Claim claim = getClaim(token).get("sharePassword");
if (null == claim) {
return null;
}
if (StringUtils.isNotBlank(claim.asString())) {
return claim.asString();
}
return null;
}
} }

@ -1,5 +1,6 @@
package com.anjiplus.template.gaea.business.util; package com.anjiplus.template.gaea.business.util;
import java.security.SecureRandom;
import java.util.UUID; import java.util.UUID;
/** /**
@ -31,6 +32,24 @@ public class UuidUtil {
} }
/**
*
* @param num
* @return
*/
public static String getRandomPwd(int num) {
StringBuilder builder = new StringBuilder();
// 因为已经把 4 种字符放进list了所以 i 取值从 4开始
// 产生随机数用于随机调用生成字符的函数
for (int i = 0; i < num; i++) {
SecureRandom random = new SecureRandom();
int funNum = random.nextInt(chars.length);
builder.append(chars[funNum]);
}
return builder.toString().toLowerCase();
}
public static String generateUuid() { public static String generateUuid() {
return UUID.randomUUID().toString().replace("-", ""); return UUID.randomUUID().toString().replace("-", "");
@ -38,7 +57,9 @@ public class UuidUtil {
public static void main(String[] args) { public static void main(String[] args) {
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
System.out.println(generateShortUuid()); // System.out.println(generateShortUuid());
System.out.println(getRandomPwd(4));
} }
} }
} }

@ -6,6 +6,18 @@
!--> !-->
<template> <template>
<div> <div>
<el-dialog
title="请输入分享码"
:visible.sync="dialogVisible"
width="30%"
:close-on-click-modal="false"
:before-close="handleClose">
<el-input v-model="password" placeholder="请输入分享码"></el-input>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false"> </el-button>
<el-button type="primary" @click="checkPassword()"> </el-button>
</span>
</el-dialog>
</div> </div>
</template> </template>
@ -18,7 +30,11 @@ export default {
}, },
data() { data() {
return { return {
password: '',
sharePassword: '',
dialogVisible: false,
reportCode: '',
shareToken: ''
}; };
}, },
@ -33,18 +49,46 @@ export default {
const {code, data} = await reportShareDetailByCode(shareCode) const {code, data} = await reportShareDetailByCode(shareCode)
if (code != '200') return if (code != '200') return
setShareToken(data.shareToken) console.log(data)
//shareToken this.reportCode = data.reportCode
// this.sharePassword = data.sharePassword
this.shareToken = data.shareToken
if (this.sharePassword) {
console.log(this.sharePassword)
this.dialogVisible = true
}else {
this.pushAj()
}
},
checkPassword(){
const md5 = require('js-md5')
const inputPassword = md5(this.password);
console.log(this.password +'--------------'+inputPassword + "--------" + this.sharePassword)
if (inputPassword == this.sharePassword) {
this.pushAj()
}else {
this.$message.error('分享码输入不正确')
}
},
pushAj(){
setShareToken(this.shareToken)
this.$router.push({ this.$router.push({
path: '/bigscreen/viewer', path: '/bigscreen/viewer',
query: { query: {
reportCode: data.reportCode reportCode: this.reportCode
}, },
}) })
}, },
handleClose(done) {
this.$confirm('确认关闭?')
.then(_ => {
done();
})
.catch(_ => {});
}
} }
}; };
</script> </script>

@ -11,19 +11,40 @@
</el-form-item> </el-form-item>
</el-col> </el-col>
</el-row> </el-row>
<el-row :gutter="10">
<el-col :xs="24" :sm="20" :md="6" :lg="6" :xl="6">
<el-form-item label="分享码" prop="sharePasswordFlag">
<el-switch
v-model="dialogForm.sharePasswordFlag">
</el-switch>
</el-form-item>
</el-col>
</el-row>
</el-form> </el-form>
<el-button type="primary" plain @click="createShare"></el-button> <el-button type="primary" plain @click="createShare"></el-button>
</div> </div>
<div v-else> <div v-else>
<el-row :gutter="10"> <el-form ref="userForm" :model="dialogForm" :rules="rules" size="small" label-width="100px">
<el-col :xs="24" :sm="20" :md="20" :lg="20" :xl="16"> <el-row :gutter="10">
<el-input v-model="reportShareUrl" :disabled="true"/> <el-col :xs="24" :sm="20" :md="20" :lg="20" :xl="16">
</el-col> <el-form-item label="链接" prop="reportShareUrl">
</el-row> <el-input v-model="reportShareUrl" :disabled="true"/>
<el-row :gutter="10"> </el-form-item>
<el-button type="primary" plain @click="copyShare"></el-button> </el-col>
</el-row>
<el-row :gutter="10" v-if="dialogForm.sharePassword != ''">
<el-col :xs="24" :sm="20" :md="6" :lg="6" :xl="6">
<el-form-item label="分享码" prop="sharePassword">
<el-input v-model="dialogForm.sharePassword" :disabled="true"/>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="10">
<el-button v-if="dialogForm.sharePassword == ''" type="primary" plain @click="copyShare"></el-button>
<el-button v-if="dialogForm.sharePassword != ''" type="primary" plain @click="copyShare"></el-button>
</el-row> </el-row>
</el-form>
</div> </div>
<div slot="footer" style="text-align: center"> <div slot="footer" style="text-align: center">
@ -71,6 +92,8 @@ export default {
reportCode: '', reportCode: '',
shareUrl: '', shareUrl: '',
shareCode: '', shareCode: '',
sharePassword: '',
sharePasswordFlag: false,
}, },
shareLinkFlag1: true, shareLinkFlag1: true,
rules: { rules: {
@ -103,24 +126,33 @@ export default {
if (code != '200') return if (code != '200') return
this.shareValidTypeOptions = data this.shareValidTypeOptions = data
this.dialogForm.shareValidType = this.shareValidTypeOptions[0].id this.dialogForm.shareValidType = this.shareValidTypeOptions[0].id
this.dialogForm.sharePasswordFlag = false
this.dialogForm.sharePassword = ''
}, },
async createShare() { async createShare() {
this.dialogForm.reportCode = this.reportCode this.dialogForm.reportCode = this.reportCode
this.dialogForm.shareUrl = window.location.href this.dialogForm.shareUrl = window.location.href
console.log(this.dialogForm) // console.log(this.dialogForm)
const {code, data} = await reportShareAdd(this.dialogForm) const {code, data} = await reportShareAdd(this.dialogForm)
if (code != '200') return if (code != '200') return
console.log(data) // console.log(data)
this.shareLinkFlag1 = false this.shareLinkFlag1 = false
this.$message({ this.$message({
message: '创建链接成功!', message: '创建链接成功!',
type: 'success', type: 'success',
}) })
this.reportShareUrl = data.shareUrl this.reportShareUrl = data.shareUrl
this.dialogForm.sharePassword = data.sharePassword
}, },
copyShare(){ copyShare(){
this.copyToClip(this.reportShareUrl) let content = ''
if (this.dialogForm.sharePassword == '') {
content = 'AJ-Report分享链接' + this.reportShareUrl
}else {
content = 'AJ-Report分享链接' + this.reportShareUrl + ' 分享码:' + this.dialogForm.sharePassword;
}
this.copyToClip(content)
this.$message({ this.$message({
message: '复制链接成功!', message: '复制链接成功!',
type: 'success', type: 'success',

Loading…
Cancel
Save