madeImg.js 6.9 KB

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