util.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { config } from "../config/config"
  2. import { log } from "./log"
  3. import { tools } from "./tools"
  4. export class util {
  5. /**
  6. * 获取本地数据
  7. */
  8. public static getStorage(key:string){
  9. let d = uni.getStorageSync(key)
  10. if(util.isNull(d)){
  11. return null
  12. }
  13. return d
  14. }
  15. /**
  16. * 设置本地数据
  17. */
  18. public static setStorage(key:string,value:string){
  19. uni.setStorageSync(key,value)
  20. }
  21. /**
  22. * 清除本地key数据
  23. */
  24. public static clearStorageForKey(key:string){
  25. let d = util.getStorage(key)
  26. if(util.isNull(d)){
  27. log.Error(`${key} is null!`);
  28. }else{
  29. util.setStorage(key,null)
  30. }
  31. }
  32. /**
  33. * 清除本地数据
  34. */
  35. public static clearAllStorage(){
  36. uni.clearStorageSync()
  37. }
  38. public static isNull(v:any){
  39. if(v==null||v==undefined||v==""){
  40. return true
  41. }
  42. return false
  43. }
  44. public static alert(v:string){
  45. let Platform = tools.getCurPlatform()
  46. if(Platform==config.Platform.H5){
  47. alert(v)
  48. }else if(Platform==config.Platform.TOUTIAO){
  49. log.Debug("TOUTIAO",v)
  50. }else if(Platform==config.Platform.WEIXIN){
  51. log.Debug("WEIXIN",v)
  52. }
  53. }
  54. public static showLoading(title='加载中...',mask:boolean=false) {
  55. uni.showLoading({
  56. title:title,
  57. mask:mask
  58. })
  59. }
  60. public static hideLoading() {
  61. uni.hideLoading()
  62. }
  63. public static showInfoToast(title:string,duration=1500,mask=false) {
  64. uni.showToast({
  65. title:title,
  66. icon:'none',
  67. duration:duration,
  68. mask:mask
  69. })
  70. }
  71. public static showSuccessToast(title:string,duration=1500,mask=false) {
  72. uni.showToast({
  73. title:title,
  74. icon:'success',
  75. duration:duration,
  76. mask:mask
  77. })
  78. }
  79. public static showErrorToast(title:string,duration=1500,mask=false) {
  80. uni.showToast({
  81. title:title,
  82. icon:'error',
  83. duration:duration,
  84. mask:mask
  85. })
  86. }
  87. }