common.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // 事件统计
  2. import store from '@/store/index.js'
  3. import {
  4. emojiMap,
  5. emojiUrl
  6. } from './emojiMap';
  7. //取值
  8. function get(key,sync = true) {
  9. try {
  10. if(sync){
  11. return uni.getStorageSync(key);
  12. }else{
  13. let data = '';
  14. uni.getStorage({
  15. key:key,
  16. success: function (res) {
  17. data = res.data;
  18. }
  19. });
  20. return data;
  21. }
  22. } catch (e) {
  23. return false;
  24. }
  25. }
  26. //赋值
  27. function set(key, value, sync = true) {
  28. try {
  29. // console.log(key)
  30. if (sync) {
  31. return uni.setStorageSync(key, value);
  32. } else {
  33. uni.setStorage({
  34. key: key,
  35. data: value
  36. });
  37. }
  38. } catch (e) {
  39. }
  40. }
  41. //移除
  42. function del(key, sync = true){
  43. try {
  44. if (sync) {
  45. return uni.removeStorageSync(key);
  46. } else {
  47. uni.removeStorage({
  48. key: key
  49. });
  50. }
  51. } catch (e) {
  52. return false;
  53. }
  54. }
  55. // 时间戳转时间 年月日
  56. function timeStamp1(value) {
  57. var date = new Date(value * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  58. var year = date.getFullYear();
  59. var month = ("0" + (date.getMonth() + 1)).slice(-2);
  60. var sdate = ("0" + date.getDate()).slice(-2);
  61. var hour = ("0" + date.getHours()).slice(-2);
  62. var minute = ("0" + date.getMinutes()).slice(-2);
  63. var second = ("0" + date.getSeconds()).slice(-2);
  64. // 拼接
  65. var result = year + '-' + month + '-' + sdate + ' ' + hour + ":" + minute; //+ ":" + second;
  66. // 返回
  67. return result;
  68. }
  69. function chooseImage(num,sType) {
  70. let { token,domain,uploadHost,httpsdomain } = store.state.qiuNiu;
  71. let _this = this;
  72. let arr = [];
  73. let sourceType = sType || ['album', 'camera'];
  74. console.log(sourceType);
  75. return new Promise((resolve, reject) => {
  76. uni.chooseImage({
  77. count: num || 9, //默认9
  78. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  79. sourceType: sourceType, //从相册选择
  80. success: function(imgRes) {
  81. let imgArr = imgRes.tempFiles
  82. uni.showLoading({
  83. title: _this.$t('common.lab')
  84. })
  85. imgArr.forEach(item => {
  86. // console.log();
  87. let type = item.name.slice(-4);
  88. let key = new Date().getTime() + type;
  89. uni.uploadFile({
  90. url: 'https://' + uploadHost, //后台地址
  91. filePath: item.path,
  92. name: 'file',
  93. header: {
  94. 'token': token
  95. },
  96. formData:{
  97. 'key': key,//key值
  98. 'token': token //七牛云token值
  99. },
  100. success: (uploadFileRes) => {
  101. let data = JSON.parse(uploadFileRes.data)
  102. if (num == undefined || num == 1) { //单张
  103. resolve(httpsdomain + '/'+ data.key);
  104. } else { //多张
  105. arr.push(httpsdomain + '/'+ data.key)
  106. if (arr.length === imgArr.length) {
  107. resolve(arr);
  108. }
  109. }
  110. uni.hideLoading()
  111. },
  112. fail(uploadFileRes) {
  113. _this.$toast(uploadFileRes)
  114. }
  115. })
  116. })
  117. },
  118. fail: function(res) {
  119. console.log(res);
  120. }
  121. });
  122. });
  123. }
  124. function parseText(message) {
  125. const renderDom = [];
  126. let temp = message.payload.data;
  127. let left = -1;
  128. let right = -1;
  129. while (temp !== '') {
  130. left = temp.indexOf('[');
  131. right = temp.indexOf(']');
  132. switch (left) {
  133. case 0:
  134. if (right === -1) {
  135. renderDom.push({
  136. name: 'span',
  137. text: temp
  138. });
  139. temp = '';
  140. } else {
  141. const _emoji = temp.slice(0, right + 1);
  142. if (emojiMap[_emoji]) {
  143. renderDom.push({
  144. name: 'img',
  145. src: emojiUrl + emojiMap[_emoji]
  146. });
  147. temp = temp.substring(right + 1);
  148. } else {
  149. renderDom.push({
  150. name: 'span',
  151. text: '['
  152. });
  153. temp = temp.slice(1);
  154. }
  155. }
  156. break;
  157. case -1:
  158. renderDom.push({
  159. name: 'span',
  160. text: temp
  161. });
  162. temp = '';
  163. break;
  164. default:
  165. renderDom.push({
  166. name: 'span',
  167. text: temp.slice(0, left)
  168. });
  169. temp = temp.substring(left);
  170. break;
  171. }
  172. }
  173. return renderDom;
  174. } // 解析群系统消息
  175. //视频选择
  176. function chooseVideo() {
  177. let _this = this
  178. /*
  179. num=5:视频上传
  180. */
  181. return new Promise((resolve, reject) => {
  182. uni.chooseVideo({
  183. count: 1, //默认9
  184. sourceType: ['album', 'camera'], //从相册选择
  185. success: function(imgRes) {
  186. let imgArr = imgRes.tempFilePath
  187. console.log(imgArr);
  188. // return
  189. uni.showLoading({
  190. title: 'loading'
  191. })
  192. uni.uploadFile({
  193. url: apiUrl.VUE_APP_API_WS + '/app-api/file/upload', //后台地址
  194. filePath: imgArr,
  195. name: 'avatarFile',
  196. header: {
  197. 'tenant-id': 1,
  198. 'Authorization': 'Bearer ' + uni.getStorageSync('token1')
  199. },
  200. success: (uploadFileRes) => {
  201. let data = JSON.parse(uploadFileRes.data)
  202. resolve(data);
  203. uni.hideLoading()
  204. // that.form.avatar = uploadFileRes.data
  205. },
  206. fail(uploadFileRes) {
  207. uni.hideLoading()
  208. console.log(uploadFileRes);
  209. // _this.$u.toast(uploadFileRes)
  210. }
  211. })
  212. },
  213. fail: function(res) {
  214. console.log(res);
  215. }
  216. });
  217. });
  218. }
  219. function parseTime(time, cFormat) {
  220. if (arguments.length === 0) {
  221. return null
  222. }
  223. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  224. let date
  225. if (typeof time === 'object') {
  226. date = time
  227. } else {
  228. if (('' + time).length === 10) time = parseInt(time) * 1000
  229. date = new Date(time)
  230. }
  231. const formatObj = {
  232. y: date.getFullYear(),
  233. m: date.getMonth() + 1,
  234. d: date.getDate(),
  235. h: date.getHours(),
  236. i: date.getMinutes(),
  237. s: date.getSeconds(),
  238. a: date.getDay()
  239. }
  240. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  241. let value = formatObj[key]
  242. // Note: getDay() returns 0 on Sunday
  243. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
  244. if (result.length > 0 && value < 10) {
  245. value = '0' + value
  246. }
  247. return value || 0
  248. })
  249. return time_str
  250. }
  251. function countdownFun(value) {
  252. if (!value) {
  253. return ''
  254. }
  255. var d = parseInt(value/(3600 * 24))
  256. if (d > 0) {
  257. var h = parseInt((value%(3600 * 24))/(3600));
  258. // if (h < 10) {
  259. // h = '0' + h
  260. // }
  261. return `${d}d ${h}h`
  262. }
  263. var h = parseInt(value/(3600));
  264. var m = parseInt((value%(3600))/60);
  265. var str ='';
  266. if (h > 0) {
  267. str += (h + 'h')
  268. if (m < 10) {
  269. m = '0' + m
  270. }
  271. str += (' ' + m + 'm')
  272. return str
  273. } else {
  274. if (m > 0) {
  275. if (m < 10) {
  276. m = '0' + m
  277. }
  278. str += (m + 'm')
  279. }
  280. }
  281. var s = parseInt(value%(3600)%60)
  282. if (s < 10) {
  283. s = '0' + s
  284. }
  285. str += (' ' + s + 's')
  286. return str
  287. }
  288. function handleDigits2(value) {
  289. if (value === undefined || isNaN(value)) {
  290. return '0.0'
  291. }
  292. let arr = value.toString().split('.')
  293. let str = arr[1]
  294. let z = ''
  295. if (!str || str.length < 1) {
  296. let length = str ? (1 - str.length) : 1
  297. for (let i = 0; i < length; i++) {
  298. z += '0'
  299. }
  300. }
  301. return `${arr[0]}.${str || ''}${z}`
  302. }
  303. export default {
  304. del,
  305. get,
  306. set,
  307. countdownFun,
  308. parseTime,
  309. parseText,
  310. timeStamp1,
  311. chooseVideo,
  312. chooseImage,
  313. handleDigits2
  314. }