shishuangxi

点赞接口调试

@@ -756,6 +756,12 @@ export class HttpUrlUtils { @@ -756,6 +756,12 @@ export class HttpUrlUtils {
756 return url 756 return url
757 } 757 }
758 758
  759 + //点赞
  760 + static executeLike() {
  761 + let url = HttpUrlUtils._hostUrl + "/api/rmrb-interact/interact/zh/c/like/executeLike";
  762 + return url;
  763 + }
  764 +
759 // static getYcgCommonHeaders(): HashMap<string, string> { 765 // static getYcgCommonHeaders(): HashMap<string, string> {
760 // let headers: HashMap<string, string> = new HashMap<string, string>() 766 // let headers: HashMap<string, string> = new HashMap<string, string>()
761 // 767 //
1 import { BottomNavi, CommonConstants, SpConstants } from 'wdConstant'; 1 import { BottomNavi, CommonConstants, SpConstants } from 'wdConstant';
2 -import { Logger, SPHelper } from 'wdKit'; 2 +import { Logger, SPHelper, StringUtils } from 'wdKit';
3 import PageViewModel from '../../viewmodel/PageViewModel'; 3 import PageViewModel from '../../viewmodel/PageViewModel';
4 import storageStatistics from "@ohos.file.storageStatistics"; 4 import storageStatistics from "@ohos.file.storageStatistics";
5 import { BusinessError } from '@ohos.base'; 5 import { BusinessError } from '@ohos.base';
@@ -12,7 +12,7 @@ import { CustomCacheDialog } from './CustomCacheDialog'; @@ -12,7 +12,7 @@ import { CustomCacheDialog } from './CustomCacheDialog';
12 import MineSettingDatasModel from '../../model/MineSettingDatasModel'; 12 import MineSettingDatasModel from '../../model/MineSettingDatasModel';
13 import { MineMainSettingFunctionItem } from '../../viewmodel/MineMainSettingFunctionItem'; 13 import { MineMainSettingFunctionItem } from '../../viewmodel/MineMainSettingFunctionItem';
14 import common from '@ohos.app.ability.common'; 14 import common from '@ohos.app.ability.common';
15 - 15 +import dataPreferences from '@ohos.data.preferences';
16 16
17 @Component 17 @Component
18 export struct MineSettingComponent { 18 export struct MineSettingComponent {
@@ -21,6 +21,7 @@ export struct MineSettingComponent { @@ -21,6 +21,7 @@ export struct MineSettingComponent {
21 @State privacySwitch: boolean = false 21 @State privacySwitch: boolean = false
22 @State cacheSize: number = 0 22 @State cacheSize: number = 0
23 @State accountState:boolean=false 23 @State accountState:boolean=false
  24 + preferences: dataPreferences.Preferences | null = null;
24 dialogController: CustomDialogController = new CustomDialogController({ 25 dialogController: CustomDialogController = new CustomDialogController({
25 builder: CustomCacheDialog({ 26 builder: CustomCacheDialog({
26 cancel: () => { 27 cancel: () => {
@@ -41,6 +42,8 @@ export struct MineSettingComponent { @@ -41,6 +42,8 @@ export struct MineSettingComponent {
41 42
42 this.getAccountState() 43 this.getAccountState()
43 44
  45 + this.addLoginStatusObserver()
  46 +
44 } 47 }
45 48
46 async getAccountState(){ 49 async getAccountState(){
@@ -304,4 +307,14 @@ export struct MineSettingComponent { @@ -304,4 +307,14 @@ export struct MineSettingComponent {
304 } 307 }
305 }); 308 });
306 } 309 }
  310 +
  311 + async addLoginStatusObserver(){
  312 + this.preferences = await SPHelper.default.getPreferences();
  313 + let observer = (key: string) => {
  314 + if(key == SpConstants.USER_ID){
  315 + this.getSettingPageData()
  316 + }
  317 + }
  318 + this.preferences.on('change', observer);
  319 + }
307 } 320 }
  1 +import { Logger } from 'wdKit/Index'
  2 +import { LikeViewModel } from '../../viewmodel/LikeViewModel'
  3 +
  4 +@Component
  5 +export struct LikeComponent {
  6 + @State likeStatus: boolean = false
  7 + viewModel: LikeViewModel = new LikeViewModel()
  8 + @Prop data: Record<string, string>
  9 + enableBtn = true
  10 +
  11 + //上层传值 样例
  12 + // this.data['contentId'] = '30035444649' //必须
  13 + // this.data['userName'] = '人民日报网友2kD2xW'
  14 + // this.data['contentType'] = '8' //必须
  15 + // this.data['title'] = '开创两校交流先河!克罗地亚教育代表团访问同济大学'
  16 + // this.data['userHeaderUrl'] = ""
  17 + // this.data['channelId'] = "2059" //必须
  18 + // this.data['status'] = "1" //必须
  19 +
  20 + aboutToAppear(): void {
  21 + if (this.data) {
  22 + Logger.debug("ddd: " + this.data['status'])
  23 + if (this.data['status'] == '1') {
  24 + this.likeStatus = true
  25 + } else {
  26 + this.likeStatus = false
  27 + }
  28 +
  29 + }
  30 +
  31 + }
  32 +
  33 + build() {
  34 + Column() {
  35 + Image(this.likeStatus ? $r('app.media.icon_like_select') : $r('app.media.icon_like_default'))
  36 + .width(24)
  37 + .height(24)
  38 + .onClick(() => {
  39 + if (!this.enableBtn) {
  40 + return
  41 + }
  42 + if (this.likeStatus) {
  43 + //1
  44 + this.executeLike('1')
  45 + } else {
  46 + //0
  47 + this.executeLike('0')
  48 + }
  49 + })
  50 + }.width(24).height(24)
  51 + }
  52 +
  53 + executeLike(status: string) {
  54 + this.data['status'] = status
  55 + this.viewModel.executeLike2(this.data).then(() => {
  56 + this.likeStatus = !this.likeStatus
  57 + this.enableBtn = true
  58 + }).catch(() => {
  59 + this.enableBtn = true
  60 + })
  61 + }
  62 +}
  1 +import { HashMap } from '@kit.ArkTS';
  2 +import { Logger } from 'wdKit/Index';
  3 +import { HttpUrlUtils, ResponseDTO } from 'wdNetwork/Index';
  4 +import { HttpRequest } from 'wdNetwork/src/main/ets/http/HttpRequest';
  5 +
  6 +export class LikeModel {
  7 + executeLike(data: Record<string, string>) {
  8 + let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders();
  9 +
  10 + return new Promise<object>((success, fail) => {
  11 + HttpRequest.post<ResponseDTO<object>>(HttpUrlUtils.executeLike(), data, headers).then((data: ResponseDTO<object>) => {
  12 + if (data.code != 0) {
  13 + fail(data.message)
  14 + return
  15 + }
  16 + success(data)
  17 + }, (error: Error) => {
  18 + fail(error.message)
  19 + Logger.debug("LoginViewModel:error ", error.toString())
  20 + })
  21 + })
  22 + }
  23 +}
  1 +import { LikeModel } from '../model/LikeModel'
  2 +
  3 +/**
  4 + * 点赞*/
  5 +export class LikeViewModel {
  6 + likeModel: LikeModel
  7 +
  8 + constructor() {
  9 + this.likeModel = new LikeModel();
  10 + }
  11 +
  12 + executeLike(contentId: string, userName: string, contentType: string, title: string, userHeaderUrl: string, channelId: string, status: string) {
  13 + let bean: Record<string, string> = {}
  14 + bean['contentId'] = contentId
  15 + bean['userName'] = userName
  16 + bean['contentType'] = contentType
  17 + bean['title'] = title
  18 + bean['userHeaderUrl'] = userHeaderUrl
  19 + bean['channelId'] = channelId
  20 + bean['status'] = status
  21 + this.likeModel.executeLike(bean)
  22 + }
  23 +
  24 + executeLike2(bean: Record<string, string>) {
  25 +
  26 + return new Promise<object>((success, fail) => {
  27 + this.likeModel.executeLike(bean).then((data) => {
  28 + success(data)
  29 + }).catch((error: string) => {
  30 + fail(error)
  31 + })
  32 + })
  33 +
  34 + }
  35 +}