oneComb.js 12 KB

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