index.js 11.2 KB
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
// 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的第一个
    activeUrl: '', // 1.3场景才有的活动配置
    sharephone: '', //主要微信用户,服务商,
    tabList: []
  },
  // 计算属性
  computed: {
    showLeftFlag(data) {
      // 1.分享出去
      // 2.点击可以改变状态
      return data.isShare && !data.showLeft
    },
    prodItemVideo(data) {
      let { senceData, actNum, activeNavIdx } = data
      // return senceData[actNum]?.productList[activeNavIdx]
      if (actNum == 2) {
        return senceData[actNum]?.noVideoPicture[0]
      }
      return senceData[actNum]?.videoList[activeNavIdx]
    },
    // 授权昵称弹框显示
    authNcknameShow(data) {
      return data.isShare && !data.hasUserInfo
    }

  },
  /**
   * 生命周期函数--监听页面加载
   *  分享出去,或扫码进来
   */
  onLoad(options) {
    this.getActiveApiFn()
    // 是否已经查看详情
    this.isgetAuthphone()

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

    this.setData({
      actNum,
      activeNavIdx,
      isShare,
      sharephone,
      tabList: senceData[actNum]?.productList[activeNavIdx].tabList || norTabList
    });

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

      utils.setNavigationBarTitle(item.productName)
    }, 100)

    let wx_user_info = wx.$storage.get('wx_user_info'),
      shareOpenId = wx_user_info.openId || ''
    // console.log('shareOpenId: ', shareOpenId);
    // &shareOpenId=${shareOpenId}

    this.shareQuery = `sceneCode=${senceItem?.sceneCode}&productCode=${item?.productCode}&isShare=${true}&productDesc=${item?.productName}&category=${item?.category}&actNum=${actNum}&activeNavIdx=${activeNavIdx}&sharephone=${sharephone}`
    this.shareTrackData = {
      sceneCode: senceItem?.sceneCode,
      productCode: item?.productCode,
      productName: item?.productName,
      category: item?.category,
      productDesc: item?.productName,
      shareOpenId: shareOpenId
      // actNum,
      // activeNavIdx,
    }

  },
  //接受跳转的路由参数(小程序页面跳转)
  onRoute(options) {
    if (!options || utils.isEmptyObj(options)) return
    let { actNum = 0, activeNavIdx = 0, phone = 123 } = options;

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

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



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

      utils.setNavigationBarTitle(item.productName)
    }, 100)

    //分享人openId
    let wx_user_info = wx.$storage.get('wx_user_info'),
      shareOpenId = wx_user_info.openId || ''
    // &shareOpenId=${shareOpenId}

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

  //获取配置url
  async getActiveApiFn() {
    let { code, data } = await getActiveApi()
    if (code == 0) {
      this.setData({
        activeUrl: data
      })
    }
  },
  // 是否已经查看详情
  isgetAuthphone() {
    let authphone = wx.$storage.get("authphone");
    if (authphone) {
      // 显示后面的内容
      setTimeout(() => {
        this.getImgHeight()
      }, 300)

      this.setData({
        showLeft: true
      })
      //触发浏览埋点
      // this.onLoad(null)
    }

  },

  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,
    });
  },
  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()
    })
  },
  // 获取用户信息(手机号)
  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;
        wx.$storage.set('authphone', phone)
        // console.log('phone: ', phone);
        wx.$storage.set("wx_user_info", {
          ...userInfo,
          phone,
        });

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

        this.setData({
          showLeft: true
        })
        //触发浏览埋点
        this.onLoad(null)


      })
      .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);

  },

  // 长按图片
  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('打开文档成功')
            }
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() { },

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
  /**
   * 浏览埋点
   */
  onShareTimeline() { },
  onViewTrack: true
});