comb.js 14 KB

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