choose-media.uvue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view>
  6. <page-head :title="title"></page-head>
  7. <view class="uni-common-mt">
  8. <view class="uni-list">
  9. <view class="uni-list-cell cell-pd">
  10. <view class="uni-list-cell-left uni-label">
  11. 来源
  12. </view>
  13. <view class="uni-list-cell-right" @click="chooseMediaSource">
  14. <text class="click-t">{{sourceTypes[sourceTypeIndex].title}}</text>
  15. </view>
  16. </view>
  17. <view class="uni-list-cell cell-pd">
  18. <view class="uni-list-cell-left uni-label">
  19. 方式
  20. </view>
  21. <view class="uni-list-cell-right" @click="chooseMediaType">
  22. <text class="click-t">{{(mediaTypes[mediaTypeIndex] as ChooseSource).title}}</text>
  23. </view>
  24. </view>
  25. <view class="uni-list-cell cell-pd">
  26. <view class="uni-list-cell-left uni-label">
  27. 数量限制
  28. </view>
  29. <view class="uni-list-cell-right">
  30. <input class="click-t" ref="refCountInput" :value="count" type="number" :maxlength="1" @blur="chooseMediaCount"/>
  31. </view>
  32. </view>
  33. <!-- #ifdef APP-ANDROID -->
  34. <view class="uni-list-cell cell-pd">
  35. <view class="uni-list-cell-left uni-label">
  36. 屏幕方向
  37. </view>
  38. <view class="uni-list-cell-right" @click="chooseOrientationType">
  39. <text class="click-t">{{orientationTypes[orientationTypeIndex].title}}</text>
  40. </view>
  41. </view>
  42. <!-- #endif -->
  43. <view class="uni-list-cell cell-pd">
  44. <view class="uni-list-cell-left uni-label">
  45. 摄像头
  46. </view>
  47. <view class="uni-list-cell-right" @click="chooseCameraType">
  48. <text class="click-t">{{cameraTypes[cameraTypeIndex].title}}</text>
  49. </view>
  50. </view>
  51. </view>
  52. <!-- #ifdef APP-IOS -->
  53. <input-data title="最长拍摄时间,单位秒" defaultValue="10" type="number" @confirm="onMaxDurationConfirm"></input-data>
  54. <!-- #endif -->
  55. <view class="uni-list list-pd" style="padding: 15px;">
  56. <view class="uni-flex" style="margin-bottom: 10px;">
  57. <view class="uni-list-cell-left">点击预览</view>
  58. <view style="margin-left: auto;">
  59. <text class="click-t">{{mediaList.length}}/{{count}}</text>
  60. </view>
  61. </view>
  62. <view class="uni-flex" style="flex-wrap: wrap;">
  63. <view v-for="(file,index) in mediaList" :key="index" class="uni-uploader__input-box" style="border: 0;">
  64. <image style="width: 104px; height: 104px;" :src="file.imagePath" :data-src="file.imagePath" @tap="previewMedia(index)">
  65. </image>
  66. <image src="/static/plus.png" class="image-remove" @click="removeMedia(index)"></image>
  67. </view>
  68. <image class="uni-uploader__input-box" @tap="chooseMedia" src="/static/plus.png"></image>
  69. </view>
  70. </view>
  71. </view>
  72. </view>
  73. <!-- #ifdef APP -->
  74. </scroll-view>
  75. <!-- #endif -->
  76. </template>
  77. <script>
  78. type FileSource = {
  79. imagePath : string;
  80. filePath : string;
  81. fileType : string;
  82. };
  83. type ChooseSource = {
  84. value : string[];
  85. title : string;
  86. };
  87. const sourceTypeList : ChooseSource[] = [
  88. {
  89. value: ['camera'],
  90. title: '拍摄',
  91. },
  92. {
  93. value: ['album'],
  94. title: '相册',
  95. },
  96. {
  97. value: ['camera', 'album'],
  98. title: '拍摄或相册',
  99. }
  100. ];
  101. const mediaTypeList : ChooseSource[] = [
  102. {
  103. value: ['image'],
  104. title: '仅图片',
  105. },
  106. {
  107. value: ['video'],
  108. title: '仅视频',
  109. },
  110. {
  111. value: ['image', 'video'],
  112. title: '不限制',
  113. }
  114. ];
  115. const orientationTypeList : ChooseSource[] = [
  116. {
  117. value: ['portrait'],
  118. title: '竖屏',
  119. },
  120. {
  121. value: ['landscape'],
  122. title: '横屏',
  123. },
  124. {
  125. value: ['auto'],
  126. title: '自动',
  127. }
  128. ];
  129. const cameraTypeList : ChooseSource[] = [
  130. {
  131. value: ['front'],
  132. title: '前置摄像头',
  133. },
  134. {
  135. value: ['back'],
  136. title: '后置摄像头',
  137. }
  138. ];
  139. export default {
  140. data() {
  141. return {
  142. title: 'chooseMedia',
  143. mediaList: [] as Array<FileSource>,
  144. sourceTypeIndex: 2,
  145. mediaTypeIndex: 2,
  146. cameraTypeIndex: 1,
  147. orientationTypeIndex: 0,
  148. albumModeTypeIndex: 0,
  149. count: 9,
  150. maxDuration: 10,
  151. sourceTypes: sourceTypeList as ChooseSource[],
  152. mediaTypes: mediaTypeList as ChooseSource[],
  153. cameraTypes: cameraTypeList as ChooseSource[],
  154. orientationTypes: orientationTypeList as ChooseSource[]
  155. }
  156. },
  157. methods: {
  158. chooseMediaSource() {
  159. uni.showActionSheet({
  160. itemList: ['拍摄', '相册', '拍摄或相册'],
  161. success: (e) => {
  162. this.sourceTypeIndex = e.tapIndex
  163. }
  164. })
  165. },
  166. chooseMediaType() {
  167. uni.showActionSheet({
  168. itemList: ['仅图片', '仅视频', '不限制'],
  169. success: (e) => {
  170. this.mediaTypeIndex = e.tapIndex
  171. }
  172. })
  173. },
  174. chooseMediaCount(event : UniInputBlurEvent) {
  175. let count = parseInt(event.detail.value)
  176. if (count < 1 || count > 9 || isNaN(count)) {
  177. uni.showToast({
  178. position: "bottom",
  179. title: "图片数量应该不小于1不大于9"
  180. })
  181. return
  182. }
  183. this.count = count
  184. },
  185. chooseOrientationType() {
  186. uni.showActionSheet({
  187. itemList: ['竖屏', '横屏', '自动'],
  188. success: (e) => {
  189. this.orientationTypeIndex = e.tapIndex
  190. }
  191. })
  192. },
  193. chooseCameraType() {
  194. uni.showActionSheet({
  195. itemList: ['前置', '后置'],
  196. success: (e) => {
  197. this.cameraTypeIndex = e.tapIndex
  198. }
  199. })
  200. },
  201. onMaxDurationConfirm(value : number) {
  202. this.maxDuration = value;
  203. },
  204. chooseMedia() {
  205. if (this.mediaList.length >= this.count) {
  206. const message = "已经有" + this.count + "个了,请删除部分后重新选择";
  207. uni.showToast({
  208. position: "bottom",
  209. title: message
  210. })
  211. return
  212. }
  213. uni.chooseMedia({
  214. count: this.count - this.mediaList.length,
  215. sourceType: sourceTypeList[this.sourceTypeIndex].value,
  216. mediaType: mediaTypeList[this.mediaTypeIndex].value,
  217. camera: cameraTypeList[this.cameraTypeIndex].value[0],
  218. // #ifdef APP-IOS
  219. maxDuration: this.maxDuration,
  220. // #endif
  221. // #ifdef APP-ANDROID
  222. pageOrientation: orientationTypeList[this.orientationTypeIndex].value[0],
  223. // #endif
  224. success: (res) => {
  225. const tempFiles : ChooseMediaTempFile[] = res.tempFiles as ChooseMediaTempFile[];
  226. for (let i = 0; i < tempFiles.length; i++) {
  227. const tempFile : ChooseMediaTempFile = tempFiles[i]
  228. const imagePath = tempFile.fileType == "image" ? tempFile.tempFilePath : tempFile.thumbTempFilePath;
  229. const file : FileSource = { imagePath: imagePath!, filePath: tempFile.tempFilePath, fileType: tempFile.fileType };
  230. this.mediaList.push(file);
  231. }
  232. },
  233. fail: (err) => {
  234. console.log("err: ", JSON.stringify(err));
  235. uni.showToast({
  236. title:"choose media error.code:" + err.errCode+";message:"+err.errMsg,
  237. position:"bottom"
  238. })
  239. }
  240. })
  241. },
  242. previewMedia: function (index : number) {
  243. const file : FileSource = this.mediaList[index];
  244. if (file.fileType == "image") {
  245. uni.previewImage({
  246. current: 0,
  247. urls: [file.filePath]
  248. })
  249. } else {
  250. uni.$once("__ONFULLVIDEOLOAD", () => {
  251. uni.$emit("__ONRECEIVEURL", {
  252. "url": file.filePath,
  253. "cover": file.imagePath
  254. })
  255. })
  256. const url = "/pages/API/choose-media/fullscreen-video";
  257. uni.navigateTo({
  258. url: url,
  259. })
  260. }
  261. },
  262. removeMedia(index : number) {
  263. this.mediaList.splice(index, 1)
  264. },
  265. }
  266. }
  267. </script>
  268. <style>
  269. .cell-pd {
  270. padding: 11px 15px;
  271. }
  272. .click-t {
  273. color: darkgray;
  274. }
  275. .list-pd {
  276. margin-top: 25px;
  277. }
  278. .uni-uploader__input-box {
  279. margin: 5px;
  280. width: 104px;
  281. height: 104px;
  282. border: 1px solid #D9D9D9;
  283. }
  284. .uni-uploader__input {
  285. position: absolute;
  286. z-index: 1;
  287. top: 0;
  288. left: 0;
  289. width: 100%;
  290. height: 100%;
  291. opacity: 0;
  292. }
  293. .image-remove {
  294. transform: rotate(45deg);
  295. width: 25px;
  296. height: 25px;
  297. position: absolute;
  298. top: 0;
  299. right: 0;
  300. border-radius: 13px;
  301. background-color: rgba(200, 200, 200, 0.8);
  302. }
  303. .item_width {
  304. width: 130px;
  305. }
  306. </style>