avatarCropper.vue 8.1 KB

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