洛阳学员端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
5.4 KiB

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