ModalToQueueControl.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
let modalQueue = []
import api from '~/api/api.js'
import { navigateToStaffDailyReport } from '~/utils/staffDailyReportNavigation.js'
import {
	isAbnormalFlowLockActive,
	shouldBypassAbnormalFlowLock,
	shouldCallAbnormalFlowShopLockApi
} from '~/utils/abnormalFlowLock.helpers.js'
// import {
//   checkUsedCarStaffIsNew,
//   checkUsedCarStaffDailyReportValid
// } from '~/api/secondHandApi'
let eventRequest = []
const initModal = () => {
	if (getCurrentPages().length > 0) {
		const currentPage = getCurrentPages()[getCurrentPages().length - 1]
		let customModal = currentPage.selectComponent('#customModal')
		return customModal
	}
}
const checkDailyReport = async sceneType => {
	let userInfo = wx.$util.getUserInfo()
	// debugger
	// 判读是否要填写日报
	if (!wx.$hasPermission?.canSelectMultiStore?.(userInfo)) {
		//  非二手车业务场景 判断员工是否已打卡上班
		if (sceneType != 7) eventRequest.push(clockedIntoWork())
		eventRequest.push(
			sceneType
				? // sceneType == 7
				//   ? usedCarStaffDailyReport(userInfo)
				//   :
				equityStaffDailyReport(userInfo)
				: staffDailyReport(userInfo)
		)
	}

	return new Promise(resolve => {
		Promise.all(eventRequest).then(results => {
			// const [reportStatus] = results;
			results = results.slice(-2)
			// console.log(reportStatus, results, results.length, results[results.length - 1], 'reportStatus');
			let resultFlag = results.some(item => item)
			showNextModal()
			console.log(resultFlag, 'resultFlag')
			resolve(resultFlag)
		})
	})
}
const showNextModal = () => {
	let _this = this
	if (modalQueue.length > 0) {
		const modalInfo = modalQueue.pop()
		if (wx.__HAS_SHOW_MODAL) return
		wx.__HAS_SHOW_MODAL = true
		wx.showModal({
			title: modalInfo.title,
			content: modalInfo.content,
			showCancel: modalInfo.showCancel,
			cancelColor: '#292D33',
			confirmText: modalInfo.confirmText,
			confirmColor: '#347BFF',
			success: function (res) {
				wx.__HAS_SHOW_MODAL = false
				// if (res.cancel) _this.showNextModal();
				if (res.cancel) showNextModal()
				modalInfo.eventCallback(res)
			}
		})
	}
}

const staffDailyReport = userInfo => {
	return new Promise((resolve, reject) => {
		api
			.staffDailyReportMerchantValid({
				merchantId: userInfo.shopMerchantId,
				yqMerchantId: userInfo.merchantId,
				staffUuid: userInfo.uuid,
				sceneType: 1
			})
			.then(res => {
				if (res.data) {
					// 获取当前日期
					let currentDate = new Date()
					// 获取上一天的日期
					let previousDate = new Date(currentDate)
					previousDate.setDate(currentDate.getDate() - 1)
					// toISOString 方法会丢失时区 导致与北京时间差八小时
					let hour = previousDate.getHours() + 8
					previousDate.setHours(hour)
					// 格式化日期为"yyyy-mm-dd"
					let formattedDate = previousDate.toISOString().split('T')[0]
					if (wx.__HAS_NOTLOGIN_MODAL) return false
					addModalToQueue(
						'日报提醒',
						'您有未填写的日报,请点击前往。',
						'前往填写',
						false,
						5,
						res => {
							if (res.confirm) {
								navigateToStaffDailyReport({
									userInfo,
									reportDate: formattedDate
								})
							}
						}
					)
					showNextModal()
					resolve(true)
				} else {
					resolve(false)
				}
			})
			.catch(err => {
				resolve()
				console.log(err)
			})
	})
}

const equityStaffDailyReport = userInfo => {
	return new Promise((resolve, reject) => {
		api
			.staffDailyReportMerchantValid({
				merchantId: userInfo?.shopMerchantId,
				yqMerchantId: userInfo?.merchantId,
				staffUuid: userInfo?.uuid,
				sceneType: 0
			})
			.then(res => {
				if (res.data) {
					// 获取当前日期
					let currentDate = new Date()
					// 获取上一天的日期
					let previousDate = new Date(currentDate)
					previousDate.setDate(currentDate.getDate() - 1)
					console.log(previousDate, 'previousDate')
					// toISOString 方法会丢失时区 导致与北京时间差八小时
					let hour = previousDate.getHours() + 8
					previousDate.setHours(hour)
					// 格式化日期为"yyyy-mm-dd"
					let formattedDate = previousDate.toISOString().split('T')[0]
					console.log('formattedDate', formattedDate)
          if (wx.__HAS_NOTLOGIN_MODAL || wx.$hasPermission?.canSelectMultiStore?.(userInfo))
						return false
					addModalToQueue(
						'日报提醒',
						'您有未填写的日报,请点击前往。',
						'前往填写',
						false,
						4,
						res => {
							if (res.confirm) {
								navigateToStaffDailyReport({
									userInfo,
									reportDate: formattedDate
								})
							}
						}
					)
					resolve(true)
				} else {
					resolve(false)
				}
			})
			.catch(err => {
				resolve(false)
			})
	})
}
// const usedCarStaffDailyReport = userInfo => {
//   return new Promise(async (resolve, reject) => {
//     const res = await checkUsedCarStaffIsNew({
//       merchantId: userInfo?.shopMerchantId,
//       yqMerchantId: userInfo?.merchantId,
//       staffUuid: userInfo?.uuid,
//       staffPhone: userInfo?.phone,
//       sceneType: 7
//     })
//     if (!res?.data) {
//       resolve(false)
//     } else {
//       const _data = await checkUsedCarStaffDailyReportValid({
//         sellerMerchantId: userInfo?.shopMerchantId
//       }) //true 已填写 false 未填写
//       if (!_data) {
//         // 获取当前日期
//         let currentDate = new Date()
//         // 获取上一天的日期
//         let previousDate = new Date(currentDate)
//         previousDate.setDate(currentDate.getDate() - 1)
//         // toISOString 方法会丢失时区 导致与北京时间差八小时
//         let hour = previousDate.getHours() + 8
//         previousDate.setHours(hour)
//         // 格式化日期为"yyyy-mm-dd"
//         let formattedDate = previousDate.toISOString().split('T')[0]
//         addModalToQueue(
//           '日报提醒',
//           '您有未填写的日报,请点击前往。',
//           '前往填写',
//           false,
//           4,
//           res => {
//             if (res.confirm) {
//               wx.navigateTo({
//                 url: `/packageUsedCar/pages/indexPage/dailyReport/dailyReport?currentDate=${formattedDate}`
//               })
//             }
//           }
//         )
//         resolve(true)
//       } else resolve(false)
//     }
//   })
// }
const clockedIntoWork = userInfo => {
	return new Promise((resolve, reject) => {
		api
			.clockInConfigPhone({})
			.then(res => {
				let { need, clockIn } = res.data
				// console.log(res, need == 0 && clockIn == 0, "clockInConfigPhone");
				// 已配置未打卡
				if (need == 1 && clockIn == 0) {
					// 有配置并且没有打卡就提示
					addModalToQueue(
						'打卡提醒',
						'您当前企微未打卡,无法录单,请前往企微打卡后再试,如有异议请联系管理员。',
						'知道了',
						false,
						4,
						res => {
							if (res.confirm) {
								// wx.$router.go("tabWorkOrder.staffDaily", {
								//   // reportDate: formattedDate,
								// });
							}
						}
					)
					resolve(true)
				} else {
					resolve(false)
				}
			})
			.catch(err => {
				resolve(false)
			})
	})
}

// 学习中心 开单限制
const checkStudyTask = () => {
	return new Promise((resolve, reject) => {
		// resolve(false)
		// return
		wx.showLoading()
		api.checkStudyTaskAPI().then(res => {
			wx.hideLoading()
			const modal = initModal()
			// res.data.lockOrder = true
			const shouldLock = !!res?.data?.lockOrder && res?.data?.lockReason !== '2'
			if (modal && shouldLock) {
				modal.open({
					title: '未完成学习任务,暂不能开单',
					type: 'permission',
					btnText: '去往学习中心',
					customContent:
						res.data?.lockReason == '0' ? '我的课程-待学习' : '我的任务'
				})
			}
			resolve(shouldLock)
		})
	})
}
/**
 * 异常流水锁单查询(独立于异常订单;接口失败默认放行)
 * @param {Object} [context] - 传给 shouldBypassAbnormalFlowLock(补录 / 白名单路由等)
 * @param {number} [priority=3]
 * @returns {Promise<boolean>} true 表示锁单拦截
 */
const checkAbnormalFlowLock = (context = {}, priority = 3) => {
	return new Promise(resolve => {
		if (shouldBypassAbnormalFlowLock(context)) {
			resolve(false)
			return
		}
		const userInfo = wx.$util.getUserInfo() || {}
		const merchantId =
			userInfo.shopMerchantId ||
			userInfo.default4SStoreInfo?.merchantId ||
			''
		if (!merchantId) {
			resolve(false)
			return
		}
		if (!shouldCallAbnormalFlowShopLockApi()) {
			resolve(false)
			return
		}
		api
			.checkAbnormalFlowShopLock({ merchantId })
			.then(res => {
				if (isAbnormalFlowLockActive(res?.data)) {
					addModalToQueue(
						'异常流水提醒',
						'存在异常流水未处理,请尽快处理!',
						'确认',
						'',
						priority,
						() => {}
					)
					showNextModal()
					resolve(true)
				} else {
					resolve(false)
				}
			})
			.catch(() => {
				resolve(false)
			})
	})
}

/**
 * 异常订单 + 异常流水串联锁单(多数开单入口使用)
 * @param {Object} [context] - 异常流水 bypass 上下文
 * @param {string} [path] - 异常订单弹窗回调路由(保留兼容)
 * @param {number} [priority=3]
 * @returns {Promise<boolean>} true 表示任一锁单生效
 */
const checkOrderLockGate = async (context = {}, path, priority = 3) => {
	const abnormalOrderCheck = await checkAbnormalOrder(path, null, priority)
	if (abnormalOrderCheck) return true
	return checkAbnormalFlowLock(context, priority)
}

// 异常订单锁单查询
const checkAbnormalOrder = (path, callback, priority = 3) => {
	return new Promise((resolve, reject) => {
		api
			.abnormalOrderLock()
			.then(res => {
				// wx.hideLoading()
				if (res.data.lockOrderFlag === 'Y') {
					console.log(res, '22')
					addModalToQueue(
						'异常订单提醒',
						`存在 ${res.data.abnormalOrderNum}条异常订单未处理,请尽快处理!`,
						'确认',
						'',
						priority,
						res => {
							// if (res.confirm) {
							// 	if (callback && typeof callback === 'function') {
							// 		callback()
							// 	} else {
							// 		wx.$router.go(path, {
							// 			// reportDate: formattedDate
							// 		})
							// 	}
							// }
						}
					)
					showNextModal()
					resolve(true) // 锁单
				} else {
					resolve(false) // 不锁单
				}
			})
			.catch(err => {
				// wx.hideLoading()
				resolve(false) // 接口失败默认放行? 或者 reject
			})
	})
}
const addModalToQueue = (
	title,
	content,
	confirmText,
	showCancel,
	priority,
	eventCallback
) => {
	if (modalQueue.findIndex(val => val.content == content) < 0)
		modalQueue.push({
			title,
			content,
			confirmText,
			showCancel,
			priority,
			eventCallback
		})
	modalQueue.sort((a, b) => a.priority - b.priority)
}
export {
	checkDailyReport,
	staffDailyReport,
	addModalToQueue,
	// usedCarStaffDailyReport,
	checkStudyTask,
	checkAbnormalOrder,
	checkAbnormalFlowLock,
	checkOrderLockGate
}