小程序模板网

微信小程序问题汇总及详解《四》图片上传和地图

发布时间:2018-04-17 09:51 所属栏目:小程序开发教程

作者:JoyJin,来自授权地址 
地图用的是百度的地图,链接:http://lbsyun.baidu.com/index.php?title=wxjsapi/guide/getlocation

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

 

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

图片上传:

 

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

Bug & Tip

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



易优小程序(企业版)+灵活api+前后代码开源 码云仓库:starfork
本文地址:https://www.eyoucms.com/wxmini/doc/course/23519.html 复制链接 如需定制请联系易优客服咨询:800182392 点击咨询
QQ在线咨询