|
@@ -1,8 +1,9 @@
|
|
|
<template>
|
|
|
- <view class="container">
|
|
|
+ <view class="avatar-container">
|
|
|
+ <view class="uni-content-header" :class="'bg-' + proxy.$settingStore.themeColor.name" :style="{ height: proxy.$settingStore.barHightTop }"></view>
|
|
|
<view class="page-body uni-content-info">
|
|
|
<view class="cropper-content">
|
|
|
- <view v-if="isShowImg" class="uni-corpper" :style="'width:' + cropperInitW + 'px;height:' + cropperInitH + 'px;background:#000'">
|
|
|
+ <view ref="uniCorpper" v-if="isShowImg" class="uni-corpper" :style="'width:' + cropperInitW + 'px;height:' + cropperInitH + 'px;background:#000'">
|
|
|
<view class="uni-corpper-content" :style="'width:' + cropperW + 'px;height:' + cropperH + 'px;left:' + cropperL + 'px;top:' + cropperT + 'px'">
|
|
|
<image :src="imageSrc" :style="'width:' + cropperW + 'px;height:' + cropperH + 'px'"></image>
|
|
|
<view
|
|
@@ -23,7 +24,8 @@
|
|
|
<view class="uni-cropper-point point-tr" data-drag="topTight"></view>
|
|
|
<view class="uni-cropper-point point-r" data-drag="right" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
|
|
|
<view class="uni-cropper-point point-rb" data-drag="rightBottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
|
|
|
- <view class="uni-cropper-point point-b" data-drag="bottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove" @touchend.stop="dragEnd"></view>
|
|
|
+ <!-- @touchend.stop="dragEnd" -->
|
|
|
+ <view class="uni-cropper-point point-b" data-drag="bottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
|
|
|
<view class="uni-cropper-point point-bl" data-drag="bottomLeft"></view>
|
|
|
<view class="uni-cropper-point point-l" data-drag="left" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
|
|
|
<view class="uni-cropper-point point-lt" data-drag="leftTop"></view>
|
|
@@ -34,17 +36,24 @@
|
|
|
</view>
|
|
|
<view class="cropper-config">
|
|
|
<button type="primary reverse" @click="getImage" style="margin-top: 30rpx">选择头像</button>
|
|
|
- <button type="warn" @click="getImageInfo" style="margin-top: 30rpx">提交</button>
|
|
|
+ <button type="primary" @click="getImageInfo" style="margin-top: 30rpx">提交</button>
|
|
|
</view>
|
|
|
<canvas canvas-id="myCanvas" :style="'position:absolute;border: 1px solid red; width:' + imageW + 'px;height:' + imageH + 'px;top:-9999px;left:-9999px;'"></canvas>
|
|
|
</view>
|
|
|
</view>
|
|
|
</template>
|
|
|
|
|
|
-<script>
|
|
|
+<script setup>
|
|
|
import config from "@/config";
|
|
|
-import store from "@/store";
|
|
|
-import { uploadAvatar } from "@/api/system/user";
|
|
|
+import { ref, reactive, computed, getCurrentInstance, toRefs, inject } from "vue";
|
|
|
+import { onReady, onShow } from "@dcloudio/uni-app";
|
|
|
+
|
|
|
+import { uploadAvatar, updateUserProfile } from "@/api/system/user";
|
|
|
+
|
|
|
+import useStores from "@/store/modules/user.js";
|
|
|
+const useStore = useStores();
|
|
|
+
|
|
|
+const { proxy } = getCurrentInstance();
|
|
|
|
|
|
const baseUrl = config.baseUrl;
|
|
|
let sysInfo = uni.getSystemInfoSync();
|
|
@@ -67,286 +76,325 @@ let PAGE_X, // 手按下的x位置
|
|
|
INIT_DRAG_POSITION = 100, // 初始化屏幕宽度和裁剪区域的宽度之差,用于设置初始化裁剪的宽度
|
|
|
DRAW_IMAGE_W = sysInfo.screenWidth; // 设置生成的图片宽度
|
|
|
|
|
|
-export default {
|
|
|
- /**
|
|
|
- * 页面的初始数据
|
|
|
- */
|
|
|
- data() {
|
|
|
- return {
|
|
|
- imageSrc: store.getters.avatar,
|
|
|
- isShowImg: false,
|
|
|
- // 初始化的宽高
|
|
|
- cropperInitW: SCREEN_WIDTH,
|
|
|
- cropperInitH: SCREEN_WIDTH,
|
|
|
- // 动态的宽高
|
|
|
- cropperW: SCREEN_WIDTH,
|
|
|
- cropperH: SCREEN_WIDTH,
|
|
|
- // 动态的left top值
|
|
|
- cropperL: 0,
|
|
|
- cropperT: 0,
|
|
|
-
|
|
|
- transL: 0,
|
|
|
- transT: 0,
|
|
|
-
|
|
|
- // 图片缩放值
|
|
|
- scaleP: 0,
|
|
|
- imageW: 0,
|
|
|
- imageH: 0,
|
|
|
-
|
|
|
- // 裁剪框 宽高
|
|
|
- cutL: 0,
|
|
|
- cutT: 0,
|
|
|
- cutB: SCREEN_WIDTH,
|
|
|
- cutR: "100%",
|
|
|
- qualityWidth: DRAW_IMAGE_W,
|
|
|
- innerAspectRadio: DRAFG_MOVE_RATIO,
|
|
|
- };
|
|
|
- },
|
|
|
- /**
|
|
|
- * 生命周期函数--监听页面初次渲染完成
|
|
|
- */
|
|
|
- onReady: function () {
|
|
|
- this.loadImage();
|
|
|
- },
|
|
|
- methods: {
|
|
|
- setData: function (obj) {
|
|
|
- let that = this;
|
|
|
- Object.keys(obj).forEach(function (key) {
|
|
|
- that.$set(that.$data, key, obj[key]);
|
|
|
- });
|
|
|
- },
|
|
|
- getImage: function () {
|
|
|
- var _this = this;
|
|
|
- uni.chooseImage({
|
|
|
- success: function (res) {
|
|
|
- _this.setData({
|
|
|
- imageSrc: res.tempFilePaths[0],
|
|
|
- });
|
|
|
- _this.loadImage();
|
|
|
- },
|
|
|
- });
|
|
|
- },
|
|
|
- loadImage: function () {
|
|
|
- var _this = this;
|
|
|
-
|
|
|
- uni.getImageInfo({
|
|
|
- src: _this.imageSrc,
|
|
|
- success: function success(res) {
|
|
|
- IMG_RATIO = 1 / 1;
|
|
|
- if (IMG_RATIO >= 1) {
|
|
|
- IMG_REAL_W = SCREEN_WIDTH;
|
|
|
- IMG_REAL_H = SCREEN_WIDTH / IMG_RATIO;
|
|
|
- } else {
|
|
|
- IMG_REAL_W = SCREEN_WIDTH * IMG_RATIO;
|
|
|
- IMG_REAL_H = SCREEN_WIDTH;
|
|
|
- }
|
|
|
- let minRange = IMG_REAL_W > IMG_REAL_H ? IMG_REAL_W : IMG_REAL_H;
|
|
|
- INIT_DRAG_POSITION = minRange > INIT_DRAG_POSITION ? INIT_DRAG_POSITION : minRange;
|
|
|
- // 根据图片的宽高显示不同的效果 保证图片可以正常显示
|
|
|
- if (IMG_RATIO >= 1) {
|
|
|
- let cutT = Math.ceil((SCREEN_WIDTH / IMG_RATIO - (SCREEN_WIDTH / IMG_RATIO - INIT_DRAG_POSITION)) / 2);
|
|
|
- let cutB = cutT;
|
|
|
- let cutL = Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH + INIT_DRAG_POSITION) / 2);
|
|
|
- let cutR = cutL;
|
|
|
- _this.setData({
|
|
|
- cropperW: SCREEN_WIDTH,
|
|
|
- cropperH: SCREEN_WIDTH / IMG_RATIO,
|
|
|
- // 初始化left right
|
|
|
- cropperL: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH) / 2),
|
|
|
- cropperT: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH / IMG_RATIO) / 2),
|
|
|
- cutL: cutL,
|
|
|
- cutT: cutT,
|
|
|
- cutR: cutR,
|
|
|
- cutB: cutB,
|
|
|
- // 图片缩放值
|
|
|
- imageW: IMG_REAL_W,
|
|
|
- imageH: IMG_REAL_H,
|
|
|
- scaleP: IMG_REAL_W / SCREEN_WIDTH,
|
|
|
- qualityWidth: DRAW_IMAGE_W,
|
|
|
- innerAspectRadio: IMG_RATIO,
|
|
|
- });
|
|
|
- } else {
|
|
|
- let cutL = Math.ceil((SCREEN_WIDTH * IMG_RATIO - SCREEN_WIDTH * IMG_RATIO) / 2);
|
|
|
- let cutR = cutL;
|
|
|
- let cutT = Math.ceil((SCREEN_WIDTH - INIT_DRAG_POSITION) / 2);
|
|
|
- let cutB = cutT;
|
|
|
- _this.setData({
|
|
|
- cropperW: SCREEN_WIDTH * IMG_RATIO,
|
|
|
- cropperH: SCREEN_WIDTH,
|
|
|
- // 初始化left right
|
|
|
- cropperL: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH * IMG_RATIO) / 2),
|
|
|
- cropperT: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH) / 2),
|
|
|
-
|
|
|
- cutL: cutL,
|
|
|
- cutT: cutT,
|
|
|
- cutR: cutR,
|
|
|
- cutB: cutB,
|
|
|
- // 图片缩放值
|
|
|
- imageW: IMG_REAL_W,
|
|
|
- imageH: IMG_REAL_H,
|
|
|
- scaleP: IMG_REAL_W / SCREEN_WIDTH,
|
|
|
- qualityWidth: DRAW_IMAGE_W,
|
|
|
- innerAspectRadio: IMG_RATIO,
|
|
|
- });
|
|
|
- }
|
|
|
- _this.setData({
|
|
|
- isShowImg: true,
|
|
|
- });
|
|
|
- uni.hideLoading();
|
|
|
- },
|
|
|
+/**
|
|
|
+ * 页面的初始数据
|
|
|
+ */
|
|
|
+const array = reactive({
|
|
|
+ imageSrc: useStore.$state.avatar,
|
|
|
+ isShowImg: false,
|
|
|
+ // 初始化的宽高
|
|
|
+ cropperInitW: SCREEN_WIDTH,
|
|
|
+ cropperInitH: SCREEN_WIDTH,
|
|
|
+ // 动态的宽高
|
|
|
+ cropperW: SCREEN_WIDTH,
|
|
|
+ cropperH: SCREEN_WIDTH,
|
|
|
+ // 动态的left top值
|
|
|
+ cropperL: 0,
|
|
|
+ cropperT: 0,
|
|
|
+
|
|
|
+ transL: 0,
|
|
|
+ transT: 0,
|
|
|
+
|
|
|
+ // 图片缩放值
|
|
|
+ scaleP: 0,
|
|
|
+ imageW: 0,
|
|
|
+ imageH: 0,
|
|
|
+
|
|
|
+ // 裁剪框 宽高
|
|
|
+ cutL: 0,
|
|
|
+ cutT: 0,
|
|
|
+ cutB: SCREEN_WIDTH,
|
|
|
+ cutR: "100%",
|
|
|
+ qualityWidth: DRAW_IMAGE_W,
|
|
|
+ innerAspectRadio: DRAFG_MOVE_RATIO,
|
|
|
+});
|
|
|
+
|
|
|
+const {
|
|
|
+ imageSrc,
|
|
|
+ isShowImg,
|
|
|
+ cropperInitW,
|
|
|
+ cropperInitH,
|
|
|
+ cropperW,
|
|
|
+ cropperH,
|
|
|
+
|
|
|
+ cropperL,
|
|
|
+ cropperT,
|
|
|
+
|
|
|
+ transL,
|
|
|
+ transT,
|
|
|
+
|
|
|
+ scaleP,
|
|
|
+ imageW,
|
|
|
+ imageH,
|
|
|
+
|
|
|
+ cutL,
|
|
|
+ cutT,
|
|
|
+ cutB,
|
|
|
+ cutR,
|
|
|
+ qualityWidth,
|
|
|
+ innerAspectRadio,
|
|
|
+} = toRefs(array);
|
|
|
+
|
|
|
+/**
|
|
|
+ * @更新图片数据
|
|
|
+ */
|
|
|
+function setData(obj) {
|
|
|
+ Object.keys(obj).forEach(function (key) {
|
|
|
+ array[key] = obj[key];
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @选择图片
|
|
|
+ */
|
|
|
+function getImage() {
|
|
|
+ uni.chooseImage({
|
|
|
+ success: function (res) {
|
|
|
+ setData({
|
|
|
+ imageSrc: res.tempFilePaths[0],
|
|
|
});
|
|
|
+ loadImage();
|
|
|
},
|
|
|
- // 拖动时候触发的touchStart事件
|
|
|
- contentStartMove(e) {
|
|
|
- PAGE_X = e.touches[0].pageX;
|
|
|
- PAGE_Y = e.touches[0].pageY;
|
|
|
- },
|
|
|
+ });
|
|
|
+}
|
|
|
|
|
|
- // 拖动时候触发的touchMove事件
|
|
|
- contentMoveing(e) {
|
|
|
- var _this = this;
|
|
|
- var dragLengthX = (PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO;
|
|
|
- var dragLengthY = (PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO;
|
|
|
- // 左移
|
|
|
- if (dragLengthX > 0) {
|
|
|
- if (this.cutL - dragLengthX < 0) dragLengthX = this.cutL;
|
|
|
+function loadImage() {
|
|
|
+ uni.getImageInfo({
|
|
|
+ src: imageSrc.value,
|
|
|
+ success: function success(res) {
|
|
|
+ IMG_RATIO = 1 / 1;
|
|
|
+ if (IMG_RATIO >= 1) {
|
|
|
+ IMG_REAL_W = SCREEN_WIDTH;
|
|
|
+ IMG_REAL_H = SCREEN_WIDTH / IMG_RATIO;
|
|
|
} else {
|
|
|
- if (this.cutR + dragLengthX < 0) dragLengthX = -this.cutR;
|
|
|
+ IMG_REAL_W = SCREEN_WIDTH * IMG_RATIO;
|
|
|
+ IMG_REAL_H = SCREEN_WIDTH;
|
|
|
}
|
|
|
-
|
|
|
- if (dragLengthY > 0) {
|
|
|
- if (this.cutT - dragLengthY < 0) dragLengthY = this.cutT;
|
|
|
+ let minRange = IMG_REAL_W > IMG_REAL_H ? IMG_REAL_W : IMG_REAL_H;
|
|
|
+ INIT_DRAG_POSITION = minRange > INIT_DRAG_POSITION ? INIT_DRAG_POSITION : minRange;
|
|
|
+ // 根据图片的宽高显示不同的效果 保证图片可以正常显示
|
|
|
+ if (IMG_RATIO >= 1) {
|
|
|
+ let cutT = Math.ceil((SCREEN_WIDTH / IMG_RATIO - (SCREEN_WIDTH / IMG_RATIO - INIT_DRAG_POSITION)) / 2);
|
|
|
+ let cutB = cutT;
|
|
|
+ let cutL = Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH + INIT_DRAG_POSITION) / 2);
|
|
|
+ let cutR = cutL;
|
|
|
+ setData({
|
|
|
+ cropperW: SCREEN_WIDTH,
|
|
|
+ cropperH: SCREEN_WIDTH / IMG_RATIO,
|
|
|
+ // 初始化left right
|
|
|
+ cropperL: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH) / 2),
|
|
|
+ cropperT: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH / IMG_RATIO) / 2),
|
|
|
+ cutL: cutL,
|
|
|
+ cutT: cutT,
|
|
|
+ cutR: cutR,
|
|
|
+ cutB: cutB,
|
|
|
+ // 图片缩放值
|
|
|
+ imageW: IMG_REAL_W,
|
|
|
+ imageH: IMG_REAL_H,
|
|
|
+ scaleP: IMG_REAL_W / SCREEN_WIDTH,
|
|
|
+ qualityWidth: DRAW_IMAGE_W,
|
|
|
+ innerAspectRadio: IMG_RATIO,
|
|
|
+ });
|
|
|
} else {
|
|
|
- if (this.cutB + dragLengthY < 0) dragLengthY = -this.cutB;
|
|
|
+ let cutL = Math.ceil((SCREEN_WIDTH * IMG_RATIO - SCREEN_WIDTH * IMG_RATIO) / 2);
|
|
|
+ let cutR = cutL;
|
|
|
+ let cutT = Math.ceil((SCREEN_WIDTH - INIT_DRAG_POSITION) / 2);
|
|
|
+ let cutB = cutT;
|
|
|
+ setData({
|
|
|
+ cropperW: SCREEN_WIDTH * IMG_RATIO,
|
|
|
+ cropperH: SCREEN_WIDTH,
|
|
|
+ // 初始化left right
|
|
|
+ cropperL: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH * IMG_RATIO) / 2),
|
|
|
+ cropperT: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH) / 2),
|
|
|
+
|
|
|
+ cutL: cutL,
|
|
|
+ cutT: cutT,
|
|
|
+ cutR: cutR,
|
|
|
+ cutB: cutB,
|
|
|
+ // 图片缩放值
|
|
|
+ imageW: IMG_REAL_W,
|
|
|
+ imageH: IMG_REAL_H,
|
|
|
+ scaleP: IMG_REAL_W / SCREEN_WIDTH,
|
|
|
+ qualityWidth: DRAW_IMAGE_W,
|
|
|
+ innerAspectRadio: IMG_RATIO,
|
|
|
+ });
|
|
|
}
|
|
|
- this.setData({
|
|
|
- cutL: this.cutL - dragLengthX,
|
|
|
- cutT: this.cutT - dragLengthY,
|
|
|
- cutR: this.cutR + dragLengthX,
|
|
|
- cutB: this.cutB + dragLengthY,
|
|
|
+ setData({
|
|
|
+ isShowImg: true,
|
|
|
});
|
|
|
-
|
|
|
- PAGE_X = e.touches[0].pageX;
|
|
|
- PAGE_Y = e.touches[0].pageY;
|
|
|
+ uni.hideLoading();
|
|
|
},
|
|
|
+ });
|
|
|
+}
|
|
|
|
|
|
- contentTouchEnd() {},
|
|
|
+/**
|
|
|
+ * @拖动时候触发的touchStart事件
|
|
|
+ */
|
|
|
+function contentStartMove(e) {
|
|
|
+ PAGE_X = e.touches[0].pageX;
|
|
|
+ PAGE_Y = e.touches[0].pageY;
|
|
|
+}
|
|
|
|
|
|
- // 获取图片
|
|
|
- getImageInfo() {
|
|
|
- var _this = this;
|
|
|
- uni.showLoading({
|
|
|
- title: "图片生成中...",
|
|
|
- });
|
|
|
- // 将图片写入画布
|
|
|
- const ctx = uni.createCanvasContext("myCanvas");
|
|
|
- ctx.drawImage(_this.imageSrc, 0, 0, IMG_REAL_W, IMG_REAL_H);
|
|
|
- ctx.draw(true, () => {
|
|
|
- // 获取画布要裁剪的位置和宽度 均为百分比 * 画布中图片的宽度 保证了在微信小程序中裁剪的图片模糊 位置不对的问题 canvasT = (_this.cutT / _this.cropperH) * (_this.imageH / pixelRatio)
|
|
|
- var canvasW = ((_this.cropperW - _this.cutL - _this.cutR) / _this.cropperW) * IMG_REAL_W;
|
|
|
- var canvasH = ((_this.cropperH - _this.cutT - _this.cutB) / _this.cropperH) * IMG_REAL_H;
|
|
|
- var canvasL = (_this.cutL / _this.cropperW) * IMG_REAL_W;
|
|
|
- var canvasT = (_this.cutT / _this.cropperH) * IMG_REAL_H;
|
|
|
- uni.canvasToTempFilePath({
|
|
|
- x: canvasL,
|
|
|
- y: canvasT,
|
|
|
- width: canvasW,
|
|
|
- height: canvasH,
|
|
|
- destWidth: canvasW,
|
|
|
- destHeight: canvasH,
|
|
|
- quality: 0.5,
|
|
|
- canvasId: "myCanvas",
|
|
|
- success: function (res) {
|
|
|
- uni.hideLoading();
|
|
|
- let data = { name: "avatarfile", filePath: res.tempFilePath };
|
|
|
- uploadAvatar(data).then((response) => {
|
|
|
- store.commit("SET_AVATAR", baseUrl + response.imgUrl);
|
|
|
- uni.showToast({ title: "修改成功", icon: "success" });
|
|
|
- uni.navigateBack();
|
|
|
- });
|
|
|
- },
|
|
|
+/**
|
|
|
+ * @拖动时候触发的touchMove事件
|
|
|
+ */
|
|
|
+function contentMoveing(e) {
|
|
|
+ var _this = this;
|
|
|
+ var dragLengthX = (PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO;
|
|
|
+ var dragLengthY = (PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO;
|
|
|
+ // 左移
|
|
|
+ if (dragLengthX > 0) {
|
|
|
+ if (cutL.value - dragLengthX < 0) dragLengthX = cutL.value;
|
|
|
+ } else {
|
|
|
+ if (cutR.value + dragLengthX < 0) dragLengthX = -cutR.value;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (dragLengthY > 0) {
|
|
|
+ if (cutT.value - dragLengthY < 0) dragLengthY = cutT.value;
|
|
|
+ } else {
|
|
|
+ if (cutB.value + dragLengthY < 0) dragLengthY = -cutB.value;
|
|
|
+ }
|
|
|
+ setData({
|
|
|
+ cutL: cutL.value - dragLengthX,
|
|
|
+ cutT: cutT.value - dragLengthY,
|
|
|
+ cutR: cutR.value + dragLengthX,
|
|
|
+ cutB: cutB.value + dragLengthY,
|
|
|
+ });
|
|
|
+
|
|
|
+ PAGE_X = e.touches[0].pageX;
|
|
|
+ PAGE_Y = e.touches[0].pageY;
|
|
|
+}
|
|
|
+
|
|
|
+function contentTouchEnd() {}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @获取图片
|
|
|
+ */
|
|
|
+function getImageInfo() {
|
|
|
+ uni.showLoading({
|
|
|
+ title: "图片生成中...",
|
|
|
+ });
|
|
|
+ // 将图片写入画布
|
|
|
+ const ctx = uni.createCanvasContext("myCanvas");
|
|
|
+ ctx.drawImage(imageSrc.value, 0, 0, IMG_REAL_W, IMG_REAL_H);
|
|
|
+ ctx.draw(true, () => {
|
|
|
+ // 获取画布要裁剪的位置和宽度 均为百分比 * 画布中图片的宽度 保证了在微信小程序中裁剪的图片模糊 位置不对的问题 canvasT = (_this.cutT / _this.cropperH) * (_this.imageH / pixelRatio)
|
|
|
+ var canvasW = ((cropperW.value - cutL.value - cutR.value) / cropperW.value) * IMG_REAL_W;
|
|
|
+ var canvasH = ((cropperH.value - cutT.value - cutB.value) / cropperH.value) * IMG_REAL_H;
|
|
|
+ var canvasL = (cutL.value / cropperW.value) * IMG_REAL_W;
|
|
|
+ var canvasT = (cutT.value / cropperH.value) * IMG_REAL_H;
|
|
|
+ uni.canvasToTempFilePath({
|
|
|
+ x: canvasL,
|
|
|
+ y: canvasT,
|
|
|
+ width: canvasW,
|
|
|
+ height: canvasH,
|
|
|
+ destWidth: canvasW,
|
|
|
+ destHeight: canvasH,
|
|
|
+ quality: 0.5,
|
|
|
+ canvasId: "myCanvas",
|
|
|
+ success: function (res) {
|
|
|
+ uni.hideLoading();
|
|
|
+ let data = { name: "file", filePath: res.tempFilePath };
|
|
|
+ uploadAvatar(data).then((response) => {
|
|
|
+ useStore.$state.avatar = response.data.url;
|
|
|
+ updateUserProfile({
|
|
|
+ avatar: response.data.url,
|
|
|
+ }).then(() => {});
|
|
|
+ uni.showToast({ title: "修改成功", icon: "success" });
|
|
|
+ uni.navigateBack();
|
|
|
});
|
|
|
+ },
|
|
|
+ });
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @设置大小的时候触发的touchStart事件
|
|
|
+ */
|
|
|
+function dragStart(e) {
|
|
|
+ T_PAGE_X = e.touches[0].pageX;
|
|
|
+ T_PAGE_Y = e.touches[0].pageY;
|
|
|
+ CUT_L = cutL.value;
|
|
|
+ CUT_R = cutR.value;
|
|
|
+ CUT_B = cutB.value;
|
|
|
+ CUT_T = cutT.value;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @设置大小的时候触发的touchMove事件
|
|
|
+ */
|
|
|
+function dragMove(e) {
|
|
|
+ var dragType = e.target.dataset.drag;
|
|
|
+ switch (dragType) {
|
|
|
+ case "right":
|
|
|
+ var dragLength = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO;
|
|
|
+ if (CUT_R + dragLength < 0) dragLength = -CUT_R;
|
|
|
+ setData({
|
|
|
+ cutR: CUT_R + dragLength,
|
|
|
});
|
|
|
- },
|
|
|
- // 设置大小的时候触发的touchStart事件
|
|
|
- dragStart(e) {
|
|
|
- T_PAGE_X = e.touches[0].pageX;
|
|
|
- T_PAGE_Y = e.touches[0].pageY;
|
|
|
- CUT_L = this.cutL;
|
|
|
- CUT_R = this.cutR;
|
|
|
- CUT_B = this.cutB;
|
|
|
- CUT_T = this.cutT;
|
|
|
- },
|
|
|
+ break;
|
|
|
+ case "left":
|
|
|
+ var dragLength = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO;
|
|
|
+ if (CUT_L - dragLength < 0) dragLength = CUT_L;
|
|
|
+ if (CUT_L - dragLength > cropperW.value - cutR.value) dragLength = CUT_L - (cropperW.value - cutR.value);
|
|
|
+ setData({
|
|
|
+ cutL: CUT_L - dragLength,
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case "top":
|
|
|
+ var dragLength = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO;
|
|
|
+ if (CUT_T - dragLength < 0) dragLength = CUT_T;
|
|
|
+ if (CUT_T - dragLength > cropperH.value - cutB.value) dragLength = CUT_T - (cropperH.value - cutB.value);
|
|
|
+ setData({
|
|
|
+ cutT: CUT_T - dragLength,
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case "bottom":
|
|
|
+ var dragLength = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO;
|
|
|
+ if (CUT_B + dragLength < 0) dragLength = -CUT_B;
|
|
|
+ setData({
|
|
|
+ cutB: CUT_B + dragLength,
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case "rightBottom":
|
|
|
+ var dragLengthX = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO;
|
|
|
+ var dragLengthY = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO;
|
|
|
+
|
|
|
+ if (CUT_B + dragLengthY < 0) dragLengthY = -CUT_B;
|
|
|
+ if (CUT_R + dragLengthX < 0) dragLengthX = -CUT_R;
|
|
|
+ let cutB = CUT_B + dragLengthY;
|
|
|
+ let cutR = CUT_R + dragLengthX;
|
|
|
+
|
|
|
+ setData({
|
|
|
+ cutB: cutB,
|
|
|
+ cutR: cutR,
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- // 设置大小的时候触发的touchMove事件
|
|
|
- dragMove(e) {
|
|
|
- var _this = this;
|
|
|
- var dragType = e.target.dataset.drag;
|
|
|
- switch (dragType) {
|
|
|
- case "right":
|
|
|
- var dragLength = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO;
|
|
|
- if (CUT_R + dragLength < 0) dragLength = -CUT_R;
|
|
|
- this.setData({
|
|
|
- cutR: CUT_R + dragLength,
|
|
|
- });
|
|
|
- break;
|
|
|
- case "left":
|
|
|
- var dragLength = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO;
|
|
|
- if (CUT_L - dragLength < 0) dragLength = CUT_L;
|
|
|
- if (CUT_L - dragLength > this.cropperW - this.cutR) dragLength = CUT_L - (this.cropperW - this.cutR);
|
|
|
- this.setData({
|
|
|
- cutL: CUT_L - dragLength,
|
|
|
- });
|
|
|
- break;
|
|
|
- case "top":
|
|
|
- var dragLength = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO;
|
|
|
- if (CUT_T - dragLength < 0) dragLength = CUT_T;
|
|
|
- if (CUT_T - dragLength > this.cropperH - this.cutB) dragLength = CUT_T - (this.cropperH - this.cutB);
|
|
|
- this.setData({
|
|
|
- cutT: CUT_T - dragLength,
|
|
|
- });
|
|
|
- break;
|
|
|
- case "bottom":
|
|
|
- var dragLength = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO;
|
|
|
- if (CUT_B + dragLength < 0) dragLength = -CUT_B;
|
|
|
- this.setData({
|
|
|
- cutB: CUT_B + dragLength,
|
|
|
- });
|
|
|
- break;
|
|
|
- case "rightBottom":
|
|
|
- var dragLengthX = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO;
|
|
|
- var dragLengthY = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO;
|
|
|
-
|
|
|
- if (CUT_B + dragLengthY < 0) dragLengthY = -CUT_B;
|
|
|
- if (CUT_R + dragLengthX < 0) dragLengthX = -CUT_R;
|
|
|
- let cutB = CUT_B + dragLengthY;
|
|
|
- let cutR = CUT_R + dragLengthX;
|
|
|
-
|
|
|
- this.setData({
|
|
|
- cutB: cutB,
|
|
|
- cutR: cutR,
|
|
|
- });
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- },
|
|
|
- },
|
|
|
-};
|
|
|
+/**
|
|
|
+ * 生命周期函数--监听页面初次渲染完成
|
|
|
+ */
|
|
|
+onReady(() => {
|
|
|
+ loadImage();
|
|
|
+});
|
|
|
+
|
|
|
+onShow(() => {
|
|
|
+ //调用系统主题颜色
|
|
|
+ proxy.$settingStore.systemThemeColor([1]);
|
|
|
+});
|
|
|
</script>
|
|
|
|
|
|
-<style>
|
|
|
+<style lang="scss">
|
|
|
/* pages/uni-cropper/index.wxss */
|
|
|
|
|
|
-.uni-content-info {
|
|
|
- /* position: fixed;
|
|
|
- top: 0;
|
|
|
- left: 0;
|
|
|
- right: 0;
|
|
|
- bottom: 0;
|
|
|
- display: block;
|
|
|
- align-items: center;
|
|
|
- flex-direction: column; */
|
|
|
+.avatar-container {
|
|
|
+ .uni-content-header {
|
|
|
+ height: 44px;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
.cropper-config {
|