print-config.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <view class="printconfig">
  3. <view v-if="initPrint==false" class="initPrint">
  4. <view class="title">使用须知</view>
  5. <view class="tips">使用前先连接wifi打印机</view>
  6. <uni-easyinput class="input" confirm-type="done" v-model="printInfo.ip" placeholder="请输入打印机ip地址" @confirm="connectPrint"></uni-easyinput>
  7. </view>
  8. <view v-else class="printInfo">
  9. <view>当前连接打印机</view>
  10. <uni-easyinput disabled v-model="printInfo.ip" placeholder="请输入打印机ip地址"></uni-easyinput>
  11. <button @click="resetPrint">断开连接</button>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. name: 'printConfig'
  18. }
  19. </script>
  20. <script setup>
  21. import { onMounted, ref } from 'vue'
  22. const plug= uni.requireNativePlugin('Html5app-Gprinter');
  23. const initPrint = ref(false)
  24. const printInfo = ref({
  25. ip:'',
  26. port:'9100'
  27. })
  28. onMounted(() => {
  29. })
  30. const emit = defineEmits(['emitClose'])
  31. const connectPrint = ()=>{
  32. const ipReg = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
  33. if(ipReg.test(printInfo.value.ip)){
  34. if(RegExp.$1<256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256){
  35. plug.connectIP({"ip":printInfo.value.ip,"port":printInfo.value.port},ret=>{
  36. console.log('connectPrint>>ret',ret)
  37. const res = JSON.parse(JSON.stringify(ret))
  38. console.log('connectPrint>>res',res.code)
  39. if(res.code==3){
  40. // initPrint.value = true
  41. uni.setStorageSync('hasPrinter', '1')
  42. // _this.tip=JSON.stringify(ret);
  43. // console.log(_this.tip);
  44. // 关闭配置页操作
  45. emit('emitClose')
  46. } else{
  47. uni.$showMsg('打印机连接失败,请重试!', 500)
  48. }
  49. });
  50. } else{
  51. uni.$showMsg('ip地址有误,请重试!', 2000)
  52. }
  53. }
  54. }
  55. const resetPrint = ()=>{
  56. plug.disconnect({},ret=>{
  57. uni.$showMsg(ret.msg, 2000)
  58. initPrint.value = false
  59. // console.log(_this.tip);
  60. });
  61. }
  62. </script>
  63. <style lang="scss">
  64. .printconfig{
  65. background-color: #fff;
  66. width: 500rpx;
  67. height: 300rpx;
  68. padding: 20rpx;
  69. .initPrint{
  70. .title{
  71. font-size: 20rpx;
  72. }
  73. .tips{
  74. font-size: 16rpx;
  75. }
  76. .input{
  77. width: 300rpx;
  78. }
  79. }
  80. }
  81. </style>