index.js 16.3 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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
import api from '~/api/api.js'
import { envIndex } from '~/config'
import { removeSystemStorage } from '~/utils/cache/storageApi'
import util from '~/utils/util.js'

// console.log(wx)globalData
/** 判断是不是超管角色 */
export const checkYqdSuper = (userInfo, globalData) => {
  console.log(userInfo, 'checkYqdSuper')
  let merchantAccountType = userInfo.merchantAccountList.every(item => {
    return item == 6 || item == 8
  })
  // 解决传入app 在初始化获取不到的bug
  // console.log(userInfo, 'userInfo')
  globalData.isYqdSuper =
    userInfo.userType != 2 && !userInfo.isSuper && merchantAccountType
  globalData.isStaff =
    userInfo.userType == 2 && !userInfo.isSuper && merchantAccountType
  const loginRole = userInfo.loginRole
  if (loginRole) {
    globalData.isYqdSuper = [2, 3].includes(loginRole)
    globalData.isStaff = loginRole == 4
    globalData.isSuper = loginRole == 1
  }
  // console.log(globalData.isStaff)
}
// 获取园区店信息
export const getYQInfo = userInfo => {
  const app = getApp()
  return new Promise((resolve, reject) => {
    // 如果有 直接返回
    if (app.globalData.yqdInfo.merchantRange) {
      return resolve(app.globalData.yqdInfo)
    }
    // getUserInfo
    // 判断是否是多账号用户
    let subIndex = 0

    // console.log(!userInfo.isSuper && userInfo.merchantType && userInfo.userType == 2);
    // console.log(userInfo.merchantId,'userInfo.merchantId');
    if (!userInfo.loginRole || userInfo.loginRole == 4) {
      // console.log(userInfo.merchantId,'userInfo.merchantId');
      app.globalData.yqdInfo = {
        subIndex,
        merchantRange: [
          {
            merchantName: userInfo.merchantName,
            merchantId: userInfo.merchantId
          }
        ]
      }
      // this.setData({
      //   merchantRange: [{ merchantName: userInfo.merchantName }]
      // })
      // return
      resolve(app.globalData.yqdInfo)
    } else {
      let all = {
        merchantName: '全部'
      }
      api
        .getYQList()
        .then(res => {
          // console.log(res)
          let { code, data } = res
          if (code == 0) {
            let arr = []
            if (data.length > 1) {
              if (userInfo.merchantId != userInfo.subLoginUser.merchantId) {
                data.map((e, index) => {
                  if (e.merchantId == userInfo.subLoginUser.merchantId)
                    subIndex = index + 1
                })
              }
              arr = [all, ...data]
            } else if (data.length == 1) {
              arr = [...data]
            } else {
              arr = [
                {
                  merchantName: userInfo.merchantName,
                  merchantId: userInfo.merchantId
                }
              ]
            }
            app.globalData.yqdInfo = {
              merchantRange: arr,
              subIndex
            }
            // this.checkSeverType(userInfo)
            resolve(app.globalData.yqdInfo)
          }
        })
        .catch(error => {
          console.log(error)
        })
    }
  })
}

export const getYQorderInfo = () => {
  const app = getApp()
  return new Promise((resolve, reject) => {
    // 如果有 直接返回
    if (app.globalData.YQorderSetting) {
      return resolve(app.globalData.YQorderSetting)
    } else {
      let userInfo = wx.$util.getUserInfo()
      if (!userInfo) this.login(true)
      let { merchantId, merchantName } = userInfo.subLoginUser
      wx.$util.showLoading()
      api
        .getMerchantRecordingSettings({
          merchantId,
          merchantName
        })
        .then(res => {
          // console.log(res)
          wx.$util.hideLoading()
          let { code, data } = res
          if (code == 0) {
            // debugger
            // console.log(typeof data.yqPhoneConfig, 'data.yqPhoneConfig')
            // 过滤园区店是否需要屏蔽手机号选项
            let filterYqPhoneConfig = JSON.parse(data.yqPhoneConfig)
            // console.log(filterYqPhoneConfig)
            data.yqPhoneConfig =
              filterYqPhoneConfig?.find(item => item.yqMerchantId == merchantId)
                ?.shopMerchantPhoneList || false
            // console.log(data)
            app.globalData.YQorderSetting = data
            // this.checkSeverType(userInfo)
            resolve(app.globalData.YQorderSetting)
          }
        })
        .catch(error => {
          console.log(error)
        })
    }
  })
}

export const setConfigInfo = globalData => {
  const app = getApp()
  return new Promise((resolve, reject) => {
    // 如果有 直接返回
    // console.log(app.globalData.YQorderSetting, 'app.globalData.YQorderSetting')
    // console.log('app.globalData.YQorderSetting', app.globalData.YQorderSetting)
    let { configInfo } = globalData
    const target = ['develop', 'develop', 'test', 'production', 'beta']
    if (configInfo) {
      return resolve(configInfo)
    } else {
      wx.request({
        // url: `${domain}${path}?method=${method}`,
        url: `https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=dj-car-boutique-work-wechat-miniapp&env=${target[envIndex]}`,
        // data,
        method: 'GET',
        // header,
        dataType: 'json',
        responseType: 'text',
        success(response) {
          app.globalData.configInfo = response.data
          resolve(response.data)
        },
        fail(error) {
          console.log(error, 'error')
        },
        complete() {
          // wx.stopPullDownRefresh()
          // wx.hideNavigationBarLoading()
        }
      })
    }
  })
}

export const getBusinessSchoolScene = () => {
  const app = getApp()
  return new Promise((resolve, reject) => {
    // 如果有 直接返回
    if (
      app.globalData.businessSchoolScene != '' &&
      app.globalData.businessSchoolScene != null
    ) {
      // debugger
      return resolve(app.globalData.businessSchoolScene)
    } else {
      // let userInfo = wx.$util.getUserInfo()
      // if (!userInfo) this.login(true)
      // let { merchantId, merchantName } = userInfo.subLoginUser
      // wx.$util.showLoading()
      // debugger
      api
        .isBusinessSchoolEnableCurrentUser({
          // merchantId,
          // merchantName
        })
        .then(res => {
          // console.log(res)
          // wx.$util.hideLoading()
          let { code, data } = res
          if (code == 0) {
            app.globalData.businessSchoolScene = data
            // this.checkSeverType(userInfo)
            resolve(app.globalData.businessSchoolScene)
          }
        })
        .catch(error => {
          console.log(error)
        })
    }
  })
}

// 扫码进来的处理
export const checkScene = options => {
  // if (getApp().globalData.isChoosingMedia) return

  // let query = options.query
  let { query } = options
  if (query) {
    // 解码
    for (const key in query) {
      query[key] = decodeURIComponent(query[key])
    }
    options.query = query
  }
  console.log(query, 'query')
  let userInfo = wx.$util.getStorageSync('dj_boutique_user_info')
  // 交车目标/日报
  if (query && query.reportDate && query.hasOwnProperty('approvedSource')) {
    const app = getApp()
    // console.log(app, 'app');
    // app.globalData.yqdInfo = {}
    // app.globalData.YQorderSetting = ''
    // app.globalData.YQList = ''
    // app.globalData.businessSchoolScene = ''
    app.globalData = {}
    util.removeStorage()
    removeSystemStorage()
    // wx.$relogin = relogin
  }
  // 判断对象是否满足任意一个key
  // console.log(query, ' query')
  if (query && (query.scene || query.merchantId || query.type || query.page)) {
    // 如果是跳转员工日报页面,需先情况之前的用户登录信息
    // h5跳转小程序 路径处理
    if (query.page) {
      const pagePath = {
        createArchives: 'tabWorkOrder/pages/createArchives/createArchives',
        createOrder: 'tabWorkOrder/pages/createOrder/createOrder',
        followUpOrder: 'tabDate/pages/detail',
        staffDaily: 'tabWorkOrder/pages/staffDaily/staffDaily',
        playVideo:
          'packageEquity/pages/trainingMaterials/pages/playVideo/index',
        upgradeStaffDaily:
          'tabWorkOrder/pages/upgradeStaffDaily/upgradeStaffDaily',
        deliveryTarget: 'packageEquity/pages/deliveryTarget/deliveryTarget',
        'aiDetail': 'packageEquity/pages/aiExcercise/ai/ai',
        'tabDateIndex': 'pages/tabDateIndex/pages/tabDateIndex',
        'tabDateIndex': 'pages/tabDateIndex/pages/tabDateIndex',
        'h5AuthPhone': 'pages/h5AuthPhone/h5AuthPhone',
        'mentorShipManage': 'tabWorkOrder/pages/mentorshipManage/mentorshipManage',
        'refundApply': 'tabWorkOrder/pages/refundApply/refundApply',
        'recruitDetail': 'packageEquity/pages/recruitManage/recruitDetail/recruitDetail',
        'performanceRate': 'packageWorkspace/pages/performanceRating/rateList/retaList',
        'validReferralCount': 'packageWorkspace/pages/validReferralCount/referralCountList/referralCountList',
				'orderApprovalDetail': '/packageWorkspace/pages/orderApprovalManage/orderApprovalDetail/orderApprovalDetail',
				'supervisorIndex': 'pages/tab/index/index'
      }
      options.path = pagePath[query.page]
    }
    console.log(options, query, 'options')
    console.log(userInfo, 'userInfo')
    wx.$SCENE = options
    // 跳转登录页就去掉这个跳转逻辑
    if (options.path == 'pages/wxLogin/wxLogin') {
      wx.$SCENE = null
    }
    if (query.merchantId && !query.reportDate && !query.title) {
      // &&options.patch == 'tabIndex/pages/index/index'

      // 需要已登录并且是当前门店的 不是就重新登录
      if (
        userInfo?.subLoginUser?.merchantId &&
        userInfo?.subLoginUser?.merchantId != query.merchantId
      ) {
        wx.$SCENE = null
        // return this.login(true)
        // return true // 重新登录
        return '当前登录园区不一致,请重新登录。'
      }
    }

    console.log(options.path, 'options.path')
    // wx.$SCENE = options
    if (
      !userInfo &&
      ![
        'pages/accountItems/accountItems',
        'pages/wxLogin/wxLogin',
        'tabWorkOrder/pages/staffDaily/staffDaily',
        'tabWorkOrder/pages/upgradeStaffDaily/upgradeStaffDaily',
        'packageEquity/pages/trainingMaterials/pages/playVideo/index',
        'packageEquity/pages/deliveryTarget/deliveryTarget',
        'pages/h5AuthPhone/h5AuthPhone'
      ].includes(options.path)
    ) {
      // if (!userInfo) {
      // console.log(userInfo);
      console.log('未登录,请登录。')
      return '未登录,请登录。'
    }
  }
}

/** 查询pos订单是否已支付成功 */
export const setWebSocket = () => {
  api.getWsUrl().then(res => {
    console.log(res.data)
    if (res.data) {
      wx.connectSocket({
        url: `wss://${res.data}`,
        success(_res_) {
          console.log('socket连接成功')
          wx.onSocketMessage(data => {
            let thisPage = wx.$router.getPageInstance()
            console.log('socket', data)
            let _data_ = JSON.parse(data.data)
            if (
              thisPage.route == 'tabWorkOrder/pages/confirmOrder/confirmOrder'
            ) {
              console.log(thisPage.data.orderInfo.orderCode, _data_.content)
              if (thisPage.data.orderInfo.orderCode == _data_.content) {
                console.log(_data_.type)
                if (_data_.type == 'PAY_SUCCESS') {
                  console.log('支付成功')
                  wx.$router.redirectTo('orderDetail', {
                    orderCode: _data_.content
                  })
                }
              }
            }
          })
        }
      })
    }
  })
}

/** 获取园区店下的4S店 */
export const get4SStoreList = (options, isSupervision, noScene) => {
  // isSupervision是否是区域运营/超管/飞行督导 noScene登录未选场景时
  return new Promise((resolve, reject) => {
    wx.$util.showLoading()
    let all = { merchantName: '全部' }
    let event =
      noScene || !isSupervision
        ? 'getSceneMerchantRefStoreList'
        : 'getCurShopList'
    if (event == 'getCurShopList') {
      options.sceneType = options.scene
    }
    // console.log(event, isSupervision, 'get4SStoreList');
    // let event = 'getSceneMerchantRefStoreList'
    api[event](options)
      .then(res => {
        wx.$util.hideLoading()
        let { code, data, message } = res
        if (code == 0) {
          // debugger
          // console.log(data);
          // getCurShopList 接口数据结构不一致,要修改
          let resolveData =
            (event == 'getCurShopList' ? data?.list : data) || []
          if (isSupervision) resolveData = [all, ...resolveData]
          // console.log(resolveData, 'resolveData');
          resolve(resolveData)
        } else {
          wx.$util.showToast(message)
        }
      })
      .catch(error => {
        console.log(error)
      })
  })
}

/** 获取门店下的员工(推荐人) */
export const getStaffListByMerchant = options => {
  return new Promise((resolve, reject) => {
    const app = getApp()
    api
      .getStaffListByMerchantAPI(options)
      .then(res => {
        let { code, data } = res
        if (code == 0) {
          app.globalData.staffListRange = data
          resolve(app.globalData.staffListRange)
        }
      })
      .catch(error => {
        console.log(error)
      })
  })
}

/** 获取精品升级场景 太阳膜类商品部位数据 */
export const getPositionList = options => {
  return new Promise((resolve, reject) => {
    const app = getApp()
    // 如果有 直接返回
    if (app.globalData.positionTypeList) {
      return resolve(app.globalData.positionTypeList)
    } else {
      api.getPositionList({}).then(res => {
        let { code, data } = res
        if (code == 0) {
          // const namReplaceobj = {
          //   2: '前侧窗玻璃',
          //   3: '后侧窗玻璃',
          //   4: '后挡'
          // }
          // data = data.map(item => {
          //   if (namReplaceobj[item.positionValue]) {
          //     item.positionName = namReplaceobj[item.positionValue]
          //   }
          //   return item;
          // })
          app.globalData.positionTypeList = data
        }
        resolve(app.globalData.positionTypeList)
      })
    }
  })
}

/** 获取4s店关联场景数据 */
export const getShopSceneConfig = options => {
  return new Promise((resolve, reject) => {
    api.getYqMerchantSceneConfigAPI(options).then(res => {
      let { data, code, message } = res
      if (code == 0) {
        resolve(data || [])
      } else {
        wx.$util.showToast(message)
      }
    })
  })
}

/** 获取当前园区列表 */
export const getYQList = () => {
  // debugger
  return new Promise((resolve, reject) => {
    const app = getApp()
    // console.log(app.globalData.YQList, 'app.globalData.YQList')
    // 如果有 直接返回
    if (app.globalData.YQList) {
      return resolve(app.globalData.YQList)
    } else {
      api.getYQList({}).then(res => {
        let { code, data } = res
        if (code == 0) {
          app.globalData.YQList = data
        }
        resolve(app.globalData.YQList)
      })
    }
  })
}

/** 获取服务商是否使用膜卷号 */
export const getServiceFilmRollSetting = yqMerchantId => {
  const app = getApp()
  return new Promise((resolve, reject) => {
    wx.$util.showLoading()
    api
      .getServiceFilmRollSetting({
        merchantId: yqMerchantId
      })
      .then(res => {
        wx.$util.hideLoading()
        let { code, data } = res
        if (code == 0) {
          app.globalData.serviceFilmRollSetting = data
          resolve(app.globalData.serviceFilmRollSetting)
        }
      })
      .catch(error => {
        console.log(error)
      })
  })
}

/** 添加登录日志记录 */
export const loginLogAdd = options => {
  wx.getSystemInfo({
    success(res) {
      let {
        // fontSizeSetting,
        // statusBarHeight,
        // pixelRatio
        screenWidth
      } = res
      // 判断是否是华为平板设备
      // && pixelRatio >= 2
      let isPad = screenWidth >= 600
      options.loginDeviceType = isPad ? 1 : 2 // 登录设备类型(1.pad,2手机)
      options.loginSource = 3 // 登录来源(1.嘀加门店管理助手小程序,默认)
      // "登录方式(1账号密码登录,2.手机号授权,3验证码)"
      api
        .loginLogAdd(options)
        .then(result => {})
        .catch(err => {})
    }
  })
}