comb.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. ctx.font = `'bold' ${fontTouch.fontSize} ${fontTouch.isColor} 'sans-serif'`;
  206. // const metrics = ctx.measureText(fontTouch.fontContent)
  207. // let movex = fontTouch.x + metrics.width / 2;
  208. // let movey = fontTouch.y + simulationFont.height / 2;
  209. // ctx.translate(movex, movey);
  210. // ctx.rotate(fontTouch.angle * Math.PI / 180);
  211. // ctx.fillText(fontTouch.fontContent, -metrics.width / 2, -simulationFont.height / 2 + simulationFont.height / 5 * 4);
  212. ctx.fillText(fontTouch.fontContent, 0, simulationFont.height / 5 * 4)
  213. ctx.draw(false,function () {
  214. wx.canvasToTempFilePath({
  215. canvasId: 'comb-font'+i,
  216. success: function (res) {
  217. console.log(res)
  218. that.getCombFontImg(that, res.tempFilePath, fontTouch,i)
  219. // let imgArray = that.data.imgSrcArray;
  220. // imgArray.push(res.tempFilePath)
  221. // that.setData({
  222. // imgSrcArray: imgArray
  223. // })
  224. // console.log(that.data.imgSrcArray, "that.data.imgSrcArray")
  225. // that.getComb(that, that.data.imgSrcArray,i,true)
  226. }
  227. }, that)
  228. })
  229. } else {
  230. that.getComb(that, that.data.imgSrcArray, i, _static)
  231. }
  232. },
  233. getCombFontImg(that, img, fontTouch,i){ // 生产字体图片后在进行处理一次
  234. wx.getSystemInfo({
  235. success: function (res) {
  236. let rpx = res.windowWidth / 750;
  237. that.setData({
  238. rpx: rpx
  239. });
  240. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  241. console.log(res)
  242. // canvas截图区域与图片的宽度比例
  243. const ctx = wx.createCanvasContext('comb-font' + i + '-2');
  244. //画布中点坐标转移到图片中心
  245. let movex = fontTouch.x + 218 * rpx / 2;
  246. let movey = fontTouch.y + 218 * rpx / 2;
  247. ctx.translate(movex, movey);
  248. ctx.rotate(fontTouch.angle * Math.PI / 180);
  249. ctx.drawImage(img, -218 * rpx / 2, -218 * rpx / 2, 218 * rpx, 218 * rpx)
  250. ctx.draw(false, function () {
  251. wx.canvasToTempFilePath({
  252. canvasId: 'comb-font' + i + '-2',
  253. success: function (res) {
  254. console.log(res)
  255. let imgArray = that.data.imgSrcArray;
  256. imgArray.push(res.tempFilePath)
  257. that.setData({
  258. imgSrcArray: imgArray
  259. })
  260. that.getComb(that, that.data.imgSrcArray,i,true)
  261. }
  262. }, that)
  263. })
  264. },
  265. })
  266. },
  267. getComb(that, imgSrcArray,i,_static) {
  268. console.log(imgSrcArray,"imgSrcArray")
  269. if(_static){
  270. let imgTouch = wx.getStorageSync('madeCupImgData' + i);
  271. let dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  272. let canvasBg = that.data.obverseImgObject[i];
  273. console.log(canvasBg)
  274. wx.getSystemInfo({
  275. success: function (res) {
  276. let rpx = res.windowWidth / 750;
  277. that.setData({
  278. rpx: rpx
  279. });
  280. const ctx = wx.createCanvasContext('comb-canvas' + i);
  281. ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 588 * rpx);
  282. // 设置背景
  283. ctx.draw(true)
  284. imgSrcArray.forEach((res) => {
  285. ctx.drawImage(res, dataPositionObj.left * rpx, dataPositionObj.top * rpx, dataPositionObj.width * rpx, dataPositionObj.height * rpx);
  286. })
  287. ctx.draw(true, function () {
  288. wx.canvasToTempFilePath({
  289. canvasId: 'comb-canvas' + i,
  290. success: function (res) {
  291. that.uploadFile(res.tempFilePath,that,function(data){
  292. console.log(res, "合成成功");
  293. let buildSrcArray = that.data.buildSrcArray;
  294. buildSrcArray.push({
  295. src: res.tempFilePath,
  296. isActive: i,
  297. });
  298. console.log(buildSrcArray, "buildSrcArray")
  299. that.setData({
  300. buildSrcArray: buildSrcArray,
  301. })
  302. console.log(that.data.buildSrcArray, "buildSrcArray")
  303. if (i < 4) {
  304. i = i + 1
  305. that.getCombImg(that, i);
  306. } else {
  307. wx.hideLoading()
  308. }
  309. })
  310. },
  311. fail: function (err) {
  312. console.log(err, "合成失败")
  313. wx.hideLoading();
  314. }
  315. }, that)
  316. })
  317. },
  318. })
  319. } else {
  320. if (i < 4) {
  321. i = i + 1
  322. that.getCombImg(that, i);
  323. } else {
  324. wx.hideLoading()
  325. }
  326. }
  327. },
  328. addImgRouter(){ // add car
  329. let that = this;
  330. console.log(that.data.customGoodsImg,"that.data.customGoodsImg")
  331. wx.request({
  332. url: host + '/addShopCar',
  333. method:'post',
  334. header: {
  335. 'content-type': 'application/x-www-form-urlencoded'
  336. },
  337. data:{
  338. customGoodsImg: that.data.customGoodsImg.substring(0, that.data.customGoodsImg.length-1),
  339. customGoodsName:'奶缸-黑色 350ml',
  340. customGoodsNum: 1,
  341. userId: app.globalData.user_id,
  342. },
  343. success:function(res){
  344. console.log(res,"加入购物车")
  345. if(res.data ==='success'){
  346. wx.showModal({
  347. title: '提示',
  348. content: '加入购物车成功,是否去购物车结算商品?',
  349. success:function(res){
  350. if (res.confirm) {
  351. console.log('用户点击确定')
  352. wx.redirectTo({
  353. url: '/customized/pages/car/car',
  354. })
  355. } else if (res.cancel) {
  356. console.log('用户点击取消')
  357. }
  358. }
  359. })
  360. }
  361. }
  362. })
  363. },
  364. uploadFile(path,that,cb){
  365. let customGoodsImg = that.data.customGoodsImg;
  366. wx.uploadFile({
  367. url: host + 'fileUpload',
  368. filePath: path,
  369. name: 'file',
  370. formData: {
  371. file: path,
  372. userId: app.globalData.user_id,
  373. imgStatus: 1,// 表示合成
  374. },
  375. success: function (res) {
  376. console.log(res, "rrrr");
  377. customGoodsImg += res.data+',';
  378. that.setData({
  379. customGoodsImg: customGoodsImg
  380. })
  381. cb(true)
  382. },
  383. fail: function (res) {
  384. cb(false)
  385. }
  386. })
  387. }
  388. })