print-config.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. uni.setStorageSync('printerIp', printInfo.value.ip)
  43. // _this.tip=JSON.stringify(ret);
  44. // console.log(_this.tip);
  45. // 关闭配置页操作
  46. emit('emitClose')
  47. } else{
  48. uni.$showMsg('打印机连接失败,请重试!', 500)
  49. }
  50. });
  51. } else{
  52. uni.$showMsg('ip地址有误,请重试!', 2000)
  53. }
  54. }
  55. }
  56. const resetPrint = ()=>{
  57. plug.disconnect({},ret=>{
  58. uni.$showMsg(ret.msg, 2000)
  59. initPrint.value = false
  60. // console.log(_this.tip);
  61. });
  62. }
  63. </script>
  64. <style lang="scss">
  65. .printconfig{
  66. background-color: #fff;
  67. width: 500rpx;
  68. height: 300rpx;
  69. padding: 20rpx;
  70. .initPrint{
  71. .title{
  72. font-size: 20rpx;
  73. }
  74. .tips{
  75. font-size: 16rpx;
  76. }
  77. .input{
  78. width: 300rpx;
  79. }
  80. }
  81. }
  82. </style>