boxComb.js 13 KB

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