upload-image.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <template>
  2. <view class="upload_box">
  3. <!-- #ifdef APP -->
  4. <template>
  5. <!-- <template v-if="authList['WRITE_EXTERNAL_STORAGE']&&authList['CAMERA']"> -->
  6. <u-upload
  7. :deletable="!isDetail"
  8. :fileList="fileList1"
  9. @afterRead="afterRead"
  10. @delete="deletePic"
  11. :name="name"
  12. :multiple="multiple"
  13. :maxCount="limit"
  14. :width="width"
  15. :height="height"
  16. :previewFullImage="true"
  17. >
  18. <view class="upload_btn">
  19. <u-image :radius="radius" :width="width" :height="height" :showError="false" :showLoading="false" src="@/static/image/upload-img.png"></u-image>
  20. </view>
  21. </u-upload>
  22. </template>
  23. <!-- <view v-else-if="!authList['WRITE_EXTERNAL_STORAGE']" class="upload_btn_auth" @tap.stop="openAuth('WRITE_EXTERNAL_STORAGE')">
  24. <view class="upload_btn">
  25. <u-image :radius="radius" :width="width" :height="height" :showError="false" :showLoading="false" :src="`${$IMG_URL}/static/common/upload-img.png`"></u-image>
  26. </view>
  27. </view>
  28. <view v-else class="upload_btn_auth" @tap.stop="openAuth('CAMERA')">
  29. <view class="upload_btn">
  30. <u-image :radius="radius" :width="width" :height="height" :showError="false" :showLoading="false" :src="`${$IMG_URL}/static/common/upload-img.png`"></u-image>
  31. </view>
  32. </view> -->
  33. <!-- <yk-authpup ref="authpup" type="top" @changeAuth="changeAuth" :permissionID="permissionID"></yk-authpup> -->
  34. <!-- #endif -->
  35. <!-- #ifndef APP -->
  36. <u-upload
  37. :deletable="!isDetail"
  38. :fileList="fileList1"
  39. @afterRead="afterRead"
  40. @delete="deletePic"
  41. :name="name"
  42. :multiple="multiple"
  43. :maxCount="limit"
  44. :width="width"
  45. :height="height"
  46. :radius="radius"
  47. :previewFullImage="true"
  48. >
  49. <view class="upload_btn">
  50. <u-image :radius="radius" :width="width" :height="height" :showError="false" :showLoading="false" src="@/static/image/upload-img.png"></u-image>
  51. </view>
  52. </u-upload>
  53. <!-- #endif -->
  54. </view>
  55. </template>
  56. <script>
  57. import { mapGetters } from 'vuex'
  58. import ykAuthpup from '@/components/yk-authpup/yk-authpup'
  59. export default {
  60. name: 'UploadImage',
  61. components: {
  62. ykAuthpup
  63. },
  64. props: {
  65. // 值
  66. value: [String, Object, Array],
  67. // 宽度
  68. width: {
  69. type: String,
  70. default: '180rpx'
  71. },
  72. // 高度
  73. height: {
  74. type: String,
  75. default: '180rpx'
  76. },
  77. // 圆角
  78. radius: {
  79. type: String,
  80. default: '12rpx'
  81. },
  82. // 数量限制
  83. limit: {
  84. type: Number,
  85. default: 9
  86. },
  87. // 大小限制(MB)
  88. fileSize: {
  89. type: Number,
  90. default: 20
  91. },
  92. // 文件类型, 例如['png', 'jpg', 'jpeg']
  93. fileType: {
  94. type: Array,
  95. default: () => []
  96. },
  97. // 标识name
  98. name: {
  99. type: String,
  100. default: '1'
  101. },
  102. // 是否多选
  103. multiple: {
  104. type: Boolean,
  105. default: true
  106. },
  107. // 是否显示提示
  108. isShowTip: {
  109. type: Boolean,
  110. default: false
  111. },
  112. // 是否详情
  113. isDetail: {
  114. type: Boolean,
  115. default: false
  116. },
  117. // 是否在上传的时候回显图片
  118. showImg: {
  119. type: Boolean,
  120. default: true
  121. }
  122. },
  123. data() {
  124. return {
  125. // 权限判断
  126. permissionID: '',
  127. fileList1: [] //图片上传的文件
  128. }
  129. },
  130. watch: {
  131. value: {
  132. async handler(val) {
  133. if (val) {
  134. if (this[`fileList${this.name}`].length > 0) return
  135. // 首先将值转为数组
  136. const list = Array.isArray(val) ? val : this.value.split(',')
  137. const files = []
  138. // 然后将数组转为对象数组
  139. for (let i = 0; i < list.length; i++) {
  140. let item = list[i]
  141. if (typeof item === 'string') {
  142. let name = item + ''
  143. item = {
  144. url: this.getUrl(item),
  145. fullurl: item,
  146. status: 'success'
  147. }
  148. }
  149. files.push(item)
  150. }
  151. this[`fileList${this.name}`] = files
  152. } else {
  153. this[`fileList${this.name}`] = []
  154. return []
  155. }
  156. },
  157. deep: true,
  158. immediate: true
  159. }
  160. },
  161. computed: {
  162. // 是否显示提示
  163. showTip() {
  164. return this.isShowTip && (this.fileType || this.fileSize)
  165. },
  166. ...mapGetters('auth', ['onceIn']),
  167. },
  168. mounted() {
  169. // #ifdef APP
  170. // if (this.onceIn) {
  171. // this.$store.commit('auth/edit', {data: false, index: 'onceIn'})
  172. // this.openAuth('WRITE_EXTERNAL_STORAGE')
  173. // }
  174. // #endif
  175. },
  176. methods: {
  177. //打开自定义权限目的弹框
  178. openAuth(permissionID) {
  179. this.permissionID = permissionID //这个是对应的权限 ACCESS_FINE_LOCATION 位置权限 / WRITE_EXTERNAL_STORAGE 存储空间/照片权限 / CAMERA相机权限 / CALL_PHONE 拨打电话
  180. setTimeout(() => {
  181. this.$refs['authpup'].open()
  182. }, 500)
  183. },
  184. //用户授权权限后的回调
  185. changeAuth() {
  186. //这里是权限通过后执行自己的代码逻辑
  187. console.log('权限已授权,可执行自己的代码逻辑了')
  188. this.authList[this.permissionID] = true
  189. const list = {...this.authList}
  190. this.$store.commit('auth/edit', {data: list, index: 'authList'})
  191. if (!this.authList['CAMERA']) {
  192. this.openAuth('CAMERA')
  193. }
  194. },
  195. // 删除图片
  196. deletePic(event) {
  197. this[`fileList${event.name}`].splice(event.index, 1)
  198. const files = this[`fileList${event.name}`].length ? this[`fileList${event.name}`].map((item) => item.fullurl).join(',') : ''
  199. this.$emit('input', files)
  200. },
  201. // 新增图片
  202. async afterRead(event) {
  203. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  204. let lists = [].concat(event.file)
  205. let fileListLen = this[`fileList${event.name}`].length
  206. lists.map((item) => {
  207. this[`fileList${event.name}`].push({
  208. ...item,
  209. status: 'uploading',
  210. message: '上传中'
  211. })
  212. })
  213. let successNum = 0
  214. let failNum = 0
  215. for (let i = 0; i < lists.length; i++) {
  216. const result = await this.uploadFilePromise(lists[i].url)
  217. let item = this[`fileList${event.name}`][fileListLen]
  218. if (result.code === 1) {
  219. successNum++
  220. } else {
  221. failNum++
  222. }
  223. this[`fileList${event.name}`].splice(
  224. fileListLen,
  225. 1,
  226. Object.assign(item, {
  227. status: result.code === 1 ? 'success' : 'fail',
  228. message: result.code === 1 ? '' : '上传失败',
  229. url: result.code === 1 ? result.data.fullurl : '',
  230. fullurl: result.code === 1 ? result.data.url : ''
  231. })
  232. )
  233. fileListLen++
  234. }
  235. if(!this.showImg) {
  236. const files = this[`fileList${event.name}`].length ? this[`fileList${event.name}`][0] : ''
  237. this[`fileList${event.name}`] = []
  238. this.$emit('change',files)
  239. return
  240. }
  241. if (lists.length > 1) {
  242. uni.showModal({
  243. content: `本次上传成功${successNum}个,失败${failNum}个`,
  244. showCancel: false
  245. })
  246. }
  247. this[`fileList${event.name}`] = this[`fileList${event.name}`].filter((item) => item.status === 'success')
  248. const files = this[`fileList${event.name}`].length ? this[`fileList${event.name}`].map((item) => item.fullurl).join(',') : ''
  249. this.$emit('input', files)
  250. },
  251. uploadFilePromise(url) {
  252. return new Promise((resolve, reject) => {
  253. let a = uni.uploadFile({
  254. url: this.$UPLOAD_URL, //
  255. filePath: url,
  256. name: 'file',
  257. header: {
  258. token: `${this.$store.getters.token}`
  259. },
  260. success: (res) => {
  261. let data = JSON.parse(res.data)
  262. resolve(data)
  263. }
  264. })
  265. })
  266. },
  267. // 设置域名
  268. getUrl(url) {
  269. if (url.indexOf('http') === -1) url = this.$UPLOAD_BASE + url
  270. return url
  271. }
  272. }
  273. }
  274. </script>
  275. <style lang="scss" scoped>
  276. .upload_box {
  277. position: relative;
  278. .upload_btn_auth {
  279. // width: 180rpx;
  280. // height: 180rpx;
  281. }
  282. .upload_btn {
  283. // width: 180rpx;
  284. // height: 180rpx;
  285. position: relative;
  286. }
  287. }
  288. </style>