clipper.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <view class="gui-img-crop">
  3. <u-navbar title="个人资料" :placeholder="true" bgColor="#2b2e3d" leftText="返回" :autoBack="true" titleStyle="font-weight:bold;color:#7a5831"></u-navbar>
  4. <view class="gui-cropper-wrapper" :style="{ height: height + 'px', width: width + 'px' }">
  5. <canvas
  6. canvas-id="bgCropper"
  7. id="bgCropper"
  8. class="gui-cropper-bg"
  9. :disable-scroll="true"
  10. @touchstart="touchStart"
  11. @touchmove="touchMove"
  12. @touchend="touchEnd"
  13. :style="{ width: width + 'px', height: height + 'px' }"
  14. ></canvas>
  15. <canvas canvas-id="prevCropper" id="prevCropper" class="gui-cropper-prev" :disable-scroll="false"></canvas>
  16. </view>
  17. <view class="gui-flex">
  18. <view class="gui-cropper-btn" @tap="selectImg">+ 选择图片</view>
  19. <view class="gui-cropper-btn" @tap="saveImg">保存图片</view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import WeCropper from '@/common/WeCropper.js';
  25. import * as common from '@/config/common.js'; //引入common
  26. import { baseUrl, baseApiUrl } from '@/config/config.js';
  27. var _this;
  28. export default {
  29. data() {
  30. return {
  31. token: '',
  32. // WeCropper 对象
  33. WeCropper: null,
  34. // 核心宽度系统会重新计算
  35. width: 300,
  36. // 核心高度系统会重新计算
  37. height: 500,
  38. // 剪裁设置对象
  39. cropperSets: {
  40. height: 500,
  41. width: 300,
  42. id: 'bgCropper',
  43. targetId: 'prevCropper',
  44. scale: 2.5, // 最大缩放倍数
  45. zoom: 8, // 缩放系数
  46. cut: {
  47. x: 0, // 裁剪框x轴起点
  48. y: 0, // 裁剪框y轴期起点
  49. width: 200, // 裁剪框宽度
  50. height: 200 // 裁剪框高度
  51. }
  52. },
  53. tmpFace: null, // 用于判断用户是否选择过图片
  54. imgType: '',
  55. transToJPG: ''
  56. };
  57. },
  58. onLoad: function (option) {
  59. // 获取屏幕信息规划剪裁组件宽度和高度
  60. const sysinfo = uni.getSystemInfoSync();
  61. this.width = sysinfo.windowWidth;
  62. this.height = sysinfo.windowHeight - sysinfo.statusBarHeight - 130;
  63. //#ifdef MP-WEIXIN
  64. let info = wx.getMenuButtonBoundingClientRect();
  65. this.height = sysinfo.windowHeight - info.top - sysinfo.statusBarHeight - 50;
  66. //#endif
  67. console.log(sysinfo);
  68. this.cropperSets.width = this.width;
  69. this.cropperSets.height = this.height;
  70. this.cropperSets.pixelRatio = sysinfo.pixelRatio;
  71. this.cropperSets.cut.x = (this.width - this.cropperSets.cut.width) / 2;
  72. this.cropperSets.cut.y = (this.height - this.cropperSets.cut.height) / 2;
  73. this.WeCropper = new WeCropper(this.cropperSets);
  74. _this = this;
  75. this.imgType = option.imgType;
  76. console.log(option);
  77. },
  78. methods: {
  79. selectImg: function () {
  80. uni.chooseImage({
  81. count: 1,
  82. sizeType: ['compressed'],
  83. sourceType: ['album', 'camera'],
  84. success: (res) => {
  85. this.WeCropper.pushOrign(res.tempFilePaths[0]);
  86. this.tmpFace = res.tempFilePaths[0];
  87. }
  88. });
  89. },
  90. touchStart(e) {
  91. this.WeCropper.touchStart(e);
  92. },
  93. touchMove(e) {
  94. this.WeCropper.touchMove(e);
  95. },
  96. touchEnd(e) {
  97. this.WeCropper.touchEnd(e);
  98. },
  99. saveImg: function () {
  100. if (this.tmpFace == null) {
  101. uni.showToast({
  102. title: '请选择图片',
  103. icon: 'none'
  104. });
  105. return;
  106. }
  107. uni.showLoading({
  108. mask: true,
  109. title: ''
  110. });
  111. this.WeCropper.getCropperImage((tempFilePath) => {
  112. if (tempFilePath) {
  113. console.log(tempFilePath);
  114. // 01. 此处 h5 端生成 base64 形式的图片数据, 其他端生成一个临时文件地址
  115. // 02. 可以连接 api 完成图片的上传工作
  116. // 03. 上传完成后关闭当页面,返回账户中心更新最新的头像地址即可
  117. let userToken = '';
  118. let auth = this.$db.get('auth');
  119. userToken = auth.token;
  120. console.log('userToken: ', userToken);
  121. // uni.getFileSystemManager().readFile({
  122. // filePath: tempFilePath, //选择图片返回的相对路径
  123. // encoding: 'jpeg', //编码格式
  124. // success: res => { //成功的回调
  125. // this.transToJPG = res.data
  126. // console.log("transToJPG", this.transToJPG);
  127. // },
  128. // fail: (err) => {
  129. // console.log("failtransToJPG", err);
  130. // }
  131. // });
  132. uni.uploadFile({
  133. url: baseApiUrl + 'common/upload?token=' + userToken,
  134. filePath: tempFilePath,
  135. fileType: 'image',
  136. name: 'file',
  137. headers: {
  138. Accept: 'application/json',
  139. 'Content-Type': 'multipart/form-data',
  140. token: userToken
  141. },
  142. formData: {},
  143. success: (uploadFileRes) => {
  144. // console.log(JSON.parse(uploadFileRes.data))
  145. var dataimg = JSON.parse(uploadFileRes.data);
  146. // console.log(dataimg.data.fullurl)
  147. let imgtype = this.imgType;
  148. console.log(imgtype);
  149. if (imgtype == 'changeIdentify') {
  150. _this.$api.updateface(
  151. {
  152. face: dataimg.data.fullurl
  153. },
  154. (data) => {
  155. if (data.code === 0) {
  156. _this.$common.errorToShow(data.msg);
  157. } else {
  158. _this.$common.successToShow('保存成功', function () {
  159. uni.navigateTo({
  160. // url: '/user_fenbao/info/getface'
  161. });
  162. });
  163. }
  164. }
  165. );
  166. } else if (imgtype == 'changeAvatar') {
  167. _this.$api.profile(
  168. {
  169. avatar: dataimg.data.fullurl
  170. },
  171. (data) => {}
  172. );
  173. uni.navigateBack({
  174. delta: 2
  175. });
  176. }
  177. },
  178. fail: (error) => {
  179. if (error && error.response) {
  180. this.$common.showError(error.response);
  181. }
  182. },
  183. complete: () => {
  184. setTimeout(function () {
  185. uni.hideLoading();
  186. }, 250);
  187. }
  188. });
  189. } else {
  190. console.log('获取图片地址失败,请稍后重试');
  191. uni.hideLoading();
  192. }
  193. });
  194. }
  195. }
  196. };
  197. </script>
  198. <style>
  199. .gui-flex {
  200. display: flex;
  201. background-color: #2b2e3d;
  202. }
  203. .gui-img-crop {
  204. width: 750rpx;
  205. background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAOwwAADsMBx2+oZAAAAB90RVh0U29mdHdhcmUATWFjcm9tZWRpYSBGaXJld29ya3MgOLVo0ngAAAAWdEVYdENyZWF0aW9uIFRpbWUAMDkvMjIvMjBpulvaAAAAQ0lEQVRIie3VsQ0AIAwDQZth2H8eljET0JgGiXf/OilNLGmqXJLVtqMNbwcMDAwMDHyck/SxXb/U/04NDAwMDPw+vAGUagde2qkMXgAAAABJRU5ErkJggg==');
  206. height: 100%;
  207. position: fixed;
  208. z-index: 90;
  209. left: 0;
  210. top: 0;
  211. }
  212. /* #ifdef H5 */
  213. .gui-img-crop {
  214. top: 44px;
  215. }
  216. /* #endif */
  217. .gui-cropper-wrapper {
  218. overflow: hidden;
  219. }
  220. .gui-cropper-bg {
  221. }
  222. .gui-cropper-prev {
  223. }
  224. .gui-cropper-btn {
  225. width: 50%;
  226. text-align: center;
  227. line-height: 50px;
  228. font-size: 28rpx;
  229. color: #ffffff;
  230. }
  231. </style>