123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- // 事件统计
- import store from '@/store/index.js'
- import {
- emojiMap,
- emojiUrl
- } from './emojiMap';
- //取值
- function get(key,sync = true) {
- try {
- if(sync){
- return uni.getStorageSync(key);
- }else{
- let data = '';
- uni.getStorage({
- key:key,
- success: function (res) {
- data = res.data;
- }
- });
- return data;
- }
- } catch (e) {
- return false;
- }
- }
- //赋值
- function set(key, value, sync = true) {
- try {
- // console.log(key)
- if (sync) {
- return uni.setStorageSync(key, value);
- } else {
- uni.setStorage({
- key: key,
- data: value
- });
- }
- } catch (e) {
- }
- }
- //移除
- function del(key, sync = true){
- try {
- if (sync) {
- return uni.removeStorageSync(key);
- } else {
- uni.removeStorage({
- key: key
- });
- }
- } catch (e) {
- return false;
- }
- }
- // 时间戳转时间 年月日
- function timeStamp1(value) {
- var date = new Date(value * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
- var year = date.getFullYear();
- var month = ("0" + (date.getMonth() + 1)).slice(-2);
- var sdate = ("0" + date.getDate()).slice(-2);
- var hour = ("0" + date.getHours()).slice(-2);
- var minute = ("0" + date.getMinutes()).slice(-2);
- var second = ("0" + date.getSeconds()).slice(-2);
- // 拼接
- var result = year + '-' + month + '-' + sdate + ' ' + hour + ":" + minute; //+ ":" + second;
- // 返回
- return result;
- }
- function chooseImage(num,sType) {
- let { token,domain,uploadHost,httpsdomain } = store.state.qiuNiu;
-
- let _this = this;
- let arr = [];
- let sourceType = sType || ['album', 'camera'];
- console.log(sourceType);
- return new Promise((resolve, reject) => {
- uni.chooseImage({
- count: num || 9, //默认9
- sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
- sourceType: sourceType, //从相册选择
- success: function(imgRes) {
- let imgArr = imgRes.tempFiles
- uni.showLoading({
- title: _this.$t('common.lab')
- })
-
- imgArr.forEach(item => {
- // console.log();
- let type = item.name.slice(-4);
- let key = new Date().getTime() + type;
- uni.uploadFile({
- url: 'https://' + uploadHost, //后台地址
- filePath: item.path,
- name: 'file',
- header: {
- 'token': token
- },
- formData:{
- 'key': key,//key值
- 'token': token //七牛云token值
- },
- success: (uploadFileRes) => {
- let data = JSON.parse(uploadFileRes.data)
- if (num == undefined || num == 1) { //单张
- resolve(httpsdomain + '/'+ data.key);
- } else { //多张
- arr.push(httpsdomain + '/'+ data.key)
- if (arr.length === imgArr.length) {
- resolve(arr);
- }
- }
- uni.hideLoading()
- },
- fail(uploadFileRes) {
- _this.$toast(uploadFileRes)
- }
- })
- })
- },
- fail: function(res) {
- console.log(res);
- }
- });
- });
- }
- function parseText(message) {
- const renderDom = [];
- let temp = message.payload.data;
- let left = -1;
- let right = -1;
- while (temp !== '') {
- left = temp.indexOf('[');
- right = temp.indexOf(']');
- switch (left) {
- case 0:
- if (right === -1) {
- renderDom.push({
- name: 'span',
- text: temp
- });
- temp = '';
- } else {
- const _emoji = temp.slice(0, right + 1);
- if (emojiMap[_emoji]) {
- renderDom.push({
- name: 'img',
- src: emojiUrl + emojiMap[_emoji]
- });
- temp = temp.substring(right + 1);
- } else {
- renderDom.push({
- name: 'span',
- text: '['
- });
- temp = temp.slice(1);
- }
- }
- break;
- case -1:
- renderDom.push({
- name: 'span',
- text: temp
- });
- temp = '';
- break;
- default:
- renderDom.push({
- name: 'span',
- text: temp.slice(0, left)
- });
- temp = temp.substring(left);
- break;
- }
- }
- return renderDom;
- } // 解析群系统消息
- //视频选择
- function chooseVideo() {
- let _this = this
- /*
- num=5:视频上传
- */
- return new Promise((resolve, reject) => {
- uni.chooseVideo({
- count: 1, //默认9
- sourceType: ['album', 'camera'], //从相册选择
- success: function(imgRes) {
- let imgArr = imgRes.tempFilePath
- console.log(imgArr);
- // return
- uni.showLoading({
- title: 'loading'
- })
- uni.uploadFile({
- url: apiUrl.VUE_APP_API_WS + '/app-api/file/upload', //后台地址
- filePath: imgArr,
- name: 'avatarFile',
- header: {
- 'tenant-id': 1,
- 'Authorization': 'Bearer ' + uni.getStorageSync('token1')
- },
- success: (uploadFileRes) => {
- let data = JSON.parse(uploadFileRes.data)
- resolve(data);
- uni.hideLoading()
- // that.form.avatar = uploadFileRes.data
- },
- fail(uploadFileRes) {
- uni.hideLoading()
- console.log(uploadFileRes);
- // _this.$u.toast(uploadFileRes)
- }
- })
- },
- fail: function(res) {
- console.log(res);
- }
- });
- });
- }
- function parseTime(time, cFormat) {
- if (arguments.length === 0) {
- return null
- }
- const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
- let date
- if (typeof time === 'object') {
- date = time
- } else {
- if (('' + time).length === 10) time = parseInt(time) * 1000
- date = new Date(time)
- }
- const formatObj = {
- y: date.getFullYear(),
- m: date.getMonth() + 1,
- d: date.getDate(),
- h: date.getHours(),
- i: date.getMinutes(),
- s: date.getSeconds(),
- a: date.getDay()
- }
- const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
- let value = formatObj[key]
- // Note: getDay() returns 0 on Sunday
- if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
- if (result.length > 0 && value < 10) {
- value = '0' + value
- }
- return value || 0
- })
- return time_str
- }
- function countdownFun(value) {
- if (!value) {
- return ''
- }
- var d = parseInt(value/(3600 * 24))
- if (d > 0) {
- var h = parseInt((value%(3600 * 24))/(3600));
- // if (h < 10) {
- // h = '0' + h
- // }
- return `${d}d ${h}h`
- }
- var h = parseInt(value/(3600));
- var m = parseInt((value%(3600))/60);
- var str ='';
- if (h > 0) {
- str += (h + 'h')
- if (m < 10) {
- m = '0' + m
- }
- str += (' ' + m + 'm')
- return str
- } else {
- if (m > 0) {
- if (m < 10) {
- m = '0' + m
- }
- str += (m + 'm')
- }
- }
-
- var s = parseInt(value%(3600)%60)
- if (s < 10) {
- s = '0' + s
- }
- str += (' ' + s + 's')
- return str
- }
- function handleDigits2(value) {
- if (value === undefined || isNaN(value)) {
- return '0.0'
- }
- let arr = value.toString().split('.')
- let str = arr[1]
- let z = ''
- if (!str || str.length < 1) {
- let length = str ? (1 - str.length) : 1
- for (let i = 0; i < length; i++) {
- z += '0'
- }
- }
- return `${arr[0]}.${str || ''}${z}`
- }
- export default {
- del,
- get,
- set,
- countdownFun,
- parseTime,
- parseText,
- timeStamp1,
- chooseVideo,
- chooseImage,
- handleDigits2
- }
|