index.js 10.5 KB
// pages/viewProd/index.js
import { behavior as computedBehavior } from "~/utils/lib/miniprogram-computed";
import { IMG_URL } from "~/utils/constantData.js";
import { senceData } from "~/pages/tab/index/constant/data";
import USERAPI from "~/api/usersApi/index";
import utils from "~/utils/util.js";
import { getActiveApi } from "~/api/viewprod";
const norTabList = ['项目亮点', '合作案例', '合作方式', '适配产品']

Page({
  /**
   * 页面的初始数据
   */
  noPageAuth: true,
  behaviors: [computedBehavior],
  data: {
    IMG_URL,
    senceData,
    isShare: false, //是否是分享出去的
    actNum: 0,
    tabAct: 0,
    showLeft: false, //更多按钮显示的内容
    scrollTop: 0,
    img_version: "0.1.0",
    // 是否授权昵称
    hasUserInfo: false,
    phone: "", //授权手机号
    prodItemVideo: "", //选中的场景下的video的第一个
    tabList: []
  },
  // 计算属性
  computed: {
    showLeftFlag(data) {
      // 1.分享出去
      // 2.点击可以改变状态
      return data.isShare && !data.showLeft;
    },
    prodItemVideo(data) {
      let { senceData, actNum, activeNavIdx } = data;
      // return senceData[actNum]?.productList[activeNavIdx]
      console.log("senceData[actNum]", senceData[actNum]);
      if (actNum == 2) {
        return senceData[actNum]?.noVideoPicture[0];
      }
      return senceData[actNum]?.videoList[0];
    },
    // 授权昵称弹框显示
    authNcknameShow(data) {
      return data.isShare && !data.hasUserInfo;
    },
  },
  /**
   * 生命周期函数--监听页面加载
   *  分享出去,或扫码进来
   */
  onLoad(options) {
    this.getActiveApiFn();
    this.noNeedPhone();

    if (!options || utils.isEmptyObj(options)) return;
    let { actNum, activeNavIdx, isShare, phone } = options;
    if (isShare) {
      wx.hideHomeButton();
    }

    this.setData({
      actNum,
      activeNavIdx,
      isShare,
      phone,
      tabList: senceData[actNum]?.tabList || norTabList
    });
    // 分享描述
    this.shareTitle = `给你分享一个${senceData[actNum]?.tabname},戳我参与~`;

    //分享数据
    let senceItem = senceData[actNum];
    let item = senceData[actNum]?.productList[activeNavIdx];
    console.log("title", item.productName);
    setTimeout(() => {
      // wx.setNavigationBarTitle({
      //   title: item.productName,
      // });
      utils.setNavigationBarTitle(senceData[actNum].tabname);
    }, 100);

    this.shareQuery = `sceneCode=${senceItem?.sceneCode}&productCode=${
      item?.productCode
    }&isShare=${true}&productDesc=${item?.productName}&category=${
      item?.category
    }&actNum=${actNum}&activeNavIdx=${activeNavIdx}&phone=${phone}`;
    this.shareTrackData = {
      sceneCode: senceItem?.sceneCode,
      productCode: item?.productCode,
      productName: item?.productName,
      category: item?.category,
      productDesc: item?.productName,
      // actNum,
      // activeNavIdx,
    };
  },
  //接受跳转的路由参数(小程序页面跳转)
  onRoute(options) {
    if (!options || utils.isEmptyObj(options)) return;
    let { actNum = 0, activeNavIdx = 0, phone = 123 } = options;

    this.setData({
      actNum, //选中的场景下标
      activeNavIdx, //选中的产品
      phone, //联系电话
      tabList: senceData[actNum]?.tabList || norTabList
    });

    // 设置转发标题
    let userInfoObj = wx.$storage.get("userInfo");
    // #服务商账号#取该分享者商户的联系人字段,无则取商户简称,无则取商户全称
    let name = userInfoObj.merchantName;
    this.shareTitle = `${name}给你分享一个${senceData[actNum]?.tabname},戳我参与~`;

    //分享数据
    let item = senceData[actNum]?.productList[activeNavIdx];
    console.log("title2", item.productName);
    setTimeout(() => {
      // wx.setNavigationBarTitle({
      //   title: item.productName,
      // });
      utils.setNavigationBarTitle(senceData[actNum].tabname);
    }, 100);

    this.shareQuery = `sceneCode=${senceData[actNum].sceneCode}&productCode=${
      item?.productCode
    }&isShare=${true}&productDesc=${item?.productName}&category=${
      item?.category
    }&actNum=${actNum}&activeNavIdx=${activeNavIdx}&phone=${phone}`;
    this.shareTrackData = {
      sceneCode: senceData[actNum]?.sceneCode,
      productCode: item?.productCode,
      productName: item?.productName,
      category: item?.category,
      productDesc: item?.productName,
      // actNum,
      // activeNavIdx,
      // apiData,
    };
  },

  // 长按图片
  handleLongPress(e) {
    let { actnum, index } = e.target.dataset
    // 膨胀金活动,合作方式
    if (actnum == 4 && index == 4) {
      wx.downloadFile({
        // 示例 url,并非真实存在
        url: 'https://scrm.glsx.com.cn/frontpdf/expand_agreement.pdf',
        success: function (res) {
          console.log(res)
          const filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            success: function (res) {
              console.log('打开文档成功')
            }
          })
        }
      })
    }
  },
  onClickTab(event) {
    // console.log('event: ', event);s
    let { index } = event.detail;
    // console.log('index: ', index);
    this.setData({
      tabAct: index,
    });
    let { imgTopArr } = this.data;
    wx.pageScrollTo({
      scrollTop: imgTopArr[index] + 5,
    });
  },
  //获取配置url
  async getActiveApiFn() {
    let { code, data } = await getActiveApi();
    if (code == 0) {
      this.setData({
        activeUrl: data,
      });
    }
  },
  onPageScroll: (function () {
    let timer = null;
    return function (e) {
      // console.log("在滚动");
      // 监听页面的滚动开始和结束
      clearTimeout(timer);
      timer = setTimeout(() => {
        let { imgTopArr } = this.data;
        // console.log("滚动结束",imgTopArr,e.scrollTop);
        // wx.$eventbus.emit('evt_page_scroll', { scrollTop: e.scrollTop })
        wx.nextTick(() => {
          let idx;
          if (imgTopArr[0] > e.scrollTop) {
            idx = 0;
          } else if (
            imgTopArr[imgTopArr.length - 1] > e.scrollTop &&
            imgTopArr[0] < e.scrollTop
          ) {
            idx = imgTopArr.findIndex((item) => item > e.scrollTop) - 1;
          } else {
            idx = imgTopArr.length - 1;
          }
          this.setData({
            tabAct: idx,
            scrollTop: e.scrollTop,
          });
        });
      }, 100);
    };
  })(),
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.init();
    this.getImgHeight();
  },
  getImgHeight() {
    let query = wx.createSelectorQuery().in(this);
    let heightArr = [];
    let imgTopArr = [];
    let s = 0;
    let top;
    let { scrollTop } = this.data;
    let _this = this;

    query
      .selectAll(".img-item")
      .boundingClientRect((react) => {
        react.forEach((item, index) => {
          //44 tab的高度
          // console.log('item',item)
          s = item.top - 44 + scrollTop;
          imgTopArr.push(s);
        });
        _this.setData({
          imgTopArr,
        });
      })
      .exec();
    // setTimeout(() => {
    //   console.log('imgTopArr',this.data.imgTopArr)
    // },500)
  },
  phoneCall(e) {
    let { phone } = e.currentTarget.dataset;
    wx.$api.phoneCall(phone);
  },
  async init() {
    try {
      const { data, code, message } = await USERAPI.wxCodeLogin();
      if (code == "0") {
        this.loginToken = data;
      } else this.reLogin(message);
      wx.hideLoading();
    } catch ({ errMsg }) {
      console.error(errMsg);
      this.reLogin();
    }
  },
  /**获取loginToken */
  reLogin(message) {
    wx.showModal({
      content: message || "获取loginToken失败",
      confirmText: "重试",
      confirmColor: "#3666FE",
    }).then(({ confirm }) => {
      if (confirm) this.init();
    });
  },
  // 不需要详情按钮
  noNeedPhone() {
    // 显示后面的内容
    setTimeout(() => {
      this.getImgHeight();
    }, 300);

    this.setData({
      showLeft: true,
    });
  },
  // 获取用户信息(手机号)
  getUserInfo({ detail }) {
    // console.log(detail);

    if (!detail.code) return;
    // this.setBtnLoading()
    USERAPI.getUserProfile(this.loginToken, detail)
      .then((result) => {
        // console.log(result)
        let userInfo = wx.$storage.get("wx_user_info");
        // console.log('userInfo: ', userInfo);
        let { phone } = result;
        // console.log('phone: ', phone);
        wx.$storage.set("wx_user_info", {
          ...userInfo,
          phone,
        });

        // 显示后面的内容
        setTimeout(() => {
          this.getImgHeight();
        }, 300);

        this.setData({
          showLeft: true,
        });
      })
      .catch(({ errMsg }) => {
        wx.$api.showToast(errMsg || "系统异常");
        // this.setBtnLoading(false)
      });
  },
  // 获取用户昵称(昵称头像)
  getUserProfile(e) {
    //关闭授权框
    this.setData({
      hasUserInfo: true,
    });
    let _this = this;
    // 推荐使用 wx.getUserProfile 获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
    wx.getUserProfile({
      desc: "用于完善资料", // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
      success: (res) => {
        // console.log('res: ', res.userInfo.nickName);
        // userInfo.nickName
        this.setData({
          userInfo: res.userInfo,
        });
        let wx_user_info = wx.$storage.get("wx_user_info");
        // console.log('wx_user_info: ', wx_user_info);
        let obj = {
          ...wx_user_info,
          nickName: res.userInfo.nickName,
          avatarUrl: res.userInfo.avatarUrl,
        };
        // console.log('obj',obj)
        wx.$storage.set("wx_user_info", obj);

        _this.onLoad(null);
      },
    });
  },
  // 1.3查看活动详情按钮
  checkActive() {
    // 跳转
    // console.log('跳转')
    let { activeUrl } = this.data;
    wx.$router.jumpH5(activeUrl);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});