import classnames from "classnames"; import { throttle } from "lodash"; import { Component, PropsWithChildren, useEffect, useState } from "react"; // import { InstrumentInfo } from "../../utils/Interface"; import Taro from "@tarojs/taro"; // 引入 Swiper, SwiperItem 组件 import { Block, View, Text, Image, Input, // Button, Video, ScrollView, Canvas, } from "@tarojs/components"; import { InstrumentInfo } from "@/utils/Interface"; import { go, msg, setStorageSync } from "@/utils/traoAPI"; import { getImgInfo, contraction } from "@/utils/compressImage"; /* 自定义组件 */ import Navbar from "@/components/navbar/navbar"; import PopupAlert from "@/components/popup/popup-alert"; import PopupBinding from "@/components/popup/popup-binding"; import NoDataComponent from "@/components/base/nodata"; /* 组件 */ import "./instrument.less"; export default class Instrument extends Component { constructor(props) { super(props); this.state = { name: "instrument", isBindingError: false, isBindingError203: false, isBindingCheckError: false, style: "font-size: 28rpx; color: #ccc;", succeedShow: false, tipShow: false, loading: true, channelList: [], channelInfo: { id: "", serialImage: "", serialCode: "", }, show: true, equipmentList: [ { id: 1, banner: "", titile: "测试", }, ], isVideo: false, inputType: 1, //1.手写 2.扫码绑定 bindCode: "", bindCodeInfo: null, bindPopup: "", userinfo: null, showLoginPopup: "", fromUrl: "", // 提示绑定与换绑 isVisibleBinding: false, isRegisterBoolean: false, isExchangeBinding: false, /** INPUT序列号:拎出来,防止上传图片被清空bug */ }; } $instance = Taro.getCurrentInstance(); isOnly: boolean = false; currentDevice: any = null; id: any = ""; async onLoad() { this.unbindingInstrumentInfoList(); } componentDidMount() {} componentWillUnmount() {} componentDidShow() { // 进入页面判断是否注册,用于扫码登录 let mobile = Taro.getStorageSync("mobile"); if (mobile) { this.setState({ isRegisterBoolean: true }); } // 用于判断是否只显示一个设备 let params: any = this.$instance.router?.params; if (params?.isOnly && params?.id) { if (params?.isOnly === "true") { this.isOnly = true; this.id = params?.id; } } console.log("params", params); console.log("channelInfo", this.state.channelInfo); } componentDidHide() {} async initData() {} onSerial = (event) => { const { value } = event.detail; let { channelInfo } = this.state; channelInfo.serialCode = value; this.setState({ channelInfo }); }; onTipShow = () => { this.setState({ tipShow: true }); }; onTabTap = (type) => { this.setState({ inputType: type, }); }; onScanTap = () => { /* // 本地测试 const result = decodeURIComponent('https%3A%2F%2Fi.flossom.com%2Fa%3Ffr200test12'); const bindCode = result.split('?')[1]; that.setData({ bindCode }) that.getBindCodeInfo(); return*/ Taro.scanCode({ scanType: ["barCode", "qrCode"], success: (res) => { const result = decodeURIComponent(res.result); if (result.indexOf("?") > -1) { const bindCode = result.split("?")[1]; console.log("bindCode", bindCode); const { channelInfo } = this.state; channelInfo.serialCode = bindCode; this.setState({ channelInfo, }); } setTimeout(() => { this.scanCodeBinding(); }, 100); }, fail(err) {}, complete(res) {}, }); }; onCustomerTap = () => { this.onCancelBind2Tap(); go("/pages/consultant/consultant"); }; onCancelBind2Tap = () => { this.setState({ bindPopup: "", bindCode: "", }); }; async onChangeImg() { Taro.chooseMedia({ count: 1, mediaType: ["image"], sourceType: ["album", "camera"], sizeType: ["compressed"], camera: "back", success: async (res) => { console.log("success", res); const tempFilePath = res.tempFiles[0].tempFilePath; let img = await getImgInfo(tempFilePath); let compressImage = await contraction(img, "compressImage"); // 压缩后文件地址 let compressTempFilePath = compressImage.tempFilePath; setTimeout(() => { let { channelInfo } = this.state; channelInfo.serialImage = compressTempFilePath; this.setState({ channelInfo }); }); }, fail: async (fail) => { console.log("fail", fail); }, }); } onSubmit = () => { const { serialImage, serialCode } = this.state.channelInfo; if (!serialCode?.trim()) return msg("请填写序列号"); if (!serialImage) return msg("请上传序列号照片"); this.manualCodeBinding(); }; onSelectChange(item) { const { channelInfo } = this.state; if (channelInfo.id === item.id) return; channelInfo.serialCode = ""; channelInfo.manualCodeBinding = ""; channelInfo.scanCodeBinding = ""; this.setState({ channelInfo: { ...channelInfo, ...item, }, isVideo: this.isVideo(item.banner), }); } closeDev = () => { // go("/pages/user/user"); let isInstrumentJump = Taro.getStorageSync("isInstrumentJump"); if (isInstrumentJump === "true") { Taro.switchTab({ url: "/pages/user/user", }); } }; isVideo = (str) => { if (str.includes(".mp4")) { return true; } return false; }; // 未绑定列表 unbindingInstrumentInfoList = async () => { let { data } = await InstrumentInfo.unbindingInstrumentInfoList(); console.log(data, "查看未绑定设备"); if (data.code === 200) { if (data.data.length) { if (!this.isOnly) { let equipmentList = data.data.filter((item) => item.status === 0); if (equipmentList.length === 0) { return; } let item = equipmentList[0]; item.serialCode = ""; this.setState({ equipmentList: equipmentList, channelInfo: item, isVideo: this.isVideo(item.banner), }); } else { let item = data.data.find((item) => String(item.id) === this.id); if (item) { item.serialCode = ""; this.setState({ equipmentList: [item], channelInfo: item, isVideo: this.isVideo(item.banner), }); } } } } }; /** * @name 手写绑定仪器 * @return code===204 用户选择的仪器与序列号对应的仪器不一致 */ manualCodeBinding = async () => { Taro.showLoading({ title: "请求中...", mask: true, }); let { channelInfo } = this.state; let res = await InstrumentInfo.manualCodeBinding({ serial: channelInfo.serialCode, serialImage: channelInfo.serialImage, instrumentId: channelInfo.id, }); Taro.hideLoading(); // 文件上传接口返回格式,不需要加data let code = Number(res.code); // 强制类型转换 if (code === 200) { msg("绑定成功"); setTimeout(() => { setStorageSync("instrument_item", JSON.stringify(channelInfo)); go("/pages/instrument/intro?id=" + channelInfo.id); }, 1000); } else if (res.data.code === 202) { this.changeBindBox(); } else if (res.data.code === 203) { this.setState({ isBindingError203: true }); } else if (res.data.code === 204) { this.onBindCheckErrorOpen(); } else if (res.data.code === 205 || res.data.code === 206) { this.onBindErrorOpen(); } }; /** * @name 扫码绑定仪器 * @return code===204 用户选择的仪器与序列号对应的仪器不一致 */ scanCodeBinding = async () => { // Taro.showLoading({ // title: "请求中...", // mask: true, // }); let { channelInfo } = this.state; let res = await InstrumentInfo.scanCodeBinding({ serial: channelInfo.serialCode, instrumentId: channelInfo.id, }); // Taro.hideLoading(); let code = Number(res.data.code); console.log("scanCodeBinding code", code); setTimeout(() => { console.log("scanCodeBinding code", code, code === 204); if (code === 200) { msg("绑定成功"); setTimeout(() => { setStorageSync("instrument_item", JSON.stringify(channelInfo)); go("/pages/instrument/intro?id=" + channelInfo.id); }, 1000); } else if (res.data.code === 202) { this.changeBindBox(); } else if (res.data.code === 203) { this.setState({ isBindingError203: true }); } else if (res.data.code === 204) { this.onBindCheckErrorOpen(); } else if (res.data.code === 205 || res.data.code === 206) { this.onBindErrorOpen(); } }, 100); }; // 调用绑定接口 bindingInstrument = async () => { Taro.showLoading({ title: "请求中...", mask: true, }); let { channelInfo } = this.state; let res = await InstrumentInfo.binding({ serial: channelInfo.serialCode, }); Taro.hideLoading(); setTimeout(() => { if (res.data.code === 200) { // this.setState({ isVisibleBinding: false }); msg("绑定成功"); setStorageSync("instrument_item", JSON.stringify(channelInfo)); setTimeout(() => { go("/pages/instrument/intro?id=" + channelInfo.id); }, 1000); } else if (res.data.code === 202) { this.changeBindBox(); } else if (res.data.code === 203) { this.setState({ isBindingError203: true }); } else if (res.data.code === 204) { this.onBindCheckErrorOpen(); } else if (res.data.code === 205 || res.data.code === 206) { this.onBindErrorOpen(); } }, 100); }; // 换绑仪器 exchangeBinding = async () => { Taro.showLoading({ title: "请求中...", mask: true, }); let { channelInfo } = this.state; let { data } = await InstrumentInfo.exchangeBinding({ serial: channelInfo.serialCode, }); Taro.hideLoading(); this.closeBinding(); if (data.code === 200) { msg("换绑成功"); } }; /**绑定错误弹窗*/ onBindErrorOpen = () => { this.setState({ isBindingError: true }); }; onBindErrorClose = () => { this.setState({ isBindingError: false }); }; onBindErrorConfirm = () => { this.onBindErrorClose(); go("/pages/consultant/consultant"); }; /**绑定错误:已被别人绑定弹窗*/ onBindErrorOpen203 = () => { this.setState({ isBindingError203: true }); }; onBindErrorClose203 = () => { this.setState({ isBindingError203: false }); }; onBindErrorConfirm203 = () => { this.onBindErrorClose203(); go("/pages/consultant/consultant"); }; /**绑定错误:检查序列号弹窗*/ onBindCheckErrorOpen = () => { this.setState({ isBindingCheckError: true }); }; onBindCheckErrorClose = () => { this.setState({ isBindingCheckError: false }); }; /** 换绑弹窗 */ changeBindBox() { this.setState({ isVisibleBinding: true, isExchangeBinding: true }); } customBack = () => { Taro.reLaunch({ url: "/pages/index/index" }); }; // 打开绑定弹窗 openBindingVisible = () => { this.setState({ isVisibleBinding: true }); }; closeBinding = () => { this.setState({ isVisibleBinding: false }); }; confirmBinding = () => { let { isExchangeBinding } = this.state; if (!isExchangeBinding) { // 绑定仪器 this.bindingInstrument(); } else { // 换绑仪器 this.exchangeBinding(); } }; render() { let { isBindingError, isBindingError203, isBindingCheckError, show, channelInfo, inputType, style, equipmentList, isVisibleBinding, isRegisterBoolean, isExchangeBinding, } = this.state; return ( {/* */} 仪器绑定 请参考图片指引,扫描或输入产品序列号 完成花至美容仪绑定 {inputType === 1 ? ( 需要绑定的仪器 * {equipmentList.length > 0 && equipmentList.map((item, index) => { return ( {item.name} ); })} {equipmentList.length === 0 && ( )} 手写输入绑定 {channelInfo.isScanCode === 1 && ( 扫码绑定 )} {inputType == 1 && ( 填写序列号 * 查看序列号位置 上传序列号照片 * {channelInfo.serialImage && ( )} {!channelInfo.serialImage && ( )} )} {inputType == 1 && ( 确认绑定 )} {inputType == 2 && ( 扫码绑定 )} ); } }