upload.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <template>
  2. <view class="content">
  3. <view class="cropper-wrapper" style="height:100vh">
  4. <canvas class="cropper" disable-scroll="true" @touchstart="touchStart" @touchmove="touchMove"
  5. @touchend="touchEnd" :style="{ width: cropperOpt.width, height: cropperOpt.height }"
  6. canvas-id="cropper"></canvas>
  7. </view>
  8. <view class="cropper-buttons">
  9. <view class="upload" @tap="uploadTap">重新选择</view>
  10. <view class="getCropperImage" @tap="getCropperImage">确定</view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import weCropper from '../../components/upload/weCropper';
  16. import * as config from '@/config'
  17. const device = uni.getSystemInfoSync();
  18. const width = device.windowWidth;
  19. const height = device.windowHeight - 50;
  20. console.log(device);
  21. export default {
  22. data() {
  23. return {
  24. cropperOpt: {
  25. id: 'cropper',
  26. width: width,
  27. height: height,
  28. scale: 2.5,
  29. zoom: 8,
  30. cut: {
  31. x: (width - 295) / 2,
  32. y: (height - 300) / 2,
  33. width: 295,
  34. height: 300
  35. }
  36. },
  37. weCropper: '',
  38. };
  39. },
  40. methods: {
  41. back() {
  42. uni.redirectTo({
  43. url: '../infoDetail/infoDetail'
  44. });
  45. },
  46. touchStart(e) {
  47. this.weCropper.touchStart(e);
  48. },
  49. touchMove(e) {
  50. this.weCropper.touchMove(e);
  51. },
  52. touchEnd(e) {
  53. this.weCropper.touchEnd(e);
  54. },
  55. convertBase64UrlToBlob(dataURI, type) {
  56. var binary = atob(dataURI.split(',')[1]);
  57. var array = [];
  58. for (var i = 0; i < binary.length; i++) {
  59. array.push(binary.charCodeAt(i));
  60. }
  61. return new Blob([new Uint8Array(array)], {
  62. type: type
  63. }, {
  64. filename: '1111.jpg'
  65. });
  66. },
  67. blobToDataURL(blob) {
  68. var a = new FileReader();
  69. a.readAsDataURL(blob); //读取文件保存在result中
  70. a.onload = function(e) {
  71. var getRes = e.target.result; //读取的结果在result中
  72. console.log(getRes);
  73. };
  74. },
  75. getCropperImage() {
  76. let _this = this;
  77. // let pathurl = url + '/user/upload';上传服务器地址
  78. this.weCropper.getCropperImage(avatar => {
  79. if (avatar) {
  80. //下面是上传到服务器的方法
  81. // #ifdef H5
  82. this.parseBlob(avatar)
  83. // #endif
  84. // #ifdef APP-PLUS
  85. let pcUserInfo = uni.getStorageSync('pcUserInfo')
  86. uni.uploadFile({
  87. url: config.def().baseUrlNew + 'appendix/api/uploadFiles',
  88. filePath: avatar,
  89. name: 'fileName',
  90. formData: {
  91. companyId: pcUserInfo.compId,
  92. modelId: '',
  93. vesselId: '',
  94. },
  95. success: res => {
  96. let src = JSON.parse(res.data).data.appendixPath
  97. console.log(src)
  98. uni.redirectTo({
  99. url: '/pages/user/set_picture?src=' + src
  100. });
  101. },
  102. ail: err => {
  103. console.log('uploadImage fail', err);
  104. uni.showModal({
  105. content: err.errMsg,
  106. showCancel: false
  107. });
  108. uni.hideLoading();
  109. },
  110. complete: () => {
  111. console.log('complate');
  112. }
  113. });
  114. // #endif
  115. } else {
  116. console.log('获取图片失败,请稍后重试');
  117. }
  118. });
  119. },
  120. parseBlob(base64) {
  121. let that = this
  122. var arr = base64.split(',');
  123. var mime = arr[0].match(/:(.*?);/)[0];
  124. // var mime = arr[0].slice(arr[0].lastIndexOf("/")+1);
  125. var bstr = atob(arr[1]);
  126. var n = bstr.length;
  127. var u8arr = new Uint8Array(n);
  128. for (var i = 0; i < n; i++) {
  129. u8arr[i] = bstr.charCodeAt(i);
  130. }
  131. var url = URL || webkitURL;
  132. let pcUserInfo = uni.getStorageSync('pcUserInfo')
  133. let address = url.createObjectURL(new Blob([u8arr], {
  134. type: mime
  135. }))
  136. uni.uploadFile({
  137. url: config.def().baseUrlNew + 'appendix/api/uploadFiles',
  138. filePath: address,
  139. name: 'fileName',
  140. formData: {
  141. companyId: pcUserInfo.compId,
  142. modelId: '',
  143. vesselId: '',
  144. },
  145. success: (res) => {
  146. let src = JSON.parse(res.data).data.appendixPath
  147. uni.redirectTo({
  148. url: '/pages/user/set_picture?src=' + src
  149. });
  150. // uni.showToast({
  151. // title: '上传成功',
  152. // icon: 'success',
  153. // duration: 3000,
  154. // complete(){
  155. // }
  156. // });
  157. },
  158. ail: err => {
  159. console.log('uploadImage fail', err);
  160. uni.showModal({
  161. content: err.errMsg,
  162. showCancel: false
  163. });
  164. uni.hideLoading();
  165. },
  166. complete: () => {
  167. console.log('complate');
  168. }
  169. });
  170. return url.createObjectURL(new Blob([u8arr], {
  171. type: mime
  172. }));
  173. },
  174. uploadTap() {
  175. const self = this;
  176. uni.chooseImage({
  177. count: 1, // 默认9
  178. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  179. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  180. success(res) {
  181. let src = res.tempFilePaths[0];
  182. // 获取裁剪图片资源后,给data添加src属性及其值
  183. self.weCropper.pushOrign(src);
  184. }
  185. });
  186. }
  187. },
  188. onLoad(option) {
  189. // do something
  190. const cropperOpt = this.cropperOpt;
  191. const src = option.src;
  192. if (src) {
  193. Object.assign(cropperOpt, {
  194. src
  195. });
  196. this.weCropper = new weCropper(cropperOpt)
  197. .on('ready', function(ctx) {})
  198. .on('beforeImageLoad', ctx => {
  199. uni.showToast({
  200. title: '上传中',
  201. icon: 'loading',
  202. duration: 3000
  203. });
  204. })
  205. .on('imageLoad', ctx => {
  206. uni.hideToast();
  207. });
  208. }
  209. }
  210. };
  211. </script>
  212. <style>
  213. .content {
  214. background: rgba(255, 255, 255, 1);
  215. }
  216. .head-list {
  217. height: 43px;
  218. width: 100%;
  219. background: #ffffff;
  220. justify-content: center;
  221. align-items: center;
  222. display: flex;
  223. border-bottom: 1px solid rgba(244, 244, 244, 1);
  224. }
  225. .head-info {
  226. text-align: center;
  227. font-size: 18px;
  228. color: #000000;
  229. font-weight: bold;
  230. }
  231. .save-box {
  232. position: absolute;
  233. right: 0px;
  234. width: 50px;
  235. height: 43px;
  236. line-height: 43px;
  237. }
  238. .save {
  239. color: rgba(98, 111, 252, 1);
  240. font-size: 16px;
  241. font-weight: 400;
  242. }
  243. .icon-back {
  244. margin-top: 11px;
  245. width: 10px;
  246. height: 18px;
  247. color: #000000;
  248. margin-left: 6px;
  249. }
  250. .icon-back-box {
  251. display: block;
  252. position: absolute;
  253. left: 6px;
  254. height: 43px;
  255. width: 30px;
  256. align-items: center;
  257. }
  258. .cropper {
  259. position: absolute;
  260. top: 0;
  261. left: 0;
  262. width: 100%;
  263. height: 100%;
  264. }
  265. .cropper-buttons {
  266. background-color: #FFFFFF;
  267. color: #22C572;
  268. }
  269. .cropper-wrapper {
  270. position: relative;
  271. display: flex;
  272. flex-direction: row;
  273. justify-content: space-between;
  274. align-items: center;
  275. width: 100%;
  276. background-color: #F0F0F0;
  277. }
  278. .cropper-buttons {
  279. width: 100vw;
  280. height: 60px;
  281. display: flex;
  282. flex-direction: row;
  283. justify-content: space-between;
  284. align-items: center;
  285. position: fixed;
  286. bottom: 0;
  287. left: 0;
  288. line-height: 50px;
  289. }
  290. .cropper-buttons .upload,
  291. .cropper-buttons .getCropperImage {
  292. width: 50%;
  293. text-align: center;
  294. }
  295. </style>