mixin.js 2.44 KB
/*
 * @Date: 2024-05-15 10:28:10
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2024-11-26 12:49:10
 * @FilePath: /tswj/src/common/mixin.js
 * @Description: 文件描述
 */
import axios from 'axios';
import { showToast, showSuccessToast } from 'vant';

export default {
  // 初始化设置
  init: {
    mounted () {
      document.title = this.$route.meta.title;
    }
  },
  likeFn: {
    methods: {
      /**
       * 用户相关操作
       * @param {String} type 动作类型:like, favor, play
       * @param {String} id 作品 ID
       */
      handleAction (type, id) { // 用户操作
        axios.post('/srv/?a=prod_action', {
          action_type: type,
          prod_id: id
        })
        .then(res => {
          if (res.data.code === 1) {
            if (res.data.msg === `${type}-add-OK`) { // 动作操作成功
              this.getProductDetail(type, id);
              if (type === 'favor') {
                showSuccessToast('收藏成功');
              }
              if (type === 'like') {
                showSuccessToast('点赞成功');
              }
            } else { // 取消操作
              this.getProductDetail(type, id);
              if (type !== 'play') {
                showSuccessToast('取消成功');
              }
            }
          } else {
            // tslint:disable-next-line: no-console
            console.warn(res);
            if (!res.data.show) return false;
            showToast({
              icon: 'close',
              message: res.data.msg
            });
          }
        })
        .catch(err => {
          // tslint:disable-next-line: no-console
          console.error(err);
        });
      },
      getProductDetail (type, id) { // 查询更新作品详情
        axios.get('/srv/?a=prod_info', {
          params: {
            prod_id: id
          }
        })
        .then(res => {
          if (res.data.code === 1) {
            this.detail[`is_${type}`] = res.data.data[`is_${type}`];
            this.detail[`${type}_num`] = res.data.data[`${type}_num`];
          } else {
            // tslint:disable-next-line: no-console
            console.warn(res);
            if (!res.data.show) return false;
            showToast({
              icon: 'close',
              message: res.data.msg
            });
          }
        })
        .catch(err => {
          // tslint:disable-next-line: no-console
          console.error(err);
        });
      }
    }
  }
};