avatarCropper.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <view class="content">
  3. <view class="cropper-wrapper" :style="{ height: cropperOpt.height + 'px' }">
  4. <canvas class="cropper" :disable-scroll="true" @touchstart="touchStart" @touchmove="touchMove"
  5. @touchend="touchEnd"
  6. :style="{ width: cropperOpt.width, height: cropperOpt.height, backgroundColor: '#999' }"
  7. canvas-id="cropper" id="cropper"></canvas>
  8. <canvas class="cropper" :disable-scroll="true" :style="{
  9. border:'2px solid #000',
  10. position: 'fixed',
  11. top: `-${cropperOpt.width * cropperOpt.pixelRatio}px`,
  12. left: `-${cropperOpt.height * cropperOpt.pixelRatio}px`,
  13. width: `${cropperOpt.width * cropperOpt.pixelRatio}px`,
  14. height: `${cropperOpt.height * cropperOpt.pixelRatio}`
  15. }" canvas-id="targetId" id="targetId"></canvas>
  16. </view>
  17. <view class="cropper-buttons safe-area-padding" :style="{ height: bottomNavHeight + 'px' }">
  18. <!-- #ifdef H5 -->
  19. <view class="upload" @tap="uploadTap">选择图片</view>
  20. <!-- #endif -->
  21. <!-- #ifndef H5 -->
  22. <view class="upload" @tap="uploadTap">重新选择</view>
  23. <!-- #endif -->
  24. <view class="getCropperImage" @tap="getCropperImage(false)">确定</view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. import {
  30. pathToBase64,
  31. base64ToPath
  32. } from '@/common/pdf.js'
  33. import store from '@/store';
  34. import WeCropper from './weCropper.js';
  35. export default {
  36. props: {
  37. // 裁剪矩形框的样式,其中可包含的属性为lineWidth-边框宽度(单位rpx),color: 边框颜色,
  38. // mask-遮罩颜色,一般设置为一个rgba的透明度,如"rgba(0, 0, 0, 0.35)"
  39. boundStyle: {
  40. type: Object,
  41. default () {
  42. return {
  43. lineWidth: 4,
  44. borderColor: 'rgb(245, 245, 245)',
  45. mask: 'rgba(0, 0, 0, 0.35)'
  46. };
  47. }
  48. }
  49. // // 裁剪框宽度,单位rpx
  50. // rectWidth: {
  51. // type: [String, Number],
  52. // default: 400
  53. // },
  54. // // 裁剪框高度,单位rpx
  55. // rectHeight: {
  56. // type: [String, Number],
  57. // default: 400
  58. // },
  59. // // 输出图片宽度,单位rpx
  60. // destWidth: {
  61. // type: [String, Number],
  62. // default: 400
  63. // },
  64. // // 输出图片高度,单位rpx
  65. // destHeight: {
  66. // type: [String, Number],
  67. // default: 400
  68. // },
  69. // // 输出的图片类型,如果发现裁剪的图片很大,可能是因为设置为了"png",改成"jpg"即可
  70. // fileType: {
  71. // type: String,
  72. // default: 'jpg',
  73. // },
  74. // // 生成的图片质量
  75. // // H5上无效,目前不考虑使用此参数
  76. // quality: {
  77. // type: [Number, String],
  78. // default: 1
  79. // }
  80. },
  81. data() {
  82. return {
  83. // 底部导航的高度
  84. bottomNavHeight: 50,
  85. originWidth: 200,
  86. width: 0,
  87. height: 200,
  88. cropperOpt: {
  89. id: 'cropper',
  90. targetId: 'targetCropper',
  91. pixelRatio: 1,
  92. width: 0,
  93. height: 0,
  94. scale: 2.5,
  95. zoom: 8,
  96. cut: {
  97. x: (this.width - this.originWidth) / 2,
  98. y: (this.height - this.originWidth) / 2,
  99. width: this.originWidth,
  100. height: this.originWidth
  101. },
  102. boundStyle: {
  103. lineWidth: uni.upx2px(this.boundStyle.lineWidth),
  104. mask: this.boundStyle.mask,
  105. color: this.boundStyle.borderColor
  106. }
  107. },
  108. // 裁剪框和输出图片的尺寸,高度默认等于宽度
  109. // 输出图片宽度,单位px
  110. destWidth: 300,
  111. // 裁剪框宽度,单位px
  112. rectWidth: 300,
  113. // 输出的图片类型,如果'png'类型发现裁剪的图片太大,改成"jpg"即可
  114. fileType: 'jpg',
  115. src: '', // 选择的图片路径,用于在点击确定时,判断是否选择了图片
  116. };
  117. },
  118. onLoad(option) {
  119. let rectInfo = uni.getSystemInfoSync();
  120. this.width = rectInfo.windowWidth;
  121. this.height = rectInfo.windowHeight - this.bottomNavHeight;
  122. this.cropperOpt.width = this.width;
  123. this.cropperOpt.height = rectInfo.screenHeight;
  124. this.cropperOpt.pixelRatio = rectInfo.pixelRatio;
  125. let rectWidth = Number(280);
  126. this.cropperOpt.cut = {
  127. x: (this.width - rectWidth) / 2,
  128. y: (this.height - rectWidth) / 2,
  129. width: rectWidth,
  130. height: rectWidth
  131. };
  132. // 初始化
  133. this.cropper = new WeCropper(this.cropperOpt)
  134. .on('ready', ctx => {
  135. // wecropper is ready for work!
  136. })
  137. .on('beforeImageLoad', ctx => {
  138. // before picture loaded, i can do something
  139. })
  140. .on('imageLoad', ctx => {
  141. // picture loaded
  142. })
  143. .on('beforeDraw', (ctx, instance) => {
  144. // before canvas draw,i can do something
  145. });
  146. // 设置导航栏样式,以免用户在page.json中没有设置为黑色背景
  147. uni.setNavigationBarColor({
  148. frontColor: '#ffffff',
  149. backgroundColor: '#000000'
  150. });
  151. this.src = option.src; //已选图片
  152. this.cropper.pushOrign(this.src); //图片赋值给裁剪框
  153. },
  154. methods: {
  155. touchStart(e) {
  156. this.cropper.touchStart(e);
  157. },
  158. touchMove(e) {
  159. this.cropper.touchMove(e);
  160. },
  161. touchEnd(e) {
  162. this.cropper.touchEnd(e);
  163. },
  164. getCropperImage(isPre = false) {
  165. if (!this.src) return this.$u.toast('请先选择图片再裁剪');
  166. let cropper_opt = {
  167. destHeight: Number(this.destWidth), // uni.canvasToTempFilePath要求这些参数为数值
  168. destWidth: Number(this.destWidth),
  169. fileType: this.fileType
  170. };
  171. this.cropper.getCropperImage(cropper_opt, (path, err) => {
  172. if (err) {
  173. uni.showModal({
  174. title: '温馨提示',
  175. content: err.message
  176. });
  177. } else {
  178. if (isPre) {
  179. uni.previewImage({
  180. current: '', // 当前显示图片的 http 链接
  181. urls: [path] // 需要预览的图片 http 链接列表
  182. });
  183. } else {
  184. //上传裁剪后的图片
  185. uni.uploadFile({
  186. url: this.$base.baseUrl + '/ins/taskImage/uploadFile',
  187. filePath: path,
  188. name: "multipartFile",
  189. formData: {
  190. 'type': 'image',
  191. },
  192. header: {
  193. Authorization: store.state.token,
  194. },
  195. success: async (imgRes) => {
  196. let data = JSON.parse(imgRes.data).data;
  197. //头像上传接口
  198. let res = await this.$http.post('/partner/avatarUpdate', {
  199. pictureUrl: data.id
  200. });
  201. if (res.code == '200') {
  202. this.$u.route({
  203. type: 'back'
  204. });
  205. } else {
  206. uni.showToast({
  207. title: res.msg,
  208. icon: 'none',
  209. });
  210. }
  211. }
  212. });
  213. // uni.$emit('uAvatarCropper', path);
  214. }
  215. }
  216. });
  217. },
  218. uploadTap() {
  219. const self = this;
  220. uni.chooseImage({
  221. count: 1, // 默认9
  222. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  223. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  224. success: (res) => {
  225. self.src = res.tempFilePaths[0];
  226. // 获取裁剪图片资源后,给data添加src属性及其值
  227. self.cropper.pushOrign(this.src);
  228. }
  229. });
  230. }
  231. }
  232. };
  233. </script>
  234. <style scoped lang="scss">
  235. @mixin vue-flex($direction: row) {
  236. /* #ifndef APP-NVUE */
  237. display: flex;
  238. flex-direction: $direction;
  239. /* #endif */
  240. }
  241. .content {
  242. background: rgba(255, 255, 255, 1);
  243. height: 100vh;
  244. }
  245. .cropper {
  246. position: absolute;
  247. top: 0;
  248. left: 0;
  249. width: 100%;
  250. height: 100vh;
  251. z-index: 11;
  252. }
  253. #targetId {
  254. border: 2px solid #fff !important;
  255. }
  256. .cropper-wrapper {
  257. position: relative;
  258. @include vue-flex;
  259. flex-direction: row;
  260. justify-content: space-between;
  261. align-items: center;
  262. width: 100%;
  263. height: 100vh;
  264. background-color: #000;
  265. }
  266. .cropper-buttons {
  267. padding: 0 46rpx;
  268. box-sizing: border-box;
  269. color: #eee;
  270. width: 100vw;
  271. height: auto;
  272. @include vue-flex;
  273. flex-direction: row;
  274. justify-content: space-between;
  275. align-items: center;
  276. position: fixed;
  277. bottom: 200rpx;
  278. left: 0;
  279. font-size: 28rpx;
  280. z-index: 999;
  281. view {
  282. width: 48%;
  283. text-align: center;
  284. padding: 18rpx 65rpx;
  285. box-sizing: border-box;
  286. background-color: #fff;
  287. border-radius: 4rpx;
  288. font-size: 30rpx;
  289. }
  290. view:first-child {
  291. color: #333;
  292. }
  293. view:last-child {
  294. background: linear-gradient(132deg, #2DD9FF 0%, #2D6DFF 100%);
  295. border-radius: 4rpx 4rpx 4rpx 4rpx;
  296. }
  297. }
  298. </style>