oneComb.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. console.log(res)
  187. that.getComb(that, that.data.imgSrcArray, i)
  188. }
  189. }, that)
  190. })
  191. } else {
  192. that.getComb(that, that.data.imgSrcArray, i)
  193. }
  194. },
  195. getComb(that, imgSrcArray, i) {
  196. console.log(imgSrcArray, "imgSrcArray")
  197. let imgTouch = wx.getStorageSync('madeImgData' + i);
  198. let dataPositionObj = that.data.dataPositionObj;
  199. let canvasBg = that.data.obverseImgObject[i+'_2'];//获取背景没有线框的
  200. wx.getSystemInfo({
  201. success: function (res) {
  202. let rpx = res.windowWidth / 750;
  203. that.setData({
  204. rpx: rpx
  205. });
  206. const ctx = wx.createCanvasContext('comb-canvas');
  207. ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 800 * rpx);
  208. // 设置背景
  209. ctx.draw(true)
  210. imgSrcArray.forEach((res) => {
  211. ctx.drawImage(res, dataPositionObj.left * rpx, dataPositionObj.top * rpx, dataPositionObj.width * rpx, dataPositionObj.height * rpx);
  212. })
  213. console.log('开始执行叠加', that.data.obverseImgObject[i + '_1'])
  214. ctx.drawImage(that.data.obverseImgObject[i + '_1'], 0, 0, 750 * rpx, 800 * rpx)
  215. ctx.draw(true, function () {
  216. console.log("开始合成")
  217. wx.canvasToTempFilePath({
  218. canvasId: 'comb-canvas',
  219. success: function (res) {
  220. console.log(res,"合成结果")
  221. that.uploadFile(res.tempFilePath, that, function (data) {
  222. console.log(res, "合成成功");
  223. let buildSrcArray = that.data.buildSrcArray;
  224. buildSrcArray.push({
  225. src: res.tempFilePath,
  226. isActive: i,
  227. });
  228. console.log(buildSrcArray, "buildSrcArray")
  229. that.setData({
  230. buildSrcArray: buildSrcArray,
  231. })
  232. console.log(that.data.buildSrcArray, "buildSrcArray")
  233. wx.hideLoading()
  234. })
  235. },
  236. fail: function (err) {
  237. console.log(err, "合成失败")
  238. wx.hideLoading();
  239. }
  240. }, that)
  241. })
  242. },
  243. })
  244. },
  245. addImgRouter() { // add car
  246. let that = this;
  247. console.log(that.data.customGoodsImg, "that.data.customGoodsImg")
  248. wx.request({
  249. url: host + '/addShopCar',
  250. method: 'post',
  251. header: {
  252. 'content-type': 'application/x-www-form-urlencoded'
  253. },
  254. data: {
  255. customGoodsImg: that.data.customGoodsImg.substring(0, that.data.customGoodsImg.length - 1),
  256. customGoodsName: '拉杆箱-银灰色',
  257. customGoodsNum: 1,
  258. userId: app.globalData.user_id,
  259. customGoodsId:wx.getStorageSync('detail').id,
  260. },
  261. success: function (res) {
  262. console.log(res, "加入购物车")
  263. if (res.data === 'success') {
  264. wx.showModal({
  265. title: '提示',
  266. content: '加入购物车成功,是否去购物车结算商品?',
  267. success: function (res) {
  268. if (res.confirm) {
  269. console.log('用户点击确定')
  270. wx.redirectTo({
  271. url: '/customized/pages/car/car',
  272. })
  273. } else if (res.cancel) {
  274. console.log('用户点击取消')
  275. }
  276. }
  277. })
  278. }
  279. }
  280. })
  281. },
  282. uploadFile(path, that, cb) {
  283. let customGoodsImg = that.data.customGoodsImg;
  284. wx.uploadFile({
  285. url: host + 'fileUpload',
  286. filePath: path,
  287. name: 'file',
  288. formData: {
  289. file: path,
  290. userId: app.globalData.user_id,
  291. imgStatus: 1, // 表示合成
  292. },
  293. success: function (res) {
  294. console.log(res, "rrrr");
  295. customGoodsImg += res.data + ',';
  296. that.setData({
  297. customGoodsImg: customGoodsImg
  298. })
  299. cb(true)
  300. },
  301. fail: function (res) {
  302. cb(false)
  303. }
  304. })
  305. },
  306. })