uniapp-api.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * 基于uniapp的api进行二次封装,便于全局管理
  3. *
  4. * @copyByUniappApi 全局复制到剪贴板方法
  5. */
  6. // 全局复制到剪贴板方法
  7. export const copyByUniappApi = (data, msg = '已复制到剪贴板') => {
  8. console.log(".>>>>>复制",data, msg)
  9. uni.setClipboardData({
  10. data: data.toString(),
  11. success: function() {
  12. uni.showToast({
  13. title: msg,
  14. icon: 'none'
  15. });
  16. },
  17. fail: (error) => {
  18. console.log('error',error);
  19. }
  20. });
  21. }
  22. // 全局更新购物车角标
  23. export const updateCartBadge = () => {
  24. return new Promise((resolve) => {
  25. // 检查是否登录,未登录不请求
  26. const token = uni.getStorageSync('token');
  27. if (!token) {
  28. uni.removeTabBarBadge({ index: 2 });
  29. resolve(0);
  30. return;
  31. }
  32. // 确保 uView http 可用
  33. if (uni.$u && uni.$u.http) {
  34. uni.$u.http.post('/token/shop/cart/getCount').then(res => {
  35. if (res.code == 200) {
  36. const count = res.data;
  37. uni.$emit('cartCountChanged', count);
  38. if (count > 0) {
  39. uni.setTabBarBadge({
  40. index: 2,
  41. text: String(count)
  42. });
  43. } else {
  44. uni.removeTabBarBadge({
  45. index: 2
  46. });
  47. }
  48. resolve(count);
  49. } else {
  50. resolve(0);
  51. }
  52. }).catch(e => {
  53. console.log('更新购物车角标失败', e);
  54. resolve(0);
  55. });
  56. } else {
  57. resolve(0);
  58. }
  59. });
  60. }