放肆青春的博客
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)

放肆青春

一个前端菜鸟的技术成长之路
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)
  • 前端

    • 前端 概览
    • 前端汇总

    • front 博文

    • front 项目总结

    • front 高级

    • front tools

  • vue

    • vue 概览
    • vue 汇总

    • vue 博文

    • vue 项目总结

    • vue 高级

  • html

    • html 概览
    • html 汇总

    • html 博文

  • css

    • css 概览
    • css 汇总

    • css 博文

    • sass

    • less

  • js

    • javascript 概览
    • JS 汇总

    • ES6

    • JS 博文

    • JS 工具

  • node

    • node 概览
    • node 汇总

    • node 框架

    • node 博文

  • react

    • react 概览
    • react 汇总

    • react 博文

    • react 高级

  • 微信小程序

    • 微信小程序 概览
    • 微信小程序总结
      • 微信小程序总结
        • 微信小程序前端直传
    • 微信小程序文章
    • 微信小程序 博文

    • 微信小程序 高级

  • 微信公众号

    • 微信公众号 概览
    • 微信公众号总结
    • 微信公众号文章
  • 多端开发

    • 多端开发
    • dsbridge 概览
    • jsbridge 概览
    • webview
    • uniapp

      • uniapp 概览
    • taro

      • taro 概览
    • flutter

      • flutter 概览
      • flutter 环境搭建
    • electron

      • electron 概览
  • front
放肆青春
2020-07-09

微信小程序总结

# 微信小程序总结

# 微信小程序前端直传

  1. 通过微信 API 调起相册或者摄像头

  2. 调用获取 oss 配置接口

  3. 设置 key(key 为 oss 云上的文件夹+文件名)

  4. 通过微信 API 调起上传

附:1、oss 服务器通过 http 请求的 code 状态码告知前端是否上传成功

2、该方案服务器无感知,需要配好key,在表单提交的时候将key发送给服务器
// 选择图片
  choosePhoto () {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: res => {
        this.uploadPhoto(res);
      }
    })
  },
// 直传oss
  async uploadPhoto (res) {
    wx.showLoading({ title: '处理中...' });

    let r = await getPolicy()
    if (r.code != 200) {
      wx.showToast({icon: 'none', title: '获取oss配置异常'})
      return
    }

    let ret = r.data
    var tempFilePaths = res.tempFilePaths
    // key为oss云上的文件夹+文件名
    let key = `${ret.dir}img_${new Date().getTime()}.${tempFilePaths[0].split('.').pop()}`
    // console.log('chooseImage success, temp path is: ', tempFilePaths[0])

    wx.uploadFile({
      url: ret.host,
      filePath: tempFilePaths[0],
      name: 'file',
      formData: {
        name: tempFilePaths[0],
        key: key,
        policy: ret.policy,
        OSSAccessKeyId: ret.accessid,
        success_action_status: "200",
        signature: ret.signature,
      },
      success: (res) => {
        wx.hideLoading()
        // console.log('chooseImage success, temp path is: ', tempFilePaths[0])
        this.setData({
          'photoPath': tempFilePaths[0],
          'rqdata.teamLogoKey': key
        })
        wx.showToast({
          title: "上传成功",
          icon: 'success',
          duration: 1000
        })
      },
      fail: ({errMsg}) => {
        wx.hideLoading()
        // console.log('upladImage fail, errMsg is: ', errMsg)
        wx.showToast({
          title: "上传失败",
          duration: 1000
        })
      },
    })
  },
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
更新时间: 11/5/2021, 5:21:30 PM
微信小程序 概览
微信小程序文章

← 微信小程序 概览 微信小程序文章→

最近更新
01
前端权限管理
02-24
02
vue2指令
02-24
03
vue2 hook
02-24
更多文章>
Theme by Vdoing | Copyright © 2019-2022 放肆青春
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式