boxComb.js 13 KB

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