mentoring.js 6.25 KB
// packageCrew/pages/mentoring/mentoring.js
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/supervisor-miniprogram/'
import api from '~/api/crew/index'
import indexApi from '~/api/index/index'
import studyApi from '~/api/learningCenter/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
		IMG_URL,
		detailInfo: {
			staffName: '赵惠惠',
			sex: 1,
			staffPhone: '18902640418',
			entryDate: '2025-12-12',
			scene: '延保,轻改,权益'
		},
		detailItems: [{
			label: '手机号码',
			key: 'staffPhone'
		}, {
			label: '入职时间',
			key: 'entryDate',
			width: '290rpx'
		}, {
			label: '归属业务',
			key: 'scene',
			inline: true
		}],
		orderObj: {
			taskContext: '',
			imageList: [],
			merchantName: '',
			courseList: []
		},
	},
	
	navbarReady(e) {
    let navBarHeight = e.detail.height
    this.setData({
      navBarHeight
    })
	},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
		// 任务列表进入
		if (options.id) {
			this.getDetail(options.id)
		}
		// 获取门店列表
		this.getMerchantList()
		// 获取课程列表
		this.getCourseList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

	},
	
	back() {
		wx.navigateBack()
	},
	getMerchantList() {
		indexApi.getSupervisorMerchantList({}).then(res => {
			let { code, data, message } = res
			if (code == 0) {
				this.setData({
					merchantList: data || []
				})
			} else {
				wx.$util.showToast(message)
			}
		})
	},
	getCourseList() {
		let userInfo = wx.$util.getUserInfo()
		let params = {
      pageNum: 1,
      pageSize: 9999,
      staffPhone: userInfo?.phone,
      sortType: 0,
      sortOrder: 'desc'
		}
		studyApi.getRecommendedCourseList(params).then(res => {
			let { code, data, message } = res
			if (code == 0) {
				this.setData({allCourseList: data?.list || []})
			} else {
				wx.$util.showToast(message)
			}
		})
	},
	// 获取带教任务详情
	getDetail(id) {
		indexApi.getTaskDetail({id}).then(res => {
			let { code, data, message } = res
			if (code == 0) {
				data.courseList = data.taskCourseList || [],
				data.imageList = data.taskImageList || []
				this.setData({
					merchantList: data || []
				})
			} else {
				wx.$util.showToast(message)
			}
		})
	},

	chooseSelect(e) {
		let { type } = e.currentTarget.dataset
		let { orderObj, merchantList, allCourseList } = this.data
		if (orderObj.taskStatus == 2) return
		this.setData({
			selectType: type,
			showSelectPopup: true,
			selectOptions: type == 'merchant' ? merchantList : allCourseList,
			selectTitle: type == 'merchant' ? '门店选择' : '课程选择',
			selectMultiple: type != 'merchant',
			labelKey: type == 'merchant' ? 'merchantName' : 'title',
			valueKey: type == 'merchant'? 'merchantId' : 'courseId',
      selectValue: type == 'merchant' ? [orderObj.merchantId] : orderObj.course
		})
	},

	handlePopupConfirm(e) {
		let { values, items } = e.detail
		let { selectType } = this.data
		if (selectType == 'merchant') {
			this.setData({
				[`orderObj.merchantId`]: values[0],
				['orderObj.merchantName']: items[0]?.merchantName
			})
		} else {
			this.setData({
				['orderObj.courseList']: items
			})
		}
		this.hideSelectPopup()
	},

	hideSelectPopup() {
		this.setData({
			showSelectPopup: false
		})
	},

	taskContextInput(e) {
		let val = e.detail.value || ""
    if (val.length > 500) {
      val = val.substring(0, 500)
    }
    this.setData({ 
      'orderObj.taskContext': val, 
      taskContextLength: val.length 
    })
	},

	handleImageUpload() {
		let _this = this
		let { orderObj } = this.data
		wx.chooseImage({
			count: 9 - orderObj.imageList?.length,
			mediaType: ['image'],
			success(res) {
				console.log(res)
				for (const file of res.tempFiles) {
					if (file.size > 10 * 1024 * 1024) {
						return wx.showToast({
							title: '图片上传最大10MB',
							icon: 'none'
						})
					}
				}
				wx.$util.showLoading()
				const uploadImages = async (tempFiles) => {
					try {
						for (const item of tempFiles) {
							const val = item.path
							const imgType = val.split('.')[1]
							const type = imgType === 'unknown' ? 'jpg' : imgType
							const _res = await new Promise((resolve, reject) => {
								wx.getFileSystemManager().readFile({
									filePath: val, // 选择图片返回的相对路径
									encoding: 'base64', // 编码格式
									success: resolve,
									fail: reject
								})
							})
							const img = await uploadImg({
								file: `${type}@${_res.data}`
							})
							const { code, data, message } = img
							if (code == 0) {
								orderObj.imageList.push({ imageUrl: data })
								_this.setData({
									orderObj
								})
							} else {
								wx.$util.showToast(message)
							}
						}
					} catch (error) {
						wx.$util.showToast("上传失败,请重试")
					} finally {
						wx.$util.hideLoading()
					}
				}
				uploadImages(res?.tempFiles)
			}
		})
	},

	previewImage(e) {
    let { url, index } = e.currentTarget.dataset
    wx.previewImage({
      urls: url?.map(val => {return val.imageUrl}),
      current: url[index].imageUrl
    })
  },

	delPhoto(e) {
		let { index } = e.currentTarget.dataset
		let { orderObj } = this.data
		orderObj.imageList.splice(index, 1)
		this.setData({
			'orderObj.imageList': orderObj.imageList
		})
	},

	submit() {
		if (!orderObj.merchantId || !orderObj.courseList.length || !orderObj.taskContext || !orderObj.imageList.length) return wx.$util.showToast('请完成所有必填项')
		let params = {
			merchantId: orderObj.merchantId,
			merchantName: orderObj.merchantName,
			storeManagerPhone: orderObj.phone,
			taskContext: orderObj.taskContext,
			courseList: orderObj.courseList?.map(val => {return {id: val.id}}),
			imageList: orderObj.imageList
		}
		api.submitMentorData(params).then(res => {

		})
	},

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

  },

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

  },

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

  },

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

  },

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

  }
})