wlzboy
2025-11-08 d3fd4b0ab851bab89c30c199e27245f7f45f1c0e
feat:实现了微信上传图片
4个文件已修改
99 ■■■■■ 已修改文件
app/App.vue 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/manifest.json 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/pages/task/detail.vue 7 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/utils/storage.js 52 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/App.vue
@@ -3,6 +3,7 @@
  import store from '@/store'
  import { getToken } from '@/utils/auth'
  import { getUnreadCount } from '@/api/message'
  import storage from '@/utils/storage'
  export default {
    data() {
@@ -14,6 +15,9 @@
    onLaunch: function() {
      this.lastToken = getToken()
      this.initApp()
      // 检查并清理存储空间
      this.checkStorage()
      
      // 监听用户登录事件
      uni.$on('user-login', () => {
@@ -148,6 +152,34 @@
          clearInterval(this.messagePollingTimer)
          this.messagePollingTimer = null
        }
      },
      // 检查存储空间
      checkStorage() {
        try {
          // 获取存储信息
          const info = storage.getStorageInfo()
          if (info) {
            const usagePercent = (info.currentSize / info.limitSize) * 100
            console.log(`💾 存储使用情况: ${usagePercent.toFixed(2)}% (${info.currentSize}KB / ${info.limitSize}KB)`)
            // 如果使用超过 80%,自动清理
            if (usagePercent > 80) {
              console.warn('⚠️ 存储空间使用超过 80%,开始自动清理...')
              storage.checkAndClean()
              // 再次检查
              const newInfo = storage.getStorageInfo()
              if (newInfo) {
                const newUsagePercent = (newInfo.currentSize / newInfo.limitSize) * 100
                console.log(`✅ 清理后使用情况: ${newUsagePercent.toFixed(2)}%`)
              }
            }
          }
        } catch (e) {
          console.error('检查存储失败:', e)
        }
      }
    }
  }
app/manifest.json
@@ -46,7 +46,9 @@
            "urlCheck" : false,
            "es6" : false,
            "minified" : true,
            "postcss" : true
            "postcss" : true,
            "ignoreDevUnusedFiles" : false,
            "ignoreUploadUnusedFiles" : false
        },
        "optimization" : {
            "subPackages" : true
@@ -63,7 +65,9 @@
            "scope.userLocation" : {
                "desc" : "需要自动获得当前用户位置,以判断司机是否在常驻地还是在目的地"
            }
        }
        },
        "cloudfunctionRoot" : "",
        "lazyCodeLoading" : "requiredComponents"
    },
    "vueVersion" : "2",
    "h5" : {
app/pages/task/detail.vue
@@ -743,13 +743,18 @@
          
          // 第一步:从后端获取AccessToken
          getWechatAccessToken().then(tokenResponse => {
            const accessToken = tokenResponse.data || tokenResponse
            // 接口返回格式:{"msg":"token值","code":200}
            console.log('获取AccessToken成功:', tokenResponse)
            const accessToken = tokenResponse.msg || tokenResponse.data || tokenResponse
            if (!accessToken) {
              uni.hideLoading()
              that.$modal.showToast('获取AccessToken失败')
              console.error('获取AccessToken失败,响应数据:', tokenResponse)
              return
            }
            
            console.log('获取到AccessToken:', accessToken)
            // 第二步:上传到微信服务器
            const uploadUrl = `https://api.weixin.qq.com/cgi-bin/media/upload?access_token=${accessToken}&type=image`
            
app/utils/storage.js
@@ -26,6 +26,58 @@
  },
  clean: function() {
    uni.removeStorageSync(storageKey)
  },
  // 新增:获取存储信息
  getStorageInfo: function() {
    try {
      const info = uni.getStorageInfoSync()
      console.log('💾 存储信息:', {
        keys: info.keys,
        currentSize: info.currentSize,
        limitSize: info.limitSize,
        usage: ((info.currentSize / info.limitSize) * 100).toFixed(2) + '%'
      })
      return info
    } catch (e) {
      console.error('获取存储信息失败:', e)
      return null
    }
  },
  // 新增:清理所有存储(慎用)
  clearAll: function() {
    try {
      uni.clearStorageSync()
      console.log('✅ 清理所有存储成功')
    } catch (e) {
      console.error('清理存储失败:', e)
    }
  },
  // 新增:检查并清理过期数据
  checkAndClean: function() {
    try {
      const info = uni.getStorageInfoSync()
      const usagePercent = (info.currentSize / info.limitSize) * 100
      // 如果使用超过 80%,提示用户
      if (usagePercent > 80) {
        console.warn('⚠️ 存储空间使用超过 80%:', usagePercent.toFixed(2) + '%')
        // 自动清理非必要的缓存
        const keysToKeep = [storageKey, 'token', 'uni-id-pages-userInfo']
        info.keys.forEach(key => {
          if (!keysToKeep.includes(key)) {
            try {
              uni.removeStorageSync(key)
              console.log('🗑️ 清理缓存:', key)
            } catch (e) {
              console.error('清理失败:', key, e)
            }
          }
        })
      }
    } catch (e) {
      console.error('检查存储失败:', e)
    }
  }
}