VimUpload.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <template>
  2. <view>
  3. <view class="flex bg-white">
  4. <view class="flex-sub padding" @tap="chooseImage">
  5. <text class="cuIcon-pic text-grey text-xxl"></text>
  6. <view class="text-center text-xs">发送图片</view>
  7. </view>
  8. <!-- #ifndef APP-PLUS -->
  9. <view class="flex-sub padding" @tap="chooseVideo">
  10. <text class="cuIcon-video text-grey text-xxl"></text>
  11. <view class="text-center text-xs">发送视频</view>
  12. </view>
  13. <view class="flex-sub padding" @tap="chooseFile">
  14. <text class="cuIcon-file text-grey text-xxl"></text>
  15. <view class="text-center text-xs">发送文件</view>
  16. </view>
  17. <!-- #endif -->
  18. <!-- #ifdef APP-PLUS -->
  19. <view class="flex-sub padding" @tap="chooseAppFile">
  20. <text class="cuIcon-file text-grey text-xxl"></text>
  21. <view class="text-center text-xs">发送文件</view>
  22. </view>
  23. <view v-if="chatType===ChatType.FRIEND" v-for="(item) in VimPlugin.chatViewPlugins()" class="flex-sub padding" @tap="item.handle(chatId)">
  24. <text class="text-grey text-xxl" :class="item.icon"></text>
  25. <view class="text-center text-xs">{{item.title}}</view>
  26. </view>
  27. <view class="flex-sub padding" v-if="chatType===ChatType.GROUP" @tap="phoneCall">
  28. <text class="cuIcon-phone text-grey text-xxl"></text>
  29. <view class="text-center text-xs">语音通话</view>
  30. </view>
  31. <view class="flex-sub padding" v-if="chatType===ChatType.GROUP" @tap="videoCall">
  32. <text class="cuIcon-record text-grey text-xxl"></text>
  33. <view class="text-center text-xs">视频通话</view>
  34. </view>
  35. <!-- #endif -->
  36. </view>
  37. </view>
  38. </template>
  39. <script setup lang="ts">
  40. import Auth from '@/api/Auth';
  41. import FetchRequest from '@/api/FetchRequest';
  42. import MessageType from '@/utils/MessageType';
  43. //#ifdef APP-PLUS
  44. import filePicker from "@/uni_modules/file-picker"
  45. //#endif
  46. import {getFileType} from "@/utils/FileUtils";
  47. import MessageUtils from "@/utils/MessageUtils";
  48. import type Message from "@/mode/Message";
  49. import {useWsStore} from "@/store/WsStore";
  50. import {useUserStore} from "@/store/userStore";
  51. import VimPlugin from "@/plugins/VimPlugin";
  52. import ChatType from "@/utils/ChatType";
  53. import VimConfig from "@/config/VimConfig";
  54. const props = defineProps({
  55. chatId: {
  56. type: String,
  57. required: true,
  58. default: ''
  59. },
  60. chatType: {
  61. type: String,
  62. required: true,
  63. default: ''
  64. }
  65. })
  66. const userId = useUserStore().getUser()?.id
  67. const chooseFile = () => {
  68. //#ifdef H5
  69. uni.chooseFile({
  70. count: 1,
  71. extension: ['.zip', '.doc', '.docx', '.pdf', '.xls', '.xlsx'],
  72. success: function (res) {
  73. upload(res.tempFilePaths[0], MessageType.file, res.tempFiles[0].name)
  74. }
  75. });
  76. //#endif
  77. //#ifdef MP
  78. wx.chooseMessageFile({
  79. count: 1,
  80. type: 'file',
  81. success (res:any) {
  82. // tempFilePath可以作为img标签的src属性显示图片
  83. upload(res.tempFiles[0].path, MessageType.file, res.tempFiles[0].name)
  84. }
  85. })
  86. //#endif
  87. }
  88. //#ifdef APP-PLUS
  89. const chooseAppFile = () => {
  90. filePicker({
  91. scope: "/Download",
  92. permission: false,
  93. mimetype: "*/*",
  94. success(res: any) {
  95. const fileType = getFileType(res.fileName).toLowerCase()
  96. if (fileType === "mp4") {
  97. upload(res.filePath, MessageType.video, res.fileName)
  98. } else if (fileType === "mp3") {
  99. upload(res.filePath, MessageType.voice, res.fileName)
  100. } else if (['jpg', 'png', 'gif'].some(item => item === fileType)) {
  101. upload(res.filePath, MessageType.image, res.fileName)
  102. } else {
  103. upload(res.filePath, MessageType.file, res.fileName)
  104. }
  105. },
  106. fail(err: any) {
  107. console.log(err);
  108. }
  109. })
  110. }
  111. //#endif
  112. const chooseVideo = () => {
  113. //#ifdef H5
  114. uni.chooseFile({
  115. count: 1,
  116. extension: ['.mp4'],
  117. success: function (res) {
  118. upload(res.tempFilePaths[0], MessageType.video, res.tempFiles[0].name)
  119. }
  120. });
  121. //#endif
  122. //#ifdef MP
  123. wx.chooseMessageFile({
  124. count: 1,
  125. type: 'video',
  126. success (res:any) {
  127. // tempFilePath可以作为img标签的src属性显示图片
  128. upload(res.tempFiles[0].path, MessageType.file, res.tempFiles[0].name)
  129. }
  130. })
  131. //#endif
  132. }
  133. const chooseImage = () => {
  134. uni.chooseImage({
  135. count: 1,
  136. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  137. sourceType: ['album', 'camera'], //从相册选择或者相机
  138. success: function (res) {
  139. upload(res.tempFilePaths[0], MessageType.image, '')
  140. }
  141. });
  142. }
  143. const upload = (path: string, messageType: string, fileName: string) => {
  144. // uni.showLoading({
  145. // title: '上传中'
  146. // })
  147. var localUrl='';
  148. if(messageType === MessageType.file){
  149. localUrl=path;
  150. }
  151. else if(messageType === MessageType.image){
  152. localUrl='/static/mix-tree/shangctp.png';
  153. }
  154. else{
  155. localUrl='/static/mix-tree/shangctp.png';
  156. }
  157. console.log(path)
  158. const extendA = (messageType === MessageType.file) ? {url:localUrl, fileName: fileName} : {url:localUrl};
  159. let msgA: Message = {
  160. messageType: messageType,
  161. chatId: props.chatId,
  162. fromId: userId,
  163. content: '',
  164. type: props.chatType,
  165. extend:extendA
  166. }
  167. var currentTime = new Date().getTime();
  168. msgA.localtime=currentTime;
  169. msgA.timestamp=currentTime;
  170. msgA.mine=true;
  171. msgA.id=JSON.stringify(currentTime);
  172. useWsStore().sendfileMessage(msgA);
  173. uni.uploadFile({
  174. url: `${FetchRequest.getHost()}/${VimConfig.uploadType}/upload`, //仅为示例,非真实的接口地址
  175. filePath: path,
  176. name: 'file',
  177. header: {
  178. "Access-Control-Allow-Origin": "*",
  179. "Authorization": "Bearer " + Auth.getToken(),
  180. },
  181. formData: {
  182. 'type': messageType
  183. },
  184. success: (res) => {
  185. //uni.hideLoading()
  186. const data = JSON.parse(res.data)
  187. if(data.code==500){
  188. MessageUtils.message('上传失败')
  189. useWsStore().sendfileMsgFalse(msgA);
  190. return;
  191. }
  192. const extendB = (messageType === MessageType.file) ? {url: data.url, fileName: fileName} : {url: data.url};
  193. if(userId){
  194. let msg: Message = {
  195. messageType: messageType,
  196. chatId: props.chatId,
  197. fromId: userId,
  198. content: '',
  199. localtime:currentTime,
  200. type: props.chatType,
  201. extend: extendB
  202. }
  203. useWsStore().sendMessage(msg)
  204. }
  205. },
  206. fail: () => {
  207. //uni.hideLoading()
  208. useWsStore().sendfileMsgFalse(msgA);
  209. MessageUtils.message('上传失败')
  210. },
  211. });
  212. }
  213. const phoneCall = () =>{
  214. console.log('phoneCall')
  215. uni.navigateTo({
  216. url: `/pages/group/xuanzequanyuan?id=${props.chatId}&showVideo=false&isCaller=true`
  217. })
  218. }
  219. const videoCall = () =>{
  220. uni.navigateTo({
  221. url: `/pages/group/xuanzequanyuan?id=${props.chatId}&showVideo=true&isCaller=true`
  222. })
  223. }
  224. </script>
  225. <style scoped>
  226. .text-xxl {
  227. font-size: 64upx;
  228. }
  229. </style>