index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Created by JianJia.Zhou<jianjia.zhou> on 2022/5/26.
  3. */
  4. export function copyText(text, tips) {
  5. uni.setClipboardData({
  6. data: text,
  7. success: function() {
  8. uni.hideLoading()
  9. uni.showToast({
  10. title: tips || '复制成功',
  11. icon: 'none'
  12. })
  13. }
  14. })
  15. }
  16. export function checkOS() {
  17. if (uni.getSystemInfoSync().platform === 'ios') {
  18. uni.showModal({
  19. title: '提示',
  20. content: '由于相关规范,iOS功能暂不可用',
  21. showCancel: false,
  22. success: function(res) {
  23. // res.confirm res.cancel
  24. }
  25. })
  26. return false
  27. }
  28. return true
  29. }
  30. const shareMessage = user => {
  31. // #ifdef MP-KUAISHOU
  32. const title ="四海剧场"
  33. // #endif
  34. // #ifdef MP-TOUTIAO | MP-WEIXIN
  35. const title ="张四爷剧场"
  36. // #endif
  37. console.log('-->data', user)
  38. return {
  39. path: `/pages/index/index?user_id=${user.id}`,
  40. title: title,
  41. desc: title
  42. // imageUrl: ''
  43. }
  44. }
  45. const tranNumber = (num, point = 2) => {
  46. let numStr = parseFloat(num).toString()
  47. // 万以内直接返回
  48. if (numStr.length < 5) {
  49. return numStr;
  50. }
  51. //大于5位数是万 (以1W分割 1W以下全部显示)
  52. else if (numStr.length > 4) {
  53. let decimal = numStr.substring(numStr.length - 4, numStr.length - 4 + point)
  54. return parseFloat(parseInt(num / 10000) + '.' + decimal) + '万';
  55. }
  56. }
  57. const saveImage = url => {
  58. return new Promise((resolve, reject) => {
  59. uni.downloadFile({
  60. url: url,
  61. // #ifdef MP-TOUTIAO
  62. header: {
  63. "content-type": "application/json",
  64. },
  65. // #endif
  66. success: (res) => {
  67. if (res.statusCode === 200) {
  68. console.log('下载成功');
  69. uni.authorize({
  70. // #ifdef MP-WEIXIN
  71. scope: 'scope.writePhotosAlbum',
  72. // #endif
  73. // #ifdef MP-TOUTIAO
  74. scope: "scope.album",
  75. // #endif
  76. success() {
  77. uni.saveImageToPhotosAlbum({
  78. filePath: res.tempFilePath,
  79. success: function(red) {
  80. uni.$u.toast(`保存成功`)
  81. //uni.$u.toast(`保存路径:${red.savedFilePath}`)
  82. resolve()
  83. },
  84. fail: function(err) {
  85. console.log('-->save error',err)
  86. uni.$u.toast(`保存失败`)
  87. reject()
  88. }
  89. });
  90. },
  91. fail: err => {
  92. console.log('-->authorize fail',err)
  93. uni.$u.toast(`授权失败`+JSON.stringify(err))
  94. reject()
  95. }
  96. })
  97. }else{
  98. uni.$u.toast(`保存失败`)
  99. reject()
  100. }
  101. },
  102. fail: err => {
  103. uni.$u.toast(`保存失败`+JSON.stringify(err))
  104. reject()
  105. }
  106. });
  107. })
  108. }
  109. export default {
  110. copyText,
  111. checkOS,
  112. shareMessage,
  113. tranNumber,
  114. saveImage
  115. }