boxMadeImg.js 7.6 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. '5': '/customized/images/customized/box_white_big_1.png',
  15. /*正面 */
  16. '6': '/customized/images/customized/box_white_big_2.png',
  17. /*反面 */
  18. },
  19. fontContent: '可定制区域',
  20. madeImg: null, // 定制图
  21. madeStatic: false, // 定制状态,表示是否已经定制了,false 表示没有定制
  22. animationData: {}, //旋转动画
  23. imageArray: [], //请求数据后的图片列表
  24. imageId: null, // 用户选中图片的id
  25. imageSrc: null, // 选中图片后的路径
  26. imgTouch: {
  27. isActive: '5', // 1表示是正面
  28. src: null,
  29. x: 0,
  30. y: 0,
  31. clientX: 0,
  32. clientY: 0,
  33. width: null, // 图片的宽度
  34. height: null,
  35. scale: 1, // 图片宽高比例
  36. angle: 0, // 旋转度数
  37. },
  38. box:{},
  39. fontTouch:{},//文字对象
  40. fontAnimationData:{},
  41. },
  42. /**
  43. * 生命周期函数--监听页面加载
  44. */
  45. onLoad: function (options) {
  46. let that = this
  47. that.animation = wx.createAnimation({
  48. timingFunction: 'step-start', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
  49. })
  50. that.fontAnimation = wx.createAnimation({
  51. timingFunction: 'step-start', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
  52. })
  53. if ('id' in options) {
  54. let cupNum = util.changeCup(options.id);
  55. let madeTouchImg = wx.getStorageSync('madeCupImgData' + options.id);
  56. if (madeTouchImg) {
  57. that.animation.rotate(madeTouchImg.angle).step();
  58. that.setData({
  59. imgTouch: madeTouchImg,
  60. animationData: this.animation.export(),
  61. dataPositionObj: cupNum.dataPositionObj,
  62. propSize: cupNum.propSize,
  63. madeStatic: true,
  64. });
  65. } else {
  66. that.setData({
  67. 'imgTouch.isActive': options.id,
  68. dataPositionObj: cupNum.dataPositionObj,
  69. propSize: cupNum.propSize
  70. });
  71. }
  72. }
  73. },
  74. /**
  75. * 生命周期函数--监听页面初次渲染完成
  76. */
  77. onReady: function () {
  78. let that = this;
  79. that.setData({
  80. host: host,
  81. })
  82. },
  83. /**
  84. * 生命周期函数--监听页面显示
  85. */
  86. onShow: function () {
  87. let that = this;
  88. that.setData({
  89. box:wx.getStorageSync('box')
  90. })
  91. that.getImgList();
  92. that.getDataTouch(that)
  93. },
  94. /**
  95. * 生命周期函数--监听页面隐藏
  96. */
  97. onHide: function () {
  98. },
  99. /**
  100. * 生命周期函数--监听页面卸载
  101. */
  102. onUnload: function () {
  103. },
  104. /**
  105. * 页面相关事件处理函数--监听用户下拉动作
  106. */
  107. onPullDownRefresh: function () {
  108. },
  109. /**
  110. * 页面上拉触底事件的处理函数
  111. */
  112. onReachBottom: function () {
  113. },
  114. /**
  115. * 用户点击右上角分享
  116. */
  117. onShareAppMessage: function () {
  118. },
  119. touchstart(e){
  120. this.fontTouchstart(e)
  121. },
  122. touchmove(e){
  123. this.fontTouchmove(e)
  124. },
  125. // touch 事件
  126. fontTouchstart(e) {
  127. //1.获取鼠标点击下去的
  128. this.setData({
  129. "imgTouch.clientX": e.touches[0].clientX - this.data.imgTouch.x,
  130. "imgTouch.clientY": e.touches[0].clientY - this.data.imgTouch.y
  131. });
  132. console.log(this.data.imgTouch, "touch")
  133. },
  134. fontTouchmove(e) {
  135. // 鼠标移动的位置
  136. this.setData({
  137. "imgTouch.x": e.changedTouches[0].clientX - this.data.imgTouch.clientX,
  138. "imgTouch.y": e.changedTouches[0].clientY - this.data.imgTouch.clientY
  139. });
  140. // console.log(this.data.touch)
  141. },
  142. imgDelete() { // 图片删除
  143. let that = this;
  144. that.setData({
  145. 'imgTouch.src': null,
  146. madeStatic: false, // 表示已经有图片
  147. })
  148. },
  149. imgEnlarge(e) {
  150. let that = this;
  151. console.log(e.detail.value, 'eee')
  152. that.setData({
  153. 'imgTouch.width': e.detail.value,
  154. 'imgTouch.height': e.detail.value / that.data.imgTouch.scale
  155. })
  156. },
  157. imgRotate(e) { // 旋转
  158. this.animation.rotate(e.detail.value).step();
  159. this.setData({
  160. animationData: this.animation.export(),
  161. 'imgTouch.angle': e.detail.value
  162. })
  163. },
  164. getImgList() { // 根据userid 获取图片列表
  165. // /queryCustomGoodsUserListByUserId
  166. wx.showLoading({
  167. title: '加载中...',
  168. icon: 'none'
  169. })
  170. let that = this
  171. wx.request({
  172. url: host + '/queryImgListByPlaceIdAndGoodsId',
  173. data: {
  174. customGoodsId: 6, // 商品id,白色箱子是6
  175. customGoodsImgPlaceId: that.data.imgTouch.isActive * 1 - 4
  176. },
  177. type: 'get',
  178. success(res) {
  179. console.log(res, '图片列表请求结束')
  180. that.setData({
  181. imageArray: res.data
  182. })
  183. },
  184. complete() {
  185. wx.hideLoading()
  186. }
  187. })
  188. },
  189. uploadImg() {
  190. console.log('点击上传图片');
  191. let that = this;
  192. wx.chooseImage({
  193. success(res) {
  194. const tempFilePaths = res.tempFilePaths
  195. wx.uploadFile({
  196. url: host + 'fileUpload', // 仅为示例,非真实的接口地址
  197. filePath: tempFilePaths[0],
  198. name: 'file',
  199. formData: {
  200. file: tempFilePaths[0],
  201. userId: app.globalData.user_id,
  202. imgStatus: 0, // 表示素材
  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. })