index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. "use strict";
  2. const common_vendor = require("../../../common/vendor.js");
  3. const config = require("../../../config.js");
  4. const store_index = require("../../../store/index.js");
  5. const api_system_user = require("../../../api/system/user.js");
  6. require("../../../store/modules/user.js");
  7. require("../../../utils/storage.js");
  8. require("../../../utils/constant.js");
  9. require("../../../api/login.js");
  10. require("../../../utils/request.js");
  11. require("../../../utils/auth.js");
  12. require("../../../utils/errorCode.js");
  13. require("../../../utils/common.js");
  14. require("../../../store/getters.js");
  15. require("../../../utils/upload.js");
  16. const baseUrl = config.config.baseUrl;
  17. let sysInfo = common_vendor.index.getSystemInfoSync();
  18. let SCREEN_WIDTH = sysInfo.screenWidth;
  19. let PAGE_X, PAGE_Y;
  20. sysInfo.pixelRatio;
  21. let T_PAGE_X, T_PAGE_Y, CUT_L, CUT_T, CUT_R, CUT_B, IMG_RATIO, IMG_REAL_W, IMG_REAL_H, DRAFG_MOVE_RATIO = 1, INIT_DRAG_POSITION = 100, DRAW_IMAGE_W = sysInfo.screenWidth;
  22. const _sfc_main = {
  23. /**
  24. * 页面的初始数据
  25. */
  26. data() {
  27. return {
  28. imageSrc: store_index.store.getters.avatar,
  29. isShowImg: false,
  30. // 初始化的宽高
  31. cropperInitW: SCREEN_WIDTH,
  32. cropperInitH: SCREEN_WIDTH,
  33. // 动态的宽高
  34. cropperW: SCREEN_WIDTH,
  35. cropperH: SCREEN_WIDTH,
  36. // 动态的left top值
  37. cropperL: 0,
  38. cropperT: 0,
  39. transL: 0,
  40. transT: 0,
  41. // 图片缩放值
  42. scaleP: 0,
  43. imageW: 0,
  44. imageH: 0,
  45. // 裁剪框 宽高
  46. cutL: 0,
  47. cutT: 0,
  48. cutB: SCREEN_WIDTH,
  49. cutR: "100%",
  50. qualityWidth: DRAW_IMAGE_W,
  51. innerAspectRadio: DRAFG_MOVE_RATIO
  52. };
  53. },
  54. /**
  55. * 生命周期函数--监听页面初次渲染完成
  56. */
  57. onReady: function() {
  58. this.loadImage();
  59. },
  60. methods: {
  61. setData: function(obj) {
  62. let that = this;
  63. Object.keys(obj).forEach(function(key) {
  64. that.$set(that.$data, key, obj[key]);
  65. });
  66. },
  67. getImage: function() {
  68. var _this = this;
  69. common_vendor.index.chooseImage({
  70. success: function(res) {
  71. _this.setData({
  72. imageSrc: res.tempFilePaths[0]
  73. });
  74. _this.loadImage();
  75. }
  76. });
  77. },
  78. loadImage: function() {
  79. var _this = this;
  80. common_vendor.index.getImageInfo({
  81. src: _this.imageSrc,
  82. success: function success(res) {
  83. IMG_RATIO = 1 / 1;
  84. if (IMG_RATIO >= 1) {
  85. IMG_REAL_W = SCREEN_WIDTH;
  86. IMG_REAL_H = SCREEN_WIDTH / IMG_RATIO;
  87. } else {
  88. IMG_REAL_W = SCREEN_WIDTH * IMG_RATIO;
  89. IMG_REAL_H = SCREEN_WIDTH;
  90. }
  91. let minRange = IMG_REAL_W > IMG_REAL_H ? IMG_REAL_W : IMG_REAL_H;
  92. INIT_DRAG_POSITION = minRange > INIT_DRAG_POSITION ? INIT_DRAG_POSITION : minRange;
  93. if (IMG_RATIO >= 1) {
  94. let cutT = Math.ceil((SCREEN_WIDTH / IMG_RATIO - (SCREEN_WIDTH / IMG_RATIO - INIT_DRAG_POSITION)) / 2);
  95. let cutB = cutT;
  96. let cutL = Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH + INIT_DRAG_POSITION) / 2);
  97. let cutR = cutL;
  98. _this.setData({
  99. cropperW: SCREEN_WIDTH,
  100. cropperH: SCREEN_WIDTH / IMG_RATIO,
  101. // 初始化left right
  102. cropperL: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH) / 2),
  103. cropperT: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH / IMG_RATIO) / 2),
  104. cutL,
  105. cutT,
  106. cutR,
  107. cutB,
  108. // 图片缩放值
  109. imageW: IMG_REAL_W,
  110. imageH: IMG_REAL_H,
  111. scaleP: IMG_REAL_W / SCREEN_WIDTH,
  112. qualityWidth: DRAW_IMAGE_W,
  113. innerAspectRadio: IMG_RATIO
  114. });
  115. } else {
  116. let cutL = Math.ceil((SCREEN_WIDTH * IMG_RATIO - SCREEN_WIDTH * IMG_RATIO) / 2);
  117. let cutR = cutL;
  118. let cutT = Math.ceil((SCREEN_WIDTH - INIT_DRAG_POSITION) / 2);
  119. let cutB = cutT;
  120. _this.setData({
  121. cropperW: SCREEN_WIDTH * IMG_RATIO,
  122. cropperH: SCREEN_WIDTH,
  123. // 初始化left right
  124. cropperL: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH * IMG_RATIO) / 2),
  125. cropperT: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH) / 2),
  126. cutL,
  127. cutT,
  128. cutR,
  129. cutB,
  130. // 图片缩放值
  131. imageW: IMG_REAL_W,
  132. imageH: IMG_REAL_H,
  133. scaleP: IMG_REAL_W / SCREEN_WIDTH,
  134. qualityWidth: DRAW_IMAGE_W,
  135. innerAspectRadio: IMG_RATIO
  136. });
  137. }
  138. _this.setData({
  139. isShowImg: true
  140. });
  141. common_vendor.index.hideLoading();
  142. }
  143. });
  144. },
  145. // 拖动时候触发的touchStart事件
  146. contentStartMove(e) {
  147. PAGE_X = e.touches[0].pageX;
  148. PAGE_Y = e.touches[0].pageY;
  149. },
  150. // 拖动时候触发的touchMove事件
  151. contentMoveing(e) {
  152. var dragLengthX = (PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO;
  153. var dragLengthY = (PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO;
  154. if (dragLengthX > 0) {
  155. if (this.cutL - dragLengthX < 0)
  156. dragLengthX = this.cutL;
  157. } else {
  158. if (this.cutR + dragLengthX < 0)
  159. dragLengthX = -this.cutR;
  160. }
  161. if (dragLengthY > 0) {
  162. if (this.cutT - dragLengthY < 0)
  163. dragLengthY = this.cutT;
  164. } else {
  165. if (this.cutB + dragLengthY < 0)
  166. dragLengthY = -this.cutB;
  167. }
  168. this.setData({
  169. cutL: this.cutL - dragLengthX,
  170. cutT: this.cutT - dragLengthY,
  171. cutR: this.cutR + dragLengthX,
  172. cutB: this.cutB + dragLengthY
  173. });
  174. PAGE_X = e.touches[0].pageX;
  175. PAGE_Y = e.touches[0].pageY;
  176. },
  177. contentTouchEnd() {
  178. },
  179. // 获取图片
  180. getImageInfo() {
  181. var _this = this;
  182. common_vendor.index.showLoading({
  183. title: "图片生成中..."
  184. });
  185. const ctx = common_vendor.index.createCanvasContext("myCanvas");
  186. ctx.drawImage(_this.imageSrc, 0, 0, IMG_REAL_W, IMG_REAL_H);
  187. ctx.draw(true, () => {
  188. var canvasW = (_this.cropperW - _this.cutL - _this.cutR) / _this.cropperW * IMG_REAL_W;
  189. var canvasH = (_this.cropperH - _this.cutT - _this.cutB) / _this.cropperH * IMG_REAL_H;
  190. var canvasL = _this.cutL / _this.cropperW * IMG_REAL_W;
  191. var canvasT = _this.cutT / _this.cropperH * IMG_REAL_H;
  192. common_vendor.index.canvasToTempFilePath({
  193. x: canvasL,
  194. y: canvasT,
  195. width: canvasW,
  196. height: canvasH,
  197. destWidth: canvasW,
  198. destHeight: canvasH,
  199. quality: 0.5,
  200. canvasId: "myCanvas",
  201. success: function(res) {
  202. common_vendor.index.hideLoading();
  203. let data = { name: "avatarfile", filePath: res.tempFilePath };
  204. api_system_user.uploadAvatar(data).then((response) => {
  205. store_index.store.commit("SET_AVATAR", baseUrl + response.imgUrl);
  206. common_vendor.index.showToast({ title: "修改成功", icon: "success" });
  207. common_vendor.index.$emit("refresh");
  208. common_vendor.index.navigateBack();
  209. });
  210. }
  211. });
  212. });
  213. },
  214. // 设置大小的时候触发的touchStart事件
  215. dragStart(e) {
  216. T_PAGE_X = e.touches[0].pageX;
  217. T_PAGE_Y = e.touches[0].pageY;
  218. CUT_L = this.cutL;
  219. CUT_R = this.cutR;
  220. CUT_B = this.cutB;
  221. CUT_T = this.cutT;
  222. },
  223. // 设置大小的时候触发的touchMove事件
  224. dragMove(e) {
  225. var dragType = e.target.dataset.drag;
  226. switch (dragType) {
  227. case "right":
  228. var dragLength = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO;
  229. if (CUT_R + dragLength < 0)
  230. dragLength = -CUT_R;
  231. this.setData({
  232. cutR: CUT_R + dragLength
  233. });
  234. break;
  235. case "left":
  236. var dragLength = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO;
  237. if (CUT_L - dragLength < 0)
  238. dragLength = CUT_L;
  239. if (CUT_L - dragLength > this.cropperW - this.cutR)
  240. dragLength = CUT_L - (this.cropperW - this.cutR);
  241. this.setData({
  242. cutL: CUT_L - dragLength
  243. });
  244. break;
  245. case "top":
  246. var dragLength = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO;
  247. if (CUT_T - dragLength < 0)
  248. dragLength = CUT_T;
  249. if (CUT_T - dragLength > this.cropperH - this.cutB)
  250. dragLength = CUT_T - (this.cropperH - this.cutB);
  251. this.setData({
  252. cutT: CUT_T - dragLength
  253. });
  254. break;
  255. case "bottom":
  256. var dragLength = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO;
  257. if (CUT_B + dragLength < 0)
  258. dragLength = -CUT_B;
  259. this.setData({
  260. cutB: CUT_B + dragLength
  261. });
  262. break;
  263. case "rightBottom":
  264. var dragLengthX = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO;
  265. var dragLengthY = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO;
  266. if (CUT_B + dragLengthY < 0)
  267. dragLengthY = -CUT_B;
  268. if (CUT_R + dragLengthX < 0)
  269. dragLengthX = -CUT_R;
  270. let cutB = CUT_B + dragLengthY;
  271. let cutR = CUT_R + dragLengthX;
  272. this.setData({
  273. cutB,
  274. cutR
  275. });
  276. break;
  277. }
  278. }
  279. }
  280. };
  281. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  282. return common_vendor.e({
  283. a: $data.isShowImg
  284. }, $data.isShowImg ? {
  285. b: $data.imageSrc,
  286. c: common_vendor.s("width:" + $data.cropperW + "px;height:" + $data.cropperH + "px"),
  287. d: common_vendor.o((...args) => $options.dragStart && $options.dragStart(...args)),
  288. e: common_vendor.o((...args) => $options.dragMove && $options.dragMove(...args)),
  289. f: common_vendor.o((...args) => $options.dragStart && $options.dragStart(...args)),
  290. g: common_vendor.o((...args) => $options.dragMove && $options.dragMove(...args)),
  291. h: common_vendor.o((...args) => $options.dragStart && $options.dragStart(...args)),
  292. i: common_vendor.o((...args) => $options.dragMove && $options.dragMove(...args)),
  293. j: common_vendor.o((...args) => $options.dragStart && $options.dragStart(...args)),
  294. k: common_vendor.o((...args) => $options.dragMove && $options.dragMove(...args)),
  295. l: common_vendor.o((...args) => $options.dragStart && $options.dragStart(...args)),
  296. m: common_vendor.o((...args) => $options.dragMove && $options.dragMove(...args)),
  297. n: common_vendor.o((...args) => $options.dragStart && $options.dragStart(...args)),
  298. o: common_vendor.o((...args) => $options.dragMove && $options.dragMove(...args)),
  299. p: common_vendor.o((...args) => $options.dragStart && $options.dragStart(...args)),
  300. q: common_vendor.o((...args) => $options.dragMove && $options.dragMove(...args)),
  301. r: common_vendor.o((...args) => $options.dragStart && $options.dragStart(...args)),
  302. s: common_vendor.o((...args) => $options.dragMove && $options.dragMove(...args)),
  303. t: common_vendor.o((...args) => _ctx.dragEnd && _ctx.dragEnd(...args)),
  304. v: common_vendor.o((...args) => $options.dragStart && $options.dragStart(...args)),
  305. w: common_vendor.o((...args) => $options.dragMove && $options.dragMove(...args)),
  306. x: common_vendor.o((...args) => $options.contentStartMove && $options.contentStartMove(...args)),
  307. y: common_vendor.o((...args) => $options.contentMoveing && $options.contentMoveing(...args)),
  308. z: common_vendor.o((...args) => $options.contentTouchEnd && $options.contentTouchEnd(...args)),
  309. A: common_vendor.s("left:" + $data.cutL + "px;top:" + $data.cutT + "px;right:" + $data.cutR + "px;bottom:" + $data.cutB + "px"),
  310. B: common_vendor.s("width:" + $data.cropperW + "px;height:" + $data.cropperH + "px;left:" + $data.cropperL + "px;top:" + $data.cropperT + "px"),
  311. C: common_vendor.s("width:" + $data.cropperInitW + "px;height:" + $data.cropperInitH + "px;background:#000")
  312. } : {}, {
  313. D: common_vendor.o((...args) => $options.getImage && $options.getImage(...args)),
  314. E: common_vendor.o((...args) => $options.getImageInfo && $options.getImageInfo(...args)),
  315. F: common_vendor.s("position:absolute;border: 1px solid red; width:" + $data.imageW + "px;height:" + $data.imageH + "px;top:-9999px;left:-9999px;")
  316. });
  317. }
  318. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "C:/Users/Administrator/Desktop/srcaaa/pages_mine/pages/avatar/index.vue"]]);
  319. wx.createPage(MiniProgramPage);