有人知道发布页demo吗?

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

HarmonyOS
2024-05-26 14:31:07
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
济南二狗子

核心代码:

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() 
      } 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270.
  • 271.
  • 272.
  • 273.
  • 274.
  • 275.
  • 276.
  • 277.
  • 278.
  • 279.
  • 280.
  • 281.
  • 282.
  • 283.
  • 284.
  • 285.
  • 286.
  • 287.
  • 288.
  • 289.

实现效果

分享
微博
QQ
微信
回复
2024-05-27 18:18:51
相关问题
有人知道关于demo
1146浏览 • 1回复 待解决
webview组件demo ,有人知道
1313浏览 • 1回复 待解决
如何跳转到app设置有人知道
1015浏览 • 1回复 待解决
如何跳出ForEach,有人知道
2559浏览 • 1回复 待解决
taskpool 使用问题,有人知道
1613浏览 • 1回复 待解决
有人知道JS menu如何隐藏
4978浏览 • 1回复 待解决
有人知道
797浏览 • 1回复 待解决
SnapShot定位,有人知道怎么处理
1605浏览 • 1回复 待解决
如何保存faultLogger ,有人知道
1128浏览 • 1回复 待解决
有人知道
1191浏览 • 1回复 待解决
如何发送短信,有人知道?
2467浏览 • 1回复 待解决
如何实现振动,有人知道
1670浏览 • 2回复 待解决
clientid相关问题,有人知道
2332浏览 • 1回复 待解决
导包报错,有人知道原因
1643浏览 • 1回复 待解决
如何获取windowStage,有人知道
1187浏览 • 1回复 待解决
ArkTS支持反射,有人知道反射用法?
2971浏览 • 1回复 待解决
导航栏如何适配,有人知道?
2166浏览 • 0回复 待解决
如何使用快速修复,有人知道
1138浏览 • 1回复 待解决
如何引用HSP库,有人知道?
2076浏览 • 1回复 待解决
IDE如何开启ASAN,有人知道
694浏览 • 1回复 待解决
有人知道如何实现图文混排
1266浏览 • 1回复 待解决
List组件性能问题,有人知道
2506浏览 • 1回复 待解决
有人知道
703浏览 • 0回复 待解决
如何实现翻页功能,有人知道
2424浏览 • 1回复 待解决
如何获取wifi列表,有人知道
1249浏览 • 1回复 待解决