_data.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. export default {
  2. /**
  3. * [设置获取globalData数据]
  4. * @param {Object} k 设置/获取的键
  5. * @param {Object} v 设置的值,没有传值就是获取这个键的值
  6. * @return {String|Array|Object}
  7. */
  8. data(k,v){
  9. if(v === undefined){
  10. return getApp().globalData[k];
  11. }
  12. else{
  13. getApp().globalData[k] = v;
  14. }
  15. },
  16. /**
  17. * [设置获取保存在本地的页面数据]
  18. * @param {Object} k 设置/获取的键
  19. * @param {Object} v 设置的值,v为undefined获取这个键的值,v为null,移除这个键的数据
  20. * @return {String|Array|Object}
  21. */
  22. localData(k,v){
  23. if(v === undefined){
  24. return uni.getStorageSync(k);
  25. }
  26. else if(v === null){
  27. uni.removeStorage({
  28. key: k,
  29. fail(err){
  30. console.log(err,'uni.removeStorage');
  31. }
  32. });
  33. }
  34. else {
  35. uni.setStorage({
  36. key: k,
  37. data: v,
  38. fail(){
  39. console.log(err,'uni.setStorage');
  40. }
  41. });
  42. }
  43. },
  44. domainUrl(){
  45. return getApp().globalData.http_url
  46. },
  47. staticUrl(){
  48. return getApp().globalData.static_url
  49. },
  50. /** 聊天静态文件地址 */
  51. staticChat(){
  52. return getApp().globalData.static_url + '/static/chat/';
  53. },
  54. /** 朋友圈静态文件地址 */
  55. staticCircle(){
  56. return getApp().globalData.static_url + '/static/circle/';
  57. },
  58. /** 头像地址 */
  59. staticPhoto(){
  60. return getApp().globalData.static_url + '/static/photo/';
  61. },
  62. /** 视频图片地址 */
  63. staticVideoImg(){
  64. return getApp().globalData.static_url + '/static/photo/video_gif/';
  65. },
  66. /** 获取会话界面有多少未读消息 */
  67. chatTipsNum(){
  68. let num = 0,
  69. chat_list = uni.getStorageSync('chat_list');
  70. if(chat_list){
  71. for(let value of chat_list){
  72. // todo屏蔽 如果屏蔽了则走以下消息提示规则, 如果屏蔽,但是@本人和所有人all,则不提示消息
  73. // if(value.is_disturb == 1){
  74. // }else{
  75. // num += (value.no_reader_num * 1);
  76. // }
  77. num += (value.no_reader_num * 1);
  78. }
  79. }
  80. return num;
  81. },
  82. //根据网络地址获取本地地址
  83. getDowndloadVedio(url,cb){
  84. //ifdef H5
  85. return url;
  86. //endif
  87. let _this = this;
  88. let key = 'VEDIO_URL_'+ url;
  89. let address = this.localData(key);
  90. console.log(address)
  91. if(address == undefined || address == null || !address){
  92. const downloadTask = uni.downloadFile({
  93. url: url, //仅为示例,并非真实的资源
  94. success: (res) => {
  95. if (res.statusCode === 200) {
  96. uni.saveFile({
  97. tempFilePath: res.tempFilePath,
  98. success: function(red) {
  99. //下载到本地下次秒读取
  100. address = red.savedFilePath
  101. _this.localData(key,address);
  102. if(cb)cb(address)
  103. }
  104. });
  105. }
  106. }
  107. });
  108. downloadTask.onProgressUpdate((res) => {
  109. console.log('下载进度' + res.progress);
  110. console.log('已经下载的数据长度' + res.totalBytesWritten);
  111. console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
  112. });
  113. }else {
  114. if(cb)cb(address);
  115. return address
  116. }
  117. }
  118. }