uniapp-api.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // 检查是否登录,未登录不请求
  25. const token = uni.getStorageSync('token');
  26. if (!token) {
  27. uni.removeTabBarBadge({ index: 2 });
  28. return;
  29. }
  30. // 确保 uView http 可用
  31. if (uni.$u && uni.$u.http) {
  32. uni.$u.http.get('/token/shop/cart/getCount').then(res => {
  33. if (res.code == 200) {
  34. const count = res.data;
  35. if (count > 0) {
  36. uni.setTabBarBadge({
  37. index: 2,
  38. text: String(count)
  39. });
  40. } else {
  41. uni.removeTabBarBadge({
  42. index: 2
  43. });
  44. }
  45. }
  46. }).catch(e => {
  47. console.log('更新购物车角标失败', e);
  48. });
  49. }
  50. }