userAlarmUpload.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <view>
  3. <view class="input-box ddflex">
  4. <view class="input fflex">
  5. <input class="fflex" placeholder="请输入设备编号"/>
  6. </view>
  7. <image class="saoma" src="../../static/pages/images/saoma.png"></image>
  8. </view>
  9. <view>
  10. <view class="item ddflex">
  11. <view class="label">所属门店</view>
  12. <view class="item-input fflex">
  13. <input v-model="name" :disabled="true" class="fflex"/>
  14. </view>
  15. </view>
  16. <view class="item ddflex">
  17. <view class="label">机柜名称</view>
  18. <view class="item-input fflex">
  19. <input v-model="name" :disabled="true" class="fflex"/>
  20. </view>
  21. </view>
  22. <view class="item ddflex">
  23. <view class="label">巡检时间</view>
  24. <view class="item-input fflex">
  25. <biaofundatetimepicker class="biao fflex" :dateStr="time"
  26. placeholder="巡检时间" fields="minute" @change="changeEndDatetimePicker"></biaofundatetimepicker>
  27. </view>
  28. </view>
  29. <view class="item ddflex">
  30. <view class="label">检修记录表</view>
  31. <view class="item-input fflex" style="color: #2A82E4;" @click="choiceImages">
  32. 点击上传
  33. </view>
  34. </view>
  35. <view class="ddflex" style="margin-bottom: 40rpx;padding: 30rpx;">
  36. <view class="product-image" v-for="item,index in imageList">
  37. <image style="width: 100%;height: 100%;margin-bottom: 0;" :src="item"></image>
  38. <image class="product-image-delete" src="../static/images/imagecuo.png" @click="removeProductImage(index)"></image>
  39. </view>
  40. </view>
  41. </view>
  42. <!-- 保存按钮 -->
  43. <view class="savepadding submit-box">
  44. <view class="submit-btn">
  45. 保存
  46. </view>
  47. </view>
  48. <canvas canvas-id="canvas" :style="'width:' + cWidth + 'px;height:' + cHeight +'px;position: absolute;left:-1000px;top:-1000px;'"></canvas>
  49. </view>
  50. </template>
  51. <script>
  52. const app = getApp();
  53. const req = require("../../utils/request.js");
  54. import biaofundatetimepicker from '../../components/biaofun-datetime-picker/biaofun-datetime-picker.vue';
  55. export default {
  56. components: {
  57. biaofundatetimepicker
  58. },
  59. props: {},
  60. data() {
  61. return {
  62. systems: {},
  63. isTop:0,
  64. name:'白云科技门店',
  65. time:'',
  66. imageList:[],
  67. cWidth: 0,
  68. cHeight: 0,
  69. }
  70. },
  71. onLoad(options) {
  72. },
  73. onShow() {
  74. },
  75. methods: {
  76. changeEndDatetimePicker(val) {
  77. this.time = val.f1 + " " + val.hh+":"+val.mm+":"+"00"
  78. },
  79. // 图片移除
  80. // 移除产品图片
  81. removeProductImage(index){
  82. this.imageList.splice(index,1)
  83. },
  84. // 图片上传
  85. choiceImages(event) {
  86. let that = this;
  87. uni.chooseImage({
  88. count: 9,
  89. sourceType: ['album', 'camera'],
  90. sizeType: ['compressed'],
  91. async success(res) {
  92. console.log(res);
  93. let list = res.tempFiles;
  94. console.log(list);
  95. let allList = [];
  96. for (let i = 0; i < list.length; i++) {
  97. //两M
  98. if (list[i].size > 50000) {
  99. var path = await that.compressImg(list[i].path)
  100. console.log(path)
  101. var data = {
  102. formData: {
  103. flag: 726
  104. },
  105. filePath: path,
  106. name: 'file'
  107. }
  108. allList.push(data);
  109. } else {
  110. var data = {
  111. formData: {
  112. flag: 722
  113. },
  114. filePath: list[i].path,
  115. name: 'file'
  116. }
  117. allList.push(data);
  118. }
  119. }
  120. console.log(allList)
  121. let successUp = 0;
  122. let failUp = 0;
  123. let count = 0
  124. // 多图上传 allList:图片数组 successUp:上传成功次数 现在上传的下标
  125. that.uploadImgs(allList, successUp, failUp, count);
  126. },
  127. fail(err) {}
  128. })
  129. },
  130. //通过canvans缩放长宽来压缩图片 path:图片本地路径 limitNum:压缩到长宽多大
  131. async compressImg(path, multiple = 2, limitNum = 600) {
  132. console.log('图片链接', path)
  133. let that = this;
  134. return new Promise((resolve, reject) => {
  135. uni.getImageInfo({
  136. src: path,
  137. success(res) {
  138. var canvasWidth = res.width //图片原始长宽
  139. var canvasHeight = res.height
  140. console.log('图片的基本信息', res)
  141. // while循环 保证宽高在100以内 Math.trunc()去掉小数点右边数
  142. while (canvasWidth > limitNum || canvasHeight > limitNum) {
  143. canvasWidth = Math.trunc(res.width / multiple)
  144. canvasHeight = Math.trunc(res.height / multiple)
  145. multiple++
  146. }
  147. //设置画布长宽
  148. that.setData({
  149. cWidth: canvasWidth,
  150. cHeight: canvasHeight
  151. })
  152. //----------绘制图形并取出图片路径--------------
  153. var ctx = uni.createCanvasContext('canvas')
  154. ctx.drawImage(res.path, 0, 0, canvasWidth, canvasHeight)
  155. ctx.draw(false, setTimeout(() => {
  156. uni.canvasToTempFilePath({
  157. canvasId: 'canvas',
  158. destWidth: canvasWidth,
  159. destHeight: canvasHeight,
  160. success: function(res) {
  161. console.log(res.tempFilePath) //图片路径
  162. resolve(res.tempFilePath)
  163. },
  164. fail: function(res) {
  165. console.log(res.errMsg)
  166. }
  167. })
  168. }, 100))
  169. },
  170. fail: function(res) {
  171. console.log(res.errMsg)
  172. },
  173. })
  174. })
  175. },
  176. //多张图片上传
  177. uploadImgs: function(allList, successUp, failUp, count) {
  178. console.log(allList, successUp, failUp, count)
  179. let that = this;
  180. let length = allList.length; //总共上传的数量
  181. var promise = Promise.all(allList.map(tempFilePath => {
  182. return new Promise(function(resolve, reject) {
  183. req.loadIng("上传中");
  184. req.uploadFile('/api/upload', tempFilePath.filePath, res => {
  185. that.imageList = that.imageList.concat(res.src);
  186. resolve()
  187. });
  188. });
  189. }));
  190. promise.then(function(results) {
  191. uni.showToast({
  192. title: '上传成功',
  193. icon: 'success',
  194. duration: 2000
  195. });
  196. // console.log(results);
  197. }).catch(function(err) {
  198. uni.hideToast()
  199. // console.log(err);
  200. });
  201. },
  202. },
  203. mounted() {
  204. const systemInfo = uni.getSystemInfoSync();
  205. // px转换到rpx的比例
  206. let pxToRpxScale = 750 / systemInfo.windowWidth;
  207. let systems = {
  208. ktxStatusHeight: systemInfo.statusBarHeight * pxToRpxScale, // 状态栏的高度
  209. navigationHeight: 44 * pxToRpxScale // 导航栏的高度
  210. };
  211. systems.barHeight = systems.ktxStatusHeight + systems.navigationHeight;
  212. this.systems = systems;
  213. },
  214. onPageScroll: function(e) {
  215. if (e.scrollTop > this.systems.barHeight) {
  216. this.isTop = 1;
  217. } else {
  218. this.isTop = 0;
  219. }
  220. }
  221. }
  222. </script>
  223. <style>
  224. @import "./userAlarmUpload.css";
  225. </style>