upload-image.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <view>
  3. <view class="uni-uploader">
  4. <view class="uni-uploader-head">
  5. <view class="uni-uploader-info">{{imageList.length}}/6</view>
  6. </view>
  7. <view class="uni-uploader-body">
  8. <view class="uni-uploader__files">
  9. <block v-for="(image,index) in imageList" :key="index">
  10. <view class="uni-uploader__file" style="position: relative;">
  11. <image class="uni-uploader__img" :src="image" mode="aspectFill" :data-src="image" @tap="previewImage"></image>
  12. <view class="del" style="position: absolute;width: 32rpx;height: 32rpx;top: 0;right: 5rpx;" @click.stop="del(index)">
  13. <image src="../static/icon/delete.png" style="width: 32rpx;height: 32rpx;" mode=""></image>
  14. </view>
  15. </view>
  16. </block>
  17. <view class="uni-uploader__input-box" v-if="imageList.length < 6">
  18. <view class="uni-uploader__input" @tap="chooseImage"></view>
  19. </view>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import permision from "@/components/permission.js"
  27. var sourceType = [
  28. ['camera'],
  29. ['album'],
  30. ['camera', 'album']
  31. ]
  32. var sizeType = [
  33. ['compressed'],
  34. ['original'],
  35. ['compressed', 'original']
  36. ]
  37. export default {
  38. data() {
  39. return {
  40. title: 'choose/previewImage',
  41. imageList: [],
  42. sourceTypeIndex: 2,
  43. sourceType: ['拍照', '相册', '拍照或相册'],
  44. sizeTypeIndex: 2,
  45. sizeType: ['压缩', '原图', '压缩或原图'],
  46. countIndex: 8,
  47. count: [1, 2, 3, 4, 5, 6, 7, 8, 9]
  48. }
  49. },
  50. onUnload() {
  51. this.imageList = [],
  52. this.sourceTypeIndex = 2,
  53. this.sourceType = ['拍照', '相册', '拍照或相册'],
  54. this.sizeTypeIndex = 2,
  55. this.sizeType = ['压缩', '原图', '压缩或原图'],
  56. this.countIndex = 8;
  57. },
  58. methods: {
  59. //删除图片
  60. del(index){
  61. this.imageList.splice(index,1);
  62. this.$emit('change',this.imageList);
  63. },
  64. chooseImage: async function() {
  65. // #ifdef APP-PLUS
  66. // TODO 选择相机或相册时 需要弹出actionsheet,目前无法获得是相机还是相册,在失败回调中处理
  67. if (this.sourceTypeIndex !== 2) {
  68. let status = await this.checkPermission();
  69. if (status !== 1) {
  70. return;
  71. }
  72. }
  73. // #endif
  74. if (this.imageList.length === 6) {
  75. let isContinue = await this.isFullImg();
  76. console.log("是否继续?", isContinue);
  77. if (!isContinue) {
  78. return;
  79. }
  80. }
  81. uni.chooseImage({
  82. sourceType: sourceType[this.sourceTypeIndex],
  83. sizeType: sizeType[this.sizeTypeIndex],
  84. count: this.imageList.length + this.count[this.countIndex] > 9 ? 9 - this.imageList.length : this.count[this.countIndex],
  85. success: (res) => {
  86. this.imageList = this.imageList.concat(res.tempFilePaths);
  87. this.imageList = this.imageList.slice(0,6);
  88. this.$emit('change',this.imageList);
  89. },
  90. fail: (err) => {
  91. // #ifdef APP-PLUS
  92. if (err['code'] && err.code !== 0 && this.sourceTypeIndex === 2) {
  93. this.checkPermission(err.code);
  94. }
  95. // #endif
  96. // #ifdef MP
  97. uni.getSetting({
  98. success: (res) => {
  99. let authStatus = false;
  100. switch (this.sourceTypeIndex) {
  101. case 0:
  102. authStatus = res.authSetting['scope.camera'];
  103. break;
  104. case 1:
  105. authStatus = res.authSetting['scope.album'];
  106. break;
  107. case 2:
  108. authStatus = res.authSetting['scope.album'] && res.authSetting['scope.camera'];
  109. break;
  110. default:
  111. break;
  112. }
  113. if (!authStatus) {
  114. uni.showModal({
  115. title: '授权失败',
  116. content: 'Hello uni-app需要从您的相机或相册获取图片,请在设置界面打开相关权限',
  117. success: (res) => {
  118. if (res.confirm) {
  119. uni.openSetting()
  120. }
  121. }
  122. })
  123. }
  124. }
  125. })
  126. // #endif
  127. }
  128. })
  129. },
  130. isFullImg: function() {
  131. return new Promise((res) => {
  132. uni.showModal({
  133. content: "已经有9张图片了,是否清空现有图片?",
  134. success: (e) => {
  135. if (e.confirm) {
  136. this.imageList = [];
  137. res(true);
  138. } else {
  139. res(false)
  140. }
  141. },
  142. fail: () => {
  143. res(false)
  144. }
  145. })
  146. })
  147. },
  148. previewImage: function(e) {
  149. var current = e.target.dataset.src
  150. uni.previewImage({
  151. current: current,
  152. urls: this.imageList
  153. })
  154. },
  155. async checkPermission(code) {
  156. let type = code ? code - 1 : this.sourceTypeIndex;
  157. let status = permision.isIOS ? await permision.requestIOS(sourceType[type][0]) :
  158. await permision.requestAndroid(type === 0 ? 'android.permission.CAMERA' :
  159. 'android.permission.READ_EXTERNAL_STORAGE');
  160. if (status === null || status === 1) {
  161. status = 1;
  162. } else {
  163. uni.showModal({
  164. content: "没有开启权限",
  165. confirmText: "设置",
  166. success: function(res) {
  167. if (res.confirm) {
  168. permision.gotoAppSetting();
  169. }
  170. }
  171. })
  172. }
  173. return status;
  174. }
  175. }
  176. }
  177. </script>
  178. <style>
  179. .cell-pd {
  180. padding: 22rpx 30rpx;
  181. }
  182. .list-pd {
  183. margin-top: 50rpx;
  184. }
  185. </style>