Commit 43fb6fac by unknown

'由前端生成群活码'

1 parent ca055cf0
......@@ -39,6 +39,7 @@
},
"dependencies": {
"@riophae/vue-treeselect": "0.4.0",
"awesome-qr": "^2.0.1-rc.0",
"axios": "0.18.1",
"clipboard": "2.0.4",
"core-js": "3.6.5",
......@@ -56,6 +57,7 @@
"promise.prototype.finally": "^3.1.2",
"screenfull": "4.2.0",
"sortablejs": "1.8.4",
"uuid": "^8.3.2",
"video.js": "^7.10.2",
"vue": "2.6.10",
"vue-count-to": "1.0.13",
......
import request from '@/utils/request'
import { dataURLtoFile } from '@/utils/common'
/**
* 下载网络连接文件
......@@ -14,3 +15,24 @@ export function download(url, name) {
`/common/download/url?url=${url}&name=${name}`
)
}
/**
* 上传文件
* @param {*} data
{
"dataURL": 本地文件(base64)
}
*/
export function uploadDataURL(dataURL) {
const data = new window.FormData()
const f = dataURLtoFile(dataURL)
data.append('file', f)
return request({
url: process.env.VUE_APP_BASE_API +
'/common/uploadFile2Cos',
method: 'post',
processData: false,
data,
})
}
......@@ -105,6 +105,7 @@ export default {
// this.$emit('update:fileName', res.data.materialName)
this.$emit('update:fileUrl', res.url)
this.$emit('update:fileName', res.fileName)
this.$emit('update:file', file)
// this.fileUrl = URL.createObjectURL(file.raw)
}
},
......
......@@ -165,3 +165,20 @@ export function handleTree(data, id, parentId, children) {
});
return result;
}
/**
* dataURL转化为File对象
* @param {*} dataURL 本地数据 base64
*/
export function dataURLtoFile (dataURL) {
const arr = dataURL.split(',')
const mime = arr[0].match(/:(.*?);/)[1]
const bstr = atob(arr[1])
let n = bstr.length
const u8arr = new Uint8Array(n)
while (n) {
u8arr[n - 1] = bstr.charCodeAt(n - 1)
n -= 1 // to make eslint happy
}
return new File([u8arr], '', { type: mime })
}
......@@ -47,6 +47,7 @@
<upload
:fileUrl.sync="form.activityHeadUrl"
class="image-uploader"
@update:file="handleUploadedHeadImage"
>
<div slot="tip">
注: 只支持2M以内的jpg/png格式图片
......@@ -184,6 +185,9 @@
<script>
import { add, update, getDetail } from '@/api/drainageCode/group'
import { uploadDataURL } from '@/api/common'
import { v4 as uuidv4 } from 'uuid'
import { AwesomeQR } from 'awesome-qr'
export default {
props: {
......@@ -203,8 +207,16 @@ export default {
guide: '',
joinGroupIsTip: 0,
tipMsg: '',
customerServerQrCode: ''
customerServerQrCode: '',
// 生成的群活码UUID
uuid: '',
// 群活码链接
codeUrl: ''
},
// 活码头像数据
headImage: null,
// 群活码H5页面地址
h5uri: '',
rules: {
activityName: [
{ required: true, message: '请输入活码名称', trigger: 'blur' }
......@@ -219,15 +231,59 @@ export default {
methods: {
// 新增群活码
add () {
this.$refs.form.validate((valid) => {
this.$refs.form.validate(async (valid) => {
if (!valid) return
add(this.form).then((res) => {
this.$emit('next', res.id)
// 用来区分群活码,生成的二维码需要带有唯一区分信息,所以由前端生成
if (!this.form.uuid) {
this.form.uuid = uuidv4().replace(/-/g, '')
}
let options = {
text: this.qrCodeText,
size: 200,
margin: 0
}
// 添加logo信息
if (this.form.activityHeadUrl) {
options = Object.assign(options, {
logoImage: this.headImage,
logoMargin: 2,
logoCornerRadius: 3
})
}
const qr = new AwesomeQR(options)
qr.draw().then((dataURL) => {
// 上传群活码图片到云存储
uploadDataURL(dataURL).then((res) => {
if (res.code === 200) {
this.form.codeUrl = res.url
// 新增群活码数据至数据库
add(this.form).then((res) => {
this.$emit('next', res.id)
})
} else {
}
})
})
})
},
// 获取上传的头像数据
handleUploadedHeadImage (file) {
const reader = new FileReader()
reader.readAsDataURL(file.raw)
reader.onload = () => {
this.headImage = reader.result
}
},
// 更新群活码
update () {
this.$refs.form.validate((valid) => {
......@@ -252,7 +308,9 @@ export default {
guide: res.data.guide,
joinGroupIsTip: parseInt(res.data.joinGroupIsTip),
tipMsg: res.data.tipMsg,
customerServerQrCode: res.data.customerServerQrCode
customerServerQrCode: res.data.customerServerQrCode,
uuid: res.data.uuid,
codeUrl: res.data.codeUrl
}
} else {
}
......@@ -264,7 +322,13 @@ export default {
if (!this.groupCodeId) return this.add()
this.update()
// this.$emit('next')
this.$emit('next')
}
},
computed: {
qrCodeText () {
return process.env.VUE_APP_BASE_API + this.h5uri + '?id=' + this.form.uuid
}
},
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!