Blame view

apps/pages/userpage/components/author.vue 15.8 KB
yyq authored
1 2 3 4 5 6 7 8 9 10 11 12
<template>
  <Layout class="author-page">
    <LayoutHeader slot='header' theme="white" class="author-page-header">
      <div ref="headerAuthor" class="header-author">
        <div class="h-name flex">
          <span class="h-name-b">{{authorBaseData.nickName}}</span>
          <span v-if="authorBaseData.sex" class="iconfont icon-women author-sex-icon" :class="`icon-${authorBaseData.sex}`"></span>
        </div>
        <div class="h-more">
          <div class="flex">
            <WidgetAvatar class="h-headico" :src="authorBaseData.headIco" :width="100" :height="100"></WidgetAvatar>
          </div>
yyq authored
13 14 15
          <div v-if="!isOwner" class="h-follow flex">
            <WidgetFollow class="widget-follow" :author-uid="authorInfo.authorUid" :author-type="authorInfo.authorType" :follow="authorBaseData.isAttention" @on-follow="follow => onFollow(follow)" :pos-id="sceneId"></WidgetFollow>
          </div>
yyq authored
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
        </div>
      </div>
    </LayoutHeader>

    <div class="fixed-tab">
      <FavTabBlock v-if="tabFixed" :tabs-num="tabsNum" :active-index="activeIndex" @change="changeTab"></FavTabBlock>
    </div>

    <cube-scroll
      class="main-container"
      ref="scroll"
      :options="scrollOpts"
      :scroll-events="['scroll', 'scroll-end', 'before-scroll-start']"
      @scroll="onScrollHandle"
      @scroll-end="onScrollEndHandle"
      @before-scroll-start="beforeScrollStartHandle">
      <div ref="authorProfile" class="author-profile">
yyq authored
33
        <span class="avatar-box" :class="{'avatar-opacity': authorBaseData.showAvatar}">
yyq authored
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
          <WidgetAvatar :src="authorBaseData.headIco" :width="100" :height="100"></WidgetAvatar>
        </span>
        <div class="author-section">
          <ul class="author-fans">
            <li v-for="item in fansList" :key="item.key">
              <div class="click-wrap" @click="toPage(item.type)"></div>
              <span class="num">{{authorBaseData[item.key] || 0}}</span>
              <p class="name"><span>{{item.name}}</span></p>
            </li>
          </ul>
          <div class="operate-wrap">
            <a v-if="isOwner" class="operate-btn btn-user-edit" :href="mineInfoUrl">编辑个人资料</a>
            <WidgetFollow v-if="!isOwner" class="operate-btn" :author-uid="authorInfo.authorUid" :author-type="authorInfo.authorType" :follow="authorBaseData.isAttention" @on-follow="follow => onFollow(follow)" :pos-id="sceneId"></WidgetFollow>
          </div>
        </div>
      </div>
      <p v-if="authorBaseData.signature" class="author-desc">{{authorBaseData.signature}}</p>
yyq authored
51
      <div ref="tabBlock" class="tab-block">
yyq authored
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
        <FavTabBlock :tabs-num="tabsNum" :active-index="activeIndex" @change="changeTab"></FavTabBlock>
      </div>
      <div ref="contantList" class="contant-list" :style="`min-height: ${listMinHeight}px;`">
        <p v-if="emptyTip" class="empty-tip">{{emptyTip}}</p>
        <WaterFall class="pannel-wrap" :list="list" :params="params" :tab="activeIndex"></WaterFall>
        <div v-if="loadStatus && !emptyTip" class="loading">
          <Loading v-if="loadStatus === 1" class="load-icon" :size="20"></Loading>
          <p v-else class="load-text">没有更多了</p>
        </div>
      </div>
    </cube-scroll>

    <a v-if="isOwner" class="publish hover-opacity" :class="{'scroll-opacity': scrolling}" :href="publishUrl"></a>
  </Layout>
</template>

<script>
import {assign, get} from 'lodash';
import {Scroll, Loading} from 'cube-ui';
import FavTabBlock from './fav-tab-block';
import WaterFall from './scroll-reveal';
import YAS from 'utils/yas-constants';

import {createNamespacedHelpers} from 'vuex';
const {mapState, mapActions, mapMutations} = createNamespacedHelpers('user');

export default {
  data() {
    return {
      scrollY: 0,
      scrolling: false,
      mineInfoUrl: '//m.yohobuy.com/home/mydetails?openby:yohobuy={"action":"go.mineinfo"}',
      publishUrl: '?openby:yohobuy={"action":"go.grasspublish"}',
      authorInfo: {},
      fansList: [
        {name: '关注', key: 'attCount', type: 'follow'},
        {name: '粉丝', key: 'fansCount', type: 'fans'},
        {name: '获赞与收藏', key: 'praiseAndfavorite'}
      ],
      isOwner: false,
      authorBaseData: {},
      tabFixed: false,
      tabsNum: [0, 0],
      activeIndex: 0,
      listMinHeight: 0,
      fetchInfo: {},
      loadStatus: '',
      emptyTip: '',
      scrollOpts: {
        bounce: false,
      },
      sceneId: YAS.scene.author
    };
  },
  created() {
    this.isOwner = !this.$route.params.id;
  },
  mounted() {
    let $dom = this.$refs.headerAuthor;

    this.init(this.$route.params);

    this.listMinHeight = this.$el.offsetHeight - this.$refs.tabBlock.offsetHeight - $dom.offsetHeight;
yyq authored
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

    if ($dom.offsetHeight) {
      this._animeDuration = 300;
      import('animejs').then(({default: anime}) => {
        this._animeEl = anime({
          targets: $dom,
          translateY: -$dom.offsetHeight,
          easing: 'easeInOutSine',
          duration: this._animeDuration,
          autoplay: false
        });
      });
    }

    this.beforeRouteEnter = this.beforeRouteUpdate;
yyq authored
130 131 132 133 134 135 136 137 138 139 140 141 142
  },
  computed: {
    list() {
      return get(this.fetchInfo, `${this.activeIndex}.list`) || [];
    },
    params() {
      return {
        params: {
          type: this.waterFallType,
          authorType: this.authorInfo.authorType,
          authorUid: this.authorInfo.authorUid,
        },
        query: {
yyq authored
143
          userName: this.detailTitle
yyq authored
144 145 146 147 148
        }
      };
    },
    waterFallType() {
      return ['publish', 'fav'][this.activeIndex];
yyq authored
149
    },
yyq authored
150 151
    detailTitle() {
      return [this.authorBaseData.nickName, '收藏'][this.activeIndex];
yyq authored
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    }
  },
  methods: {
    ...mapActions(['authorBaseInfo', 'authorAritcleNum', 'authorPubList', 'authorFavList', 'authorMineBaseInfo', 'authorMineAritcleNum', 'authorMinePubList', 'authorMineFavList']),
    init(params) {
      params = params || {};

      if (+params.id === this.authorInfo.authorUid) {
        return;
      }

      this._apiNamePre = 'author';

      this.isOwner = !params.id;
      this.tabFixed = false;
      this.tabsNum = [0, 0];
      this.activeIndex = 0;
      this.fetchInfo = {};
      this.loadStatus = '';
      this.emptyTip = '';

      if (this.scrollY > 0) {
yyq authored
174 175 176 177
        if (this._animeEl) {
          this._animePlayed = false;
          this._animeEl.seek(0);
        }
yyq authored
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
        this.$refs.scroll.scrollTo(0, 0);
      }

      if (!params.id) {
        this._apiNamePre += 'Mine';

        this.checkLogin(uid => {
          this.fetchData(uid, 1);
        });
      } else {
        this.fetchData(+params.id, params.type || 1);
      }
    },
    fetchData(authorUid, authorType, noBase) {
      this.authorInfo = {authorUid, authorType};

      this.fetchBaseInfo();
      this.fetchList(true);
    },
    checkLogin(cb) {
      try {
        this.$sdk.getUser().then(res => {
          if (get(res, 'uid') > 0) {
            return cb && cb(res.uid);
          } else {
            this.$sdk.goLogin();
          }
        });
      } catch (e) {
        console.log(e);
      }
    },
    onScrollHandle({y}) {
      this.scrollY = -y;

      let animePlayed = this.scrollY > this.$refs.authorProfile.offsetHeight;

      if (!this._animePlayed === !animePlayed) {
        return;
      }

      this.tabFixed = animePlayed;
yyq authored
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
      let start;
      let self = this;

      function step(timestamp) {
        if (!start) {
          start = timestamp
        };

        let progress = Math.floor(timestamp - start);

        self._animeEl.seek(animePlayed ? progress : self._animeDuration - progress);

        if (progress < self._animeDuration) {
          window.requestAnimationFrame(step);
        }
      };

      window.requestAnimationFrame(step);
yyq authored
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
      this._animePlayed = animePlayed;
    },
    onScrollEndHandle(scroll) {
      this.scrolling = false;
      if (1000 - scroll.y > this.$refs.contantList.offsetHeight) {
        this._listTimer && clearTimeout(this._listTimer);
        this._listTimer = setTimeout(() => {
          this.fetchList();
        }, 50);
      }
    },
    beforeScrollStartHandle() {
      this.scrolling = true;
    },
    changeTab(index) {
      if (this.activeIndex !== index) {
        this.activeIndex = index;
        this.loadStatus = '';
        this.emptyTip = '';

        let info = this.fetchInfo[index] || {};

        info.page = 1;
        info.lastedTime = '';

        this.fetchList();

        let top = this.$refs.authorProfile.offsetHeight;

        if (this.scrollY > top) {
          this.$nextTick(() => {
            this.$refs.scroll.scrollTo(0, -top - 1, 0);
          });
        }
      }
    },
    fetchBaseInfo() {
      this[this._apiNamePre + 'BaseInfo'](this.authorInfo).then(data => {
        data.isAttention = data.hasAttention === 'Y';
yyq authored
279
        data.showAvatar = true;
yyq authored
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
        this.authorBaseData = data;
        this.isOwner = +data.userType === 1;
      });

      this[this._apiNamePre + 'AritcleNum'](this.authorInfo).then(res => {
        this.tabsNum = [get(res, 'data.articleCount'), get(res, 'data.favoriteCount')];
      });
    },
    async fetchList(wait) {
      this.fetchInfo = this.fetchInfo || [];

      if (this.syncing) {
        return;
      }

      let info = this.fetchInfo[this.activeIndex] || {};
      let result;

      info.page = info.page || 1;

      if (info.page > info.totalPage) {
        this.loadStatus = 2;
        return;
      }

      this.loadStatus = 1;

      let syncServiceName;

      if (this.activeIndex === 1) {
        syncServiceName = this._apiNamePre + 'FavList';
      } else {
        syncServiceName = this._apiNamePre + 'PubList';
      }

      if (this[syncServiceName]) {
        this.syncing = true;
        result = await this[syncServiceName](assign({
          limit: 20,
          page: info.page,
          lastedTime: info.lastedTime || ''
        }, this.authorInfo));

        this.syncing = false;
      }

      if (result && result.code === 200) {
        if (info.page === 1) {
          info.list = [];
        }

        info.list = (info.list || []).concat(result.data.list || []);
        info.page++;
        info.totalPage = result.data.totalPage || 1;
        info.lastedTime = result.data.lastedTime;

        this.setEmptyTip(info.list, result.data.totalCount);

        if (info.list.length) {
          info.list[0]._type = `${info.list[0].articleId}_${this.activeIndex}`;
        }
      }

      setTimeout(() => {
        if (info.page > info.totalPage) {
          this.loadStatus = 2;
        }

        this.fetchInfo[this.activeIndex] = info;
        this.fetchInfo = {...this.fetchInfo};
        setTimeout(() => {
          this.$refs.scroll.refresh();
        }, 100);
      }, wait ? 300 : 0);
    },
    setEmptyTip(list, totalCount) {
      let tip = '';

      if (!list.length && !totalCount) {
        switch (this.activeIndex) {
          case 0:
            tip = this.isOwner ? '发布你的第一篇潮人态度' : 'TA还没有分享过哦';
            break;
          case 1:
            tip = this.isOwner ? '快去收藏你的第一篇内容吧' : 'TA还没有收藏内容';
            break;
          default:
            break;
        }
      }

      this.emptyTip = tip;
    },
    onFollow(follow) {
      this.authorBaseData.isAttention = follow;
    },
    toPage(type) {
      let routerName = '';
      let params = {};

      if (type) {
        routerName = `${this.isOwner ? 'mine' : 'author'}.${type}`;

        if (!this.isOwner) {
          params.id = this.authorInfo.authorUid;
          params.type = this.authorInfo.authorType;
        }

        this.$router.push({
          name: routerName,
          params
        });
      }
    }
  },
  components: {
    CubeScroll: Scroll,
    Loading,
    FavTabBlock,
    WaterFall
  }
};
</script>

<style lang="scss">
  .author-page {
    box-sizing: border-box;
    color: #4a4a4a;
  }

  .author-page-header > .title {
    overflow: visible!important;
  }

  .fixed-tab {
    width: 100%;
    position: absolute;
    z-index: 2;
  }

  .header-author {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;

    .flex {
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .h-name {
      font-size: 36px;
      font-weight: 500;

      .h-name-b {
yyq authored
439
        max-width: 80%;
yyq authored
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
        line-height: 1.4;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        display: inline-block;
      }

      .author-sex-icon {
        font-size: 28px;
        padding-left: 10px;
        padding-right: 0;
      }

      .icon-man {
        color: #96d8f3;
      }

      .icon-women {
        color: #f7c3e1;
      }
    }

    .h-more {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 100%;
      left: 0;
    }

    .h-headico {
      width: 60px;
      height: 60px;
    }

    .h-follow {
      position: absolute;
      top: 0;
      right: -130px;
    }
  }

  .header-share {
    margin-right: 26px;
    color: #222;
    font-weight: bold;
  }

  .author-profile {
    padding: 24px 30px;
    display: flex;
    justify-content: space-between;
    overflow: hidden;

    .avatar-box {
      width: 150px;
      height: 150px;
      overflow: hidden;
      border-radius: 50%;
yyq authored
499 500 501 502 503 504
      opacity: 0;
      transition: opacity 300ms ease-in-out;

      &.avatar-opacity {
        opacity: 1;
      }
yyq authored
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

      > img {
        width: 100%;
        height: 100%;
        display: block;
      }
    }
  }

  .author-section {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }

  .author-fans {
    display: flex;
    justify-content: flex-end;
    padding-top: 4px;
yyq authored
524
    padding-right: 40px;
yyq authored
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

    li {
      margin-left: 140px;
      position: relative;
      z-index: 1;

      &:first-child {
        margin-left: 0;
      }

      .click-wrap {
        position: absolute;
        left: -50px;
        right: -50px;
        top: -14px;
        bottom: -50px;
        z-index: 10;
      }

      .num {
        min-width: 30px;
        font-size: 28px;
        font-weight: 500;
        padding-bottom: 6px;
        display: block;
        text-align: center;
      }

      .name {
        position: absolute;
        font-size: 20px;
        font-weight: 300;
        color: #9b9b9b;
        margin-left: 50%;
        word-break: keep-all;
        white-space: nowrap;

        > * {
          position: relative;
          left: -50%;
        }
      }
    }
  }

  .operate-wrap {
    text-align: right;

    .operate-btn {
yyq authored
574
      width: calc(100% + 10px);
yyq authored
575 576 577 578 579
      height: 50px;
      font-size: 23px;
      line-height: 50px;
      display: inline-block;
      box-sizing: border-box;
yyq authored
580
      float: right;
yyq authored
581 582 583 584 585
    }

    .btn-user-edit {
      color: #222;
      border: 1px solid #4a4a4a;
yyq authored
586
      border-radius: 26px;
yyq authored
587 588 589 590 591 592 593 594 595 596 597 598 599
      text-align: center;
    }
  }

  .author-desc {
    margin: 20px 30px;
    font-size: 24px;
    font-weight: 300;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
yyq authored
600 601 602 603 604 605 606 607 608 609 610 611 612 613
  .tab-block {
    position: relative;

    &:before {
      content: "";
      height: 1px;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      background-color: #f4f4f4;
    }
  }
yyq authored
614 615 616 617
  .contant-list {
    position: relative;
  }
yyq authored
618 619 620 621
  .pannel-wrap {
    background-color: #f7f7f7;
  }
yyq authored
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
  .empty-tip {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 28px;
    color: #ddd;
    position: absolute;
    top: 0;
    bottom: 240px;
  }

  .loading {
    padding: 20px 0;

    .load-icon > span {
      margin: auto;
    }

    .load-text {
      text-align: center;
    }
  }

  .publish {
    width: 100px;
    height: 100px;
    position: absolute;
    bottom: 52px;
    right: 40px;
    background-image: url('../../../statics/image/userpage/publish.png');
    background-size: 100% 100%;
    z-index: 10;
    transition: all 600ms ease-in-out;

    &.scroll-opacity {
      opacity: 0.3;
    }
  }
</style>