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.

form-image.vue 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. pathToBase64
  95. } from "@/utils/util/imageTool.js";
  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: 9,
  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. pathToBase64(tempFilePaths[i])
  248. .then((path) => {
  249. var data = {
  250. fileBase64: path
  251. };
  252. const options = {
  253. type: 2,
  254. data: data,
  255. method: "POST",
  256. showLoading: true,
  257. };
  258. request(fileUpload, options).then((res) => {
  259. const data = stringToJson(res.bizContent);
  260. _this.imageList.push(data.data.url);
  261. for (var k = 0; k < _this.imageList.length; k++) {
  262. _this.imageList[k] = this.strReplace(_this
  263. .imageList[k])
  264. }
  265. _this.$emit("backImg", _this
  266. .imageList); //测试环境下(正式环境请切换至接口返回地址)
  267. console.log(" _this.imageList", _this.imageList)
  268. });
  269. })
  270. .catch((error) => {
  271. console.log(error)
  272. });
  273. /* 图片上传 */
  274. // uni.uploadFile({
  275. // url: 'https://www.example.com/upload', //仅为示例,非真实的接口地址
  276. // filePath: tempFilePaths[i],
  277. // name: 'file',
  278. // header: {
  279. // 'X-Access-Token': uni.getStorageSync('token')
  280. // },
  281. // formData: {
  282. // biz: 'app'
  283. // },
  284. // success: (uploadFileRes) => {
  285. // let imgData = JSON.parse(uploadFileRes.data)
  286. // /* 数据拼接 */
  287. // if (imgUrl === undefined || imgUrl === '') {
  288. // imgUrl = imgData.message
  289. // } else {
  290. // imgUrl += ',' + imgData.message
  291. // }
  292. // _this.$emit('backImg', _this.imageList) //正式环境下
  293. // },
  294. // fail: (res) => {
  295. // console.log(res);
  296. // }
  297. // });
  298. }
  299. },
  300. fail: (err) => {
  301. if (err.errMsg.indexOf("cancel") !== "-1") {
  302. return;
  303. }
  304. uni.getSetting({
  305. success: (res) => {
  306. let authStatus = false;
  307. switch (this.sourceTypeIndex) {
  308. case 0:
  309. authStatus = res.authSetting["scope.camera"];
  310. break;
  311. case 1:
  312. authStatus = res.authSetting["scope.album"];
  313. break;
  314. case 2:
  315. authStatus =
  316. res.authSetting["scope.album"] &&
  317. res.authSetting["scope.camera"];
  318. break;
  319. default:
  320. break;
  321. }
  322. if (!authStatus) {
  323. uni.showModal({
  324. title: "授权失败",
  325. content: "需要从您的相机或相册获取图片,请在设置界面打开相关权限",
  326. success: (res) => {
  327. if (res.confirm) {
  328. uni.openSetting();
  329. }
  330. },
  331. });
  332. }
  333. },
  334. });
  335. },
  336. });
  337. },
  338. },
  339. };
  340. </script>
  341. <style>
  342. .text8 {
  343. font-size: 26rpx;
  344. font-family: PingFang SC;
  345. color: #333333;
  346. opacity: 1;
  347. }
  348. .layout4 {
  349. background: #f3f3f3;
  350. opacity: 1;
  351. border-radius: 8px;
  352. border: 1px solid #dcdcdc;
  353. }
  354. </style>