_data.js 2.7 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. k=uni.getStorageSync('access_token')+'_'+k;
  10. if(v === undefined){
  11. return getApp().globalData[k];
  12. }
  13. else{
  14. getApp().globalData[k] = v;
  15. }
  16. },
  17. /**
  18. * [设置获取保存在本地的页面数据]
  19. * @param {Object} k 设置/获取的键
  20. * @param {Object} v 设置的值,v为undefined获取这个键的值,v为null,移除这个键的数据
  21. * @return {String|Array|Object}
  22. */
  23. localData(k,v){
  24. k=uni.getStorageSync('access_token')+'_'+k;
  25. if(v === undefined){
  26. return uni.getStorageSync(k);
  27. }
  28. else if(v === null){
  29. uni.removeStorage({
  30. key: k,
  31. fail(){
  32. uni.showModal({
  33. content: '删除本地数据失败',
  34. });
  35. }
  36. });
  37. }
  38. else {
  39. uni.setStorage({
  40. key: k,
  41. data: v,
  42. fail(){
  43. uni.showModal({
  44. content: '本地数据设置失败,请检测storage存储',
  45. });
  46. }
  47. });
  48. }
  49. },
  50. cache(k){
  51. k=uni.getStorageSync('access_token')+'_'+k;
  52. return uni.getStorageSync(k);
  53. },
  54. addData(k,v){
  55. k=uni.getStorageSync('access_token')+'_'+k;
  56. //console.log(k);
  57. // console.log(v);
  58. let data=uni.getStorageSync(k);
  59. // data=[];
  60. if(v!=undefined){
  61. if(!data){
  62. data=[];
  63. v.map(item=>{
  64. data.push(item);
  65. })
  66. }
  67. else {
  68. v.map(item=>{
  69. data.unshift(item);
  70. })
  71. }
  72. }
  73. let item11=[];
  74. data.map(item=>{
  75. if(item!=null) {
  76. var add=1;
  77. item11.map(item2=>{
  78. if(item2.id==item.id) {
  79. add=0;
  80. }
  81. })
  82. if(add==1)
  83. item11.push(item);
  84. }
  85. })
  86. item11.sort((a, b) => parseInt(b.time)- parseInt(a.time));
  87. uni.setStorageSync(k,item11);
  88. return item11;
  89. },
  90. /** 聊天静态文件地址 */
  91. staticChat(){
  92. return getApp().globalData.static_url + '/static/chat/';
  93. },
  94. /** 朋友圈静态文件地址 */
  95. staticCircle(){
  96. return getApp().globalData.static_url + '/static/circle/';
  97. },
  98. /** 头像地址 */
  99. staticPhoto(){
  100. return getApp().globalData.static_url + '/static/photo/';
  101. },
  102. /** 获取会话界面有多少未读消息 */
  103. chatTipsNum(){
  104. let num = 0,
  105. chat_list = uni.getStorageSync('chat_list');
  106. if(chat_list){
  107. for(let value of chat_list){
  108. num += (value.no_reader_num * 1);
  109. }
  110. }
  111. return num;
  112. },
  113. }