imageCropper.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // var config = require("../../../config.js");
  2. const api = require('../../config/api.js');
  3. const util = require('../../utils/util.js');
  4. Page({
  5. data: {
  6. src: ''
  7. },
  8. onLoad: function (options) {
  9. //获取到image-cropper对象
  10. this.cropper = this.selectComponent("#image-cropper");
  11. //设置需要裁剪的图片路径,开始裁剪
  12. this.setData({
  13. src: options.image,
  14. });
  15. wx.showLoading({
  16. title: '加载中'
  17. })
  18. },
  19. cropperload(e) {
  20. console.log("cropper初始化完成");
  21. },
  22. loadimage(e) {
  23. console.log("图片加载完成", e.detail);
  24. wx.hideLoading();
  25. //重置图片角度、缩放、位置
  26. this.cropper.imgReset();
  27. },
  28. clickcut(e) {
  29. console.log(e.detail);
  30. //点击裁剪框阅览图片
  31. wx.previewImage({
  32. current: e.detail.url, // 当前显示图片的http链接
  33. urls: [e.detail.url] // 需要预览的图片http链接列表
  34. })
  35. },
  36. /**
  37. * 确认裁剪图片
  38. */
  39. getImg() {
  40. //调用函数说明中的getImg得到wx图片临时路径res.url
  41. this.cropper.getImg(res => {
  42. console.log(res)
  43. wx.showLoading({
  44. title: '上传中...',
  45. mask: true
  46. })
  47. //上传图片至FastDFS
  48. wx.uploadFile({
  49. filePath: res.url,
  50. name: 'file',
  51. url: api.UploadFile,
  52. header: {
  53. 'token': wx.getStorageSync('token')
  54. },
  55. success: (res) => {
  56. var data = JSON.parse(res.data);
  57. console.log(data)
  58. var pages = getCurrentPages();
  59. var backPage = pages[pages.length - 2]; //上一个页面
  60. //将上传返回的图片地址记录下来,设置到上一个界面的this.data.promotionIcon中
  61. backPage.setData({
  62. avatar: data.data.link
  63. });
  64. wx.hideLoading()
  65. //成功了返回到上一个界面
  66. wx.navigateBack()
  67. }
  68. })
  69. })
  70. }
  71. })