index.js 6.95 KB
// pages/customer/index.js
var mta = require('mta-wechat-analysis')
import App from '../../utils/api';
import util from '../../utils/util'

const {regeneratorRuntime} = global
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadHidden: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  islove: '',//用户在哪端
  enter_params: '',//商户入口参数
  isInvitation: false,//客户经理
  async onLoad(opt) {
    console.log('onLoad', opt, mta);
    //转化场景值到opt
    this.selectComponent("#network").debug(opt, 'opt');
    if (opt.scene) {//扫码进入
      let query = decodeURIComponent(opt.scene);
      query.split('&').forEach(q => {
        let _q = q.split('=');
        opt[_q[0]] = _q[1];
      })
      this.selectComponent("#network").debug(opt, 'scene');
    }
    if (opt.act == 'tobook') {//消息跳入运营处理预订
      delete opt.act
      return this.redirectTo({url: util.http_build_query(opt, '/pack-book/pages/book/b_index')});
    }
    mta.Page.init({
      "appID": "500667280",
      "eventID": "500667287",
      "statPullDownFresh": true,
      "statShareApp": true,
      "statReachBottom": true,
      "lauchOpts": opt, //渠道分析
    });
    opt.openid = await this.getOpenid();
    if (!opt.openid) {
      console.log('App.Orders.CheckOrder no openid');
      this.selectComponent("#network").refresh();
      return;
    }
	if(opt.act=='coupon'){
		wx.redirectTo({
			url:'../../me/coupon'
		})
	}
	if(opt.act=='order'){
		wx.redirectTo({
			url:'../../me/list'
		})
	}
    if (opt.islove) {
      util.cache('islove', opt.islove);
      this.islove = opt.islove
    } else {
      this.islove = await util.cache('islove')
    }
    this.usi = {data: {seller: {}, invitation: {}}}
    try {
      this.usi = await App.Invitation.UserIdentity({openid: opt.openid});
    } catch (e) {
      console.log('App.Invitation.UserIdentity no usi');
    }
    this.options = opt
    setTimeout(() => {
      this.initRun();
    }, 2000)
  },
  async initRun() {
    let opt = this.options
    let usi=this.usi
    if (usi.data.seller.flag) {//商户
      //获得商户数据
      //this.enter_params=await this.SellerForOpenid(opt.openid);
      this.enter_params = usi.data.data;
    }
    if (usi.data.invitation.flag) {//已是客户经理
      this.isInvitation = true;
    }
    //console.log('this.enter_params',this.enter_params)
    let url = '/pages/reserve/index';
    if (opt.id > 0 || opt.sms_code) {
      if (opt.invite_name) {
        let invite_names = wx.getStorageSync('invite_names') || {};
        invite_names[opt.id] = opt.invite_name;
        wx.setStorage({
          key: 'invite_names',
          data: invite_names
        });
      }
      let param = {
        openid: opt.openid,
      }
      if (opt.sms_code) {
        param.code = opt.sms_code;
      } else {
        param.rid = opt.id
      }

      App.Reserve.Detail(param).then(res => {
        console.log(res.data.note_openid, this.enter_params.seller_openid)
        if (this.enter_params) {//商户
          //if(res.data.sid==this.enter_params.seller_id && opt.hasSellerShare==1){//不是客户端分享,且是同一个店的
          if (res.data.note_openid == this.enter_params.seller_openid && opt.hasSellerShare == 1) {//不是客户端分享,且是记录人
            util.cache('enter_params', this.enter_params)
            //url = '/pages/book/b_detail';
            url = '/pack-book/pages/book/b_invitation';
          }
        }
        if (url != '/pack-book/pages/book/b_invitation') {
          //设置关系
          App.Reserve.SetRelaOpenid({
            rid: res.data.id,
            openid: opt.openid,
            fromid: opt.fromid ? opt.fromid : ''
          })
          if (!res.data.openid) {//无预订人
            url = '/pages/customer/detail_owner';
            if (opt.hasInvitation == 1) {
              url = '/pages/customer/detail_owner_invitation';
            }
          } else {
            url = '/pages/customer/detail_customer';
            if (opt.hasInvitation == 1) {
              url = '/pages/customer/detail_invitation';
            }
            if (res.data.openid == opt.openid) {
              url = '/pages/customer/detail_owner';
              if (opt.hasInvitation == 1) {
                url = '/pages/customer/detail_owner_invitation';
              }
            }
          }
        }
        if (!opt.tplid || typeof opt.tplid == 'undefined' || opt.tplid == 'undefined') {
          opt.tplid = 1
        }
        url += '?id=' + res.data.id + '&tplid=' + opt.tplid
        console.log('wx.redirectTo', url, res);
        this.redirectTo({url: url});
      }).catch(res => {
        this.redirectTo({url: url});
      })
    } else {
      if (this.enter_params && this.islove != 'customer' && this.islove != 'invitation') {
        //商户且未手动切换到顾客端或客户经理
        wx.reLaunch({
          url: util.http_build_query(this.enter_params, '/pack-book/pages/book/b_index')
        })
        return;
      }
      if (this.isInvitation && this.islove != 'customer' && this.islove != 'book') {
        //客户经理且未手动切换到顾客端或商户
        wx.reLaunch({
          url: '/pages/manager-invitation/review'
        })
        return;
      }
      console.log('url', url);
      this.redirectTo({url: url});
    }
  },
  redirectTo(red) {
    console.log('this.redirectTo', red);
    if (red.url.indexOf('me/list') > 0
      || red.url.indexOf('reserve/search') > 0
      || red.url.indexOf('reserve/index') > 0) {
      wx.switchTab(red);
    } else {
      wx.redirectTo(red);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  async getOpenid() {
    let openid = await getApp().getOpenid();
    return openid;
  },
  async SellerForOpenid(openid) {
    let enter_params = '';
    let res = await App.Reserve.SellerForOpenid({openid: openid});
    if (res.data && res.data.seller_id) {
      enter_params = res.data;
    }
    if (enter_params) {
      return enter_params;
    }
    // if(enter_params) {
    //   console.log('seller_enter_params', enter_params)
    //   wx.reLaunch({
    //     url:util.http_build_query(enter_params,'/pages/book/b_index')
    //   })
    //   return true
    // }
    return false
  },
  networkEvent(e) {
    console.log('networkEvent', e.detail);
    if (!e.detail.hasWork) {
      this.setData({
        loadHidden: true,
      })
    }

  },
})