image.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. console.log("err: ",err);
  133. // #ifdef APP-PLUS
  134. if (err['code'] && err.code !== 0 && this.sourceTypeIndex === 2) {
  135. this.checkPermission(err.code);
  136. }
  137. // #endif
  138. // #ifdef MP
  139. if(err.errMsg.indexOf('cancel') !== '-1'){
  140. return;
  141. }
  142. uni.getSetting({
  143. success: (res) => {
  144. let authStatus = false;
  145. switch (this.sourceTypeIndex) {
  146. case 0:
  147. authStatus = res.authSetting['scope.camera'];
  148. break;
  149. case 1:
  150. authStatus = res.authSetting['scope.album'];
  151. break;
  152. case 2:
  153. authStatus = res.authSetting['scope.album'] && res.authSetting['scope.camera'];
  154. break;
  155. default:
  156. break;
  157. }
  158. if (!authStatus) {
  159. uni.showModal({
  160. title: '授权失败',
  161. content: 'Hello uni-app需要从您的相机或相册获取图片,请在设置界面打开相关权限',
  162. success: (res) => {
  163. if (res.confirm) {
  164. uni.openSetting()
  165. }
  166. }
  167. })
  168. }
  169. }
  170. })
  171. // #endif
  172. }
  173. })
  174. },
  175. isFullImg: function() {
  176. return new Promise((res) => {
  177. uni.showModal({
  178. content: "已经有9张图片了,是否清空现有图片?",
  179. success: (e) => {
  180. if (e.confirm) {
  181. this.imageList = [];
  182. res(true);
  183. } else {
  184. res(false)
  185. }
  186. },
  187. fail: () => {
  188. res(false)
  189. }
  190. })
  191. })
  192. },
  193. previewImage: function(e) {
  194. var current = e.target.dataset.src
  195. uni.previewImage({
  196. current: current,
  197. urls: this.imageList
  198. })
  199. },
  200. async checkPermission(code) {
  201. let type = code ? code - 1 : this.sourceTypeIndex;
  202. let status = permision.isIOS ? await permision.requestIOS(sourceType[type][0]) :
  203. await permision.requestAndroid(type === 0 ? 'android.permission.CAMERA' :
  204. 'android.permission.READ_EXTERNAL_STORAGE');
  205. if (status === null || status === 1) {
  206. status = 1;
  207. } else {
  208. uni.showModal({
  209. content: "没有开启权限",
  210. confirmText: "设置",
  211. success: function(res) {
  212. if (res.confirm) {
  213. permision.gotoAppSetting();
  214. }
  215. }
  216. })
  217. }
  218. return status;
  219. }
  220. }
  221. }
  222. </script>
  223. <style>
  224. .cell-pd {
  225. padding: 22rpx 30rpx;
  226. }
  227. .list-pd {
  228. margin-top: 50rpx;
  229. }
  230. </style>