boxComb.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. dataPositionObj1: {},
  11. dataPositionObj2: {},
  12. imgTouch: {
  13. isActive: "1",
  14. src: "https://dlz.info666.com//temp/d945bfb0-5cc6-406d-a814-cf63b099041f.jpg",
  15. x: -82,
  16. y: 17,
  17. clientX: 195,
  18. clientY: 121,
  19. width: 517,
  20. height: 208.17866666666666,
  21. scale: 2.4834437086092715,
  22. angle: 27
  23. },
  24. fontTouch: {
  25. isActive: "1",
  26. fontContent: "Gg",
  27. isColor: "#ffffff",
  28. x: 10,
  29. y: 10,
  30. angle: 0,
  31. fontSize: 100,
  32. clientX: 0,
  33. clientY: 0,
  34. width: 138.34375,
  35. height: 132
  36. },
  37. rpx: 1,
  38. obverseImgObject: {},
  39. imgSrcArray: [],
  40. buildSrcArrayText: {
  41. '1': '正面',
  42. '2': '背面',
  43. },
  44. buildSrcArray: [],
  45. dataId:[],
  46. customGoodsImg: "",
  47. customGoodsMaterial:"",//定制资源图片
  48. },
  49. /**
  50. * 生命周期函数--监听页面加载
  51. */
  52. onLoad: function (options) {
  53. if ('dataId' in options) {
  54. this.setData({
  55. obverseImgObject: util.changeImg,//素材
  56. dataPositionObj1: util.changeCup(options.dataId.split(',')[0].toString()).dataPositionObj,
  57. dataPositionObj2: util.changeCup(options.dataId.split(',')[1].toString()).dataPositionObj,
  58. dataId: options.dataId.split(',')
  59. })
  60. }
  61. },
  62. /**
  63. * 生命周期函数--监听页面初次渲染完成
  64. */
  65. onReady: function () {
  66. wx.showLoading({
  67. title: '图片合成中...'
  68. })
  69. let that = this,
  70. result = true,
  71. dataId = that.data.dataId;
  72. if (wx.getStorageSync('madeCupImgData' + dataId[0]) || wx.getStorageSync('madeCupFontData' + dataId[0])) {
  73. that.getCombImg(that, dataId[0],1);
  74. result = false
  75. } else if (wx.getStorageSync('madeCupImgData' + dataId[1]) || wx.getStorageSync('madeCupFontData' + dataId[1])) {
  76. that.getCombImg(that, dataId[1],2);
  77. result = false
  78. }
  79. if (result) {
  80. wx.hideLoading();
  81. }
  82. },
  83. /**
  84. * 生命周期函数--监听页面显示
  85. */
  86. onShow: function () {
  87. },
  88. /**
  89. * 生命周期函数--监听页面隐藏
  90. */
  91. onHide: function () {
  92. },
  93. /**
  94. * 生命周期函数--监听页面卸载
  95. */
  96. onUnload: function () {
  97. },
  98. /**
  99. * 页面相关事件处理函数--监听用户下拉动作
  100. */
  101. onPullDownRefresh: function () {
  102. },
  103. /**
  104. * 页面上拉触底事件的处理函数
  105. */
  106. onReachBottom: function () {
  107. },
  108. /**
  109. * 用户点击右上角分享
  110. */
  111. onShareAppMessage: function () {
  112. },
  113. getCombImg(that, i,id) {
  114. let imgTouch = null,
  115. dataPositionObj = null;
  116. that.setData({ // imgSrcArray 初始化
  117. imgSrcArray: [],
  118. })
  119. console.log(wx.getStorageSync('madeCupImgData' + i), "wx.getStorageSync('madeCupImgData' + i)")
  120. if (wx.getStorageSync('madeCupImgData' + i)) {
  121. imgTouch = wx.getStorageSync('madeCupImgData' + i);
  122. dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  123. console.log(dataPositionObj, imgTouch,"dataPositionObj")
  124. let customGoodsMaterial = that.data.customGoodsMaterial;
  125. customGoodsMaterial += imgTouch.src.replace(host,'')+',';
  126. that.setData({
  127. customGoodsMaterial: customGoodsMaterial
  128. })
  129. wx.getSystemInfo({
  130. success: function (res) {
  131. let rpx = res.windowWidth / 750;
  132. that.setData({
  133. rpx: rpx
  134. });
  135. wx.downloadFile({
  136. url: imgTouch.src, // 仅为示例,并非真实的资源
  137. success(res) {
  138. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  139. console.log(res)
  140. if (res.statusCode === 200) {
  141. // canvas截图区域与图片的宽度比例
  142. let dWidth = dataPositionObj.width / imgTouch.width;
  143. // canvas截图区域与图片的高度比例
  144. let dHeight = dataPositionObj.height / imgTouch.height;
  145. const ctx = wx.createCanvasContext('comb-img' + id);
  146. //画布中点坐标转移到图片中心
  147. let movex = imgTouch.x + imgTouch.width * rpx / 2;
  148. let movey = imgTouch.y + imgTouch.height * rpx / 2;
  149. ctx.translate(movex, movey);
  150. ctx.rotate(imgTouch.angle * Math.PI / 180);
  151. ctx.drawImage(res.tempFilePath, -imgTouch.width * rpx / 2, -imgTouch.height * rpx / 2, imgTouch.width * rpx, imgTouch.height * rpx)
  152. ctx.draw(false, function () {
  153. wx.canvasToTempFilePath({
  154. canvasId: 'comb-img' + id,
  155. success: function (res) {
  156. console.log(res)
  157. let imgArray = that.data.imgSrcArray;
  158. imgArray.push(res.tempFilePath)
  159. that.setData({
  160. imgSrcArray: imgArray
  161. })
  162. that.getCombFont(that, i, true,id);
  163. }
  164. }, that)
  165. })
  166. }
  167. },
  168. error(err){
  169. console.log('err下载失败',err)
  170. }
  171. })
  172. },
  173. })
  174. } else {
  175. that.getCombFont(that, i, false,id);
  176. console.log("没有图片往下执行")
  177. }
  178. },
  179. getCombFont(that, i, _static,id) {
  180. let fontTouch = null,
  181. dataPositionObj = null,
  182. simulationFont = null;
  183. if (wx.getStorageSync('madeCupFontData' + i)) {
  184. fontTouch = wx.getStorageSync('madeCupFontData' + i);
  185. simulationFont = wx.getStorageSync('simulationFont' + i);
  186. dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  187. const ctx = wx.createCanvasContext('comb-font' + id);
  188. console.log(id, ctx,"console.log(id,)")
  189. // ctx.textBaseline = 'top';
  190. ctx.setFontSize(fontTouch.fontSize);
  191. ctx.setFillStyle(fontTouch.isColor);
  192. if (fontTouch.bold) {
  193. ctx.fillText(fontTouch.fontContent, -1, simulationFont.height / 5 * 4)
  194. ctx.fillText(fontTouch.fontContent, 0, simulationFont.height / 5 * 4 - 1)
  195. }
  196. ctx.fillText(fontTouch.fontContent, 0, simulationFont.height / 5 * 4)
  197. if (fontTouch.bold) {
  198. ctx.fillText(fontTouch.fontContent, 1, simulationFont.height / 5 * 4)
  199. ctx.fillText(fontTouch.fontContent, 0, simulationFont.height / 5 * 4 + 1)
  200. }
  201. ctx.restore()
  202. ctx.draw(false, function () {
  203. wx.canvasToTempFilePath({
  204. canvasId: 'comb-font' + id,
  205. success: function (res) {
  206. console.log(res)
  207. that.getCombFontImg(that, res.tempFilePath, fontTouch, i,id)
  208. }
  209. }, that)
  210. })
  211. } else {
  212. that.getComb(that, that.data.imgSrcArray, i, _static,id)
  213. }
  214. },
  215. getCombFontImg(that, img, fontTouch, i,id) { // 生产字体图片后在进行处理一次
  216. let dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  217. wx.getSystemInfo({
  218. success: function (res) {
  219. let rpx = res.windowWidth / 750;
  220. that.setData({
  221. rpx: rpx
  222. });
  223. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  224. console.log(res)
  225. // canvas截图区域与图片的宽度比例
  226. const ctx = wx.createCanvasContext('comb-font' + id + '-2');
  227. //画布中点坐标转移到图片中心
  228. let movex = fontTouch.x + dataPositionObj.width * rpx / 2;
  229. let movey = fontTouch.y + dataPositionObj.height * rpx / 2;
  230. ctx.translate(movex, movey);
  231. ctx.rotate(fontTouch.angle * Math.PI / 180);
  232. ctx.drawImage(img, -dataPositionObj.width * rpx / 2, -dataPositionObj.height * rpx / 2, dataPositionObj.width * rpx, dataPositionObj.height * rpx)
  233. ctx.draw(false, function () {
  234. wx.canvasToTempFilePath({
  235. canvasId: 'comb-font' + id + '-2',
  236. success: function (res) {
  237. console.log(res)
  238. let imgArray = that.data.imgSrcArray;
  239. imgArray.push(res.tempFilePath)
  240. that.setData({
  241. imgSrcArray: imgArray
  242. })
  243. that.getComb(that, that.data.imgSrcArray, i, true,id)
  244. }
  245. }, that)
  246. })
  247. },
  248. })
  249. },
  250. getComb(that, imgSrcArray, i, _static,id) {
  251. console.log(imgSrcArray, "imgSrcArray")
  252. if (_static) {
  253. let imgTouch = wx.getStorageSync('madeCupImgData' + i);
  254. let dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  255. wx.downloadFile({
  256. url: that.data.obverseImgObject[i],
  257. success(res){
  258. if (res.statusCode === 200) {
  259. let canvasBg = res.tempFilePath;
  260. wx.getSystemInfo({
  261. success: function (res) {
  262. let rpx = res.windowWidth / 750;
  263. that.setData({
  264. rpx: rpx
  265. });
  266. const ctx = wx.createCanvasContext('comb-canvas' + id);
  267. ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 800 * rpx);
  268. // 设置背景
  269. ctx.draw(true)
  270. imgSrcArray.forEach((res) => {
  271. ctx.drawImage(res, dataPositionObj.left * rpx, dataPositionObj.top * rpx, dataPositionObj.width * rpx, dataPositionObj.height * rpx);
  272. })
  273. wx.downloadFile({
  274. url: that.data.obverseImgObject[i+'_2'],
  275. success(file){
  276. if (file.statusCode === 200) {
  277. ctx.drawImage(file.tempFilePath, 0, 0, 750 * rpx, 800 * rpx);
  278. ctx.draw(true, function () {
  279. wx.canvasToTempFilePath({
  280. canvasId: 'comb-canvas' + id,
  281. success: function (res) {
  282. that.uploadFile(res.tempFilePath, that, function (data) {
  283. console.log(res, "合成成功");
  284. let buildSrcArray = that.data.buildSrcArray;
  285. buildSrcArray.push({
  286. src: res.tempFilePath,
  287. isActive: i,
  288. });
  289. console.log(buildSrcArray, "buildSrcArray")
  290. that.setData({
  291. buildSrcArray: buildSrcArray,
  292. })
  293. console.log('第一张图片合成结束,i='+i)
  294. if (i < that.data.dataId[1]) {
  295. i = i*1 + 1
  296. that.getCombImg(that, i,2);
  297. } else {
  298. wx.hideLoading()
  299. }
  300. })
  301. },
  302. fail: function (err) {
  303. console.log(err, "合成失败")
  304. wx.hideLoading();
  305. }
  306. }, that)
  307. })
  308. } else {
  309. wx.showToast({
  310. title: '合成资源图片不存在,请联系客服',
  311. icon: 'error'
  312. })
  313. wx.hideLoading();
  314. }
  315. }
  316. })
  317. },
  318. })
  319. } else {
  320. wx.showToast({
  321. title: '合成资源图片不存在,请联系客服',
  322. icon:'error'
  323. })
  324. wx.hideLoading();
  325. }
  326. }
  327. })
  328. } else {
  329. console.log("走了这里哈,i值等于",i)
  330. if (i < that.data.dataId[1]) {
  331. i = i*1 + 1
  332. that.getCombImg(that, i,2);
  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: wx.getStorageSync('detail').customgoodsName,
  350. customGoodsNum: 1,
  351. userId: app.globalData.user_id,
  352. customGoodsId:wx.getStorageSync('detail').id,
  353. customGoodsMaterial: that.data.customGoodsMaterial.substring(0, that.data.customGoodsMaterial.length-1),
  354. },
  355. success: function (res) {
  356. console.log(res, "加入购物车")
  357. if (res.data === 'success') {
  358. wx.showModal({
  359. title: '提示',
  360. content: '加入购物车成功,是否去购物车结算商品?',
  361. success: function (res) {
  362. if (res.confirm) {
  363. console.log('用户点击确定')
  364. wx.redirectTo({
  365. url: '/customized/pages/car/car',
  366. })
  367. } else if (res.cancel) {
  368. console.log('用户点击取消')
  369. }
  370. }
  371. })
  372. }
  373. }
  374. })
  375. },
  376. uploadFile(path, that, cb) {
  377. let customGoodsImg = that.data.customGoodsImg;
  378. wx.uploadFile({
  379. url: host + 'fileUpload',
  380. filePath: path,
  381. name: 'file',
  382. formData: {
  383. file: path,
  384. userId: app.globalData.user_id,
  385. imgStatus: 1, // 表示合成
  386. },
  387. success: function (res) {
  388. console.log(res, "rrrr");
  389. customGoodsImg += res.data + ',';
  390. that.setData({
  391. customGoodsImg: customGoodsImg
  392. })
  393. cb(true)
  394. },
  395. fail: function (res) {
  396. cb(false)
  397. }
  398. })
  399. },
  400. })