oneMadeImg.js 7.7 KB

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