api-mock.js 4.4 KB
Newer Older
ZK645945's avatar
init  
ZK645945 已提交
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
/**
 * 简单做了下数据本地存储, 生产API涉及签名不开源
 */

import { collectDB } from './utils/db';

export default {
  async communityCheck() {
    return {
      code: 200,
      msg: 'success',
      data: {
        qaUrl: 'https://bbs.csdn.net/forums/placard?category=0&typeId=23466',
        community: {
          id: 1,
          userName: 'User',
          communityId: 1000,
          uriName: 'test',
          homePageUrl: 'https://bbs.csdn.net/ccloud/test',
          status: 1,
          createTime: '2022-04-20T08:11:23.000+0000',
        },
        url: 'https://blog.csdn.net/SoftwareTeacher/article/details/123940505',
      },
    };
  },
  async communityCreate() {
    return await this.communityCheck();
  },
  async collected(url) {
    const list = await collectDB.list();
    const collectItem = list.filter(v => v.type === 'collect').find(v => v.url === url);

    if (!collectItem) {
      return null;
    }
    const [comment] = collectItem.comment;
    const result = {
      topicInfo: {
        url: 'https://bbs.csdn.net/topics/XXX',
        id: collectItem.id,
        cateName: '全部',
        cateId: comment.tagId,
        contentId: collectItem.id,
        topicTitle: collectItem.title,
        content: collectItem.content,
      },
      comments: collectItem.comment,
    };
    return result;
  },
  async createCollect(formData) {
    const list = await collectDB.list();
    const { topicId, replyId, type, source, ...data } = formData;
    const _replyId = replyId || +new Date() + '';

    // 编辑
    if (topicId) {
      let collectItem = list.find(v => v.id === topicId);
      if (!collectItem) throw new Error('ID不存在');

      if (type === 'collect') {
        // 编辑笔记单项
        if (replyId) {
          const commentIndex = collectItem.comment.findIndex(v => v.id === replyId);
          if (commentIndex === -1) throw new Error('commentId不存在');
          collectItem.comment[commentIndex] = {
            ...collectItem.comment[commentIndex],
            ...data,
          };
        } else {
          // 有id, 没commentId代表新增一个评论
          collectItem.comment.push({
            ...data,
            id: _replyId,
          });
        }
      } else {
        collectItem = {
          ...collectItem,
          ...data,
        };
      }
      await collectDB.update(topicId, collectItem);
      return { code: 200, data: { topicId, replyId: type === 'collect' ? _replyId : '' } };
    } else {
      let postData = {
        type: type,
        title: source.title || '',
        url: source ? source.url : '', // 去除?后的URL
        source: source || {}, // 来源网站信息
        comment: [], // 评论数据
      };
      // 收藏修改标题
      if (type === 'collect') {
        postData.mdContent = `>标题: ${source.title}\n` + `  链接: ${source.url}\n` + `${source.selection ? `  引用: ${source.selection}\n>` : '>'}`;
        postData.content = `<blockquote>\n<p>标题:${source.title}}<br>链接:${source.url}${source.selection ? `<br>引用: ${source.selection}` : ''}</p>\n</blockquote>\n`;
        postData.comment.push({
          ...data,
          id: _replyId,
        });
      } else {
        postData = { ...postData, ...data };
      }
      const retId = await collectDB.add(postData);
      return { code: 200, data: { id: retId, replyId: type === 'collect' ? _replyId : '' } };
    }
  },
  async tagList() {
    return {
      code: 200,
      msg: 'success',
      data: [
        { tabContribute: 0, cdId: 4451, tabName: '全部', createTime: null, sortType: 1, cardType: 0, tabType: 4, indexOrder: 0, id: 23632, tabUrl: '', tabSwitch: 1, status: 0 },
        {
          tabContribute: 1,
          cdId: 4451,
          tabName: '博客笔记',
          createTime: null,
          sortType: 1,
          cardType: 0,
          tabType: 1,
          indexOrder: 0,
          id: 23646,
          tabUrl: '',
          tabSwitch: 1,
          status: 0,
        },
      ],
    };
    // return tagService.list();
  },
  async tagCreate(tagName) {
    return {
      tabContribute: 1,
      cdId: +`${+new Date()}`.slice(9),
      tabName: tagName,
      createTime: null,
      sortType: null,
      cardType: null,
      tabType: 1,
      indexOrder: null,
      id: +(100 + `${+new Date()}`.slice(9)),
      tabUrl: null,
      tabSwitch: 1,
      status: 0,
    };
  },
};