comb.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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'],
  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. '3':'手柄',
  58. '4':'底部',
  59. },
  60. buildSrcArray: [],
  61. customGoodsImg:"",
  62. },
  63. /**
  64. * 生命周期函数--监听页面加载
  65. */
  66. onLoad: function (options) {
  67. // if (wx.getStorageSync('madeCupImgData1')) {
  68. // this.setData({
  69. // imgTouch: wx.getStorageSync('madeCupImgData1')
  70. // })
  71. // }
  72. // if (wx.getStorageSync('madeCupFontData1')) {
  73. // this.setData({
  74. // fontTouch: wx.getStorageSync('madeCupFontData1')
  75. // })
  76. // }
  77. },
  78. /**
  79. * 生命周期函数--监听页面初次渲染完成
  80. */
  81. onReady: function () {
  82. wx.showLoading({
  83. title: '图片合成中...'
  84. })
  85. let that = this, result = true;
  86. // for (let i = 1; i <= 4; i++) {
  87. // if (wx.getStorageSync('madeCupImgData' + i) || wx.getStorageSync('madeCupFontData' + i)) {
  88. // console.log(i, "iiiii")
  89. // that.getCombImg(that, i);
  90. // }
  91. // }
  92. if (wx.getStorageSync('madeCupImgData1') || wx.getStorageSync('madeCupFontData1')) {
  93. that.getCombImg(that, 1);
  94. result = false
  95. } else if (wx.getStorageSync('madeCupImgData2') || wx.getStorageSync('madeCupFontData2')) {
  96. that.getCombImg(that, 2);
  97. result = false
  98. } else if (wx.getStorageSync('madeCupImgData3') || wx.getStorageSync('madeCupFontData3')) {
  99. that.getCombImg(that, 3);
  100. result = false
  101. } else if (wx.getStorageSync('madeCupImgData4') || wx.getStorageSync('madeCupFontData4')) {
  102. that.getCombImg(that, 4);
  103. result = false
  104. }
  105. if (result) {
  106. wx.hideLoading();
  107. }
  108. },
  109. /**
  110. * 生命周期函数--监听页面显示
  111. */
  112. onShow: function () {
  113. },
  114. /**
  115. * 生命周期函数--监听页面隐藏
  116. */
  117. onHide: function () {
  118. },
  119. /**
  120. * 生命周期函数--监听页面卸载
  121. */
  122. onUnload: function () {
  123. },
  124. /**
  125. * 页面相关事件处理函数--监听用户下拉动作
  126. */
  127. onPullDownRefresh: function () {
  128. },
  129. /**
  130. * 页面上拉触底事件的处理函数
  131. */
  132. onReachBottom: function () {
  133. },
  134. /**
  135. * 用户点击右上角分享
  136. */
  137. onShareAppMessage: function () {
  138. },
  139. getCombImg(that,i) {
  140. let imgTouch = null, dataPositionObj=null;
  141. that.setData({ // imgSrcArray 初始化
  142. imgSrcArray:[],
  143. })
  144. console.log(wx.getStorageSync('madeCupImgData' + i),"wx.getStorageSync('madeCupImgData' + i)")
  145. if(wx.getStorageSync('madeCupImgData' + i)){
  146. imgTouch = wx.getStorageSync('madeCupImgData'+i);
  147. dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  148. console.log(dataPositionObj,"dataPositionObj")
  149. wx.getSystemInfo({
  150. success: function (res) {
  151. let rpx = res.windowWidth / 750;
  152. that.setData({
  153. rpx: rpx
  154. });
  155. wx.downloadFile({
  156. url: imgTouch.src, // 仅为示例,并非真实的资源
  157. success(res) {
  158. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  159. console.log(res)
  160. if (res.statusCode === 200) {
  161. // canvas截图区域与图片的宽度比例
  162. let dWidth = dataPositionObj.width / imgTouch.width;
  163. // canvas截图区域与图片的高度比例
  164. let dHeight = dataPositionObj.height / imgTouch.height;
  165. const ctx = wx.createCanvasContext('comb-img'+i);
  166. //画布中点坐标转移到图片中心
  167. let movex = imgTouch.x + imgTouch.width * rpx / 2;
  168. let movey = imgTouch.y + imgTouch.height * rpx / 2;
  169. ctx.translate(movex, movey);
  170. ctx.rotate(imgTouch.angle * Math.PI / 180);
  171. ctx.drawImage(res.tempFilePath, -imgTouch.width * rpx / 2, -imgTouch.height * rpx / 2, imgTouch.width * rpx, imgTouch.height * rpx)
  172. ctx.draw(false,function () {
  173. wx.canvasToTempFilePath({
  174. canvasId: 'comb-img'+i,
  175. success: function (res) {
  176. console.log(res)
  177. let imgArray = that.data.imgSrcArray;
  178. imgArray.push(res.tempFilePath)
  179. that.setData({
  180. imgSrcArray: imgArray
  181. })
  182. that.getCombFont(that,i,true);
  183. }
  184. }, that)
  185. })
  186. }
  187. }
  188. })
  189. },
  190. })
  191. } else {
  192. that.getCombFont(that,i,false);
  193. }
  194. },
  195. getCombFont(that,i,_static) {
  196. let fontTouch = null, dataPositionObj = null, simulationFont=null;
  197. if (wx.getStorageSync('madeCupFontData' + i)) {
  198. fontTouch = wx.getStorageSync('madeCupFontData' + i);
  199. simulationFont = wx.getStorageSync('simulationFont' + i);
  200. dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  201. const ctx = wx.createCanvasContext('comb-font'+i);
  202. // ctx.textBaseline = 'top';
  203. ctx.setFontSize(fontTouch.fontSize);
  204. ctx.setFillStyle(fontTouch.isColor);
  205. if(fontTouch.bold){
  206. ctx.fillText(fontTouch.fontContent, -1, simulationFont.height / 5 * 4)
  207. ctx.fillText(fontTouch.fontContent, 0, simulationFont.height / 5 * 4 -1)
  208. }
  209. ctx.fillText(fontTouch.fontContent, 0, simulationFont.height / 5 * 4)
  210. if (fontTouch.bold) {
  211. ctx.fillText(fontTouch.fontContent, 1, simulationFont.height / 5 * 4)
  212. ctx.fillText(fontTouch.fontContent, 0, simulationFont.height / 5 * 4 + 1)
  213. }
  214. ctx.restore()
  215. // ctx.font = `'bold' ${fontTouch.fontSize} ${fontTouch.isColor} 'sans-serif'`;
  216. // const metrics = ctx.measureText(fontTouch.fontContent)
  217. // let movex = fontTouch.x + metrics.width / 2;
  218. // let movey = fontTouch.y + simulationFont.height / 2;
  219. // ctx.translate(movex, movey);
  220. // ctx.rotate(fontTouch.angle * Math.PI / 180);
  221. // ctx.fillText(fontTouch.fontContent, -metrics.width / 2, -simulationFont.height / 2 + simulationFont.height / 5 * 4);
  222. // ctx.fillText(fontTouch.fontContent, 0, simulationFont.height / 5 * 4)
  223. ctx.draw(false,function () {
  224. wx.canvasToTempFilePath({
  225. canvasId: 'comb-font'+i,
  226. success: function (res) {
  227. console.log(res)
  228. that.getCombFontImg(that, res.tempFilePath, fontTouch,i)
  229. // let imgArray = that.data.imgSrcArray;
  230. // imgArray.push(res.tempFilePath)
  231. // that.setData({
  232. // imgSrcArray: imgArray
  233. // })
  234. // console.log(that.data.imgSrcArray, "that.data.imgSrcArray")
  235. // that.getComb(that, that.data.imgSrcArray,i,true)
  236. }
  237. }, that)
  238. })
  239. } else {
  240. that.getComb(that, that.data.imgSrcArray, i, _static)
  241. }
  242. },
  243. getCombFontImg(that, img, fontTouch,i){ // 生产字体图片后在进行处理一次
  244. wx.getSystemInfo({
  245. success: function (res) {
  246. let rpx = res.windowWidth / 750;
  247. that.setData({
  248. rpx: rpx
  249. });
  250. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  251. console.log(res)
  252. // canvas截图区域与图片的宽度比例
  253. const ctx = wx.createCanvasContext('comb-font' + i + '-2');
  254. //画布中点坐标转移到图片中心
  255. let movex = fontTouch.x + 218 * rpx / 2;
  256. let movey = fontTouch.y + 218 * rpx / 2;
  257. ctx.translate(movex, movey);
  258. ctx.rotate(fontTouch.angle * Math.PI / 180);
  259. ctx.drawImage(img, -218 * rpx / 2, -218 * rpx / 2, 218 * rpx, 218 * rpx)
  260. ctx.draw(false, function () {
  261. wx.canvasToTempFilePath({
  262. canvasId: 'comb-font' + i + '-2',
  263. success: function (res) {
  264. console.log(res)
  265. let imgArray = that.data.imgSrcArray;
  266. imgArray.push(res.tempFilePath)
  267. that.setData({
  268. imgSrcArray: imgArray
  269. })
  270. that.getComb(that, that.data.imgSrcArray,i,true)
  271. }
  272. }, that)
  273. })
  274. },
  275. })
  276. },
  277. getComb(that, imgSrcArray,i,_static) {
  278. console.log(imgSrcArray,"imgSrcArray")
  279. if(_static){
  280. let imgTouch = wx.getStorageSync('madeCupImgData' + i);
  281. let dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  282. let canvasBg = that.data.obverseImgObject[i];
  283. console.log(canvasBg)
  284. wx.getSystemInfo({
  285. success: function (res) {
  286. let rpx = res.windowWidth / 750;
  287. that.setData({
  288. rpx: rpx
  289. });
  290. const ctx = wx.createCanvasContext('comb-canvas' + i);
  291. ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 588 * rpx);
  292. // 设置背景
  293. ctx.draw(true)
  294. imgSrcArray.forEach((res) => {
  295. ctx.drawImage(res, dataPositionObj.left * rpx, dataPositionObj.top * rpx, dataPositionObj.width * rpx, dataPositionObj.height * rpx);
  296. })
  297. ctx.draw(true, function () {
  298. wx.canvasToTempFilePath({
  299. canvasId: 'comb-canvas' + i,
  300. success: function (res) {
  301. that.uploadFile(res.tempFilePath,that,function(data){
  302. console.log(res, "合成成功");
  303. let buildSrcArray = that.data.buildSrcArray;
  304. buildSrcArray.push({
  305. src: res.tempFilePath,
  306. isActive: i,
  307. });
  308. console.log(buildSrcArray, "buildSrcArray")
  309. that.setData({
  310. buildSrcArray: buildSrcArray,
  311. })
  312. console.log(that.data.buildSrcArray, "buildSrcArray")
  313. if (i < 4) {
  314. i = i + 1
  315. that.getCombImg(that, i);
  316. } else {
  317. wx.hideLoading()
  318. }
  319. })
  320. },
  321. fail: function (err) {
  322. console.log(err, "合成失败")
  323. wx.hideLoading();
  324. }
  325. }, that)
  326. })
  327. },
  328. })
  329. } else {
  330. if (i < 4) {
  331. i = i + 1
  332. that.getCombImg(that, i);
  333. } else {
  334. wx.hideLoading()
  335. }
  336. }
  337. },
  338. addImgRouter(){ // add car
  339. let that = this;
  340. console.log(that.data.customGoodsImg,"that.data.customGoodsImg")
  341. wx.request({
  342. url: host + '/addShopCar',
  343. method:'post',
  344. header: {
  345. 'content-type': 'application/x-www-form-urlencoded'
  346. },
  347. data:{
  348. customGoodsImg: that.data.customGoodsImg.substring(0, that.data.customGoodsImg.length-1),
  349. customGoodsName:'奶缸-黑色 350ml',
  350. customGoodsNum: 1,
  351. userId: app.globalData.user_id,
  352. },
  353. success:function(res){
  354. console.log(res,"加入购物车")
  355. if(res.data ==='success'){
  356. wx.showModal({
  357. title: '提示',
  358. content: '加入购物车成功,是否去购物车结算商品?',
  359. success:function(res){
  360. if (res.confirm) {
  361. console.log('用户点击确定')
  362. wx.redirectTo({
  363. url: '/customized/pages/car/car',
  364. })
  365. } else if (res.cancel) {
  366. console.log('用户点击取消')
  367. }
  368. }
  369. })
  370. }
  371. }
  372. })
  373. },
  374. uploadFile(path,that,cb){
  375. let customGoodsImg = that.data.customGoodsImg;
  376. wx.uploadFile({
  377. url: host + 'fileUpload',
  378. filePath: path,
  379. name: 'file',
  380. formData: {
  381. file: path,
  382. userId: app.globalData.user_id,
  383. imgStatus: 1,// 表示合成
  384. },
  385. success: function (res) {
  386. console.log(res, "rrrr");
  387. customGoodsImg += res.data+',';
  388. that.setData({
  389. customGoodsImg: customGoodsImg
  390. })
  391. cb(true)
  392. },
  393. fail: function (res) {
  394. cb(false)
  395. }
  396. })
  397. }
  398. })