boxMadeImg.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. customGoodsId: wx.getStorageSync('detail').id, // 商品id,白色箱子是6
  173. customGoodsImgPlaceId: that.data.dataId[0] == that.data.imgTouch.isActive ? 1 : 2, // that.data.dataId[0] 是正面,1是背面
  174. },
  175. type: 'get',
  176. success(res) {
  177. console.log(res, '图片列表请求结束')
  178. that.setData({
  179. imageArray: res.data
  180. })
  181. },
  182. complete() {
  183. wx.hideLoading()
  184. }
  185. })
  186. },
  187. uploadImg() {
  188. console.log('点击上传图片');
  189. let that = this;
  190. wx.chooseImage({
  191. success(res) {
  192. const tempFilePaths = res.tempFilePaths
  193. wx.uploadFile({
  194. url: host + 'fileUpload', // 仅为示例,非真实的接口地址
  195. filePath: tempFilePaths[0],
  196. name: 'file',
  197. formData: {
  198. file: tempFilePaths[0],
  199. userId: app.globalData.user_id,
  200. imgStatus: 0, // 表示素材
  201. },
  202. success(res) {
  203. console.log(res, '我上传完了')
  204. that.getImgList()
  205. }
  206. })
  207. }
  208. })
  209. },
  210. selectImgBtn(e) {
  211. console.log('选中图片', e.currentTarget.dataset.id);
  212. this.setData({
  213. imageId: e.currentTarget.dataset.id,
  214. imageSrc: e.currentTarget.dataset.src
  215. })
  216. // 'touch.src': e.currentTarget.dataset.src,
  217. // madeStatic: true, // 表示已经有图片
  218. // this.getScale(this, e.currentTarget.dataset.src);
  219. },
  220. getScale(that, src) { // 获取图片的比例
  221. wx.getImageInfo({
  222. src: src,
  223. success: function (res) {
  224. console.log(res, "fff")
  225. that.setData({
  226. 'imgTouch.width': that.data.dataPositionObj.width,
  227. 'imgTouch.height': res.height * that.data.dataPositionObj.width / res.width,
  228. 'imgTouch.scale': res.width / res.height, // 宽高比例
  229. })
  230. console.log(that.data.imgTouch)
  231. }
  232. })
  233. },
  234. addImg() { // 添加图片
  235. let that = this;
  236. if (that.data.imageSrc) {
  237. that.setData({
  238. 'imgTouch.src': that.data.imageSrc,
  239. madeStatic: true, // 表示已经有图片
  240. })
  241. that.getScale(that, that.data.imageSrc)
  242. } else {
  243. wx.showToast({
  244. title: '您还没有选中图片',
  245. icon: 'none'
  246. })
  247. }
  248. },
  249. madeConfirm() {
  250. // 确认设计
  251. if (this.data.madeStatic) { // 设计完成存储数据
  252. wx.setStorageSync('madeCupImgData' + this.data.imgTouch.isActive, this.data.imgTouch);
  253. wx.navigateBack()
  254. // wx.redirectTo({
  255. // url: '/customized/pages/made/made?id=' + this.data.touch.isActive
  256. // })
  257. } else {
  258. wx.showToast({
  259. title: '您还没有选择图片进行设计',
  260. icon: 'none'
  261. })
  262. }
  263. },
  264. getDataTouch(that) {
  265. let madeTouchFont = wx.getStorageSync('madeCupFontData' + that.data.imgTouch.isActive)
  266. console.log(madeTouchFont, "madeTouchFont")
  267. if (madeTouchFont) {
  268. console.log(that.fontAnimation, "that.fontAnimation")
  269. that.fontAnimation.rotate(madeTouchFont.angle).step();
  270. that.setData({
  271. fontAnimationData: that.fontAnimation.export(),
  272. fontTouch: madeTouchFont ? madeTouchFont : {},
  273. madeStatic: true,
  274. })
  275. }
  276. },
  277. })