index.js 8.83 KB
/*
 * @Description:
 * @Author: huyue
 * @Date: 2022-08-30 11:26:21
 * @LastEditors: huyue
 * @LastEditTime: 2022-09-09 14:41:59
 */
// pages/tab/index/index.js
import { menuMixin } from "../../../mixins/index";
import Draw from "../../../utils/Draw";
import { ssoApi, entmicroApi } from "../../../api/api";
import util from "../../../utils/util";
import { trackData } from "../../../utils/data";
Page({
  behaviors: [menuMixin],
  /**
   * 页面的初始数据
   */
  data: {
    introduceName: "",
    tabList: [
      {
        name: "公司介绍",
        icon: "icon-company",
        icon1: "icon-company1",
      },
      {
        name: "方案介绍",
        icon: "icon-product",
        icon1: "icon-product1",
      },
      {
        name: "视频直播",
        icon: "icon-video",
        icon1: "icon-video",
      },
      // {
      //   name: "合作申请",
      //   icon: "icon-cooperation",
      //   icon1: "icon-cooperation",
      // },
    ],
    curTabIndex: 0,
    // 0-员工,1-代理商
    customerType: 0,
    // 是否授权昵称
    hasUserInfo: false,
    // 是否授权手机号
    hasPhoneNumber: false,
    curTab: [trackData.COMPANY, trackData.CUBE],
    // 员工名片信息
    cardInfo: null,
    defaultInfo: {
      staffName: '虚拟员工',
      operation: '虚拟职位',
      staffPhone: 'xxxxxxxx',
      image: 'https://img.zcool.cn/community/01e47e60ab5b4011013eaf70962e3f.jpg@520w_390h_1c_1e_2o_100sh.jpg',
      qrCode: 'https://img.zcool.cn/community/01e47e60ab5b4011013eaf70962e3f.jpg@520w_390h_1c_1e_2o_100sh.jpg'
    }
  },
  introduce(e) {
    const curTabIndex = e.currentTarget.dataset.index;
    // 视频直播和合作申请是跳转新页面,不需要设置激活样式
    // 0-公司介绍 1-方案介绍 2-视频直播 3-合作申请
    console.log(this.data.curTabIndex);
    switch (curTabIndex) {
      case 2:
        console.log("跳转直播");
        wx.openChannelsActivity({
          finderUserName: 'sphPIdcy0NVpuao',
          feedId: 'export/UzFfAgtgekIEAQAAAAAAOQ8BJrsjzwAAAAstQy6ubaLX4KHWvLEZgBPEmIEUOQV4c62CzNPgMJqRnrGh3TfaajoIU4mGBiio'
        })
        wx.$Track.sendTrack(trackData.VIDEO);
        break;
      default:
        this.setData({
          curTabIndex: curTabIndex,
        });
        wx.$Track.sendTrack(this.data.curTab[this.data.curTabIndex]);
    }
  },

  // 跳转咨询页面
  goConsult() {
    console.log("跳转到咨询页面");
  },

  // 设置手机号
  getPhoneNumber() {
    console.log(11111111);
    this.setData({
      hasPhoneNumber: true,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.$util.showLoading('加载中...')
    getApp()
      .login()
      .then(({ data }) => {
        wx.hideLoading()
        if (data && data.nickName) {
          this.setData({
            hasUserInfo: true,
          });
        } else {
          this.setData({
            hasUserInfo: false
          })
        }
        
        if (data && data.phone) {
          this.setData({
            hasPhoneNumber: true,
          });
        }
        this.getCardDetail();
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(wx.$trackData, wx.getEnterOptionsSync())
    const options = wx.getEnterOptionsSync()
    wx.$trackData = options.query
    console.log(options.query,'--------------')
    if (wx.$trackData.merchant) {
      wx.$Track.sendTrack(this.data.curTab[this.data.curTabIndex])        
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: `您好,我向你推荐了广联数科的${this.data.cardInfo.staffName}的智能名片~`,
      imageUrl: this.data.shareImg,
    };
  },
  setShareInfo(data = {}) {
    const canvasWidth = 208,
      canvasHeight = 166,
      textBaseline = "top";
    const draw_function = new Draw(this, {
      width: canvasWidth,
      height: canvasHeight,
      // pixelRatio: 2,
      // backgroundColor: '#096'
    });
    const drawCard = (callBack) => {
      let index = 0;
      const iconWidth = 9,
        iconHeight = 9,
        iconX = 18,
        iconY = 79,
        imgList = [
          {
            url: "https://img02.glsx.com.cn/weapp/resource/miniprogram/ai-card/share/bg.png",
            x: 0,
            y: 0,
            width: canvasWidth,
            height: 124,
          },
          {
            url: "https://img02.glsx.com.cn/weapp/resource/miniprogram/ai-card/share/logo.png",
            x: 15,
            y: 18,
            width: 152,
            height: 10,
          },
          // 头像的
          {
            // url: "https://img.zcool.cn/community/01e47e60ab5b4011013eaf70962e3f.jpg@520w_390h_1c_1e_2o_100sh.jpg",
            url: data.image,
            x: 154,
            y: 36,
            width: 36,
            height: 36,
          },
          {
            url: "https://img02.glsx.com.cn/weapp/resource/miniprogram/ai-card/share/icon-phone.png",
            x: iconX,
            y: iconY,
            width: iconWidth,
            height: iconHeight,
          },
          {
            url: "https://img02.glsx.com.cn/weapp/resource/miniprogram/ai-card/share/icon-address.png",
            x: iconX,
            y: iconY + 13,
            width: iconWidth,
            height: iconHeight,
          },
          {
            url: "https://img02.glsx.com.cn/weapp/resource/miniprogram/ai-card/share/btn.png",
            x: 64,
            y: 138,
            width: 80,
            height: 24,
          },
        ],
        textList = [
          {
            text: data.staffName,
            x: 18,
            y: 36,
            color: "#2D2D33",
            fontWeight: 600,
            fontSize: 14,
          },
          {
            text: data.operation,
            x: 22 + draw_function.getTextWidth(data.staffName, 14),
            y: 40,
            color: "#6E6E7A",
            fontWeight: 400,
            fontSize: 8,
          },
          {
            text: data.staffPhone,
            x: 30,
            y: 80,
            color: "#6E6E7A",
            fontSize: 7,
          },
          {
            text: "深圳市南山区留仙大道3370号南山智园崇文园区1号楼14层",
            x: 30,
            y: 94,
            color: "#6E6E7A",
            fontSize: 7,
            lineNum: 2,
            width: 185,
            lineHeight: 9,
          },
        ];
      const drawImages = (callBack) => {
        draw_function.drawImages(imgList[index], () => {
          index += 1;
          if (index != imgList.length) drawImages(callBack);
          else callBack();
        });
      };
      drawImages(() => {
        try {
          textList.map((e, ind) => {
            e.textBaseline = textBaseline;
            draw_function.drawText(e);
            if (ind == textList.length - 1) throw true;
          });
        } catch (error) {
          draw_function.toDataURL(({ path }) => {
            this.setData({
              shareImg: path,
            });
          });
        }
      });
    };
    drawCard();
  },
  // 获取用户昵称
  getUserProfile(e) {
    this.setData({
      hasUserInfo: true,
    });
    // 推荐使用 wx.getUserProfile 获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
    wx.getUserProfile({
      desc: "用于完善资料", // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
      success: (res) => {
        console.log(this.data.hasUserInfo);
        ssoApi
          .authUserInfo({
            ...res,
          })
          .then(({ code }) => {
            if (code == "0") {
              getApp()
                .login()
                .then(({ data }) => {
                  this.setData({
                    userInfo: data,
                    // hasUserInfo: true
                  });
                });
            }
          });
      },
    });
  },
  // 获取员工名片信息
  getCardDetail() {
    const sessionId = util.getSessionId();
    let params = {};
    params.cardId = wx.$trackData.cardId
    entmicroApi.getCardDetail(params).then(({data, code, message}) => {
      if (code == -1) {
        wx.showToast({
          title: message,
          icon: 'none',
          duration: 2000
        })
      }
      this.setData({
        cardInfo: data || this.data.defaultInfo
      })
      this.setShareInfo(this.data.cardInfo);
    });
  },
});