boxMadeImg.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // pages/customized/made/madeImg/madeImg.js
  2. import * as util from '../../../../utils/util.js'
  3. let app = getApp();
  4. let host = app.globalData.servsers; // 请求的url
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. dataPositionObj: {},
  11. propSize: {},
  12. host: null,
  13. obverseImgObject: {},
  14. fontContent: '可定制区域',
  15. madeImg: null, // 定制图
  16. madeStatic: false, // 定制状态,表示是否已经定制了,false 表示没有定制
  17. animationData: {}, //旋转动画
  18. imageArray: [], //请求数据后的图片列表
  19. imageId: null, // 用户选中图片的id
  20. imageSrc: null, // 选中图片后的路径
  21. imgTouch: {
  22. isActive: '5', // 1表示是正面
  23. src: null,
  24. x: 0,
  25. y: 0,
  26. clientX: 0,
  27. clientY: 0,
  28. width: null, // 图片的宽度
  29. height: null,
  30. scale: 1, // 图片宽高比例
  31. angle: 0, // 旋转度数
  32. },
  33. dataId: [],
  34. box: {},
  35. fontTouch: {}, //文字对象
  36. fontAnimationData: {},
  37. },
  38. /**
  39. * 生命周期函数--监听页面加载
  40. */
  41. onLoad: function (options) {
  42. let that = this
  43. that.animation = wx.createAnimation({
  44. timingFunction: 'step-start', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
  45. })
  46. that.fontAnimation = wx.createAnimation({
  47. timingFunction: 'step-start', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
  48. })
  49. if ('id' in options) {
  50. let cupNum = util.changeCup(options.id);
  51. let madeTouchImg = wx.getStorageSync('madeCupImgData' + options.id);
  52. if (madeTouchImg) {
  53. that.animation.rotate(madeTouchImg.angle).step();
  54. that.setData({
  55. imgTouch: madeTouchImg,
  56. animationData: this.animation.export(),
  57. dataPositionObj: cupNum.dataPositionObj,
  58. propSize: cupNum.propSize,
  59. madeStatic: true,
  60. });
  61. } else {
  62. that.setData({
  63. 'imgTouch.isActive': options.id,
  64. dataId: options.dataId.split(','),
  65. dataPositionObj: cupNum.dataPositionObj,
  66. propSize: cupNum.propSize
  67. });
  68. }
  69. }
  70. },
  71. /**
  72. * 生命周期函数--监听页面初次渲染完成
  73. */
  74. onReady: function () {
  75. let that = this;
  76. that.setData({
  77. host: host,
  78. })
  79. },
  80. /**
  81. * 生命周期函数--监听页面显示
  82. */
  83. onShow: function () {
  84. let that = this;
  85. that.setData({
  86. box: wx.getStorageSync('box'),
  87. obverseImgObject: util.changeImg
  88. })
  89. that.getImgList();
  90. that.getDataTouch(that)
  91. },
  92. /**
  93. * 生命周期函数--监听页面隐藏
  94. */
  95. onHide: function () {
  96. },
  97. /**
  98. * 生命周期函数--监听页面卸载
  99. */
  100. onUnload: function () {
  101. },
  102. /**
  103. * 页面相关事件处理函数--监听用户下拉动作
  104. */
  105. onPullDownRefresh: function () {
  106. },
  107. /**
  108. * 页面上拉触底事件的处理函数
  109. */
  110. onReachBottom: function () {
  111. },
  112. /**
  113. * 用户点击右上角分享
  114. */
  115. onShareAppMessage: function () {
  116. },
  117. touchstart(e) {
  118. this.fontTouchstart(e)
  119. },
  120. touchmove(e) {
  121. this.fontTouchmove(e)
  122. },
  123. // touch 事件
  124. fontTouchstart(e) {
  125. //1.获取鼠标点击下去的
  126. this.setData({
  127. "imgTouch.clientX": e.touches[0].clientX - this.data.imgTouch.x,
  128. "imgTouch.clientY": e.touches[0].clientY - this.data.imgTouch.y
  129. });
  130. console.log(this.data.imgTouch, "touch")
  131. },
  132. fontTouchmove(e) {
  133. // 鼠标移动的位置
  134. this.setData({
  135. "imgTouch.x": e.changedTouches[0].clientX - this.data.imgTouch.clientX,
  136. "imgTouch.y": e.changedTouches[0].clientY - this.data.imgTouch.clientY
  137. });
  138. // console.log(this.data.touch)
  139. },
  140. imgDelete() { // 图片删除
  141. let that = this;
  142. that.setData({
  143. 'imgTouch.src': null,
  144. madeStatic: false, // 表示已经有图片
  145. })
  146. },
  147. imgEnlarge(e) {
  148. let that = this;
  149. console.log(e.detail.value, 'eee')
  150. that.setData({
  151. 'imgTouch.width': e.detail.value,
  152. 'imgTouch.height': e.detail.value / that.data.imgTouch.scale
  153. })
  154. },
  155. imgRotate(e) { // 旋转
  156. this.animation.rotate(e.detail.value).step();
  157. this.setData({
  158. animationData: this.animation.export(),
  159. 'imgTouch.angle': e.detail.value
  160. })
  161. },
  162. getImgList() { // 根据userid 获取图片列表
  163. // /queryCustomGoodsUserListByUserId
  164. wx.showLoading({
  165. title: '加载中...',
  166. icon: 'none'
  167. })
  168. let that = this
  169. wx.request({
  170. url: host + '/queryImgListByPlaceIdAndGoodsId',
  171. data: {
  172. userId: app.globalData.user_id,
  173. customGoodsId: wx.getStorageSync('detail').id, // 商品id,白色箱子是6
  174. customGoodsImgPlaceId: that.data.dataId[0] == that.data.imgTouch.isActive ? 1 : 2, // that.data.dataId[0] 是正面,1是背面
  175. },
  176. type: 'get',
  177. success(res) {
  178. console.log(res, '图片列表请求结束')
  179. that.setData({
  180. imageArray: res.data
  181. })
  182. },
  183. complete() {
  184. wx.hideLoading()
  185. }
  186. })
  187. },
  188. uploadImg() {
  189. console.log('点击上传图片');
  190. let that = this;
  191. wx.chooseImage({
  192. success(res) {
  193. const tempFilePaths = res.tempFilePaths
  194. wx.uploadFile({
  195. url: host + 'adminFileUpload', // 仅为示例,非真实的接口地址
  196. filePath: tempFilePaths[0],
  197. name: 'file',
  198. formData: {
  199. file: tempFilePaths[0],
  200. userId: app.globalData.user_id,
  201. customGoodsId:wx.getStorageSync('detail').id,
  202. customGoodsImgPlaceId: that.data.dataId[0] == that.data.imgTouch.isActive ? 1 : 2, // that.data.dataId[0] 是正面,1是背面
  203. },
  204. success(res) {
  205. console.log(res, '我上传完了')
  206. that.getImgList()
  207. }
  208. })
  209. }
  210. })
  211. },
  212. selectImgBtn(e) {
  213. console.log('选中图片', e.currentTarget.dataset.id);
  214. this.setData({
  215. imageId: e.currentTarget.dataset.id,
  216. imageSrc: e.currentTarget.dataset.src
  217. })
  218. // 'touch.src': e.currentTarget.dataset.src,
  219. // madeStatic: true, // 表示已经有图片
  220. // this.getScale(this, e.currentTarget.dataset.src);
  221. },
  222. getScale(that, src) { // 获取图片的比例
  223. wx.getImageInfo({
  224. src: src,
  225. success: function (res) {
  226. console.log(res, "fff")
  227. that.setData({
  228. 'imgTouch.width': that.data.dataPositionObj.width,
  229. 'imgTouch.height': res.height * that.data.dataPositionObj.width / res.width,
  230. 'imgTouch.scale': res.width / res.height, // 宽高比例
  231. })
  232. console.log(that.data.imgTouch)
  233. }
  234. })
  235. },
  236. addImg() { // 添加图片
  237. let that = this;
  238. if (that.data.imageSrc) {
  239. that.setData({
  240. 'imgTouch.src': that.data.imageSrc,
  241. madeStatic: true, // 表示已经有图片
  242. })
  243. that.getScale(that, that.data.imageSrc)
  244. } else {
  245. wx.showToast({
  246. title: '您还没有选中图片',
  247. icon: 'none'
  248. })
  249. }
  250. },
  251. madeConfirm() {
  252. // 确认设计
  253. if (this.data.madeStatic) { // 设计完成存储数据
  254. wx.setStorageSync('madeCupImgData' + this.data.imgTouch.isActive, this.data.imgTouch);
  255. wx.navigateBack()
  256. // wx.redirectTo({
  257. // url: '/customized/pages/made/made?id=' + this.data.touch.isActive
  258. // })
  259. } else {
  260. wx.showToast({
  261. title: '您还没有选择图片进行设计',
  262. icon: 'none'
  263. })
  264. }
  265. },
  266. getDataTouch(that) {
  267. let madeTouchFont = wx.getStorageSync('madeCupFontData' + that.data.imgTouch.isActive)
  268. console.log(madeTouchFont, "madeTouchFont")
  269. if (madeTouchFont) {
  270. console.log(that.fontAnimation, "that.fontAnimation")
  271. that.fontAnimation.rotate(madeTouchFont.angle).step();
  272. that.setData({
  273. fontAnimationData: that.fontAnimation.export(),
  274. fontTouch: madeTouchFont ? madeTouchFont : {},
  275. madeStatic: true,
  276. })
  277. }
  278. },
  279. })