choose-image.uvue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <page-head :title="title"></page-head>
  6. <view class="uni-common-mt">
  7. <view class="uni-list">
  8. <view class="uni-list-cell cell-pd">
  9. <text class="uni-list-cell-left uni-label">
  10. 图片来源
  11. </text>
  12. <view class="uni-list-cell-right" @click="chooseImageSource">
  13. <text class="click-t">{{sourceType[sourceTypeIndex]}}</text>
  14. </view>
  15. </view>
  16. <view class="uni-list-cell cell-pd">
  17. <text class="uni-list-cell-left uni-label">
  18. 图片质量
  19. </text>
  20. <view class="uni-list-cell-right" @click="chooseImageType">
  21. <text class="click-t">{{sizeType[sizeTypeIndex]}}</text>
  22. </view>
  23. </view>
  24. <view class="uni-list-cell cell-pd">
  25. <text class="uni-list-cell-left uni-label">
  26. 数量限制
  27. </text>
  28. <view class="uni-list-cell-right">
  29. <input class="click-t" :value="count" type="number" :maxlength="1" @blur="chooseImageCount" />
  30. </view>
  31. </view>
  32. <!-- #ifdef APP-ANDROID || APP-IOS -->
  33. <view class="uni-list-cell cell-pd">
  34. <text class="uni-list-cell-left uni-label">
  35. 屏幕方向
  36. </text>
  37. <view class="uni-list-cell-right" @click="chooseOrientationType">
  38. <text class="click-t">{{orientationType[orientationTypeIndex]}}</text>
  39. </view>
  40. </view>
  41. <!-- #endif -->
  42. <!-- #ifdef APP-ANDROID -->
  43. <view class="uni-list-cell cell-pd">
  44. <text class="uni-list-cell-left uni-label">
  45. 相册模式
  46. </text>
  47. <view class="uni-list-cell-right" @click="albumModeChange">
  48. <text class="click-t">{{albumModeType[albumModeTypeIndex]}}</text>
  49. </view>
  50. </view>
  51. <!-- #endif -->
  52. <view class="uni-list-cell cell-pd">
  53. <text class="uni-list-cell-left uni-label">
  54. 图像裁剪
  55. </text>
  56. <view class="uni-list-cell-right">
  57. <switch :checked="isCrop" @change="switchCrop"></switch>
  58. </view>
  59. </view>
  60. <view ref="cropOptionNode" class="crop-option"
  61. :style="{'height':isCrop?'200px':'0px','margin-bottom':isCrop?'11px':'0px'}">
  62. <view class="uni-list-cell cell-pd">
  63. <view class="uni-list-cell-left item_width">
  64. 图片质量(%)
  65. </view>
  66. <view class="uni-list-cell-right">
  67. <input :value="cropPercent" @confirm="cropPercentConfim" type="number" maxlength="-1" />
  68. </view>
  69. </view>
  70. <view class="uni-list-cell cell-pd">
  71. <view class="uni-list-cell-left item_width">
  72. 裁剪宽度(px)
  73. </view>
  74. <view class="uni-list-cell-right">
  75. <input :value="cropWidth" @confirm="cropWidthConfim" type="number" maxlength="-1" />
  76. </view>
  77. </view>
  78. <view class="uni-list-cell cell-pd">
  79. <view class="uni-list-cell-left item_width">
  80. 裁剪高度(px)
  81. </view>
  82. <view class="uni-list-cell-right">
  83. <input :value="cropHeight" @confirm="cropHeightConfim" type="number" maxlength="-1" />
  84. </view>
  85. </view>
  86. <view class="uni-list-cell cell-pd">
  87. <view class="uni-list-cell-left item_width">
  88. 保留原宽高
  89. </view>
  90. <view class="uni-list-cell-right">
  91. <switch :checked="cropResize" @change="cropResizeChange"></switch>
  92. </view>
  93. </view>
  94. </view>
  95. </view>
  96. <view class="uni-list list-pd" style="padding: 15px;">
  97. <view class="uni-flex" style="margin-bottom: 10px;">
  98. <view class="uni-list-cell-left">点击可预览选好的图片</view>
  99. <view style="margin-left: auto;">
  100. <text class="click-t">{{imageList.length}}/{{count}}</text>
  101. </view>
  102. </view>
  103. <view class="uni-flex" style="flex-wrap: wrap;">
  104. <view v-for="(image,index) in imageList" :key="index" class="uni-uploader__input-box" style="border: 0;">
  105. <image style="width: 104px; height: 104px;" :src="image" :data-src="image" @tap="previewImage(index)">
  106. </image>
  107. <image src="/static/plus.png" class="image-remove" @click="removeImage(index)"></image>
  108. </view>
  109. <image class="uni-uploader__input-box" @tap="chooseImage" src="/static/plus.png"></image>
  110. </view>
  111. </view>
  112. </view>
  113. <!-- #ifdef APP -->
  114. </scroll-view>
  115. <!-- #endif -->
  116. </template>
  117. <script>
  118. var sourceTypeArray = [
  119. ['camera'],
  120. ['album'],
  121. ['camera', 'album']
  122. ]
  123. var sizeTypeArray = [
  124. ['compressed'],
  125. ['original'],
  126. ['compressed', 'original']
  127. ]
  128. var orientationTypeArray = [
  129. 'portrait',
  130. 'landscape',
  131. 'auto'
  132. ]
  133. var albumModeTypeArray = [
  134. "custom",
  135. "system"
  136. ]
  137. export default {
  138. data() {
  139. return {
  140. title: 'chooseImage',
  141. imageList: [] as Array<string>,
  142. sourceTypeIndex: 2,
  143. sourceType: ['拍照', '相册', '拍照或相册'],
  144. sizeTypeIndex: 2,
  145. sizeType: ['压缩', '原图', '压缩或原图'],
  146. orientationTypeIndex: 0,
  147. orientationType: ['竖屏', '横屏', '自动'],
  148. albumModeTypeIndex:0,
  149. albumModeType:["自定义相册","系统相册"],
  150. count: 9,
  151. isCrop: false,
  152. cropPercent: 80,
  153. cropWidth: 100,
  154. cropHeight: 100,
  155. cropResize: false
  156. }
  157. },
  158. onHide() {
  159. console.log("Page Hide");
  160. },
  161. onUnload() {
  162. this.imageList = [];
  163. this.sourceTypeIndex = 2
  164. this.sourceType = ['拍照', '相册', '拍照或相册']
  165. this.sizeTypeIndex = 2
  166. this.sizeType = ['压缩', '原图', '压缩或原图']
  167. this.orientationTypeIndex = 0
  168. this.orientationType = ['竖屏', '横屏', '自动']
  169. },
  170. methods: {
  171. cropHeightConfim(e : InputConfirmEvent) {
  172. let value = parseInt(e.detail.value)
  173. if (value > 0) {
  174. this.cropHeight = value
  175. } else {
  176. uni.showToast({
  177. position: "bottom",
  178. title: "裁剪高度需要大于0"
  179. })
  180. }
  181. },
  182. cropWidthConfim(e : InputConfirmEvent) {
  183. let value = parseInt(e.detail.value)
  184. if (value > 0) {
  185. this.cropWidth = value
  186. } else {
  187. uni.showToast({
  188. position: "bottom",
  189. title: "裁剪宽度需要大于0"
  190. })
  191. }
  192. },
  193. cropPercentConfim(e : InputConfirmEvent) {
  194. let value = parseInt(e.detail.value)
  195. if (value > 0 && value <= 100) {
  196. this.cropPercent = value
  197. } else {
  198. uni.showToast({
  199. position: "bottom",
  200. title: "请输入0~100之间的值"
  201. })
  202. }
  203. },
  204. albumModeChange(){
  205. uni.showActionSheet({
  206. itemList: this.albumModeType,
  207. success: (e) => {
  208. this.albumModeTypeIndex = e.tapIndex
  209. }
  210. })
  211. },
  212. cropResizeChange(e : UniSwitchChangeEvent) {
  213. this.cropResize = e.detail.value
  214. },
  215. switchCrop(e : UniSwitchChangeEvent) {
  216. this.isCrop = e.detail.value
  217. },
  218. removeImage(index : number) {
  219. this.imageList.splice(index, 1)
  220. },
  221. chooseImageSource() {
  222. uni.showActionSheet({
  223. itemList: ['拍照', '相册', '拍照或相册'],
  224. success: (e) => {
  225. this.sourceTypeIndex = e.tapIndex
  226. }
  227. })
  228. },
  229. chooseImageType() {
  230. uni.showActionSheet({
  231. itemList: ['压缩', '原图', '压缩或原图'],
  232. success: (e) => {
  233. this.sizeTypeIndex = e.tapIndex
  234. }
  235. })
  236. },
  237. chooseOrientationType(){
  238. uni.showActionSheet({
  239. itemList: ['竖屏', '横屏', '自动'],
  240. success: (e) => {
  241. this.orientationTypeIndex = e.tapIndex
  242. }
  243. })
  244. },
  245. chooseImageCount(event : InputBlurEvent) {
  246. let count = parseInt(event.detail.value)
  247. if (count < 0) {
  248. uni.showToast({
  249. position: "bottom",
  250. title: "图片数量应该大于0"
  251. })
  252. return
  253. }
  254. this.count = count
  255. },
  256. chooseImage: function () {
  257. if (this.imageList.length >= this.count) {
  258. uni.showToast({
  259. position: "bottom",
  260. title: `已经有 ${this.count} 张图片了,请删除部分图片之后重新选择`
  261. })
  262. return
  263. }
  264. uni.chooseImage({
  265. sourceType: sourceTypeArray[this.sourceTypeIndex],
  266. sizeType: sizeTypeArray[this.sizeTypeIndex],
  267. crop: this.isCrop ? { "quality": this.cropPercent, "width": this.cropWidth, "height": this.cropHeight, "resize": this.cropResize } as ChooseImageCropOptions : null,
  268. count: this.count - this.imageList.length,
  269. // #ifdef APP
  270. pageOrientation: orientationTypeArray[this.orientationTypeIndex],
  271. // #endif
  272. // #ifdef APP-ANDROID
  273. albumMode: albumModeTypeArray[this.albumModeTypeIndex],
  274. // #endif
  275. success: (res) => {
  276. this.imageList = this.imageList.concat(res.tempFilePaths);
  277. console.log("this.imageList: ",this.imageList)
  278. },
  279. fail: (err) => {
  280. console.log("err: ", JSON.stringify(err));
  281. uni.showToast({
  282. title:"choose image error.code:" + err.errCode+";message:"+err.errMsg,
  283. position:"bottom"
  284. })
  285. }
  286. })
  287. },
  288. previewImage: function (index : number) {
  289. uni.previewImage({
  290. current: index,
  291. urls: this.imageList
  292. })
  293. }
  294. }
  295. }
  296. </script>
  297. <style>
  298. .cell-pd {
  299. padding: 11px 15px;
  300. }
  301. .click-t {
  302. color: darkgray;
  303. }
  304. .list-pd {
  305. margin-top: 25px;
  306. }
  307. .uni-uploader__input-box {
  308. margin: 5px;
  309. width: 104px;
  310. height: 104px;
  311. border: 1px solid #D9D9D9;
  312. }
  313. .uni-uploader__input {
  314. position: absolute;
  315. z-index: 1;
  316. top: 0;
  317. left: 0;
  318. width: 100%;
  319. height: 100%;
  320. opacity: 0;
  321. }
  322. .image-remove {
  323. transform: rotate(45deg);
  324. width: 25px;
  325. height: 25px;
  326. position: absolute;
  327. top: 0;
  328. right: 0;
  329. border-radius: 13px;
  330. background-color: rgba(200, 200, 200, 0.8);
  331. }
  332. .item_width {
  333. width: 130px;
  334. }
  335. .crop-option {
  336. margin-left: 11px;
  337. margin-right: 11px;
  338. border-radius: 11px;
  339. background-color: #eee;
  340. transition-property: height, margin-bottom;
  341. transition-duration: 200ms;
  342. }
  343. </style>