index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import WeCropper from '../../lib/we-cropper.min.js'
  2. const app = getApp()
  3. const config = app.globalData.config
  4. const device = wx.getSystemInfoSync()
  5. const width = device.windowWidth
  6. const height = device.windowHeight - 50
  7. 610 / 310
  8. Page({
  9. data: {
  10. originimg: '',
  11. cropperOpt: {
  12. id: 'cropper',
  13. targetId: 'targetCropper',
  14. pixelRatio: device.pixelRatio,
  15. width,
  16. height,
  17. scale: 2.5,
  18. zoom: 8,
  19. cut: {
  20. x: (width - 300) / 2,
  21. y: (height - 300) / 2,
  22. width: 305,
  23. height: 155
  24. },
  25. boundStyle: {
  26. color: config.getThemeColor(),
  27. mask: 'rgba(0,0,0,0.8)',
  28. lineWidth: 1
  29. }
  30. }
  31. },
  32. touchStart(e) {
  33. this.cropper.touchStart(e)
  34. },
  35. touchMove(e) {
  36. this.cropper.touchMove(e)
  37. },
  38. touchEnd(e) {
  39. this.cropper.touchEnd(e)
  40. },
  41. getCropperImage() {
  42. const that = this;
  43. this.cropper.getCropperImage(function (path, err) {
  44. if (err) {
  45. wx.showModal({
  46. title: '温馨提示',
  47. content: err.message
  48. })
  49. } else {
  50. // wx.previewImage({
  51. // current: '', // 当前显示图片的 http 链接
  52. // urls: [path] // 需要预览的图片 http 链接列表
  53. // });
  54. wx.showLoading({
  55. title: '上传中...',
  56. });
  57. wx.uploadFile({
  58. url: 'https://hw.cirray.cn/api/upload_image', //仅为示例,非真实的接口地址
  59. filePath: path,
  60. name: 'file',
  61. formData: {},
  62. success: function (res) {
  63. // wx.hideLoading()
  64. wx.hideLoading()
  65. const pages = getCurrentPages();
  66. const prevPage = pages[pages.length - 2]; //上一个页面
  67. //直接调用上一个页面的setData()方法,把数据存到上一个页面中去
  68. prevPage.setData({
  69. imgList: [...prevPage.data.imgList, res.data],
  70. bigImgList: [...prevPage.data.bigImgList, that.data.originimg]
  71. })
  72. wx.navigateBack({ //返回
  73. delta: 1
  74. })
  75. },
  76. fail(e) {
  77. wx.showToast({
  78. title: '上传失败,请稍后重试!',
  79. icon: 'none'
  80. })
  81. // wx.hideLoading()
  82. }
  83. })
  84. }
  85. })
  86. },
  87. uploadTap() {
  88. const self = this
  89. wx.chooseImage({
  90. count: 1, // 默认9
  91. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  92. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  93. success(res) {
  94. const src = res.tempFilePaths[0]
  95. // 获取裁剪图片资源后,给data添加src属性及其值
  96. self.cropper.pushOrign(src)
  97. wx.uploadFile({
  98. url: 'https://hw.cirray.cn/api/upload_image', //仅为示例,非真实的接口地址
  99. filePath: src,
  100. name: 'file',
  101. formData: {},
  102. success: function (res) {
  103. self.setData({
  104. originimg: res.data
  105. })
  106. }
  107. })
  108. }
  109. })
  110. },
  111. onLoad(option) {
  112. const {
  113. cropperOpt
  114. } = this.data
  115. cropperOpt.boundStyle.color = config.getThemeColor()
  116. this.setData({
  117. cropperOpt
  118. })
  119. if (option.src) {
  120. cropperOpt.src = option.src;
  121. console.log(option);
  122. this.setData({
  123. originimg: option.originimg
  124. })
  125. this.cropper = new WeCropper(cropperOpt)
  126. .on('ready', (ctx) => {
  127. console.log(`wecropper is ready for work!`)
  128. })
  129. .on('beforeImageLoad', (ctx) => {
  130. console.log(`before picture loaded, i can do something`)
  131. console.log(`current canvas context:`, ctx)
  132. wx.showToast({
  133. title: '上传中',
  134. icon: 'loading',
  135. duration: 20000
  136. })
  137. })
  138. .on('imageLoad', (ctx) => {
  139. console.log(`picture loaded`)
  140. console.log(`current canvas context:`, ctx)
  141. wx.hideToast()
  142. })
  143. .on('beforeDraw', (ctx, instance) => {
  144. console.log(`before canvas draw,i can do something`)
  145. console.log(`current canvas context:`, ctx)
  146. })
  147. }
  148. }
  149. })