oneComb.js 12 KB

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