import Taro from "@tarojs/taro"; import classnames from "classnames"; import { Component, PropsWithChildren, useEffect, useState } from "react"; import { Block, View, Video, Text, Image, Input, Button, ScrollView, Textarea, Canvas, } from "@tarojs/components"; import Navbar from "@/components/navbar/navbar"; import PopupInstrumentUploadTips from "@/components/popup/popup-instrument-upload-tips"; import { InstrumentInfo } from "@/utils/Interface"; import "./index.less"; import { contraction, getImgInfo } from "@/utils/compressImage"; import PopupAlert from "@/components/popup/popup-alert"; import { setStorageSync, getStorageSync, msg } from "@/utils/traoAPI"; export default class InstrumentClickInUpload extends Component { constructor(props) { super(props); this.state = { name: "仪器打卡上传", year: new Date().getFullYear(), month: String(new Date().getMonth() + 1).padStart(2, "0"), day: String(new Date().getDate()).padStart(2, "0"), zkmoshiindex: 0, moshiindex: null, buzhouindex: null, autoplay: false, scrollleft: 0, sysinfo: Taro.getSystemInfoSync(), showVideoPlayBtn: true, duration: 0, videoContext: {}, // 视频播放器 isTipShow: false, clockContent: "", // 默认展示 instrumentDetail: {}, punchInInfo: { clockImageList: [], clockContent: "", instrumentId: "84", firstClockImg: null, secondClockImg: null, thirdClockImg: null, }, // 现在选中的数据 currentInfo: { modeVideo: "", }, modeInfo: [], nurseInfo: [], // 首次进入介绍弹窗 isModeLock: false, isSubmit: false, // 已提交则禁用提交按钮 }; } async onLoad() { this.initData(); } componentDidMount() {} componentWillUnmount() {} componentDidShow() { let videoContext = Taro.createVideoContext("myVideo"); this.setState({ videoContext }); } componentDidHide() {} async initData() { let objStr = getStorageSync("instrument_detail"); if (objStr) { let instrumentDetail = JSON.parse(objStr); this.setState({ instrumentDetail }); } setTimeout(() => { this.firstNurseInfo(); this.getLatestClockRecord(); this.modeInfoList(); }, 10); } // 打卡介绍 firstNurseInfo = async () => { let { instrumentDetail } = this.state; let res = await InstrumentInfo.firstNurseInfo({ instrumentId: instrumentDetail.id, }); if (res.data.code === 200) { let isTipShow = getStorageSync("first_instrument_" + instrumentDetail.id); if (!isTipShow) { // 首次进入页面:自动打开打卡介绍弹窗 this.setState({ nurseInfo: res.data.data, isTipShow: true }); } else { this.setState({ nurseInfo: res.data.data }); } } }; // 获取最新一条打卡记录 getLatestClockRecord = async () => { let { punchInInfo } = this.state; let res = await InstrumentInfo.apiClock.getLatestClockRecord(); if (res.data.code === 200) { punchInInfo.clockContent = res.data.data.clockContent; punchInInfo.clockImageList = res.data.data.clockImg; } this.setState({ punchInInfo }); }; // 获取仪器模式列表 modeInfoList = async () => { let res = await InstrumentInfo.modeInfoList({ instrumentId: "84", }); if (res.data.code === 200) { if (res.data.data.length) { this.setState({ currentInfo: res.data.data[0], modeInfo: res.data.data, }); } } }; bindMoshi = async (index) => { let { currentInfo, modeInfo, punchInInfo } = this.state; currentInfo = modeInfo[index]; if (currentInfo.lock) { this.onModeLockOpen(); // 点击了锁定模式 return; } this.setState({ zkmoshiindex: index, currentInfo }); }; // 选择图片上传 handleChooseImage = async () => { let { punchInInfo } = this.state; Taro.chooseMedia({ count: 1, mediaType: ["image"], sourceType: ["album", "camera"], sizeType: ["compressed"], camera: "back", success: async (res) => { const tempFilePath = res.tempFiles[0].tempFilePath; let img = await getImgInfo(tempFilePath); let compressImage = await contraction(img, "compressImage"); // 压缩后文件地址 let compressTempFilePath = compressImage.tempFilePath; punchInInfo.clockImageList.push(compressTempFilePath); this.setState({ punchInInfo }); }, }); }; // 删除打卡图片 handleDeleteImage = async (index) => { let { punchInInfo } = this.state; punchInInfo.clockImageList.splice(index, 1); this.setState({ punchInInfo }); }; // 提交打卡 handleSubmit = async () => { let { punchInInfo } = this.state; let obj = punchInInfo; if (obj.clockImageList.length === 0) { msg("至少上传一张图片"); return; } let promiseTasks: any = []; let imgList: any[] = []; obj.clockImageList.map((imgUrl: string) => { let isTmpFile = imgUrl.indexOf("//tmp") > -1; let isWXFile = imgUrl.indexOf("wxfile://") > -1; if (isTmpFile || isWXFile) { let params = { clockImg: imgUrl, }; promiseTasks.push(InstrumentInfo.apiClock.clockFileUpload(params)); imgList.push("null"); } else { imgList.push(imgUrl); } }); if (promiseTasks.length) { let res = await Promise.all(promiseTasks); //具体处理写在如下 if (res.length) { res.map((item) => { if (item.code === 200) { let index = imgList.findIndex((url) => url === "null"); if (index > -1) { imgList[index] = item.data.url; } } }); } } let clockParmas = { clockImageList: imgList, clockContent: obj.clockContent, }; let res = await InstrumentInfo.apiClock.postInsertClockLog(clockParmas); if (res.data.code === 200) { msg("打卡成功"); setTimeout(() => { Taro.switchTab({ url: "/pages/index/index", }); }, 2000); } }; onPlayTap = async () => { let { videoContext } = this.state; videoContext.play(); this.setState({ showVideoPlayBtn: false, }); }; videoBindTimeUpdate = async () => {}; videoEnded = async () => {}; videoPause = async () => {}; videoPlay = async () => {}; videoLoadedMetaData = async () => {}; handleTextareaInput = async (e) => { let { punchInInfo } = this.state; punchInInfo.clockContent = e.detail.value; this.setState({ punchInInfo }); }; onModeLockOpen = async () => { this.setState({ isModeLock: true }); }; onModeLockClose = async () => { this.setState({ isModeLock: false }); }; onTipShowOpen = async () => { this.setState({ isTipShow: true }); }; onTipShowClose = async () => { setStorageSync( "first_instrument_" + this.state.instrumentDetail.id, "true" ); this.setState({ isTipShow: false }); }; render() { let { name, duration, showVideoPlayBtn, scrollleft, zkmoshiindex, punchInInfo, isTipShow, currentInfo, modeInfo, nurseInfo, isModeLock, isSubmit, } = this.state; return ( {modeInfo.map((item, index) => { return ( {item.modeName} {item.modeDesc} {!item.lock && zkmoshiindex !== index && ( )} {!item.lock && zkmoshiindex == index && ( )} {item.lock && ( )} {item.modeBanner && ( )} ); })} 打卡上传 {punchInInfo.clockImageList.length > 0 && punchInInfo.clockImageList.map((item, index) => { return ( ); })} {punchInInfo.clockImageList.length < 3 && ( )} {/* {tipshow && ( {punchInInfo.clockContent ? punchInInfo.clockContent : "请记录一下今天打卡的心得吧~"} )} */} {punchInInfo.clockContent.length}/100 {!isSubmit && ( 提交打卡 )} {isSubmit && ( 提交打卡 )} ); } }