util.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // Loadding
  55. public static showLoading(title='加载中...',mask:boolean=false) {
  56. uni.showLoading({
  57. title:title,
  58. mask:mask
  59. })
  60. }
  61. public static hideLoading() {
  62. uni.hideLoading()
  63. }
  64. // Toast
  65. public static showInfoToast(title:string,duration=1500,mask=false) {
  66. uni.showToast({
  67. title:title,
  68. icon:'none',
  69. duration:duration,
  70. mask:mask
  71. })
  72. }
  73. public static showSuccessToast(title:string,duration=1500,mask=false) {
  74. uni.showToast({
  75. title:title,
  76. icon:'success',
  77. duration:duration,
  78. mask:mask
  79. })
  80. }
  81. public static showErrorToast(title:string,duration=1500,mask=false) {
  82. uni.showToast({
  83. title:title,
  84. icon:'error',
  85. duration:duration,
  86. mask:mask
  87. })
  88. }
  89. // Modal
  90. public static showModal(title:string, content:string, confirm_cb:Function=null, cancel_cb:Function=null, confirmText:string='确定', showCancel:boolean=true, cancelText:string='取消') {
  91. uni.showModal({
  92. title: title,
  93. content: content,
  94. confirmText: confirmText,
  95. showCancel: showCancel,
  96. cancelText: cancelText,
  97. success: function (res) {
  98. if (res.confirm) {
  99. // console.log('用户点击确定')
  100. confirm_cb && confirm_cb()
  101. } else if (res.cancel) {
  102. // console.log('用户点击取消')
  103. cancel_cb && cancel_cb()
  104. }
  105. }
  106. })
  107. }
  108. public static showModalNoCancel(title:string, content:string, confirm_cb:Function=null, cancel_cb:Function=null, confirmText:string='确定'){
  109. this.showModal(title,content,confirm_cb,cancel_cb,confirmText,false)
  110. }
  111. // ActionSheet
  112. public static showActionSheet(alertText:string, itemList:string[], confirm_cb:Function, cancel_cb:Function=null) {
  113. uni.showActionSheet({
  114. alertText:alertText,
  115. itemList: itemList,
  116. success: function (res) {
  117. // console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
  118. confirm_cb && confirm_cb(res.tapIndex)
  119. },
  120. fail: function (err) {
  121. // console.log('showActionSheet err=',err.errMsg);
  122. cancel_cb && cancel_cb()
  123. }
  124. });
  125. }
  126. }