image.vue 6.3 KB

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