Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <view>
  3. <view class="as-gravity-center" style="
  4. display: flex;
  5. flex-direction: row;
  6. flex-wrap: wrap;
  7. position: relative;
  8. " :style="
  9. 'justify-content:' +
  10. (imageList.length === 0 ? 'flex-start' : 'space-between')
  11. ">
  12. <!-- 嵌套这层就over了 -->
  13. <view :style="
  14. 'margin-left: ' +
  15. (retract + 10) +
  16. 'rpx;margin-right: ' +
  17. retract +
  18. 'rpx'
  19. " style="
  20. justify-content: flex-start;
  21. align-items: center;
  22. display: flex;
  23. flex-direction: row;
  24. flex-wrap: wrap;
  25. ">
  26. <block v-for="(image, indexs) in imageList" :key="indexs">
  27. <view :style="
  28. 'width:' + innerWidth + 'px;' + 'height:' + innerWidth + 'px'
  29. " style="
  30. display: block;
  31. margin-left: 5px;
  32. margin-top: 5px;
  33. opacity: 0.7;
  34. ">
  35. <view class="as-gravity-center" @tap="previewImage(indexs, imageList)" :style="
  36. 'width:' +
  37. (isDelete ? innerWidth / 2 : innerWidth / 2) +
  38. 'px;' +
  39. 'height:' +
  40. innerWidth +
  41. 'px'
  42. " style="position: absolute; z-index: 3; opacity: 1">
  43. <image v-show="isDelete" :src="`${$imgUrl}common/big.png`"
  44. style="width: 40rpx; height: 40rpx" mode="aspectFill">
  45. </image>
  46. </view>
  47. <view v-show="isDelete" class="as-gravity-center" @tap="removeImg(indexs, imageList)" :style="
  48. 'width:' +
  49. innerWidth / 2 +
  50. 'px;' +
  51. 'height:' +
  52. innerWidth +
  53. 'px;' +
  54. 'margin-left: ' +
  55. innerWidth / 2 +
  56. 'px;'
  57. " style="position: absolute; z-index: 3; opacity: 1">
  58. <image :src="`${$imgUrl}common/remove.png`" style="width: 40rpx; height: 40rpx"
  59. mode="aspectFill">
  60. </image>
  61. </view>
  62. <view :style="
  63. 'width:' + innerWidth + 'px;' + 'height:' + innerWidth + 'px'
  64. " style="
  65. position: absolute;
  66. z-index: 2;
  67. background-color: #b2b2b2;
  68. opacity: 0.5;
  69. "></view>
  70. <image :style="
  71. 'width:' + innerWidth + 'px;' + 'height:' + innerWidth + 'px'
  72. " class="as-radius-8px" style="position: relative; display: block" :src="isUrl ? urls + image : image"
  73. :data-src="isUrl ? urls + image : image" mode="aspectFill">
  74. </image>
  75. </view>
  76. </block>
  77. <view v-if="canChoose" class="layout4 as-gravity-center" @tap="chooseImage(item)"
  78. :style="'width:' + innerWidth + 'px;height:' + innerWidth + 'px'" style="
  79. display: flex;
  80. flex-direction: column;
  81. margin-top: 8px;
  82. margin-left: 5px;
  83. ">
  84. <image mode="aspectFill" :src="`${$imgUrl}common/photo.png`" style="height: 56rpx; width: 56rpx">
  85. </image>
  86. <view v-if="showTxt" class="text8" style="margin-top: 8px">上传图片</view>
  87. </view>
  88. </view>
  89. </view>
  90. </view>
  91. </template>
  92. <script>
  93. import {
  94. uploadFile
  95. } from "@/utils/utils";
  96. import {
  97. request
  98. } from "@/utils/network/request";
  99. import {
  100. envs,
  101. fileUpload
  102. } from "@/utils/network/api";
  103. import {
  104. stringToJson
  105. } from "@/utils/network/encryption";
  106. export default {
  107. name: "form-image",
  108. props: {
  109. showImageList: {
  110. /* 图片数组*/
  111. type: Array,
  112. default: function() {
  113. return [];
  114. },
  115. },
  116. imgWidth: {
  117. /*获取屏幕宽度*/
  118. type: Number,
  119. default: 0,
  120. required: false,
  121. },
  122. retract: {
  123. /*缩进*/
  124. type: Number,
  125. default: 0,
  126. },
  127. isChoose: {
  128. /*是否选择图片*/
  129. type: Boolean,
  130. default: true,
  131. },
  132. isDelete: {
  133. /* 是否具备删除*/
  134. type: Boolean,
  135. default: true,
  136. },
  137. isUrl: {
  138. /* 是否拼接图片地址*/
  139. type: Boolean,
  140. default: false,
  141. },
  142. count: {
  143. /* 图片上传数量*/
  144. type: Number,
  145. default: 5,
  146. },
  147. showTxt: {
  148. /* 是否显示上传图片文字*/
  149. type: Boolean,
  150. default: true,
  151. },
  152. },
  153. created() {
  154. let {
  155. showImageList
  156. } = this;
  157. if (showImageList.length > 0) {
  158. this.imageList = showImageList;
  159. }
  160. let _this = this;
  161. //console.log('输出内容', this.imgWidth)
  162. if (isNaN(this.imgWidth) || this.imgWidth === 0) {
  163. //判断是否不是数字
  164. uni.getSystemInfo({
  165. success: function(res) {
  166. _this.innerWidth = res.windowWidth / 4 - 18;
  167. },
  168. });
  169. } else {
  170. _this.innerWidth = this.imgWidth - 18;
  171. }
  172. },
  173. mounted() {
  174. //当视图运行完后执行
  175. },
  176. data() {
  177. return {
  178. sourceTypeIndex: 2,
  179. innerWidth: 0,
  180. imageList: [],
  181. item: {},
  182. urls: "",
  183. update: true,
  184. canChoose: this.isChoose,
  185. };
  186. },
  187. watch: {
  188. //计算属性
  189. imageList(val) {
  190. //console.log('图片数据改变了', this.imageList.length);
  191. },
  192. },
  193. methods: {
  194. strReplace(str) {
  195. let imgUrl = str.replace("http://192.168.101.145:9000", envs[process.env.NODE_ENV].baseUrl);
  196. imgUrl = imgUrl.replace("http://100.64.2.113:9000", envs[process.env.NODE_ENV].baseUrl);
  197. console.log(imgUrl)
  198. return imgUrl;
  199. },
  200. previewImage(e, item) {
  201. //放大图片
  202. // var current = this.isUrl ? this.urls + item[e] : item[e]
  203. if (this.isUrl) {
  204. for (var i = 0; i < item.length; i++) {
  205. if (item[i].indexOf("http") == -1) {
  206. item[i] = this.urls + item[i];
  207. }
  208. }
  209. }
  210. var current = item[e];
  211. console.log("输出内容", current);
  212. uni.previewImage({
  213. current: current,
  214. urls: item,
  215. });
  216. },
  217. removeImg(e, item) {
  218. //删除图片
  219. this.$emit("removeImg", item, e);
  220. item.splice(e, 1);
  221. this.canChoose = this.imageList.length === this.count ? false : true;
  222. },
  223. chooseImage: function(item) {
  224. //选择相机或相册时
  225. let _this = this;
  226. if (_this.imageList === undefined) {
  227. _this.imageList = [];
  228. }
  229. if (_this.imageList.length === this.count) {
  230. uni.showToast({
  231. icon: "none",
  232. title: "最多只能选择" + _this.count + "张图片",
  233. });
  234. return;
  235. }
  236. /* 图片选择 */
  237. uni.chooseImage({
  238. count: _this.count,
  239. success: (res) => {
  240. //选择成功
  241. // _this.imageList = _this.imageList.concat(res.tempFilePaths);
  242. const tempFilePaths = res.tempFilePaths;
  243. const imgUrl = "";
  244. //_this.$emit('backImg', _this.imageList) //测试环境下(正式环境请切换至接口返回地址)
  245. this.canChoose = _this.imageList.length === this.count ? false : true;
  246. for (var i = 0; i < tempFilePaths.length; i++) {
  247. uploadFile(tempFilePaths[i], "", "").then((data) => {
  248. _this.imageList.push(data);
  249. if (_this.imageList.length > 5) {
  250. var newImg = _this.imageList.slice(0, 5)
  251. for (var k = 0; k < newImg.length; k++) {
  252. newImg[k] = this.strReplace(newImg[k])
  253. }
  254. _this.imageList = newImg
  255. _this.$emit("backImg", newImg); //测试环境下(正式环境请切换至接口返回地址)
  256. uni.showToast({
  257. icon: "none",
  258. title: "最多只能选择" + _this.count + "张图片",
  259. });
  260. return;
  261. } else {
  262. for (var k = 0; k < _this.imageList.length; k++) {
  263. _this.imageList[k] = this.strReplace(_this
  264. .imageList[k])
  265. }
  266. _this.$emit("backImg", _this
  267. .imageList); //测试环境下(正式环境请切换至接口返回地址)
  268. }
  269. console.log(" _this.imageList", _this.imageList)
  270. })
  271. }
  272. },
  273. fail: (err) => {
  274. if (err.errMsg.indexOf("cancel") !== "-1") {
  275. return;
  276. }
  277. uni.getSetting({
  278. success: (res) => {
  279. let authStatus = false;
  280. switch (this.sourceTypeIndex) {
  281. case 0:
  282. authStatus = res.authSetting["scope.camera"];
  283. break;
  284. case 1:
  285. authStatus = res.authSetting["scope.album"];
  286. break;
  287. case 2:
  288. authStatus =
  289. res.authSetting["scope.album"] &&
  290. res.authSetting["scope.camera"];
  291. break;
  292. default:
  293. break;
  294. }
  295. if (!authStatus) {
  296. uni.showModal({
  297. title: "授权失败",
  298. content: "需要从您的相机或相册获取图片,请在设置界面打开相关权限",
  299. success: (res) => {
  300. if (res.confirm) {
  301. uni.openSetting();
  302. }
  303. },
  304. });
  305. }
  306. },
  307. });
  308. },
  309. });
  310. },
  311. },
  312. };
  313. </script>
  314. <style>
  315. .text8 {
  316. font-size: 26rpx;
  317. font-family: PingFang SC;
  318. color: #333333;
  319. opacity: 1;
  320. }
  321. .layout4 {
  322. background: #f3f3f3;
  323. opacity: 1;
  324. border-radius: 8px;
  325. border: 1px solid #dcdcdc;
  326. }
  327. </style>