createVisit.js 4.08 KB
// packageCustomer/pages/createVisit/createVisit.js
import mapApi from "~/utils/mapapi/mapapi";
import { IMG_URL } from '~/utils/constantData'
Page({
  data: {
    IMG_URL: IMG_URL,
    navHeight: 0,
    showTimePicker: false,
    timeValue: Date.now(),
    minDate: new Date(2000, 0, 1).getTime(),
    maxDate: new Date(2100, 0, 1).getTime(),
    visitTimeText: "",
    contactName: "",
    content: "",
    contentLength: 0,
    fileList: [],
    locationName: "",
    latitude: null,
    longitude: null,
    infoData: {}
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  onReady() {
    this.resultDialog = this.selectComponent('#resultDialog')
  },

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

  },

  onNavbarReady(e) {
    const { height } = e.detail || {};
    this.setData({ navHeight: height || 0 });
  },

  onContactInput(e) {
    this.setData({ 
      'infoData.contactName': e.detail.value 
    })
  },

  onContentInput(e) {
    const val = e.detail.value || ""
    this.setData({ 
      'infoData.content': val, 
      contentLength: val.length 
    })
  },
  copy() {
    wx.setClipboardData({
      data: 1111,
    })
  },

  // 上传图片
  uploadImages() {
    let _this = this
    wx.chooseImage({
      count: 1,
      success(res) {
        console.log(res)
        let [tempFiles] = res.tempFilePaths
        let tempFilesList = tempFiles.split('.')
        let imgType = tempFilesList[tempFilesList.length - 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) {
                _this.setData({
                  'infoData.attachmentList': data
                })
              } else {
                wx.$util.showToast(message)
              }
            })
          }
        })
      },
      fail(err) {
        console.log(err)
      }
    })
  },

  // 图片预览
  previewImage(e) {
    let { url, index } = e.currentTarget.dataset
    let imageUrl = url?.map(v => { return v.fileUrl})
    wx.previewImage({
      urls: imageUrl,
      current: imageUrl[index]
    })
  },

  // 删除图片
  deleteFile(e) {
    let { index } = e.currentTarget.dataset
    let { infoData } = this.data
    infoData.attachmentList.splice(index, 1)
    this.setData({infoData})
  },

  async getLocation() {
    try {
      const loc = await wx.getLocation({ type: "gcj02" })
      const { latitude, longitude } = loc
      const res = await mapApi.detailedAdrress({ location: `${latitude},${longitude}` })
      const locationName = res?.result?.address || res?.result?.formatted_addresses?.recommend || "已定位"
      this.setData({ latitude, longitude, locationName })
    } catch (err) {
      wx.showToast({ title: "定位失败,请检查权限", icon: "none" })
    }
  },

  handleCancel() {
    if (wx.$router && wx.$router.back) {
      wx.$router.back()
    } else {
      wx.navigateBack({})
    }
  },

  handleConfirm() {
    const { infoData } = this.data
    if (!infoData.visitTimeText || !infoData.contactName || !infoData.content) return wx.$api.showToast("请输入必填信息")
    const res = {code: 1}
    const okText = '我知道了'
    if (this.resultDialog) {
      if (res.code == 0) {
        this.resultDialog.open({
          type: 'success',
          title: '新增拜访记录成功!',
          description: '',
          okText
        })
      } else {
        this.resultDialog.open({
          type: 'fail',
          title: '新增拜访记录失败,请稍后重试',
          description: '',
          okText
        })
      }
    }
  },

  // 接口请求结果弹窗确认
  onDialogOk() {
    this.handleCancel()
  },
})