shareByWebClient.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <el-form ref="shareform" :model="formData" label-width="120px" label-suffix=" :">
  3. <el-form-item :label="T('ID')" prop="id" required>
  4. {{ formData.id }}
  5. </el-form-item>
  6. <el-form-item :label="T('PasswordType')">
  7. <div>
  8. <el-radio-group v-model="formData.password_type" @change="changePwdType">
  9. <el-radio value="once">{{ T('OncePassword') }}</el-radio>
  10. <el-radio value="fixed">{{ T('FixedPassword') }}</el-radio>
  11. </el-radio-group>
  12. <div v-if="formData.password_type==='fixed'" style="color: red">
  13. {{ T('FixedPasswordWarning') }}
  14. </div>
  15. </div>
  16. </el-form-item>
  17. <el-form-item :label="T('Password')" prop="password" required>
  18. <el-input v-model="formData.password" type="password" show-password></el-input>
  19. </el-form-item>
  20. <el-form-item :label="T('ExpireTime')" prop="expire" required>
  21. <el-select v-model="formData.expire">
  22. <el-option
  23. v-for="item in expireTimes"
  24. :key="item.value"
  25. :label="item.label"
  26. :value="item.value"
  27. ></el-option>
  28. </el-select>
  29. </el-form-item>
  30. <el-form-item v-if="link" :label="T('Link')">
  31. <el-input v-model="link" readonly>
  32. <template #append>
  33. <el-button :icon="CopyDocument" @click="copyLink"/>
  34. </template>
  35. </el-input>
  36. </el-form-item>
  37. <el-form-item>
  38. <el-button v-if="!link" @click="cancel">{{ T('Cancel') }}</el-button>
  39. <el-button v-if="!link" :loading="loading" @click="submitShare" type="primary">{{ T('Submit') }}</el-button>
  40. <el-button v-else @click="cancel" type="success">{{ T('Close') }}</el-button>
  41. </el-form-item>
  42. </el-form>
  43. </template>
  44. <script setup>
  45. import { T } from '@/utils/i18n'
  46. import { computed, reactive, ref, watch } from 'vue'
  47. import { getPeerSlat, rustdeskConfig } from '@/utils/webclient'
  48. import * as sha256 from 'fast-sha256'
  49. import { shareByWebClient } from '@/api/address_book'
  50. import { CopyDocument } from '@element-plus/icons'
  51. import { handleClipboard } from '@/utils/clipboard'
  52. const props = defineProps({
  53. id: String,
  54. hash: String,
  55. })
  56. const emits = defineEmits(['cancel', 'success'])
  57. const formData = reactive({
  58. id: props.id,
  59. password_type: 'once',
  60. password: '',
  61. expire: 1800,
  62. hash: props.hash,
  63. })
  64. watch(() => props.id, () => {
  65. init()
  66. })
  67. const init = () => {
  68. console.log('init')
  69. formData.id = props.id
  70. formData.hash = props.hash
  71. formData.password = ''
  72. formData.expire = 300
  73. formData.password_type = 'once'
  74. link.value = ''
  75. }
  76. const link = ref('')
  77. const expireTimes = computed(() => [
  78. { label: T('Minutes', { param: 5 }, 5), value: 300 },
  79. { label: T('Minutes', { param: 30 }, 30), value: 1800 },
  80. { label: T('Hours', { param: 1 }, 1), value: 3600 },
  81. { label: T('Days', { param: 1 }, 1), value: 86400 },
  82. { label: T('Weeks', { param: 1 }, 1), value: 604800 },
  83. { label: T('Months', { param: 1 }, 1), value: 2592000 },
  84. { label: T('Forever'), value: 0 },
  85. ])
  86. const changePwdType = (val) => {
  87. if (val === 'fixed' && !formData.password) {
  88. formData.password = props.hash
  89. }
  90. if (val === 'once') {
  91. formData.password = ''
  92. }
  93. }
  94. const cancel = () => {
  95. loading.value = false
  96. emits('cancel')
  97. init()
  98. }
  99. const loading = ref(false)
  100. const submitShare = async () => {
  101. if (!formData.password) {
  102. return
  103. }
  104. loading.value = true
  105. const _formData = { ...formData }
  106. if (formData.password !== formData.hash) {
  107. const res = await getPeerSlat(formData.id).catch(_ => false)
  108. if (!res) {
  109. loading.value = false
  110. return
  111. }
  112. const p = hash([formData.password, res.salt])
  113. _formData.password = btoa(p.toString().split(',').map((v) => String.fromCharCode(v)).join(''))
  114. }
  115. const res = await shareByWebClient(_formData).catch(_ => false)
  116. if (res) {
  117. link.value = `${rustdeskConfig.value.api_server}/webclient/#/?share_token=${res.data.share_token}`
  118. emits('success')
  119. }
  120. loading.value = false
  121. }
  122. const copyLink = (e) => {
  123. handleClipboard(link.value, e)
  124. }
  125. const hash = (datas) => {
  126. const hasher = new sha256.Hash()
  127. datas.forEach((data) => {
  128. if (typeof data == 'string') {
  129. data = new TextEncoder().encode(data)
  130. }
  131. hasher.update(data)
  132. })
  133. return hasher.digest()
  134. }
  135. </script>
  136. <style scoped lang="scss">
  137. </style>