oneMadeImg.js 7.3 KB

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