Skip to content
stuff-detail.ts 5.71 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component, ElementRef, Renderer2, ViewChild} from '@angular/core';
wangqinghua's avatar
wangqinghua committed
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {TabsService} from "../../tabs/tabs.service";
import {Storage} from "@ionic/storage";
import {CommonService} from "../../../provide/common.service";
wangqinghua's avatar
wangqinghua committed
import {DomSanitizer} from "@angular/platform-browser";
wangqinghua's avatar
wangqinghua committed
import {AppGlobal} from "../../../service/http.service";
wangqinghua's avatar
wangqinghua committed

@IonicPage()
@Component({
    selector: 'page-stuff-detail',
    templateUrl: 'stuff-detail.html',
})
export class StuffDetailPage {
wangqinghua's avatar
wangqinghua committed
    @ViewChild('like') like: ElementRef;
    @ViewChild('collection') collection: ElementRef;
wangqinghua's avatar
wangqinghua committed

    stuffId;   //文章id
wangqinghua's avatar
wangqinghua committed
    content: string;   //评论内容
wangqinghua's avatar
wangqinghua committed
    userId;  //用户id
wangqinghua's avatar
wangqinghua committed
    username;   //用户姓名
wangqinghua's avatar
wangqinghua committed
    stuffObj;   //文章对象

wangqinghua's avatar
wangqinghua committed
    //点赞,评价,收藏对象;
    countObj;

wangqinghua's avatar
wangqinghua committed
    footerView: boolean;
    comment: boolean = false;

wangqinghua's avatar
wangqinghua committed
    url = AppGlobal.domain + '/wisdomgroup';
wangqinghua's avatar
wangqinghua committed

    attachments;   //附件

wangqinghua's avatar
wangqinghua committed
    constructor(public navCtrl: NavController, public navParams: NavParams, public sanitizer: DomSanitizer,
                public renderer: Renderer2,
wangqinghua's avatar
wangqinghua committed
                public tabSer: TabsService, public storage: Storage, public commonSer: CommonService) {
    }

    ionViewDidLoad() {
        this.storage.get('userLoginInfo').then((value) => {
            this.userId = value.userid;
wangqinghua's avatar
wangqinghua committed
            this.username = value.loginName;
wangqinghua's avatar
wangqinghua committed
            this.lookHandle();
wangqinghua's avatar
wangqinghua committed
        })
        this.stuffId = this.navParams.get('id');
        this.getStuff();
wangqinghua's avatar
wangqinghua committed
        setTimeout(() => {
wangqinghua's avatar
wangqinghua committed
            this.footerView = true;
wangqinghua's avatar
wangqinghua committed
        }, 300)
wangqinghua's avatar
wangqinghua committed
        //增加浏览量
    }

    //获取文章信息
    getStuff() {
        this.tabSer.stuffInfo(this.stuffId).subscribe(
            (res) => {
                this.stuffObj = res.data;
wangqinghua's avatar
wangqinghua committed
                this.countObj = res.data;   //防止点赞,评论,收藏之后页面刷新
wangqinghua's avatar
wangqinghua committed
                this.attachments = res.data.attachments;
wangqinghua's avatar
wangqinghua committed
                let con = this.stuffObj.content.replace(/\r?\n/g, "<br />");
                this.stuffObj.content = this.sanitizer.bypassSecurityTrustHtml(con);
wangqinghua's avatar
wangqinghua committed
            }
        )
    }

    ionViewWillLeave() {
        this.footerView = false;
    }

    //更新浏览 点赞 收藏 评论
    //根据类型,修改数量(1浏览,2点赞,3收藏,4评论)
wangqinghua's avatar
wangqinghua committed
    //浏览
    lookHandle() {
wangqinghua's avatar
wangqinghua committed
        const data = {
            id: this.stuffId,
wangqinghua's avatar
wangqinghua committed
            type: 1,
            num: 1,
            userid: this.userId,
            content: this.content,
        }
        this.tabSer.updateNumByType(data).subscribe(
            (res) => {
                if (res.errcode == '1000') {
                    this.commonSer.log('浏览数 +1')
                }
            }
        )
    }

    //点赞
    likeHandle() {
        let num = this.countObj.like == true ? '-1' : 1;
        const data = {
            id: this.stuffId,
            type: 2,
wangqinghua's avatar
wangqinghua committed
            num: ~~num,
wangqinghua's avatar
wangqinghua committed
            userid: this.userId,
            content: this.content,
        }
wangqinghua's avatar
wangqinghua committed
        this.tabSer.updateNumByType(data).subscribe(
            (res) => {
                if (res.errcode == '1000') {
wangqinghua's avatar
wangqinghua committed
                    this.tabSer.stuffInfo(this.stuffId).subscribe(
                        (res) => {
                            if (num == 1) {
                                this.commonSer.toastTime('谢谢点赞','1000');
                                this.renderer.addClass(this.like.nativeElement, 'animation');
                            }
                            if (num == -1){
                                this.commonSer.toastTime('点赞已取消','1000');
                                this.renderer.removeClass(this.like.nativeElement, 'animation');
                            }
                            this.countObj = res.data;   //防止点赞,评论,收藏之后文章信息刷新刷新
                        }
                    )
                }
            }
        )
    }

    //收藏
    collectionHandle() {
        let num = this.countObj.collection == true ? '-1' : 1;
        const data = {
            id: this.stuffId,
            type: 3,
            num: ~~num,
            userid: this.userId,
            content: this.content,
        };
        this.tabSer.updateNumByType(data).subscribe(
            (res) => {
                if (res.errcode == '1000') {
                    this.tabSer.stuffInfo(this.stuffId).subscribe(
                        (res) => {
                            if (num == 1) {
                                this.renderer.addClass(this.collection.nativeElement, 'animation');
                                this.commonSer.toastTime('功崇惟志,业广惟勤', '1000')
                            }
                            if (num == -1) {
                                this.renderer.removeClass(this.collection.nativeElement, 'animation');
                                this.commonSer.toastTime('收藏已取消', '1000')
                            }
                            this.countObj = res.data;   //防止点赞,评论,收藏之后文章信息刷新刷新
                        }
                    )
wangqinghua's avatar
wangqinghua committed
                }
            }
        )
    }

    //确定发布
wangqinghua's avatar
wangqinghua committed
    sure() {
wangqinghua's avatar
wangqinghua committed
        const data = {
            userid: this.userId,
            content: this.content,
            id: this.stuffId,
wangqinghua's avatar
wangqinghua committed
            num: 1,
            type: 4,
            username: this.username
wangqinghua's avatar
wangqinghua committed
        }
        this.tabSer.updateNumByType(data).subscribe(
            (res) => {
                if (res.errcode == '1000') {
                    this.getStuff();
wangqinghua's avatar
wangqinghua committed
                    this.comment = false;
wangqinghua's avatar
wangqinghua committed
                    this.commonSer.toast('发布成功');
                }
            }
        )
    }

    //取消发布
wangqinghua's avatar
wangqinghua committed
    cancel() {
wangqinghua's avatar
wangqinghua committed
        this.content = '';
        this.comment = false;
    }

}