useMqttDeviceEditHook.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { nextTick, ref } from 'vue'
  2. import type { MqttDevice } from '@/types'
  3. import { useLoad } from '@tarojs/taro'
  4. import Taro from '@tarojs/taro'
  5. import { addMqttDevice, updateMqttDevice } from '@/api/mqttDevice'
  6. import { useShopStoreHook } from '@/store/shopStore'
  7. export default function useMqttDeviceEditHook() {
  8. // 创建一个响应式的设备表单对象
  9. const deviceForm = ref<MqttDevice>({
  10. device_type: 1,
  11. device_key: '',
  12. shop_id: useShopStoreHook().currentShop.shop_id as number
  13. })
  14. //表单引用
  15. const formRef = ref()
  16. // 定义可选的设备类型列表
  17. const deviceTypes = [
  18. { text: '云打印', value: 1 },
  19. { text: '云音响', value: 2 },
  20. ]
  21. // 控制设备类型选择器的显示状态
  22. const showDeviceTypePicker = ref(false)
  23. // 处理设备类型选择确认事件
  24. const onConfirm = ({ selectedValue, selectedOptions }) => {
  25. deviceForm.value.device_type = selectedValue[0]
  26. showDeviceTypePicker.value = false
  27. nextTick(() => {
  28. customBlurValidate('deviceType')
  29. })
  30. }
  31. // 处理表单提交
  32. const submitForm = () => {
  33. // 这里可以添加表单验证逻辑
  34. formRef.value.validate().then(({ valid, errors }) => {
  35. if (valid) {
  36. if (deviceForm.value.id) {
  37. updateMqttDevice(deviceForm.value,deviceForm.value.id).then(res => {
  38. Taro.showToast({
  39. title: '修改成功',
  40. icon: 'success',
  41. duration:2000
  42. })
  43. }).catch(err => {
  44. Taro.showToast({
  45. title: err.data.message,
  46. icon: 'none'
  47. })
  48. })
  49. } else {
  50. addMqttDevice(deviceForm.value).then(res => {
  51. Object.assign(deviceForm.value, res.data)
  52. if(res.data.success){
  53. Taro.showToast({
  54. title: '添加成功',
  55. icon: 'success',
  56. duration:2000
  57. })
  58. }else{
  59. Taro.showToast({
  60. title: res.data.message || '添加失败',
  61. icon: 'error'
  62. })
  63. }
  64. }).catch(err => {
  65. Taro.showToast({
  66. title: err.data.message,
  67. icon: 'error'
  68. })
  69. })
  70. }
  71. } else {
  72. Taro.showToast({
  73. title: '请检查表单',
  74. icon: 'none'
  75. })
  76. }}).catch(err => {
  77. console.log(err)
  78. })
  79. }
  80. //自定义表单验证
  81. const customBlurValidate = (key: string) => {
  82. formRef.value.validate(key)
  83. }
  84. //onload
  85. useLoad(async (option) => {
  86. if (option?.id) {
  87. Taro.setNavigationBarTitle({
  88. title: '编辑设备'
  89. })
  90. // 设置 id,
  91. deviceForm.value.id = option.id
  92. } else {
  93. Taro.setNavigationBarTitle({
  94. title: '添加设备'
  95. })
  96. }
  97. //设置设备key和设备类型
  98. if(option?.device_key){
  99. deviceForm.value.device_key = option.device_key
  100. }
  101. if(option?.device_type){
  102. deviceForm.value.device_type = Number(option.device_type)
  103. }
  104. })
  105. // 返回hook中的响应式数据和方法
  106. return {
  107. deviceForm,
  108. formRef,
  109. deviceTypes,
  110. showDeviceTypePicker,
  111. onConfirm,
  112. submitForm,
  113. customBlurValidate,
  114. }
  115. }