| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- import { CropController, CropView, IBestToast, YTAvoid, YTLog, yTRouter, YTUserRequest } from 'basic'
- import { fileIo } from '@kit.CoreFileKit'
- import { util } from '@kit.ArkTS'
- import { emitter } from '@kit.BasicServicesKit'
- @Builder
- function delPhotoPageBuilder() {
- NavDestination() {
- DelPhotoPage()
- }
- .hideTitleBar(true)
- }
- @Component
- struct DelPhotoPage {
- @StorageProp(YTAvoid.SAFE_BOTTOM_KEY) private safeBottom: number = 0
- @State private selectPhoto: string = ''
- @State private frameWidth: number = 220
- @State private frameHeight: number = 220
- @State private startWidth: number = 220
- @State private startHeight: number = 220
- private declare type: 'bill' | 'header' | 'image'
- private controller: CropController = new CropController()
- private declare context: Context
- aboutToAppear(): void {
- this.selectPhoto = yTRouter.getDelPhotoParam().src
- this.type = yTRouter.getDelPhotoParam().type
- const tempContext = this.getUIContext().getHostContext()
- if (tempContext === undefined) {
- //抛出异常
- throw new Error('DelPhotoPage context is null')
- } else {
- this.context = tempContext
- }
- YTAvoid.setStatusBarContentColor('#FFFFFFFF', this.context)
- }
- aboutToDisappear(): void {
- YTAvoid.setStatusBarContentColor('#FF000000', this.context)
- }
- build() {
- Column() {
- Stack({ alignContent: Alignment.Center }) {
- CropView({
- src: this.selectPhoto,
- controller: this.controller, // 控制器必须设置
- maskColor: '#4D000000',
- frameWidth: this.frameWidth,
- frameHeight: this.frameHeight
- })
- .width("100%")
- .layoutWeight(1)
- .backgroundColor(Color.Black)
- Column() {
- Row() {
- Text()
- .height(30)
- .aspectRatio(1)
- .border({ width: { left: 8, top: 8 }, color: Color.White })
- .offset({ x: -8, y: -8 })
- .borderRadius({ topLeft: 5 })
- .gesture(
- PanGesture({ distance: 1 })
- .onActionStart(() => {
- // 记录拖拽开始时的初始尺寸
- this.startWidth = this.frameWidth
- this.startHeight = this.frameHeight
- })
- .onActionUpdate(event => {
- // 基于初始尺寸计算新尺寸
- const newWidth = Math.max(220, this.startWidth - event.offsetX)
- const newHeight = Math.max(220, this.startHeight - event.offsetY)
- // 同时更新两个维度
- this.frameWidth = newWidth
- this.frameHeight = newHeight
- })
- )
- Text()
- .height(30)
- .aspectRatio(1)
- .border({ width: { right: 8, top: 8 }, color: Color.White })
- .offset({ x: 8, y: -8 })
- .borderRadius({ topRight: 5 })
- .gesture(
- PanGesture({ distance: 1 })
- .onActionStart(() => {
- // 记录拖拽开始时的初始尺寸
- this.startWidth = this.frameWidth
- this.startHeight = this.frameHeight
- })
- .onActionUpdate(event => {
- // 基于初始尺寸计算新尺寸
- const newWidth = Math.max(220, this.startWidth + event.offsetX)
- const newHeight = Math.max(220, this.startHeight - event.offsetY)
- // 同时更新两个维度
- this.frameWidth = newWidth
- this.frameHeight = newHeight
- })
- )
- }
- .width('100%')
- .height(25)
- .justifyContent(FlexAlign.SpaceBetween)
- .borderRadius(8)
- Row() {
- Text()
- .height(30)
- .aspectRatio(1)
- .border({ width: { left: 8, bottom: 8 }, color: Color.White })
- .offset({ x: -8, y: 8 })
- .borderRadius({ bottomLeft: 5 })
- .gesture(
- PanGesture({ distance: 1 })
- .onActionStart(() => {
- // 记录拖拽开始时的初始尺寸
- this.startWidth = this.frameWidth
- this.startHeight = this.frameHeight
- })
- .onActionUpdate(event => {
- // 基于初始尺寸计算新尺寸
- const newWidth = Math.max(220, this.startWidth - event.offsetX)
- const newHeight = Math.max(220, this.startHeight + event.offsetY)
- // 同时更新两个维度
- this.frameWidth = newWidth
- this.frameHeight = newHeight
- })
- )
- Text()
- .height(30)
- .aspectRatio(1)
- .border({ width: { right: 8, bottom: 8 }, color: Color.White })
- .borderRadius({ bottomRight: 5 })
- .offset({ x: 8, y: 8 })
- .gesture(
- PanGesture({ distance: 1 })
- .onActionStart(() => {
- // 记录拖拽开始时的初始尺寸
- this.startWidth = this.frameWidth
- this.startHeight = this.frameHeight
- })
- .onActionUpdate(event => {
- // 基于初始尺寸计算新尺寸
- const newWidth = Math.max(220, this.startWidth + event.offsetX)
- const newHeight = Math.max(220, this.startHeight + event.offsetY)
- // 同时更新两个维度
- this.frameWidth = newWidth
- this.frameHeight = newHeight
- })
- )
- }
- .width('100%')
- .height(25)
- .justifyContent(FlexAlign.SpaceBetween)
- }
- .width(this.frameWidth)
- .height(this.frameHeight)
- .justifyContent(FlexAlign.SpaceBetween)
- .hitTestBehavior(HitTestMode.Transparent)
- }
- .layoutWeight(1)
- Row() {
- Image($r('app.media.back'))
- .width(36)
- .aspectRatio(1)
- .onClick(() => {
- yTRouter.routerBack()
- })
- Image($r('app.media.make_sure'))
- .height(56)
- .aspectRatio(1)
- .onClick(() => {
- //使用 controller 获取裁剪图片
- this.controller.cropWithPath({ format: 'image/jpeg', quality: 100 })
- .then(async (path) => {
- // path 为裁剪后的文件路径
- try {
- const photoName = util.generateRandomUUID()
- const photoType = '.jpeg'
- const fullpath = this.context.cacheDir + '/' + photoName + photoType
- const photo = fileIo.openSync(path)
- await fileIo.copyFile(photo.fd, fullpath)
- if (this.type == 'header') {
- IBestToast.showLoading({ message: '上传中...' })
- YTUserRequest.uploadHeadImg(this.getUIContext().getHostContext()!, fullpath, () => {
- YTUserRequest.refreshUserInfo(() => {
- IBestToast.hide()
- setTimeout(() => {
- IBestToast.show({ message: '头像修改成功', type: 'success' })
- }, 100)
- })
- })
- yTRouter.routerBack()
- }
- if (this.type == 'image') {
- IBestToast.showLoading({ message: '上传中...' })
- YTUserRequest.uploadFile(this.getUIContext().getHostContext()!, fullpath, (url) => {
- emitter.emit('upLoadEnd', { data: url })
- IBestToast.hide()
- yTRouter.routerBack()
- })
- }
- } catch (e) {
- YTLog.error(e)
- }
- })
- })
- Image($r('app.media.rotate'))
- .height(36)
- .aspectRatio(1)
- .onClick(() => {
- this.controller.rotate()
- })
- }
- .width('100%')
- .height(84)
- .justifyContent(FlexAlign.SpaceAround)
- }
- .padding({ bottom: this.safeBottom })
- }
- }
|