common.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import cacheManager from "./cacheManager.js"
  2. /**
  3. * 显示消息提示框
  4. * @param content 提示的标题
  5. */
  6. export function toast(content) {
  7. uni.showToast({
  8. icon: 'none',
  9. title: content
  10. })
  11. }
  12. /**
  13. * 显示模态弹窗
  14. * @param content 提示的标题
  15. */
  16. export function showConfirm(content) {
  17. return new Promise((resolve, reject) => {
  18. uni.showModal({
  19. title: '提示',
  20. content: content,
  21. cancelText: '取消',
  22. confirmText: '确定',
  23. success: function(res) {
  24. resolve(res)
  25. }
  26. })
  27. })
  28. }
  29. /**
  30. * 参数处理
  31. * @param params 参数
  32. */
  33. export function tansParams(params) {
  34. let result = ''
  35. // FIXME 拼接参数
  36. return result
  37. }
  38. /**
  39. * @summary 获取请求异常与正常返回
  40. * @param {Object} promise
  41. */
  42. export function catchError(promise) {
  43. return new Promise((resolve,reject) => {
  44. promise.then(data => {
  45. resolve([undefined, data.data])
  46. }).catch(err => {
  47. reject([err])
  48. })
  49. })
  50. }
  51. // 是否是会员
  52. export function getUserIdentity() {
  53. const auth = cacheManager.get('auth');
  54. if (auth) {
  55. if ((auth.levelIdList || []).some(item => item == auth.levelId)) {
  56. // 购买此levelId
  57. return 'VIP'
  58. }
  59. // 无购买此levelId
  60. return 'Not-Vip';
  61. } else {
  62. // 游客
  63. return 'Visitor';
  64. }
  65. }
  66. export function hasUserIdentity() {
  67. const auth = cacheManager.get('auth');
  68. if (auth) {
  69. if (auth.cardList.length) {
  70. // VIP
  71. return 'VIP'
  72. }
  73. // 非VIP
  74. return 'Not-Vip';
  75. } else {
  76. // 游客
  77. return 'Visitor';
  78. }
  79. }
  80. export function debounce(func, wait) {
  81. let timeout;
  82. return function(...args) {
  83. // 清除之前的定时器
  84. clearTimeout(timeout);
  85. // 设置新的定时器
  86. timeout = setTimeout(() => {
  87. func.apply(this, args);
  88. }, wait);
  89. };
  90. }