有人知道发布页demo吗?

发布页的内容主要包括用户要发布的文本、图片、视频等媒体内容,以及相关的标签、话题、权限、范围设定等选项。

HarmonyOS
2024-05-26 14:31:07
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
LenZhong

核心代码:

import { router } from '@kit.ArkUI'; 
import Constants from '../common/constants/Constants'; 
import { fileSelect, fileUpload } from '../common/utils/FileUtil'; 
import { NewsFile, NewsData } from '../viewmodel/NewsData'; 
import NewsViewModel from '../viewmodel/NewsViewModel'; 
import { showToast } from '../common/utils/ToastUtil'; 
import UploadingLayout from '../view/UploadingLayout'; 
import ResponseResult from '../viewmodel/ResponseResult'; 
import { GlobalContext } from '../viewmodel/GlobalContext'; 
import geoLocationManager from '@ohos.geoLocationManager'; 
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
import { hilog } from '@kit.PerformanceAnalysisKit'; 
 
const TAG = 'NewsEditPage' 
 
/** 
 * NewsEditPage. 
 */ 
@Entry 
@Component 
struct NewsEditPage { 
  title: string = ''; 
  content: string = ''; 
  @State imageUri: string = ''; 
  @State isUploading: boolean = false; 
  @State text: string = "选择标签" 
  @State index: number = 2 
  @State space: number = 8 
  @State arrowPosition: ArrowPosition = ArrowPosition.END 
  //纬度 
  @State lat: Number = -1; 
  //经度1 
  @State lon: Number = -1; 
  @State message: string = ''; 
 
  getLocationTwo() { 
    let requestInfo: geoLocationManager.LocationRequest = { 
      'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX, 
      'scenario': geoLocationManager.LocationRequestScenario.UNSET, 
    }; 
    try { 
      geoLocationManager.getCurrentLocation(requestInfo).then((data) => { 
        this.lat = data.latitude; 
        this.lon = data.longitude; 
        let info: geoLocationManager.ReverseGeoCodeRequest = { 
          // 获取当前位置的纬度 
          latitude: data.latitude, 
          // 获取当前位置的经度 
          longitude: data.longitude, 
          // 获取次数大于10次以便获得cityCode 
          maxItems: 10 
        } 
        // 使用逆地理编码 
        geoLocationManager.getAddressesFromLocation(info).then((code) => { 
          this.message = code[0].placeName!; 
          console.log("here is resgeo code :" + JSON.stringify(code)) 
        }) 
      }); 
    } catch (e) { 
 
    } 
  } 
 
  private requestLocationPermissions() { 
    const permissions: Array<Permissions> = ['ohos.permission.APPROXIMATELY_LOCATION', 'ohos.permission.LOCATION'] 
    let atManager = abilityAccessCtrl.createAtManager() 
    // requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗 
    atManager.requestPermissionsFromUser(getContext(this), permissions).then((data) => { 
      let grantStatus: Array<number> = data.authResults; 
      let length: number = grantStatus.length; 
      for (let i = 0; i < length; i++) { 
        if (grantStatus[i] === 0) { 
          // 用户授权,可以继续访问目标操作 
          return 
        } else { 
          // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限 
          return; 
        } 
      } 
    }).catch((err: BusinessError) => { 
    }) 
  } 
 
  onPageShow() { 
    // 申请权限 
    this.requestLocationPermissions() 
  } 
 
  getLocation() { 
    let requestInfo: geoLocationManager.CurrentLocationRequest = { 
      'priority': 0x203, 
      'scenario': geoLocationManager.LocationRequestScenario.DAILY_LIFE_SERVICE, 
      'maxAccuracy': 1000, 
      'timeoutMs': 5000 
    }; 
 
    if (geoLocationManager.isLocationEnabled()) { 
      console.log("定位已使能"); 
    } 
    else { 
      console.log("定位未使能"); 
    } 
 
    try { 
      geoLocationManager.getCurrentLocation(requestInfo, (err, location) => { 
        if (err) { 
          console.error("定位失败,失败原因为:"); 
          console.error(err.message); 
          return; 
        } 
        console.info("位置获取成功"); 
        this.lat = location.latitude; 
        this.lon = location.longitude; 
 
      }) 
    } catch (err) { 
      console.error("报错原因为:"); 
      console.error(err); 
    } 
  } 
 
  selectImage() { 
    fileSelect().then((uri: string) => { 
      this.imageUri = uri || ''; 
    }); 
  } 
 
  uploadNewsData() { 
    // router.pushUrl({ url: "pages/RichEditorPage" }) 
    // return 
    if (this.title === '') { 
      showToast($r('app.string.prompt_no_title')); 
      return; 
    } 
    if (this.content === '') { 
      showToast($r('app.string.prompt_no_content')); 
      return; 
    } 
    if (this.imageUri === '') { 
      showToast($r('app.string.prompt_no_file')); 
      return; 
    } 
    this.isUploading = true; 
    let serverData = fileUpload(getContext(this), this.imageUri); 
    serverData.then((data: ResponseResult) => { 
      let imageUrl = data.data; 
      let newsFile = new NewsFile(); 
      newsFile.id = 0; 
      newsFile.url = imageUrl; 
      newsFile.type = 0; 
      newsFile.newsId = 0; 
      let newsData: NewsData = new NewsData(); 
      newsData.title = this.title; 
      newsData.content = this.content; 
      newsData.imagesUrl = [newsFile]; 
      NewsViewModel.uploadNews(newsData).then(() => { 
        this.isUploading = false; 
        GlobalContext.getContext().setObject('isBackRouter', true); 
        router.back(); 
      }).catch(() => { 
        this.isUploading = false; 
        showToast($r('app.string.upload_error_message')); 
      }); 
    }).catch(() => { 
      this.isUploading = false; 
      showToast($r('app.string.upload_error_message')); 
    }); 
  } 
 
  build() { 
    Stack() { 
      Navigation() { 
        Column() { 
          Column() { 
            TextInput({ placeholder: $r('app.string.title_default_text') }) 
              .fontSize($r('app.float.title_font')) 
              .placeholderFont({ size: $r('app.float.title_font') }) 
              .margin({ top: $r('app.float.common_padding') }) 
              .fontColor($r('app.color.title_color')) 
              .backgroundColor(Color.White) 
              .onChange((value: string) => { 
                this.title = value; 
              }) 
              .width(Constants.FULL_PERCENT) 
              .height($r('app.float.input_height')) 
            Divider() 
              .opacity($r('app.float.divider_opacity')) 
              .color($r('app.color.title_color')) 
              .width(Constants.DIVIDER_WIDTH) 
            TextArea({ placeholder: $r('app.string.content_default_text') }) 
              .placeholderFont({ size: $r('app.float.title_font') }) 
              .fontColor($r('app.color.title_color')) 
              .height($r('app.float.area_height')) 
              .fontSize($r('app.float.title_font')) 
              .margin({ top: $r('app.float.normal_padding') }) 
              .backgroundColor(Color.White) 
              .onChange((value: string) => { 
                this.content = value; 
              }) 
            Scroll() { 
              Row() { 
                Image(this.imageUri ? this.imageUri : $r('app.media.ic_add_pic')) 
                  .width($r('app.float.img_size')) 
                  .height($r('app.float.img_size')) 
                  .objectFit(ImageFit.Cover) 
                  .onClick(() => this.selectImage()) 
              } 
              .padding($r('app.float.common_padding')) 
            } 
            .width(Constants.FULL_PERCENT) 
            .scrollable(ScrollDirection.Horizontal) 
            .align(Alignment.Start) 
 
          } 
          .borderRadius($r('app.float.edit_view_radius')) 
          .backgroundColor(Color.White) 
          .width(Constants.FULL_PERCENT) 
 
          Row() { 
            Select([{ value: '标签1' }, 
              { value: '标签2' }, 
              { value: '标签3' }, 
              { value: '标签4' }]) 
              .selected(this.index) 
              .value(this.text) 
              .font({ size: 16, weight: 500 }) 
              .fontColor('#182431') 
              .selectedOptionFont({ size: 16, weight: 400 }) 
              .optionFont({ size: 16, weight: 400 }) 
              .space(this.space) 
              .arrowPosition(this.arrowPosition) 
              .menuAlign(MenuAlignType.START, { dx: 0, dy: 0 }) 
              .optionWidth(150) 
              .optionHeight(100) 
              .width(150) 
              .margin({ top: 10 }) 
              .onSelect((index: number, text?: string | undefined) => { 
                console.info('Select:' + index) 
                this.index = index; 
                if (text) { 
                  this.text = text; 
                } 
              }) 
            Column() { 
              Button('获取位置') 
                .onClick(() => { 
                  console.log("获取位置已点击"); 
                  this.getLocationTwo(); 
                }) 
              if (this.message) { 
                Column() { 
                  Text(`${this.message}`) 
                } 
              } 
            }.margin({ top: 10 }) 
          } 
          .width('100%') 
          .justifyContent(FlexAlign.SpaceBetween) 
 
          Blank() 
          // Button('关于页').onClick(() => router.pushUrl({ url: "pages/AboutPage" })) 
 
          Button($r('app.string.release_btn')) 
            .fontSize($r('app.float.title_font')) 
            .height($r('app.float.release_btn_height')) 
            .width($r('app.float.release_btn_width')) 
            .margin({ bottom: $r('app.float.common_padding') }) 
            .onClick(() => this.uploadNewsData()) 
        } 
        .padding({ 
          left: $r('app.float.common_padding'), 
          right: $r('app.float.common_padding'), 
          bottom: $r('app.float.common_padding') 
        }) 
        .height(Constants.FULL_PERCENT) 
        .justifyContent(FlexAlign.SpaceBetween) 
      } 
      .height(Constants.FULL_PERCENT) 
      .title(Constants.RELEASE_TITLE) 
      .titleMode(NavigationTitleMode.Mini) 
      .backgroundColor($r('app.color.listColor')) 
 
      if (this.isUploading) { 
        // UploadingLayout() 
      } 
    } 
  } 
}

实现效果

分享
微博
QQ
微信
回复
2024-05-27 18:18:51
相关问题
有人知道关于demo
388浏览 • 1回复 待解决
webview组件demo ,有人知道
418浏览 • 1回复 待解决
如何跳转到app设置有人知道
270浏览 • 1回复 待解决
SnapShot定位,有人知道怎么处理
375浏览 • 1回复 待解决
如何跳出ForEach,有人知道
590浏览 • 1回复 待解决
如何发送短信,有人知道?
551浏览 • 1回复 待解决
taskpool 使用问题,有人知道
389浏览 • 1回复 待解决
如何保存faultLogger ,有人知道
144浏览 • 1回复 待解决
有人知道社区怎么预约直播
1256浏览 • 1回复 已解决
有人知道JS menu如何隐藏
3086浏览 • 1回复 待解决
如何实现翻页功能,有人知道
571浏览 • 1回复 待解决
List组件性能问题,有人知道
589浏览 • 1回复 待解决
有人知道如何实现图文混排
339浏览 • 1回复 待解决
ArkTS支持反射,有人知道反射用法?
699浏览 • 1回复 待解决
如何引用HSP库,有人知道?
547浏览 • 1回复 待解决
如何定义dialog动画,有人知道?
697浏览 • 1回复 待解决
导航栏如何适配,有人知道?
554浏览 • 0回复 待解决
如何使用快速修复,有人知道
221浏览 • 1回复 待解决
导包报错,有人知道原因
570浏览 • 1回复 待解决
clientid相关问题,有人知道
452浏览 • 1回复 待解决
如何开启AOT编译模式,有人知道
636浏览 • 1回复 待解决
如何实现http长连接,有人知道
449浏览 • 1回复 待解决
是否有无网判断接口,有人知道
521浏览 • 1回复 待解决
有人知道关于折叠机适配问题
537浏览 • 1回复 待解决