scenarioDetail.js 10.7 KB
// 场景详情
import { behavior as computedBehavior } from "~/utils/lib/miniprogram-computed";
import { IMG_URL } from "~/utils/constantData.js";
// import { senceData, productData } from '~/pages/tab/index/constant/senceData'
import { scenceList } from '~/pages/tab/index/constant/data'
import USERAPI from "~/api/usersApi/index";
import utils from "~/utils/util.js";
import { getActiveApi } from "~/api/viewprod";

Page({
  /**
   * 页面的初始数据
   */
  noPageAuth: true,
  behaviors: [computedBehavior],
  data: {
    IMG_URL,
    // senceData,
    // productData,
    proInfo: {},
    isShare: false, //是否是分享出去的
    actNum: 0,
    tabAct: 0,
    showLeft: false, //更多按钮显示的内容
    scrollTop: 0,
    img_version: "0.1.0",
    // 是否授权昵称
    hasUserInfo: false,
    phone: "", //授权手机号
    prodItemVideo: "", //选中的场景下的video的第一个
  },
  // 计算属性
  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();
    }

    let sceneObj = scenceList.find(item => item.sceneid == actNum)
    let proInfo = sceneObj?.productList[activeNavIdx]

    console.log(sceneObj, 'sceneObj---')
    console.log(proInfo, 'proInfo---')

    this.setData({
      actNum, //选中的场景下标/sceneid
      activeNavIdx, //选中的产品
      isShare,
      phone //联系电话
    });

    //分享数据
    // const senceList = this.data.productData[this.data.actNum].filter(item => item.name != '全部')
    // for (let i = 0; i < senceList.length; i++) {
    //   let item = senceList[i]
    //   let proIdList = senceList[i]?.productList.map(k => k.id)
    //   if (proIdList.includes(activeNavIdx)) {
    //     let proInfo = item.productList.find(k => k.id == activeNavIdx)
    //     this.setData({
    //       proInfo,
    //       sceneCode: item.sceneCode || ''
    //     })
    //     break
    //   }
    // }

    // 分享描述
    this.shareTitle = `给你分享一个${sceneObj?.name},戳我参与~`;

    //分享数据
    // let item = sceneObj?.productList[activeNavIdx];
    // console.log("title", item.productName);
    setTimeout(() => {
      utils.setNavigationBarTitle(sceneObj.name);
    }, 100);

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

    let sceneObj = scenceList.find(item => item.sceneid == actNum)
    let proInfo = sceneObj?.productList[activeNavIdx]

    console.log(sceneObj, 'sceneObj---')
    console.log(proInfo, 'proInfo---')

    this.setData({
      actNum, //选中的场景下标/sceneid
      activeNavIdx, //选中的产品
      phone, //联系电话
    });
    
    // const senceList = this.data.productData[this.data.actNum].filter(item => item.name != '全部')
    // for (let i = 0; i < senceList.length; i++) {
    //   let item = senceList[i]
    //   console.log(item)
    //   let proIdList = senceList[i]?.productList.map(k => k.id)
    //   if (proIdList.includes(+activeNavIdx)) {
    //     let proInfo = item.productList.find(k => k.id == activeNavIdx)
    //     this.setData({
    //       proInfo,
    //       sceneCode: item.sceneCode || ''
    //     })
    //     break
    //   }
    // }

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

    //分享数据
    setTimeout(() => {
      utils.setNavigationBarTitle(sceneObj?.name);
    }, 100);

    this.shareQuery = `sceneCode=${sceneObj.sceneCode}&productCode=${
      proInfo?.productCode
    }&isShare=${true}&productDesc=${proInfo?.productName}&category=${
      proInfo?.category
    }&actNum=${actNum}&activeNavIdx=${activeNavIdx}&phone=${phone}`;
    this.shareTrackData = {
      sceneCode: sceneObj?.sceneCode,
      productCode: proInfo?.productCode,
      productName: proInfo?.productName,
      category: proInfo?.category,
      productDesc: proInfo?.productName,
      // actNum,
      // activeNavIdx,
      // apiData,
    };
    console.log(this.shareQuery, 'shareQuery')
    console.log(this.shareTrackData, 'this.shareTrackData-----------')
  },
  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) {
      // 监听页面的滚动开始和结束
      clearTimeout(timer);
      timer = setTimeout(() => {
        let proDetail = this.selectComponent("#pro-detail")
        proDetail.pageSroll(e)
      }, 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() {},
});