mentoring.js 7.23 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'
import { uploadImg } from '~/api/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
		IMG_URL,
		navBarHeight: 84,
		detailInfo: {},
		detailItems: [{
			label: '手机号码',
			key: 'storeManagerPhone'
		}, {
			label: '入职时间',
			key: 'entryDate',
			width: '290rpx'
		}, {
			label: '归属业务',
			key: 'business',
			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)
		} else {
			this.setData({
				'orderObj.storeManagerName': options.storeManagerName,
				'orderObj.storeManagerPhone': options.storeManagerPhone,
				'orderObj.entryDate': options.entryDate,
				'orderObj.business': options.business
			})
		}
		// 获取门店列表
		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)
			}
		})
	},
	filterBusiness(data) {
		let names = ['', '延保', '轻改', '权益']
		if (!data) return '-'
		let bizArray = data?.split(',')?.map(item => item.trim()).filter(item => item !== '')
		let result = bizArray.map(index => names[parseInt(index)]).filter(name => name !== '').join(',')
		return result
	},
	// 获取带教任务详情
	getDetail(id) {
		indexApi.getTaskDetail({id}).then(res => {
			let { code, data, message } = res
			if (code == 0) {
				data.courseList = data.taskCourseList || [],
				data.imageList = data.taskImageList || []
				data.business = this.filterBusiness(data.business)
				let taskContextLength = data?.taskContext.length
				this.setData({
					taskContextLength
				}, () => {
					this.setData({
						orderObj: data || {}
					})
				})
			} else {
				wx.$util.showToast(message)
			}
		})
	},

	chooseSelect(e) {
		let { type } = e.currentTarget.dataset
		let { orderObj, merchantList, allCourseList } = this.data
		if (orderObj.taskStatus == 2) return
		console.log(orderObj.courseList)
		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.courseList?.map(val => {return val.courseId}),
			selectValues: type == 'merchant' ? [orderObj.merchantId] : orderObj.courseList?.map(val => {return val.courseId})
		})
	},

	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 > 1000) {
      val = val.substring(0, 1000)
    }
    this.setData({ 
      'orderObj.taskContext': val, 
      taskContextLength: val.length 
    })
	},

	handleImageUpload() {
		let _this = this
		let { orderObj } = _this.data
		wx.chooseImage({
			count: 1,
			sourceType: ['camera'],
      success(res) {
				let [tempFiles] = res.tempFilePaths
				for (const file of res.tempFiles) {
					if (file.size > 10 * 1024 * 1024) {
						return wx.showToast({
							title: '图片上传最大10MB',
							icon: 'none'
						})
					}
				}
        let imgType = tempFiles.split('.')[1]
        let type = imgType == 'unknown' ? 'jpg' : imgType
        wx.getFileSystemManager().readFile({
          filePath: tempFiles, //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success(_res) {
            wx.$util.showLoading('上传中...')
            uploadImg({
              file: `${type}@${_res.data}`
            }).then(img => {
              wx.$util.hideLoading()
              let { code, data, message } = img
              if (code == 0) {
                orderObj.imageList.push({imageUrl: data})
                _this.setData({
                  orderObj
                })
              } else {
                wx.$util.showToast(message)
              }
            })
          }
        })
      }
    })
	},

	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() {
		let { orderObj } = this.data
		if (!orderObj.merchantId || !orderObj.courseList.length || !orderObj.taskContext || !orderObj.imageList.length) return wx.$util.showToast('请完成所有必填项')
		let params = {
			merchantId: orderObj.merchantId,
			merchantName: orderObj.merchantName,
			storeManagerName: orderObj.storeManagerName,
			storeManagerPhone: orderObj.storeManagerPhone,
			taskContext: orderObj.taskContext,
			courseList: orderObj.courseList?.map(val => {return {bcCourseId: val.courseId, bcCourseName: val.title}}),
			imageList: orderObj.imageList
		}
		wx.$util.showLoading()
		api.submitMentorData(params).then(res => {
			wx.$util.hideLoading()
			let { code, message } = res
			if (code == 0) {
				wx.$util.showToast('操作成功')
				this.back()
			} else {
				wx.$util.showToast(message)
			}
		}).catch(() => {
			wx.$util.hideLoading()
		})
	},

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

  },

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

  },

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

  },

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

  },

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

  }
})