mine.js 3.02 KB
const navStyle = "background-color: transparent;";
import MineApi from '~/api/mineApi/index'
import USERAPI from '~/api/usersApi/index'
import {
  setUserInfo
} from '~/utils/cache/storageApi'
const DEPARTMENTLIST = ['销售部', '售后部', '客服部', '续保部',]
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navStyle,
    showModal: false,
  },
  navbarReady(e) {
    let {
      height
    } = e.detail;
    this.setData({
      user_box_style: `padding-top:${height + 10}px`,
      count_box_style: `padding-top:${height}px`,
    });
  },
  onPageScroll: function (e) {
    this.setData({
      navStyle: e.scrollTop >= 38 ? "" : navStyle,
    });
  },

  toAddEvent() {
    wx.$eventbus.on('SETTING', (res) => {
      this.getStaffInfo()
    })
  },
  toShowBindModal(e) {
    this.setData({
      showModal: e.detail
    })
  },
  toCloseModal() {
    this.setData({
      showModal: false
    })
  },
  async toBindWechat(e) {
    console.log(e);
    if (e.detail.errMsg === "getPhoneNumber:ok") {
      wx.$api.showLoading()
      const { encryptedData, iv, code } = e.detail
      USERAPI.wxCodeLogin().then(res => {
        let { user } = this.data
        let para = {
          code,
          encryptedData,
          iv,
          platformEntrance: 'STORE_ASSISTANT',
          userType: user.userType,
          staffPhone: user.phone,
          merchantId: user.merchantId,
          loginToken: res.data,
          id:user.bussinessId
        }
        MineApi.wechatBindApi(para).then(res => {
          this.getStaffInfo()
        })
      })
    }
  },
  getStaffInfo() {
    const userStorage = wx.$storage.get('userInfo')
    let para = {
      id: userStorage.bussinessId
    }
    MineApi.getStaffDetailApi(para).then(res => {
      wx.hideLoading();
      const temp = this.handleUserInfo(res)
      this.setData({
        user: temp
      })
    })
  },
  handleUserInfo(data) {
    const userStorage = wx.$storage.get('userInfo')
    const storage = {
      merchantId: userStorage.merchantId,
      userType: userStorage.userType,
      merchantName: userStorage.merchantName,
      phone: userStorage.phone,
    }
    const { id, staffPhone, phone, department, weChatNickName, weChatAvatarUrl,weChatPhone, weChatOpenId, staffName,merchantFileResponse } = data
    let avatar = ''
    if(merchantFileResponse){
      avatar = merchantFileResponse.fileUrl
    }
    const USERINFO = {
      headImgUrl: avatar || weChatAvatarUrl,
      nickName: staffName,
      seriseName: DEPARTMENTLIST[department - 1],
      brandName: weChatNickName || weChatPhone || staffPhone,
      bindStatus: weChatOpenId ? 1 : 0,
      bussinessId: id,
      ...storage
    }
    // 更新缓存userinfo里面员工的姓名和员工部门
    userStorage.name = staffName
    userStorage.department = department
    setUserInfo(userStorage)
    return USERINFO
  },
  onLoad: function (options) {
    this.getStaffInfo()
    this.toAddEvent()
  },
  onPullDownRefresh: function () {
    this.getStaffInfo()
    wx.$eventbus.emit('REFRESH_WITHDRALWAL')
  },

})