当前位置:   article > 正文

(二) uni 实现个人中心头像上传_uniapp个人中心图片怎么搞

uniapp个人中心图片怎么搞

先看效果

说下实现思路,在用户点击头像的时候 弹出选项 选择相册打开,选择头像后跳转到 头像裁剪页面,裁剪完成点击上传图像 上传到服务器上,返回个人主页并刷新本地个人图像

大概实现方式

1.点击用户头像 调用uni api打开相册选择照片,并返回临时路径,把这个临时路径传给 图片裁剪界面。

  1. tapUpdateImg() {
  2. const that = this;
  3. uni.chooseImage({//打开手机本地相册
  4. count: 1,//只能选取一张照片
  5. sizeType: ['original'], //可以指定是原图还是压缩图,默认二者都有
  6. success: function(res) {
  7. uni.$once('updateHeadimg', that.refreshData);//监听裁剪界面返回事件
  8. uni.navigateTo({
  9. url: "../userInfo/updateHeadimg?imgUrl=" + res.tempFilePaths[0]
  10. })
  11. }
  12. })
  13. }

2.裁剪界面(通用代码自己需要修改上传业务逻辑) 代码很多耐心看,可以改成自己想要的 这个也是我找的模板

  1. <template>
  2. <view class="container">
  3. <view class="page-body uni-content-info">
  4. <view class='cropper-content'>
  5. <view v-if="isShowImg" class="uni-corpper" :style="'width:'+cropperInitW+'px;height:'+cropperInitH+'px;background:#000'">
  6. <view class="uni-corpper-content" :style="'width:'+cropperW+'px;height:'+cropperH+'px;left:'+cropperL+'px;top:'+cropperT+'px'">
  7. <image :src="imageSrc" :style="'width:'+cropperW+'px;height:'+cropperH+'px'"></image>
  8. <view class="uni-corpper-crop-box" @touchstart.stop="contentStartMove" @touchmove.stop="contentMoveing"
  9. @touchend.stop="contentTouchEnd" :style="'left:'+cutL+'px;top:'+cutT+'px;right:'+cutR+'px;bottom:'+cutB+'px'">
  10. <view class="uni-cropper-view-box">
  11. <view class="uni-cropper-dashed-h"></view>
  12. <view class="uni-cropper-dashed-v"></view>
  13. <view class="uni-cropper-line-t" data-drag="top" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  14. <view class="uni-cropper-line-r" data-drag="right" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  15. <view class="uni-cropper-line-b" data-drag="bottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  16. <view class="uni-cropper-line-l" data-drag="left" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  17. <view class="uni-cropper-point point-t" data-drag="top" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  18. <view class="uni-cropper-point point-tr" data-drag="topTight"></view>
  19. <view class="uni-cropper-point point-r" data-drag="right" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  20. <view class="uni-cropper-point point-rb" data-drag="rightBottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  21. <view class="uni-cropper-point point-b" data-drag="bottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove"
  22. @touchend.stop="dragEnd"></view>
  23. <view class="uni-cropper-point point-bl" data-drag="bottomLeft"></view>
  24. <view class="uni-cropper-point point-l" data-drag="left" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  25. <view class="uni-cropper-point point-lt" data-drag="leftTop"></view>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. <view class='cropper-config'>
  32. <!-- <button type="primary reverse" @click="getImage" style='margin-top: 30upx;'> 选择图片 </button> -->
  33. <button type="warn" @click="getImageInfo" style='margin-top: 30upx;'> 上传图像 </button>
  34. </view>
  35. <canvas canvas-id="myCanvas" :style="'position:absolute;border: 1px solid red; width:'+imageW+'px;height:'+imageH+'px;top:-9999px;left:-9999px;'"></canvas>
  36. </view>
  37. <!-- <page-foot :name="name"></page-foot> -->
  38. </view>
  39. </template>
  40. <script>
  41. import indexApi from '../../common/api/indexApi.js';
  42. let sysInfo = uni.getSystemInfoSync();
  43. let SCREEN_WIDTH = sysInfo.screenWidth
  44. let PAGE_X, // 手按下的x位置
  45. PAGE_Y, // 手按下y的位置
  46. PR = sysInfo.pixelRatio, // dpi
  47. T_PAGE_X, // 手移动的时候x的位置
  48. T_PAGE_Y, // 手移动的时候Y的位置
  49. CUT_L, // 初始化拖拽元素的left值
  50. CUT_T, // 初始化拖拽元素的top值
  51. CUT_R, // 初始化拖拽元素的
  52. CUT_B, // 初始化拖拽元素的
  53. CUT_W, // 初始化拖拽元素的宽度
  54. CUT_H, // 初始化拖拽元素的高度
  55. IMG_RATIO, // 图片比例
  56. IMG_REAL_W, // 图片实际的宽度
  57. IMG_REAL_H, // 图片实际的高度
  58. DRAFG_MOVE_RATIO = 1, //移动时候的比例,
  59. INIT_DRAG_POSITION = 100, // 初始化屏幕宽度和裁剪区域的宽度之差,用于设置初始化裁剪的宽度
  60. DRAW_IMAGE_W = sysInfo.screenWidth // 设置生成的图片宽度
  61. export default {
  62. /**
  63. * 页面的初始数据
  64. */
  65. data() {
  66. return {
  67. name: '杨大宝',
  68. imageSrc: '',
  69. isShowImg: false,
  70. // 初始化的宽高
  71. cropperInitW: SCREEN_WIDTH,
  72. cropperInitH: SCREEN_WIDTH,
  73. // 动态的宽高
  74. cropperW: SCREEN_WIDTH,
  75. cropperH: SCREEN_WIDTH,
  76. // 动态的left top值
  77. cropperL: 0,
  78. cropperT: 0,
  79. transL: 0,
  80. transT: 0,
  81. // 图片缩放值
  82. scaleP: 0,
  83. imageW: 0,
  84. imageH: 0,
  85. // 裁剪框 宽高
  86. cutL: 0,
  87. cutT: 0,
  88. cutB: SCREEN_WIDTH,
  89. cutR: '100%',
  90. qualityWidth: DRAW_IMAGE_W,
  91. innerAspectRadio: DRAFG_MOVE_RATIO
  92. }
  93. },
  94. /**
  95. * 生命周期函数--监听页面加载
  96. */
  97. onLoad: function(opt) {
  98. this.imageSrc = opt.imgUrl;
  99. },
  100. /**
  101. * 生命周期函数--监听页面初次渲染完成
  102. */
  103. mounted: function() {
  104. //this.getImage();
  105. this.loadImage();
  106. },
  107. methods: {
  108. setData: function(obj) {
  109. let that = this;
  110. Object.keys(obj).forEach(function(key) {
  111. that.$set(that.$data, key, obj[key])
  112. });
  113. },
  114. // getImage: function() {
  115. // var _this = this
  116. // uni.chooseImage({
  117. // success: function(res) {
  118. // _this.setData({
  119. // imageSrc: res.tempFilePaths[0],
  120. // })
  121. // _this.loadImage();
  122. // },
  123. // })
  124. // },
  125. loadImage: function() {
  126. var _this = this
  127. uni.showLoading({
  128. title: '图片加载中...',
  129. })
  130. uni.getImageInfo({
  131. src: _this.imageSrc,
  132. success: function success(res) {
  133. console.log(res);
  134. IMG_RATIO = res.width / res.height
  135. if (IMG_RATIO >= 1) {
  136. IMG_REAL_W = SCREEN_WIDTH
  137. IMG_REAL_H = SCREEN_WIDTH / IMG_RATIO
  138. } else {
  139. IMG_REAL_W = SCREEN_WIDTH * IMG_RATIO
  140. IMG_REAL_H = SCREEN_WIDTH
  141. }
  142. let minRange = IMG_REAL_W > IMG_REAL_H ? IMG_REAL_W : IMG_REAL_H
  143. INIT_DRAG_POSITION = minRange > INIT_DRAG_POSITION ? INIT_DRAG_POSITION : minRange
  144. // 根据图片的宽高显示不同的效果 保证图片可以正常显示
  145. if (IMG_RATIO >= 1) {
  146. let cutT = Math.ceil((SCREEN_WIDTH / IMG_RATIO - (SCREEN_WIDTH / IMG_RATIO - INIT_DRAG_POSITION)) / 2);
  147. let cutB = cutT;
  148. let cutL = Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH + INIT_DRAG_POSITION) / 2);
  149. let cutR = cutL;
  150. _this.setData({
  151. cropperW: SCREEN_WIDTH,
  152. cropperH: SCREEN_WIDTH / IMG_RATIO,
  153. // 初始化left right
  154. cropperL: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH) / 2),
  155. cropperT: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH / IMG_RATIO) / 2),
  156. cutL: cutL,
  157. cutT: cutT,
  158. cutR: cutR,
  159. cutB: cutB,
  160. // 图片缩放值
  161. imageW: IMG_REAL_W,
  162. imageH: IMG_REAL_H,
  163. scaleP: IMG_REAL_W / SCREEN_WIDTH,
  164. qualityWidth: DRAW_IMAGE_W,
  165. innerAspectRadio: IMG_RATIO
  166. })
  167. } else {
  168. let cutL = Math.ceil((SCREEN_WIDTH * IMG_RATIO - (SCREEN_WIDTH * IMG_RATIO)) / 2);
  169. let cutR = cutL;
  170. let cutT = Math.ceil((SCREEN_WIDTH - INIT_DRAG_POSITION) / 2);
  171. let cutB = cutT;
  172. _this.setData({
  173. cropperW: SCREEN_WIDTH * IMG_RATIO,
  174. cropperH: SCREEN_WIDTH,
  175. // 初始化left right
  176. cropperL: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH * IMG_RATIO) / 2),
  177. cropperT: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH) / 2),
  178. cutL: cutL,
  179. cutT: cutT,
  180. cutR: cutR,
  181. cutB: cutB,
  182. // 图片缩放值
  183. imageW: IMG_REAL_W,
  184. imageH: IMG_REAL_H,
  185. scaleP: IMG_REAL_W / SCREEN_WIDTH,
  186. qualityWidth: DRAW_IMAGE_W,
  187. innerAspectRadio: IMG_RATIO
  188. })
  189. }
  190. _this.setData({
  191. isShowImg: true
  192. })
  193. uni.hideLoading()
  194. }
  195. })
  196. },
  197. // 拖动时候触发的touchStart事件
  198. contentStartMove(e) {
  199. PAGE_X = e.touches[0].pageX
  200. PAGE_Y = e.touches[0].pageY
  201. },
  202. // 拖动时候触发的touchMove事件
  203. contentMoveing(e) {
  204. var _this = this
  205. var dragLengthX = (PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO
  206. var dragLengthY = (PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO
  207. // 左移
  208. if (dragLengthX > 0) {
  209. if (this.cutL - dragLengthX < 0) dragLengthX = this.cutL
  210. } else {
  211. if (this.cutR + dragLengthX < 0) dragLengthX = -this.cutR
  212. }
  213. if (dragLengthY > 0) {
  214. if (this.cutT - dragLengthY < 0) dragLengthY = this.cutT
  215. } else {
  216. if (this.cutB + dragLengthY < 0) dragLengthY = -this.cutB
  217. }
  218. this.setData({
  219. cutL: this.cutL - dragLengthX,
  220. cutT: this.cutT - dragLengthY,
  221. cutR: this.cutR + dragLengthX,
  222. cutB: this.cutB + dragLengthY
  223. })
  224. PAGE_X = e.touches[0].pageX
  225. PAGE_Y = e.touches[0].pageY
  226. },
  227. contentTouchEnd() {
  228. },
  229. // 获取图片
  230. getImageInfo() {
  231. var _this = this;
  232. uni.showLoading({
  233. title: '头像上传中...',
  234. });
  235. // 将图片写入画布
  236. const ctx = uni.createCanvasContext('myCanvas');
  237. ctx.drawImage(_this.imageSrc, 0, 0, IMG_REAL_W, IMG_REAL_H);
  238. ctx.draw(true, () => {
  239. // 获取画布要裁剪的位置和宽度 均为百分比 * 画布中图片的宽度 保证了在微信小程序中裁剪的图片模糊 位置不对的问题 canvasT = (_this.cutT / _this.cropperH) * (_this.imageH / pixelRatio)
  240. var canvasW = ((_this.cropperW - _this.cutL - _this.cutR) / _this.cropperW) * IMG_REAL_W;
  241. var canvasH = ((_this.cropperH - _this.cutT - _this.cutB) / _this.cropperH) * IMG_REAL_H;
  242. var canvasL = (_this.cutL / _this.cropperW) * IMG_REAL_W;
  243. var canvasT = (_this.cutT / _this.cropperH) * IMG_REAL_H;
  244. uni.canvasToTempFilePath({
  245. x: canvasL,
  246. y: canvasT,
  247. width: canvasW,
  248. height: canvasH,
  249. destWidth: canvasW,
  250. destHeight: canvasH,
  251. quality: 0.5,
  252. canvasId: 'myCanvas',
  253. success: function(res) {
  254. indexApi.uploadImg(res.tempFilePath,function(res1){
  255. uni.$emit("updateHeadimg", true);
  256. uni.navigateBack();
  257. })
  258. },
  259. fail: function(error) {
  260. console.log(error);
  261. },
  262. complete:function(){
  263. uni.hideLoading();
  264. }
  265. });
  266. });
  267. },
  268. // 设置大小的时候触发的touchStart事件
  269. dragStart(e) {
  270. T_PAGE_X = e.touches[0].pageX
  271. T_PAGE_Y = e.touches[0].pageY
  272. CUT_L = this.cutL
  273. CUT_R = this.cutR
  274. CUT_B = this.cutB
  275. CUT_T = this.cutT
  276. },
  277. // 设置大小的时候触发的touchMove事件
  278. dragMove(e) {
  279. var _this = this
  280. var dragType = e.target.dataset.drag
  281. switch (dragType) {
  282. case 'right':
  283. var dragLength = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO
  284. if (CUT_R + dragLength < 0) dragLength = -CUT_R
  285. this.setData({
  286. cutR: CUT_R + dragLength
  287. })
  288. break;
  289. case 'left':
  290. var dragLength = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO
  291. if (CUT_L - dragLength < 0) dragLength = CUT_L
  292. if ((CUT_L - dragLength) > (this.cropperW - this.cutR)) dragLength = CUT_L - (this.cropperW - this.cutR)
  293. this.setData({
  294. cutL: CUT_L - dragLength
  295. })
  296. break;
  297. case 'top':
  298. var dragLength = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO
  299. if (CUT_T - dragLength < 0) dragLength = CUT_T
  300. if ((CUT_T - dragLength) > (this.cropperH - this.cutB)) dragLength = CUT_T - (this.cropperH - this.cutB)
  301. this.setData({
  302. cutT: CUT_T - dragLength
  303. })
  304. break;
  305. case 'bottom':
  306. var dragLength = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO
  307. if (CUT_B + dragLength < 0) dragLength = -CUT_B
  308. this.setData({
  309. cutB: CUT_B + dragLength
  310. })
  311. break;
  312. case 'rightBottom':
  313. var dragLengthX = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO
  314. var dragLengthY = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO
  315. if (CUT_B + dragLengthY < 0) dragLengthY = -CUT_B
  316. if (CUT_R + dragLengthX < 0) dragLengthX = -CUT_R
  317. let cutB = CUT_B + dragLengthY;
  318. let cutR = CUT_R + dragLengthX;
  319. this.setData({
  320. cutB: cutB,
  321. cutR: cutR
  322. })
  323. break;
  324. default:
  325. break;
  326. }
  327. }
  328. }
  329. }
  330. </script>
  331. <style>
  332. /* pages/uni-cropper/index.wxss */
  333. .uni-content-info {
  334. position: fixed;
  335. top: 0;
  336. left: 0;
  337. right: 0;
  338. bottom: 0;
  339. display: block;
  340. align-items: center;
  341. flex-direction: column;
  342. }
  343. .cropper-config {
  344. padding: 20upx 40upx;
  345. }
  346. .cropper-content {
  347. min-height: 750upx;
  348. width: 100%;
  349. }
  350. .uni-corpper {
  351. position: relative;
  352. overflow: hidden;
  353. -webkit-user-select: none;
  354. -moz-user-select: none;
  355. -ms-user-select: none;
  356. user-select: none;
  357. -webkit-tap-highlight-color: transparent;
  358. -webkit-touch-callout: none;
  359. box-sizing: border-box;
  360. }
  361. .uni-corpper-content {
  362. position: relative;
  363. }
  364. .uni-corpper-content image {
  365. display: block;
  366. width: 100%;
  367. min-width: 0 !important;
  368. max-width: none !important;
  369. height: 100%;
  370. min-height: 0 !important;
  371. max-height: none !important;
  372. image-orientation: 0deg !important;
  373. margin: 0 auto;
  374. }
  375. /* 移动图片效果 */
  376. .uni-cropper-drag-box {
  377. position: absolute;
  378. top: 0;
  379. right: 0;
  380. bottom: 0;
  381. left: 0;
  382. cursor: move;
  383. background: rgba(0, 0, 0, 0.6);
  384. z-index: 1;
  385. }
  386. /* 内部的信息 */
  387. .uni-corpper-crop-box {
  388. position: absolute;
  389. background: rgba(255, 255, 255, 0.3);
  390. z-index: 2;
  391. }
  392. .uni-corpper-crop-box .uni-cropper-view-box {
  393. position: relative;
  394. display: block;
  395. width: 100%;
  396. height: 100%;
  397. overflow: visible;
  398. outline: 1upx solid #69f;
  399. outline-color: rgba(102, 153, 255, .75)
  400. }
  401. /* 横向虚线 */
  402. .uni-cropper-dashed-h {
  403. position: absolute;
  404. top: 33.33333333%;
  405. left: 0;
  406. width: 100%;
  407. height: 33.33333333%;
  408. border-top: 1upx dashed rgba(255, 255, 255, 0.5);
  409. border-bottom: 1upx dashed rgba(255, 255, 255, 0.5);
  410. }
  411. /* 纵向虚线 */
  412. .uni-cropper-dashed-v {
  413. position: absolute;
  414. left: 33.33333333%;
  415. top: 0;
  416. width: 33.33333333%;
  417. height: 100%;
  418. border-left: 1upx dashed rgba(255, 255, 255, 0.5);
  419. border-right: 1upx dashed rgba(255, 255, 255, 0.5);
  420. }
  421. /* 四个方向的线 为了之后的拖动事件*/
  422. .uni-cropper-line-t {
  423. position: absolute;
  424. display: block;
  425. width: 100%;
  426. background-color: #69f;
  427. top: 0;
  428. left: 0;
  429. height: 1upx;
  430. opacity: 0.1;
  431. cursor: n-resize;
  432. }
  433. .uni-cropper-line-t::before {
  434. content: '';
  435. position: absolute;
  436. top: 50%;
  437. right: 0upx;
  438. width: 100%;
  439. -webkit-transform: translate3d(0, -50%, 0);
  440. transform: translate3d(0, -50%, 0);
  441. bottom: 0;
  442. height: 41upx;
  443. background: transparent;
  444. z-index: 11;
  445. }
  446. .uni-cropper-line-r {
  447. position: absolute;
  448. display: block;
  449. background-color: #69f;
  450. top: 0;
  451. right: 0upx;
  452. width: 1upx;
  453. opacity: 0.1;
  454. height: 100%;
  455. cursor: e-resize;
  456. }
  457. .uni-cropper-line-r::before {
  458. content: '';
  459. position: absolute;
  460. top: 0;
  461. left: 50%;
  462. width: 41upx;
  463. -webkit-transform: translate3d(-50%, 0, 0);
  464. transform: translate3d(-50%, 0, 0);
  465. bottom: 0;
  466. height: 100%;
  467. background: transparent;
  468. z-index: 11;
  469. }
  470. .uni-cropper-line-b {
  471. position: absolute;
  472. display: block;
  473. width: 100%;
  474. background-color: #69f;
  475. bottom: 0;
  476. left: 0;
  477. height: 1upx;
  478. opacity: 0.1;
  479. cursor: s-resize;
  480. }
  481. .uni-cropper-line-b::before {
  482. content: '';
  483. position: absolute;
  484. top: 50%;
  485. right: 0upx;
  486. width: 100%;
  487. -webkit-transform: translate3d(0, -50%, 0);
  488. transform: translate3d(0, -50%, 0);
  489. bottom: 0;
  490. height: 41upx;
  491. background: transparent;
  492. z-index: 11;
  493. }
  494. .uni-cropper-line-l {
  495. position: absolute;
  496. display: block;
  497. background-color: #69f;
  498. top: 0;
  499. left: 0;
  500. width: 1upx;
  501. opacity: 0.1;
  502. height: 100%;
  503. cursor: w-resize;
  504. }
  505. .uni-cropper-line-l::before {
  506. content: '';
  507. position: absolute;
  508. top: 0;
  509. left: 50%;
  510. width: 41upx;
  511. -webkit-transform: translate3d(-50%, 0, 0);
  512. transform: translate3d(-50%, 0, 0);
  513. bottom: 0;
  514. height: 100%;
  515. background: transparent;
  516. z-index: 11;
  517. }
  518. .uni-cropper-point {
  519. width: 5upx;
  520. height: 5upx;
  521. background-color: #69f;
  522. opacity: .75;
  523. position: absolute;
  524. z-index: 3;
  525. }
  526. .point-t {
  527. top: -3upx;
  528. left: 50%;
  529. margin-left: -3upx;
  530. cursor: n-resize;
  531. }
  532. .point-tr {
  533. top: -3upx;
  534. left: 100%;
  535. margin-left: -3upx;
  536. cursor: n-resize;
  537. }
  538. .point-r {
  539. top: 50%;
  540. left: 100%;
  541. margin-left: -3upx;
  542. margin-top: -3upx;
  543. cursor: n-resize;
  544. }
  545. .point-rb {
  546. left: 100%;
  547. top: 100%;
  548. -webkit-transform: translate3d(-50%, -50%, 0);
  549. transform: translate3d(-50%, -50%, 0);
  550. cursor: n-resize;
  551. width: 36upx;
  552. height: 36upx;
  553. background-color: #69f;
  554. position: absolute;
  555. z-index: 1112;
  556. opacity: 1;
  557. }
  558. .point-b {
  559. left: 50%;
  560. top: 100%;
  561. margin-left: -3upx;
  562. margin-top: -3upx;
  563. cursor: n-resize;
  564. }
  565. .point-bl {
  566. left: 0%;
  567. top: 100%;
  568. margin-left: -3upx;
  569. margin-top: -3upx;
  570. cursor: n-resize;
  571. }
  572. .point-l {
  573. left: 0%;
  574. top: 50%;
  575. margin-left: -3upx;
  576. margin-top: -3upx;
  577. cursor: n-resize;
  578. }
  579. .point-lt {
  580. left: 0%;
  581. top: 0%;
  582. margin-left: -3upx;
  583. margin-top: -3upx;
  584. cursor: n-resize;
  585. }
  586. /* 裁剪框预览内容 */
  587. .uni-cropper-viewer {
  588. position: relative;
  589. width: 100%;
  590. height: 100%;
  591. overflow: hidden;
  592. }
  593. .uni-cropper-viewer image {
  594. position: absolute;
  595. z-index: 2;
  596. }
  597. </style>

3. 裁剪好后可以上传图像 上传业务逻辑

  1. //用户上传图像
  2. const uploadImg = (path, callback) => {
  3. uni.uploadFile({//上传文件api
  4. url: '后台上传接口',
  5. filePath: path,
  6. name: 'file',
  7. success: (uploadFileRes) => {
  8. callback(uploadFileRes.data);
  9. }
  10. });
  11. }

*** 注意事项 在个人首页 会监听裁剪页面保存动作,裁剪了并保存成功了会返回 true ,根据true刷新个人头像

*** 注意事项 如果全部成功了 但是头像没有刷新 那么你可以在 头像那个 view 里面加上个 :key 属性,动态设置 :key 就行了,其实就是 :key 没改变 所以刷新不了,这个也坑了我一会了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/287037
推荐阅读