print-config.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <view class="printconfig">
  3. <view v-if="hasPrint==''" 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="initPrint">
  9. <view class="title" style="font-size: 20rpx;margin-bottom: 10rpx;">已经连接打印机</view>
  10. <view style="font-size: 16rpx;margin-bottom: 10rpx;color: chocolate;">当前连接打印机IP</view>
  11. <uni-easyinput disabled v-model="printInfo.ip" placeholder="请输入打印机ip地址"></uni-easyinput>
  12. <button @click="resetPrint">断开连接</button>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. name: 'printConfig'
  19. }
  20. </script>
  21. <script setup>
  22. import { onMounted, ref } from 'vue'
  23. import { setIp } from '@/static/js/api.js'
  24. const plug= uni.requireNativePlugin('Html5app-Gprinter');
  25. const hasPrint = ref('')
  26. const printInfo = ref({
  27. ip:'',
  28. port:'9100'
  29. })
  30. onMounted(() => {
  31. hasPrint.value = uni.getStorageSync('hasPrinter')
  32. printInfo.value.ip = uni.getStorageSync('printerIp')
  33. })
  34. const emit = defineEmits(['emitClose'])
  35. const connectPrint = ()=>{
  36. const ipReg = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
  37. if(ipReg.test(printInfo.value.ip)){
  38. if(RegExp.$1<256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256){
  39. plug.connectIP({"ip":printInfo.value.ip,"port":printInfo.value.port},ret=>{
  40. console.log('connectPrint>>ret',ret)
  41. const res = JSON.parse(JSON.stringify(ret))
  42. console.log('connectPrint>>res',res.code)
  43. if(res.code==3){
  44. // initPrint.value = true
  45. uni.setStorageSync('hasPrinter', '1')
  46. uni.setStorageSync('printerIp', printInfo.value.ip)
  47. // _this.tip=JSON.stringify(ret);
  48. // console.log(_this.tip);
  49. // 关闭配置页操作
  50. // 保持ip和端口
  51. const param = {userId:uni.getStorageSync('userId')+'',ip:printInfo.value.ip,port:printInfo.value.port}
  52. console.log('before setIp param',param)
  53. const res2 = setIp({
  54. token: uni.getStorageSync('token'),
  55. data: JSON.stringify(param)
  56. })
  57. if (res2.code !== 200){
  58. uni.$showMsg(res2.msg, 2000)
  59. }
  60. console.log('hasPrinter', uni.getStorageSync('hasPrinter'))
  61. console.log('printerIp', uni.getStorageSync('printerIp'))
  62. emit('emitClose')
  63. } else{
  64. uni.$showMsg('打印机连接失败,请重试!', 500)
  65. }
  66. });
  67. } else{
  68. uni.$showMsg('ip地址有误,请重试!', 2000)
  69. }
  70. }
  71. }
  72. const resetPrint = ()=>{
  73. plug.disconnect({},ret=>{
  74. uni.$showMsg(ret.msg, 2000)
  75. hasPrint.value = ''
  76. printInfo.value.ip = ''
  77. uni.setStorageSync('hasPrinter', '')
  78. uni.setStorageSync('printerIp', '')
  79. const param = {userId:uni.getStorageSync('userId')+'',ip:'',port:printInfo.value.port}
  80. // console.log(_this.tip);
  81. const res2 = setIp({
  82. token: uni.getStorageSync('token'),
  83. data: JSON.stringify(param)
  84. })
  85. if (res2.code !== 200){
  86. uni.$showMsg(res2.msg, 2000)
  87. }
  88. });
  89. }
  90. </script>
  91. <style lang="scss">
  92. .printconfig{
  93. background-color: #fff;
  94. width: 500rpx;
  95. height: 300rpx;
  96. padding: 20rpx;
  97. .initPrint{
  98. .title{
  99. font-size: 20rpx;
  100. }
  101. .tips{
  102. font-size: 16rpx;
  103. }
  104. .input{
  105. width: 300rpx;
  106. }
  107. }
  108. }
  109. </style>