comb.js 11 KB

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