upload.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <view class="content">
  3. <view class="cropper-wrapper" style="height:617px">
  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. this.parseBlob(avatar)
  81. // this.saveHeadImgFile(avatar)
  82. // 获取到裁剪后的图片
  83. // uni.redirectTo({
  84. // url: '/pages/user/upload?avatar=' + avatar
  85. // })
  86. //下面是上传到服务器的方法
  87. // uni.uploadFile({
  88. // url: config.def().baseUrlNew + 'appendix/api/uploadFiles',
  89. // filePath: avatar,
  90. // name: 'file',
  91. // // formData: { token: token, userId: userId},
  92. // success: res => {
  93. // debugger
  94. // console.log('uploadImage success, res is:', res);
  95. // uni.showToast({
  96. // title: '上传成功',
  97. // icon: 'success',
  98. // duration: 1000
  99. // });
  100. // },
  101. // ail: err => {
  102. // console.log('uploadImage fail', err);
  103. // uni.showModal({
  104. // content: err.errMsg,
  105. // showCancel: false
  106. // });
  107. // uni.hideLoading();
  108. // },
  109. // complete: () => {
  110. // console.log('complate');
  111. // }
  112. // });
  113. } else {
  114. console.log('获取图片失败,请稍后重试');
  115. }
  116. });
  117. },
  118. parseBlob(base64) {
  119. let that = this
  120. var arr = base64.split(',');
  121. var mime = arr[0].match(/:(.*?);/)[1];
  122. var bstr = atob(arr[1]);
  123. var n = bstr.length;
  124. var u8arr = new Uint8Array(n);
  125. for (var i = 0; i < n; i++) {
  126. u8arr[i] = bstr.charCodeAt(i);
  127. }
  128. var url = URL || webkitURL;
  129. let pcUserInfo = uni.getStorageSync('pcUserInfo')
  130. let address = url.createObjectURL(new Blob([u8arr], {
  131. type: mime
  132. }))
  133. uni.uploadFile({
  134. url: config.def().baseUrlNew + 'appendix/api/uploadFiles',
  135. filePath: address,
  136. name: 'fileName',
  137. formData: {
  138. companyId: pcUserInfo.compId,
  139. modelId: '',
  140. vesselId: '',
  141. },
  142. success: (res) => {
  143. let src = JSON.parse(res.data).data.appendixPath
  144. uni.redirectTo({
  145. url: '/pages/user/set_picture?src=' + src
  146. });
  147. // uni.showToast({
  148. // title: '上传成功',
  149. // icon: 'success',
  150. // duration: 3000,
  151. // complete(){
  152. // }
  153. // });
  154. },
  155. ail: err => {
  156. console.log('uploadImage fail', err);
  157. uni.showModal({
  158. content: err.errMsg,
  159. showCancel: false
  160. });
  161. uni.hideLoading();
  162. },
  163. complete: () => {
  164. console.log('complate');
  165. }
  166. });
  167. return url.createObjectURL(new Blob([u8arr], {
  168. type: mime
  169. }));
  170. },
  171. uploadTap() {
  172. const self = this;
  173. uni.chooseImage({
  174. count: 1, // 默认9
  175. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  176. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  177. success(res) {
  178. let src = res.tempFilePaths[0];
  179. // 获取裁剪图片资源后,给data添加src属性及其值
  180. self.weCropper.pushOrign(src);
  181. }
  182. });
  183. }
  184. },
  185. onLoad(option) {
  186. // do something
  187. const cropperOpt = this.cropperOpt;
  188. const src = option.src;
  189. if (src) {
  190. Object.assign(cropperOpt, {
  191. src
  192. });
  193. this.weCropper = new weCropper(cropperOpt)
  194. .on('ready', function(ctx) {})
  195. .on('beforeImageLoad', ctx => {
  196. uni.showToast({
  197. title: '上传中',
  198. icon: 'loading',
  199. duration: 3000
  200. });
  201. })
  202. .on('imageLoad', ctx => {
  203. uni.hideToast();
  204. });
  205. }
  206. }
  207. };
  208. </script>
  209. <style>
  210. .content {
  211. background: rgba(255, 255, 255, 1);
  212. }
  213. .head-list {
  214. height: 43px;
  215. width: 100%;
  216. background: #ffffff;
  217. justify-content: center;
  218. align-items: center;
  219. display: flex;
  220. border-bottom: 1px solid rgba(244, 244, 244, 1);
  221. }
  222. .head-info {
  223. text-align: center;
  224. font-size: 18px;
  225. color: #000000;
  226. font-weight: bold;
  227. }
  228. .save-box {
  229. position: absolute;
  230. right: 0px;
  231. width: 50px;
  232. height: 43px;
  233. line-height: 43px;
  234. }
  235. .save {
  236. color: rgba(98, 111, 252, 1);
  237. font-size: 16px;
  238. font-weight: 400;
  239. }
  240. .icon-back {
  241. margin-top: 11px;
  242. width: 10px;
  243. height: 18px;
  244. color: #000000;
  245. margin-left: 6px;
  246. }
  247. .icon-back-box {
  248. display: block;
  249. position: absolute;
  250. left: 6px;
  251. height: 43px;
  252. width: 30px;
  253. align-items: center;
  254. }
  255. .cropper {
  256. position: absolute;
  257. top: 0;
  258. left: 0;
  259. width: 100%;
  260. height: 100%;
  261. }
  262. .cropper-buttons {
  263. background-color: #FFFFFF ;
  264. color: #22C572;
  265. }
  266. .cropper-wrapper {
  267. position: relative;
  268. display: flex;
  269. flex-direction: row;
  270. justify-content: space-between;
  271. align-items: center;
  272. width: 100%;
  273. background-color: #F0F0F0;
  274. }
  275. .cropper-buttons {
  276. width: 100vw;
  277. height: 50px;
  278. display: flex;
  279. flex-direction: row;
  280. justify-content: space-between;
  281. align-items: center;
  282. position: fixed;
  283. bottom: 0;
  284. left: 0;
  285. line-height: 50px;
  286. }
  287. .cropper-buttons .upload,
  288. .cropper-buttons .getCropperImage {
  289. width: 50%;
  290. text-align: center;
  291. }
  292. </style>