微信小程序--问题汇总及详解之图片上传和地图

地图用的是百度的地图,链接:http://lbsyun.baidu.com/index.php?title=wxjsapi/guide/getlocation

获取日期时间可以用小程序里自带的js文件,var util = require('../../utils/util.js') 引入文件

var date = new Date();   //有需要改的参数可以自己定义也可以去源文件找到然后更改
    //传后台用这个参数
    var paramTime = util.formatTime2(date);
    var dateTime = util.formatDate(date);
    var time = util.formatTime3(date);
    that.setData({
      paramTime: paramTime,
      dateTime: dateTime,
      time: time,
      type: type
    })

图片上传:

 

//点击上传图片
  bindTabTap: function () {
    var that = this
    var tokend = wx.getStorageSync('tokend')
     var indexId = wx.getStorageSync('indexId')
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], //压缩图
      sourceType: ['album'], //相册
      success: function (res) {
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: 'https://..../' + indexId + '/....?token=' + tokend, //接口地址
          filePath: tempFilePaths[0],     //要上传文件资源的路径
          name: 'pic',                   //文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
          header: { 'content-type': 'multipart/form-data' },     //客户端发起一个 HTTPS POST 请求,其中 content-type 为 multipart/form-data       HTTP 请求 Header , header 中不能设置 Referer
          formData: {         //HTTP 请求中其他额外的 form data
            'type': that.data.type,
            'time': that.data.paramTime,
            'site': that.data.address
          },
          success: function (res) {  //接口调用成功的回调函数
            var data = res.data   //开发者服务器返回的数据
            wx.redirectTo({   //关闭当前页面,跳转到应用内的某个页面。
              url: '../....',      //需要跳转的应用内非 tabBar 的页面的路径,路径后可以带参数。参数与路径之间使用?分隔,参数键与参数值用=相连,不同参数用&分隔;如 'path?key=value&key2=value2'
              success: function (res) {
                wx.showToast({
                  title: '签到成功~',
                  image:'../Image/suess.png',
                  duration: 2000
                })
              },
            })
          },
          fail: function (res) {    //接口调用失败的回调函数
            console.log('error' + ':' + res)
          }
        })
      }
    })
  },

Bug & Tip

  1. tip: 最大并发限制是 10 个
  2. tip: 默认超时时间和最大超时时间都是 60s

 

posted @ 2017-04-06 15:44  JoyJin  阅读(835)  评论(4编辑  收藏  举报