demo1

 // 计算字数
  dealWordCount(str) {
    this.isChina(str);
    if (this.wordCount > 245) {
      return true;
    } else {
      return false;
    }
  }
  // 判断是否是中文
  isChina(str) {
    this.wordCount = 0;
    //let reg = new RegExp('[\\u4E00-\\u9FFF]+');
    //reg.test(str) ? (this.wordCount += 2) : (this.wordCount += 1);
    for (let i = 0; i < str.length; i++) {
      str.charCodeAt(i) > 255 ? (this.wordCount += 2) : (this.wordCount += 1);
    }
  }
  
 // 下拉刷新
onRefreshBottom = () => {
    if (this.isPageRefresh || !this.isRefreshBottom) return;
    this.pageIndex++;
    this.queryLists();
  };
  
 
// 显示消息提示框
  showToast(obj) {
    Taro.hideToast();
    Taro.showToast({
      title: obj.title,
      icon: obj.icon || 'none',
      duration: obj.duration || 2000,
      mask: obj.mask || false,
    });
  }

//一键分享
  onShare(opts) {
    this.setClipboardData(opts.helperDesc);
    appInstance.globalData.poster = {
      ...appInstance.globalData.poster,
      punchThePicId: opts.punchThePicId,
    };
    if (!opts.punchThePicId) return;
    setTimeout(() => {
      this.navigateTo(`/poster/pages/placard/placard`);
    }, 1500);
  }
  // 复制
  setClipboardData = content => {
    this.tryIndex++;
    Taro.setClipboardData({
      data: content,
      success() {},
      fail() {
        if (this.tryIndex <= 3) {
          this.setClipboardData(content);
        }
      },
    });
  };
res.forEach(element => {
          if (element.helperDesc) {
            element.isShowAll = this.dealWordCount(element.helperDesc || '');
            element.isOpen = false;
          }
        });
queryLists() {
    if (this.isPageRefresh) return;
    this.isPageRefresh = true;
    let { tabId } = this.props,
      { lists } = this.state,
      that = this;
    comApi
      .queryBillHelperPic({ id: tabId, pageIndex: that.pageIndex, pageSize: that.pageSize })
      .then(res => {
        this.isPageRefresh = false;
        if (!(res && res.length > 0)) return;
        this.isRefreshBottom = res.length >= 10 ? true : false;
        res.forEach(element => {
          if (element.helperDesc) {
            element.isShowAll = this.dealWordCount(element.helperDesc || '');
            element.isOpen = false;
          }
        });
        res = [...lists, ...res];
        this.setState({
          lists: res,
        });
        console.log('helper-lists', lists, tabId, res);
      })
      .catch(error => {
        this.isPageRefresh = false;
        this.showToast({ title: error.message || `服务器卡顿,请稍后再试` });
      });
  }

                      {item.helperDesc}
                    
                    {item.isShowAll && (
                       this.onShowAll(index)}>
                        {item.isOpen ? '收起' : '全部'}
                      
                    )}

你可能感兴趣的:(前端,javascript)