boxComb.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // customized/pages/made/combination/combination.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. imgTouch: {
  17. isActive: "1",
  18. src: "https://dlz.info666.com//temp/d945bfb0-5cc6-406d-a814-cf63b099041f.jpg",
  19. x: -82,
  20. y: 17,
  21. clientX: 195,
  22. clientY: 121,
  23. width: 517,
  24. height: 208.17866666666666,
  25. scale: 2.4834437086092715,
  26. angle: 27
  27. },
  28. fontTouch: {
  29. isActive: "1",
  30. fontContent: "Gg",
  31. isColor: "#ffffff",
  32. x: 10,
  33. y: 10,
  34. angle: 0,
  35. fontSize: 100,
  36. clientX: 0,
  37. clientY: 0,
  38. width: 138.34375,
  39. height: 132
  40. },
  41. rpx: 1,
  42. obverseImgObject: ['', '/customized/images/customized/cup_black_big.png', '/customized/images/customized/cup_black_side.png', '/customized/images/customized/cup_black_handle.png', '/customized/images/customized/cup_black_bottom.png', '/customized/images/customized/box_white_big_1.png', '/customized/images/customized/box_white_big_2.png'],
  43. // obverseImgObject: {
  44. // '1': ,
  45. // /*正面 */
  46. // '2': '/customized/images/customized/cup_black_side.png',
  47. // /*侧面面 */
  48. // '3': '/customized/images/customized/cup_black_handle.png',
  49. // /*手柄 */
  50. // '4': '/customized/images/customized/cup_black_bottom.png',
  51. // /*底部 */
  52. // },
  53. imgSrcArray: [],
  54. buildSrcArrayText: {
  55. '1': '正面',
  56. '2': '背面',
  57. },
  58. boxImg: {
  59. '5': '/customized/images/customized/box_white_big_1_out.png',
  60. '6': '/customized/images/customized/box_white_big_2_out.png',
  61. },
  62. buildSrcArray: [],
  63. customGoodsImg: "",
  64. },
  65. /**
  66. * 生命周期函数--监听页面加载
  67. */
  68. onLoad: function (options) {},
  69. /**
  70. * 生命周期函数--监听页面初次渲染完成
  71. */
  72. onReady: function () {
  73. wx.showLoading({
  74. title: '图片合成中...'
  75. })
  76. let that = this,
  77. result = true;
  78. if (wx.getStorageSync('madeCupImgData5') || wx.getStorageSync('madeCupFontData5')) {
  79. that.getCombImg(that, 5);
  80. result = false
  81. } else if (wx.getStorageSync('madeCupImgData6') || wx.getStorageSync('madeCupFontData6')) {
  82. that.getCombImg(that, 6);
  83. result = false
  84. }
  85. if (result) {
  86. wx.hideLoading();
  87. }
  88. },
  89. /**
  90. * 生命周期函数--监听页面显示
  91. */
  92. onShow: function () {
  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. getCombImg(that, i) {
  120. let imgTouch = null,
  121. dataPositionObj = null;
  122. that.setData({ // imgSrcArray 初始化
  123. imgSrcArray: [],
  124. })
  125. console.log(wx.getStorageSync('madeCupImgData' + i), "wx.getStorageSync('madeCupImgData' + i)")
  126. if (wx.getStorageSync('madeCupImgData' + i)) {
  127. imgTouch = wx.getStorageSync('madeCupImgData' + i);
  128. dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  129. console.log(dataPositionObj, "dataPositionObj")
  130. wx.getSystemInfo({
  131. success: function (res) {
  132. let rpx = res.windowWidth / 750;
  133. that.setData({
  134. rpx: rpx
  135. });
  136. wx.downloadFile({
  137. url: imgTouch.src, // 仅为示例,并非真实的资源
  138. success(res) {
  139. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  140. console.log(res)
  141. if (res.statusCode === 200) {
  142. // canvas截图区域与图片的宽度比例
  143. let dWidth = dataPositionObj.width / imgTouch.width;
  144. // canvas截图区域与图片的高度比例
  145. let dHeight = dataPositionObj.height / imgTouch.height;
  146. const ctx = wx.createCanvasContext('comb-img' + i);
  147. //画布中点坐标转移到图片中心
  148. let movex = imgTouch.x + imgTouch.width * rpx / 2;
  149. let movey = imgTouch.y + imgTouch.height * rpx / 2;
  150. ctx.translate(movex, movey);
  151. ctx.rotate(imgTouch.angle * Math.PI / 180);
  152. ctx.drawImage(res.tempFilePath, -imgTouch.width * rpx / 2, -imgTouch.height * rpx / 2, imgTouch.width * rpx, imgTouch.height * rpx)
  153. ctx.draw(false, function () {
  154. wx.canvasToTempFilePath({
  155. canvasId: 'comb-img' + i,
  156. success: function (res) {
  157. console.log(res)
  158. let imgArray = that.data.imgSrcArray;
  159. imgArray.push(res.tempFilePath)
  160. that.setData({
  161. imgSrcArray: imgArray
  162. })
  163. that.getCombFont(that, i, true);
  164. }
  165. }, that)
  166. })
  167. }
  168. }
  169. })
  170. },
  171. })
  172. } else {
  173. that.getCombFont(that, i, false);
  174. }
  175. },
  176. getCombFont(that, i, _static) {
  177. let fontTouch = null,
  178. dataPositionObj = null,
  179. simulationFont = null;
  180. if (wx.getStorageSync('madeCupFontData' + i)) {
  181. fontTouch = wx.getStorageSync('madeCupFontData' + i);
  182. simulationFont = wx.getStorageSync('simulationFont' + i);
  183. dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  184. const ctx = wx.createCanvasContext('comb-font' + i);
  185. // ctx.textBaseline = 'top';
  186. ctx.setFontSize(fontTouch.fontSize);
  187. ctx.setFillStyle(fontTouch.isColor);
  188. if (fontTouch.bold) {
  189. ctx.fillText(fontTouch.fontContent, -1, simulationFont.height / 5 * 4)
  190. ctx.fillText(fontTouch.fontContent, 0, simulationFont.height / 5 * 4 - 1)
  191. }
  192. ctx.fillText(fontTouch.fontContent, 0, simulationFont.height / 5 * 4)
  193. if (fontTouch.bold) {
  194. ctx.fillText(fontTouch.fontContent, 1, simulationFont.height / 5 * 4)
  195. ctx.fillText(fontTouch.fontContent, 0, simulationFont.height / 5 * 4 + 1)
  196. }
  197. ctx.restore()
  198. ctx.draw(false, function () {
  199. wx.canvasToTempFilePath({
  200. canvasId: 'comb-font' + i,
  201. success: function (res) {
  202. console.log(res)
  203. that.getCombFontImg(that, res.tempFilePath, fontTouch, i)
  204. }
  205. }, that)
  206. })
  207. } else {
  208. that.getComb(that, that.data.imgSrcArray, i, _static)
  209. }
  210. },
  211. getCombFontImg(that, img, fontTouch, i) { // 生产字体图片后在进行处理一次
  212. wx.getSystemInfo({
  213. success: function (res) {
  214. let rpx = res.windowWidth / 750;
  215. that.setData({
  216. rpx: rpx
  217. });
  218. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  219. console.log(res)
  220. // canvas截图区域与图片的宽度比例
  221. const ctx = wx.createCanvasContext('comb-font' + i + '-2');
  222. //画布中点坐标转移到图片中心
  223. let movex = fontTouch.x + 536 * rpx / 2;
  224. let movey = fontTouch.y + 712 * rpx / 2;
  225. ctx.translate(movex, movey);
  226. ctx.rotate(fontTouch.angle * Math.PI / 180);
  227. ctx.drawImage(img, -536 * rpx / 2, -712 * rpx / 2, 536 * rpx, 712 * rpx)
  228. ctx.draw(false, function () {
  229. wx.canvasToTempFilePath({
  230. canvasId: 'comb-font' + i + '-2',
  231. success: function (res) {
  232. console.log(res)
  233. let imgArray = that.data.imgSrcArray;
  234. imgArray.push(res.tempFilePath)
  235. that.setData({
  236. imgSrcArray: imgArray
  237. })
  238. that.getComb(that, that.data.imgSrcArray, i, true)
  239. }
  240. }, that)
  241. })
  242. },
  243. })
  244. },
  245. getComb(that, imgSrcArray, i, _static) {
  246. console.log(imgSrcArray, "imgSrcArray")
  247. if (_static) {
  248. let imgTouch = wx.getStorageSync('madeCupImgData' + i);
  249. let dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  250. let canvasBg = that.data.obverseImgObject[i];
  251. console.log(canvasBg)
  252. wx.getSystemInfo({
  253. success: function (res) {
  254. let rpx = res.windowWidth / 750;
  255. that.setData({
  256. rpx: rpx
  257. });
  258. const ctx = wx.createCanvasContext('comb-canvas' + i);
  259. ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 800 * rpx);
  260. // 设置背景
  261. ctx.draw(true)
  262. imgSrcArray.forEach((res) => {
  263. ctx.drawImage(res, dataPositionObj.left * rpx, dataPositionObj.top * rpx, dataPositionObj.width * rpx, dataPositionObj.height * rpx);
  264. })
  265. console.log('开始执行叠加')
  266. ctx.drawImage(that.data.boxImg[i], 0, 0, 750 * rpx, 800 * rpx)
  267. ctx.draw(true, function () {
  268. wx.canvasToTempFilePath({
  269. canvasId: 'comb-canvas' + i,
  270. success: function (res) {
  271. that.uploadFile(res.tempFilePath, that, function (data) {
  272. console.log(res, "合成成功");
  273. let buildSrcArray = that.data.buildSrcArray;
  274. buildSrcArray.push({
  275. src: res.tempFilePath,
  276. isActive: i,
  277. });
  278. console.log(buildSrcArray, "buildSrcArray")
  279. that.setData({
  280. buildSrcArray: buildSrcArray,
  281. })
  282. console.log(that.data.buildSrcArray, "buildSrcArray")
  283. if (i < 7) {
  284. i = i + 1
  285. that.getCombImg(that, i);
  286. } else {
  287. wx.hideLoading()
  288. }
  289. })
  290. },
  291. fail: function (err) {
  292. console.log(err, "合成失败")
  293. wx.hideLoading();
  294. }
  295. }, that)
  296. })
  297. },
  298. })
  299. } else {
  300. if (i < 4) {
  301. i = i + 1
  302. that.getCombImg(that, i);
  303. } else {
  304. wx.hideLoading()
  305. }
  306. }
  307. },
  308. addImgRouter() { // add car
  309. let that = this;
  310. console.log(that.data.customGoodsImg, "that.data.customGoodsImg")
  311. wx.request({
  312. url: host + '/addShopCar',
  313. method: 'post',
  314. header: {
  315. 'content-type': 'application/x-www-form-urlencoded'
  316. },
  317. data: {
  318. customGoodsImg: that.data.customGoodsImg.substring(0, that.data.customGoodsImg.length - 1),
  319. customGoodsName: '拉杆箱-银灰色',
  320. customGoodsNum: 1,
  321. userId: app.globalData.user_id,
  322. customGoodsId:wx.getStorageSync('detail').id,
  323. },
  324. success: function (res) {
  325. console.log(res, "加入购物车")
  326. if (res.data === 'success') {
  327. wx.showModal({
  328. title: '提示',
  329. content: '加入购物车成功,是否去购物车结算商品?',
  330. success: function (res) {
  331. if (res.confirm) {
  332. console.log('用户点击确定')
  333. wx.redirectTo({
  334. url: '/customized/pages/car/car',
  335. })
  336. } else if (res.cancel) {
  337. console.log('用户点击取消')
  338. }
  339. }
  340. })
  341. }
  342. }
  343. })
  344. },
  345. uploadFile(path, that, cb) {
  346. let customGoodsImg = that.data.customGoodsImg;
  347. wx.uploadFile({
  348. url: host + 'fileUpload',
  349. filePath: path,
  350. name: 'file',
  351. formData: {
  352. file: path,
  353. userId: app.globalData.user_id,
  354. imgStatus: 1, // 表示合成
  355. },
  356. success: function (res) {
  357. console.log(res, "rrrr");
  358. customGoodsImg += res.data + ',';
  359. that.setData({
  360. customGoodsImg: customGoodsImg
  361. })
  362. cb(true)
  363. },
  364. fail: function (res) {
  365. cb(false)
  366. }
  367. })
  368. },
  369. })