Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

form-image.vue 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <view>
  3. <view class="as-gravity-center" style="display: flex;flex-direction: row;flex-wrap: wrap;position: relative;"
  4. :style="'justify-content:' + (imageList.length === 0 ? 'center': 'space-between')">
  5. <!-- 嵌套这层就over了 -->
  6. <view :style="'margin-left: ' + (retract + 10) + 'rpx;margin-right: '+ retract + 'rpx'"
  7. style="justify-content: flex-start; align-items: center;display: flex;flex-direction: row;flex-wrap: wrap;">
  8. <block v-for="(image,indexs) in imageList" :key="indexs">
  9. <view :style="'width:' + innerWidth + 'px;' +'height:' + innerWidth + 'px'"
  10. style="display: block;margin-left: 5px;margin-top: 5px; opacity:0.7;">
  11. <view class="as-gravity-center" @tap="previewImage(indexs,imageList)"
  12. :style="'width:' + (isDelete ? innerWidth / 2 : innerWidth / 2) + 'px;' +'height:' + innerWidth + 'px'"
  13. style="position: absolute;z-index: 3;opacity:1;">
  14. <image v-show="isDelete" src="/static/image/big.png" style="width: 12px;height: 12px;">
  15. </image>
  16. </view>
  17. <view v-show="isDelete" class="as-gravity-center" @tap="removeImg(indexs,imageList)"
  18. :style="'width:' + innerWidth / 2 + 'px;' +'height:' + innerWidth + 'px;' + 'margin-left: '+ innerWidth / 2 +'px;'"
  19. style="position: absolute;z-index: 3;opacity:1;">
  20. <image src="/static/image/remove.png" style="width: 12px;height: 12px;">
  21. </image>
  22. </view>
  23. <view :style="'width:' + innerWidth + 'px;' +'height:' + innerWidth + 'px'"
  24. style="position: absolute;z-index: 2;background-color: #B2B2B2;opacity:0.5;"></view>
  25. <image :style="'width:' + innerWidth + 'px;' +'height:' + innerWidth + 'px'"
  26. class="as-radius-8px" style="position: relative; display: block;"
  27. :src="isUrl ? urls + image : image" :data-src="isUrl ? urls + image : image">
  28. </image>
  29. </view>
  30. </block>
  31. <view v-if="isChoose" class="layout4 as-gravity-center" @tap="chooseImage(item)"
  32. :style="'width:' + innerWidth + 'px;height:' + innerWidth + 'px'"
  33. style="display: flex;flex-direction: column;margin-top: 8px;margin-left: 5px;">
  34. <image src="/static/image/photo.png" style="height: 24px;width: 28.5px;"></image>
  35. <view class="text8" style="margin-top: 8px;">上传图片</view>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. export default {
  43. name: "form-image",
  44. props: {
  45. showImageList: {
  46. /* 图片数组*/
  47. type: Array,
  48. default: function() {
  49. return []
  50. }
  51. },
  52. imgWidth: {
  53. /*获取屏幕宽度*/
  54. type: Number,
  55. default: 0,
  56. required: false
  57. },
  58. retract: {
  59. /*缩进*/
  60. type: Number,
  61. default: 0
  62. },
  63. isChoose: {
  64. /*是否选择图片*/
  65. type: Boolean,
  66. default: true
  67. },
  68. isDelete: {
  69. /* 是否具备删除*/
  70. type: Boolean,
  71. default: true
  72. },
  73. isUrl: {
  74. /* 是否拼接图片地址*/
  75. type: Boolean,
  76. default: false
  77. },
  78. count: {
  79. /* 图片上传数量*/
  80. type: Number,
  81. default: 9
  82. }
  83. },
  84. created() {
  85. let {
  86. showImageList
  87. } = this
  88. if (showImageList.length > 0) {
  89. this.imageList = showImageList
  90. }
  91. let _this = this
  92. if (isNaN(this.imgWidth) || this.imgWidth === 0) { //判断是否不是数字
  93. uni.getSystemInfo({
  94. success: function(res) {
  95. _this.innerWidth = res.windowWidth / 4 - 18
  96. }
  97. })
  98. } else {
  99. _this.innerWidth = this.imgWidth - 18
  100. }
  101. },
  102. mounted() { //当视图运行完后执行
  103. },
  104. data() {
  105. return {
  106. sourceTypeIndex: 2,
  107. innerWidth: 0,
  108. imageList: [],
  109. item: {},
  110. urls: '',
  111. update: true
  112. };
  113. },
  114. watch: { //计算属性
  115. imageList(val) {
  116. console.log('图片数据改变了', this.imageList.length);
  117. }
  118. },
  119. methods: {
  120. previewImage(e, item) { //放大图片
  121. // var current = this.isUrl ? this.urls + item[e] : item[e]
  122. if (this.isUrl) {
  123. for (var i = 0; i < item.length; i++) {
  124. if (item[i].indexOf("http") == -1) {
  125. item[i] = this.urls + item[i]
  126. }
  127. }
  128. }
  129. var current = item[e]
  130. uni.previewImage({
  131. current: current,
  132. urls: item
  133. })
  134. },
  135. removeImg(e, item) { //删除图片
  136. item.splice(e, 1)
  137. },
  138. chooseImage: function(item) { //选择相机或相册时
  139. let _this = this
  140. if (_this.imageList === undefined) {
  141. _this.imageList = []
  142. }
  143. if (_this.imageList.length === this.count) {
  144. uni.showToast({
  145. icon: 'none',
  146. title: '最多只能选择' + _this.count + '张图片'
  147. })
  148. return
  149. }
  150. /* 图片选择 */
  151. uni.chooseImage({
  152. count: _this.count,
  153. success: (res) => { //选择成功
  154. _this.imageList = _this.imageList.concat(res.tempFilePaths);
  155. const tempFilePaths = res.tempFilePaths;
  156. const imgUrl = '';
  157. _this.$emit('backImg', _this.imageList) //测试环境下(正式环境请切换至接口返回地址)
  158. //task_img
  159. for (var i = 0; i < tempFilePaths.length; i++) {
  160. /* 图片上传 */
  161. uni.uploadFile({
  162. url: 'https://www.example.com/upload', //仅为示例,非真实的接口地址
  163. filePath: tempFilePaths[i],
  164. name: 'file',
  165. header: {
  166. 'X-Access-Token': uni.getStorageSync('token')
  167. },
  168. formData: {
  169. biz: 'app'
  170. },
  171. success: (uploadFileRes) => {
  172. let imgData = JSON.parse(uploadFileRes.data)
  173. /* 数据拼接 */
  174. if (imgUrl === undefined || imgUrl === '') {
  175. imgUrl = imgData.message
  176. } else {
  177. imgUrl += ',' + imgData.message
  178. }
  179. _this.$emit('backImg', _this.imageList) //正式环境下
  180. },
  181. fail: (res) => {
  182. console.log(res);
  183. }
  184. });
  185. }
  186. },
  187. fail: (err) => {
  188. if (err.errMsg.indexOf('cancel') !== '-1') {
  189. return;
  190. }
  191. uni.getSetting({
  192. success: (res) => {
  193. let authStatus = false;
  194. switch (this.sourceTypeIndex) {
  195. case 0:
  196. authStatus = res.authSetting['scope.camera'];
  197. break;
  198. case 1:
  199. authStatus = res.authSetting['scope.album'];
  200. break;
  201. case 2:
  202. authStatus = res.authSetting['scope.album'] && res
  203. .authSetting['scope.camera'];
  204. break;
  205. default:
  206. break;
  207. }
  208. if (!authStatus) {
  209. uni.showModal({
  210. title: '授权失败',
  211. content: '需要从您的相机或相册获取图片,请在设置界面打开相关权限',
  212. success: (res) => {
  213. if (res.confirm) {
  214. uni.openSetting()
  215. }
  216. }
  217. })
  218. }
  219. }
  220. })
  221. }
  222. })
  223. }
  224. }
  225. }
  226. </script>
  227. <style>
  228. .text8 {
  229. font-size: 11px;
  230. font-family: PingFang SC;
  231. color: #C8C8C8;
  232. opacity: 1;
  233. }
  234. .layout4 {
  235. background: #F8F9FA;
  236. opacity: 1;
  237. border-radius: 8px;
  238. }
  239. </style>