Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

form-image.vue 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. console.log('输出内容', this.imgWidth)
  93. if (isNaN(this.imgWidth) || this.imgWidth === 0) { //判断是否不是数字
  94. uni.getSystemInfo({
  95. success: function(res) {
  96. _this.innerWidth = res.windowWidth / 4 - 18
  97. }
  98. })
  99. } else {
  100. _this.innerWidth = this.imgWidth - 18
  101. }
  102. },
  103. mounted() { //当视图运行完后执行
  104. },
  105. data() {
  106. return {
  107. sourceTypeIndex: 2,
  108. innerWidth: 0,
  109. imageList: [],
  110. item: {},
  111. urls: '',
  112. update: true
  113. };
  114. },
  115. watch: { //计算属性
  116. imageList(val) {
  117. console.log('图片数据改变了', this.imageList.length);
  118. }
  119. },
  120. methods: {
  121. previewImage(e, item) { //放大图片
  122. // var current = this.isUrl ? this.urls + item[e] : item[e]
  123. if (this.isUrl) {
  124. for (var i = 0; i < item.length; i++) {
  125. if (item[i].indexOf("http") == -1) {
  126. item[i] = this.urls + item[i]
  127. }
  128. }
  129. }
  130. var current = item[e]
  131. console.log('输出内容', current);
  132. uni.previewImage({
  133. current: current,
  134. urls: item
  135. })
  136. },
  137. removeImg(e, item) { //删除图片
  138. item.splice(e, 1)
  139. },
  140. chooseImage: function(item) { //选择相机或相册时
  141. let _this = this
  142. if (_this.imageList === undefined) {
  143. _this.imageList = []
  144. }
  145. if (_this.imageList.length === this.count) {
  146. uni.showToast({
  147. icon: 'none',
  148. title: '最多只能选择' + _this.count + '张图片'
  149. })
  150. return
  151. }
  152. /* 图片选择 */
  153. uni.chooseImage({
  154. count: _this.count,
  155. success: (res) => { //选择成功
  156. _this.imageList = _this.imageList.concat(res.tempFilePaths);
  157. const tempFilePaths = res.tempFilePaths;
  158. const imgUrl = '';
  159. _this.$emit('backImg', _this.imageList) //测试环境下(正式环境请切换至接口返回地址)
  160. //task_img
  161. for (var i = 0; i < tempFilePaths.length; i++) {
  162. /* 图片上传 */
  163. uni.uploadFile({
  164. url: 'https://www.example.com/upload', //仅为示例,非真实的接口地址
  165. filePath: tempFilePaths[i],
  166. name: 'file',
  167. header: {
  168. 'X-Access-Token': uni.getStorageSync('token')
  169. },
  170. formData: {
  171. biz: 'app'
  172. },
  173. success: (uploadFileRes) => {
  174. let imgData = JSON.parse(uploadFileRes.data)
  175. /* 数据拼接 */
  176. if (imgUrl === undefined || imgUrl === '') {
  177. imgUrl = imgData.message
  178. } else {
  179. imgUrl += ',' + imgData.message
  180. }
  181. _this.$emit('backImg', _this.imageList) //正式环境下
  182. },
  183. fail: (res) => {
  184. console.log(res);
  185. }
  186. });
  187. }
  188. },
  189. fail: (err) => {
  190. if (err.errMsg.indexOf('cancel') !== '-1') {
  191. return;
  192. }
  193. uni.getSetting({
  194. success: (res) => {
  195. let authStatus = false;
  196. switch (this.sourceTypeIndex) {
  197. case 0:
  198. authStatus = res.authSetting['scope.camera'];
  199. break;
  200. case 1:
  201. authStatus = res.authSetting['scope.album'];
  202. break;
  203. case 2:
  204. authStatus = res.authSetting['scope.album'] && res
  205. .authSetting['scope.camera'];
  206. break;
  207. default:
  208. break;
  209. }
  210. if (!authStatus) {
  211. uni.showModal({
  212. title: '授权失败',
  213. content: '需要从您的相机或相册获取图片,请在设置界面打开相关权限',
  214. success: (res) => {
  215. if (res.confirm) {
  216. uni.openSetting()
  217. }
  218. }
  219. })
  220. }
  221. }
  222. })
  223. }
  224. })
  225. }
  226. }
  227. }
  228. </script>
  229. <style>
  230. .text8 {
  231. font-size: 11px;
  232. font-family: PingFang SC;
  233. color: #C8C8C8;
  234. opacity: 1;
  235. }
  236. .layout4 {
  237. background: #F8F9FA;
  238. opacity: 1;
  239. border-radius: 8px;
  240. }
  241. </style>