upload-image.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. console.log(event,'event')
  204. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  205. let lists = [].concat(event.file)
  206. let fileListLen = this[`fileList${event.name}`].length
  207. lists.map((item) => {
  208. this[`fileList${event.name}`].push({
  209. ...item,
  210. status: 'uploading',
  211. message: '上传中'
  212. })
  213. })
  214. let successNum = 0
  215. let failNum = 0
  216. for (let i = 0; i < lists.length; i++) {
  217. const result = await this.uploadFilePromise(lists[i].url)
  218. console.log(result,'result')
  219. let item = this[`fileList${event.name}`][fileListLen]
  220. if (result.code === 200) {
  221. successNum++
  222. } else {
  223. failNum++
  224. }
  225. this[`fileList${event.name}`].splice(
  226. fileListLen,
  227. 1,
  228. Object.assign(item, {
  229. status: result.code === 200 ? 'success' : 'fail',
  230. message: result.code === 200 ? '' : '上传失败',
  231. url: result.code === 200 ? result.url : '',
  232. fullurl: result.code === 200 ? result.url : ''
  233. })
  234. )
  235. fileListLen++
  236. }
  237. if(!this.showImg) {
  238. const files = this[`fileList${event.name}`].length ? this[`fileList${event.name}`][0] : ''
  239. this[`fileList${event.name}`] = []
  240. this.$emit('change',files)
  241. return
  242. }
  243. if (lists.length > 1) {
  244. uni.showModal({
  245. content: `本次上传成功${successNum}个,失败${failNum}个`,
  246. showCancel: false
  247. })
  248. }
  249. this[`fileList${event.name}`] = this[`fileList${event.name}`].filter((item) => item.status === 'success')
  250. const files = this[`fileList${event.name}`].length ? this[`fileList${event.name}`].map((item) => item.fullurl).join(',') : ''
  251. this.$emit('input', files)
  252. },
  253. uploadFilePromise(url) {
  254. return new Promise((resolve, reject) => {
  255. let a = uni.uploadFile({
  256. url: this.$UPLOAD_URL, //
  257. filePath: url,
  258. name: 'file',
  259. header: {
  260. 'x-access-token': `${this.$store.getters.token}`
  261. },
  262. success: (res) => {
  263. let data = JSON.parse(res.data)
  264. resolve(data)
  265. }
  266. })
  267. })
  268. },
  269. // 设置域名
  270. getUrl(url) {
  271. if (url.indexOf('http') === -1) url = this.$UPLOAD_BASE + url
  272. return url
  273. }
  274. }
  275. }
  276. </script>
  277. <style lang="scss" scoped>
  278. .upload_box {
  279. position: relative;
  280. .upload_btn_auth {
  281. // width: 180rpx;
  282. // height: 180rpx;
  283. }
  284. .upload_btn {
  285. // width: 180rpx;
  286. // height: 180rpx;
  287. position: relative;
  288. }
  289. }
  290. </style>