project-detail.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. var width = 0
  2. var height = 0
  3. var ctx
  4. var _request = require('../../../utils/request.js')
  5. var _handle = require('../../../utils/handle.js')
  6. var WxParse = require('../../../utils/wxParse/wxParse.js')
  7. var util = require('../../../utils/accounting.min.js')
  8. var fomldsJSON = ''
  9. Page({
  10. posterImage: {},
  11. posterLoading: 0,
  12. canvasLoading: false,
  13. qcodeError: false,
  14. data: {
  15. productId: 0,
  16. indicatorDots: false,
  17. autoplay: false,
  18. interval: 5000,
  19. duration: 1000,
  20. project: {},
  21. showDialog: false,
  22. showCartDialog: false,
  23. animationData: {},
  24. count: 1,
  25. image: {},
  26. bindTel: false,
  27. balance: 0,
  28. posterPopup: false,
  29. posterImage: null,
  30. imgindex: 1,
  31. popup: false,
  32. hour: '00',
  33. min: '00',
  34. second: '00',
  35. timer: '',
  36. leftTime: '0',
  37. // msecond: '00',
  38. clock: '',
  39. todaysend: Date.parse(new Date()) / 1000,
  40. showpay: true,
  41. productSize:'',//产品规格
  42. size_list:0,
  43. color_list:0,
  44. canpay:true
  45. },
  46. onLoad: function (options) {
  47. this.data.productId = options.id;
  48. this.getBalanceInfo()
  49. },
  50. onShow: function () {
  51. this.getIphoneX()
  52. this.getProjectDetail()
  53. this.check()
  54. },
  55. onHide: function () {
  56. this.getneedshare()
  57. },
  58. dpi: function (val) {
  59. return val
  60. },
  61. onSlideChangeEnd: function(e) {
  62. var that = this;
  63. that.setData({
  64. imgindex: e.detail.current + 1
  65. })
  66. },
  67. check () {
  68. var that = this
  69. var url = 'v1/user/check'
  70. var params = {
  71. }
  72. var success = function (res) {
  73. if (res.data.user_id > 0) {
  74. that.setData({
  75. bindTel: true
  76. })
  77. }
  78. if (res.data.wx_user_id > 0) {
  79. that.getneedshare()
  80. }
  81. }
  82. _request.$get(url, params, success)
  83. },
  84. onShareAppMessage: function (val) {
  85. this.shareInfo()
  86. this.setData({
  87. showpay: true
  88. })
  89. return _request.share({
  90. title: this.data.project.share_content || this.data.project.name,
  91. path: '/pages/start/start?url=pages/projects/project-detail/project-detail&id=' + this.data.productId,
  92. imageUrl: this.data.project.share_img,
  93. sc: 'xcx_product',
  94. ri: this.data.productId,
  95. rp: this.data.productId,
  96. })
  97. },
  98. // imageLoad: function (e) {
  99. // var _width = e.detail.width,
  100. // _height = e.detail.height,
  101. // ratio = _width / _height;
  102. // var viewWidth = 750,
  103. // viewHeight = 750 / ratio;
  104. // var image = this.data.image;
  105. // image[e.target.dataset.index]={
  106. // width:viewWidth,
  107. // height:viewHeight
  108. // }
  109. // this.setData({
  110. // images:image
  111. // })
  112. // },
  113. shareInfo () {
  114. var that = this
  115. var url = "v1/share/info"
  116. var params = {
  117. sc: 'xcx_product',
  118. ri: that.data.productId,
  119. rp: that.data.productId,
  120. to: 'group'
  121. }
  122. var success = function (val) {
  123. console.log(val)
  124. }
  125. _request.$post(url, params, success)
  126. },
  127. detailImg: function (event) {
  128. var src = event.currentTarget.dataset.src;
  129. var imgList = event.currentTarget.dataset.list;
  130. var imgLists = []
  131. for (var i = 0; i < imgList.length; i++) {
  132. imgLists.push(imgList[i].img)
  133. }
  134. wx.previewImage({
  135. current: src,
  136. urls: imgLists
  137. })
  138. },
  139. getProjectDetail () {
  140. var that = this
  141. var url = "v1/product/" + that.data.productId;
  142. var params = {}
  143. var success = function (val) {
  144. var p = val.data.detail
  145. that.setData({
  146. project: val.data,
  147. clock: val.data.seckill_end
  148. });
  149. WxParse.wxParse('parse', 'html', p, that, 10)
  150. that.projectImageLoading()
  151. if (val.data.seckill_state == 'seckill') {
  152. that.starttime()
  153. }
  154. if(val.data.have_size){
  155. that.getProductSize();
  156. }
  157. }
  158. _request.$get(url, params, success)
  159. },
  160. getProductSize(){
  161. var that = this
  162. var url = "v1/product_size/" + that.data.productId;
  163. var params = {}
  164. var success = function (val) {
  165. that.setData({
  166. productSize: val.data,
  167. });
  168. }
  169. _request.$get(url, params, success)
  170. },
  171. getProductDetail () {
  172. var that = this;
  173. console.log(that.data.size_list);
  174. console.log(that.data.color_list);
  175. var url = "v1/product_detail/" + that.data.productId + "/" + that.data.size_list + "/" + that.data.color_list;
  176. var params = {}
  177. var success = function (val) {
  178. console.log('val',val)
  179. that.setData({
  180. canpay:true,
  181. project:val.data,
  182. productId:val.data.id
  183. })
  184. }
  185. var fail = function(err){
  186. that.setData({
  187. canpay:false
  188. })
  189. }
  190. _request.$get(url, params, success, fail)
  191. },
  192. add: function () {
  193. if (this.data.count < this.data.project.left_count) {
  194. var count = this.data.count + 1
  195. this.setData({
  196. count: count
  197. })
  198. }
  199. },
  200. mius: function () {
  201. if (this.data.count > 1) {
  202. var count = this.data.count - 1
  203. this.setData({
  204. count: count
  205. })
  206. }
  207. },
  208. changeCount: function (val) {
  209. var count = val.detail.value
  210. if (count <= 0) {
  211. this.setData({
  212. count: 1
  213. })
  214. } else if (count > this.data.project.left_count) {
  215. this.setData({
  216. count: this.data.project.left_count
  217. })
  218. } else {
  219. this.setData({
  220. count: count
  221. })
  222. }
  223. },
  224. // 显示弹框
  225. showModal: function (type) {
  226. var animation = wx.createAnimation({
  227. duration: 200,
  228. timingFunction: "ease-out"
  229. })
  230. this.animation = animation
  231. animation.translateY(500).step()
  232. if(type == 1){
  233. this.setData({
  234. animationData: animation.export(),
  235. showDialog: true
  236. })
  237. }else{
  238. this.setData({
  239. animationData: animation.export(),
  240. showCartDialog: true
  241. })
  242. }
  243. setTimeout(function () {
  244. animation.translateY(0).step()
  245. this.setData({
  246. animationData: animation.export()
  247. })
  248. }.bind(this), 200)
  249. },
  250. // 隐藏弹框
  251. hideModal: function (type = 1) {
  252. var animation = wx.createAnimation({
  253. duration: 200,
  254. timingFunction: "ease-out"
  255. })
  256. this.animation = animation
  257. animation.translateY(500).step()
  258. this.setData({
  259. animationData: animation.export(),
  260. })
  261. setTimeout(function () {
  262. animation.translateY(0).step()
  263. if(type == 1){
  264. this.setData({
  265. animationData: animation.export(),
  266. showDialog: false
  267. })
  268. }else{
  269. this.setData({
  270. animationData: animation.export(),
  271. showCartDialog: false
  272. })
  273. }
  274. }.bind(this), 200)
  275. },
  276. getneedshare () {
  277. var that = this
  278. var url = "v1/product/" + that.data.productId + "/share"
  279. var params = {
  280. }
  281. var success = function (val) {
  282. if (val.data.need_share == 1) {
  283. that.setData({
  284. showpay: false
  285. })
  286. }
  287. }
  288. _request.$get(url, params, success)
  289. },
  290. toPay: function (e) {
  291. _handle.setFormId(e)
  292. if (this.data.bindTel) {
  293. var that = this
  294. that.data.showDialog = !that.data.showDialog
  295. if (that.data.showDialog) {
  296. that.showModal(1)
  297. } else {
  298. that.hideModal(1)
  299. }
  300. } else {
  301. wx.navigateTo({
  302. url: '/packageUser/pages/user/bind/bind'
  303. })
  304. }
  305. },
  306. toCart: function (e) {
  307. _handle.setFormId(e)
  308. if (this.data.bindTel) {
  309. var that = this
  310. that.data.showCartDialog = !that.data.showCartDialog
  311. if (that.data.showCartDialog) {
  312. that.showModal(2)
  313. } else {
  314. that.hideModal(2)
  315. }
  316. } else {
  317. wx.navigateTo({
  318. url: '/packageUser/pages/user/bind/bind'
  319. })
  320. }
  321. },
  322. pay: function (e) {
  323. _handle.setFormId(e)
  324. var that = this
  325. var url = "v1/product/" + that.data.productId + "/order/" + that.data.count
  326. var params = {}
  327. var success = function (res) {
  328. if (res.data.order_id) {
  329. wx.navigateTo({
  330. url: '../../pay/pay?orderId=' + res.data.order_id
  331. })
  332. that.hideModal()
  333. that.setData({
  334. count: 1
  335. })
  336. }
  337. }
  338. if (that.data.count <= that.data.project.left_count) {
  339. _request.$post(url, params, success)
  340. } else {
  341. wx.showToast({
  342. title: '库存不足~',
  343. icon: 'none',
  344. duration: 2000
  345. })
  346. }
  347. },
  348. addCart:function(){
  349. var that = this;
  350. console.log('that.data.productId',that.data.productId)
  351. var url = "v1/cart/" + that.data.productId + "/" + that.data.count
  352. var params = {}
  353. var success = function (res) {
  354. if(res.data.result){
  355. that.hideModal(2);
  356. wx.showToast({
  357. title: '添加成功,请到购物车查看~',
  358. icon: 'none',
  359. duration: 2000
  360. })
  361. }
  362. // if (res.data.order_id) {
  363. // wx.navigateTo({
  364. // url: '../../pay/pay?orderId=' + res.data.order_id
  365. // })
  366. // that.hideModal()
  367. // that.setData({
  368. // count: 1
  369. // })
  370. // }
  371. }
  372. if (that.data.count <= that.data.project.left_count) {
  373. _request.$post(url, params, success)
  374. } else {
  375. wx.showToast({
  376. title: '库存不足~',
  377. icon: 'none',
  378. duration: 2000
  379. })
  380. }
  381. },
  382. getIphoneX () {
  383. var that = this
  384. wx.getSystemInfo({
  385. success: function (res) {
  386. if (res.model.toLowerCase().indexOf('iphone x') > -1) {
  387. that.setData({
  388. value: 34
  389. })
  390. }
  391. }
  392. })
  393. },
  394. getBalanceInfo () {
  395. var that = this
  396. var url = 'v1/user/balance/info'
  397. var params = {
  398. }
  399. var success = function (res) {
  400. var result = res.data.total
  401. that.setData({
  402. balance: result
  403. })
  404. }
  405. _request.$get(url, params, success)
  406. },
  407. projectImageLoading () {
  408. var that = this
  409. wx.getImageInfo({
  410. src: 'https://cfohow.hiwavo.com/xcx/poster6.jpg',
  411. success: function (res) {
  412. that.posterImage.bg = res
  413. that.posterLoading++
  414. if (that.posterLoading === 4) {
  415. that.createCtx()
  416. }
  417. }
  418. })
  419. if (this.data.project.pics.length > 0) {
  420. wx.getImageInfo({
  421. src: this.data.project.pics[0].img,
  422. success: function (res) {
  423. that.posterImage.project = res
  424. that.posterLoading++
  425. if (that.posterLoading === 4) {
  426. that.createCtx()
  427. }
  428. }
  429. })
  430. }
  431. var url = 'v1/poster/' + this.data.productId
  432. var params = {
  433. type: 'project'
  434. }
  435. var success = function (res) {
  436. wx.getImageInfo({
  437. src: res.data.qrcode_url,
  438. success: function (res) {
  439. that.posterImage.qcode = res
  440. that.posterLoading++
  441. if (that.posterLoading === 4) {
  442. that.createCtx()
  443. }
  444. },
  445. fail: function (res) {
  446. that.qcodeError = true
  447. if (that.data.posterPopup) {
  448. that.setData({
  449. posterPopup: false
  450. })
  451. wx.showToast({
  452. title: '海报生成失败,请稍后重试',
  453. icon: 'none',
  454. duration: 2000
  455. })
  456. wx.hideLoading()
  457. }
  458. }
  459. })
  460. wx.getImageInfo({
  461. src: res.data.wx_head,
  462. success: function (res) {
  463. that.posterImage.head = res
  464. that.posterLoading++
  465. if (that.posterLoading === 4) {
  466. that.createCtx()
  467. }
  468. }
  469. })
  470. }
  471. _request.$get(url, params, success)
  472. },
  473. createPoster () {
  474. if (!this.qcodeError) {
  475. if (!this.canvasLoading) {
  476. wx.showLoading({
  477. mask: true,
  478. title: ''
  479. })
  480. }
  481. this.setData({
  482. posterPopup: true
  483. })
  484. } else {
  485. wx.showToast({
  486. title: '海报生成失败,请稍后重试',
  487. icon: 'none',
  488. duration: 2000
  489. })
  490. }
  491. },
  492. posterClose () {
  493. this.setData({
  494. posterPopup: false
  495. })
  496. },
  497. createCtx () {
  498. var that = this
  499. ctx = wx.createCanvasContext('myCanvas')
  500. ctx.drawImage(this.posterImage.bg.path, 0, 0, this.dpi(this.posterImage.bg.width), this.dpi(this.posterImage.bg.height))
  501. ctx.drawImage(this.posterImage.qcode.path, this.dpi(501), this.dpi(1140), this.dpi(180), this.dpi(180))
  502. ctx.save()
  503. ctx.translate(this.dpi(44),this.dpi(46))
  504. ctx.save()
  505. ctx.arc(this.dpi(60), this.dpi(59), this.dpi(40), 0, Math.PI * 2, false)
  506. ctx.lineWidth = 1
  507. ctx.strokeStyle = '#d4d4d4'
  508. ctx.stroke()
  509. ctx.clip()
  510. ctx.drawImage(this.posterImage.head.path, this.dpi(21), this.dpi(20), this.dpi(78), this.dpi(78))
  511. ctx.restore()
  512. ctx.save()
  513. ctx.textBaseline = 'middle'
  514. ctx.font = this.dpi(24) + 'px Arial'
  515. ctx.fillStyle = '#eab86a'
  516. ctx.fillText('源处全球源产地的优质产品!', this.dpi(136), this.dpi(59))
  517. ctx.restore()
  518. ctx.drawImage(this.posterImage.project.path, this.dpi(20), this.dpi(140), this.dpi(622), this.dpi(622))
  519. ctx.beginPath()
  520. ctx.moveTo(this.dpi(0), this.dpi(119))
  521. ctx.lineTo(this.dpi(662), this.dpi(119))
  522. ctx.lineWidth = 1
  523. ctx.strokeStyle = '#eab86a'
  524. ctx.stroke()
  525. ctx.closePath()
  526. ctx.save()
  527. ctx.translate(this.dpi(20),this.dpi(784))
  528. ctx.textBaseline = 'middle'
  529. ctx.font = this.dpi(28) + 'px Arial'
  530. ctx.fillStyle = '#bbbbbb'
  531. ctx.fillText('购买单价', this.dpi(46), this.dpi(50))
  532. ctx.fillStyle = '#eab86a'
  533. ctx.setTextAlign('right')
  534. ctx.font = 'bold ' + this.dpi(54) + 'px Arial'
  535. ctx.fillText(util.formatMoney(this.data.project.price / 100, '¥', 2), this.dpi(584), this.dpi(50))
  536. ctx.setTextAlign('left')
  537. ctx.font = this.dpi(32) + 'px Arial'
  538. ctx.fillStyle = '#2b2b2b'
  539. this.textAutoLine(this.data.project.name, this.dpi(30), this.dpi(150), this.dpi(40))
  540. ctx.restore()
  541. ctx.restore()
  542. ctx.draw(true, function callback () {
  543. that.createImage()
  544. })
  545. },
  546. createImage () {
  547. var that = this
  548. wx.canvasToTempFilePath({
  549. destWidth: 750,
  550. destHeight: 1334,
  551. canvasId: 'myCanvas',
  552. fileType: 'jpg',
  553. success: function (res) {
  554. that.setData({
  555. posterImage: res.tempFilePath
  556. })
  557. that.canvasLoading = true
  558. wx.hideLoading()
  559. }
  560. })
  561. },
  562. saveImage () {
  563. var that = this
  564. wx.showLoading({
  565. mask: true,
  566. title: ''
  567. })
  568. wx.saveImageToPhotosAlbum({
  569. filePath: this.data.posterImage,
  570. success (res) {
  571. wx.showToast({
  572. title: '已保存相册',
  573. icon: 'success',
  574. duration: 2000
  575. })
  576. },
  577. fail (res) {
  578. wx.getSetting({
  579. success (res) {
  580. if (!res.authSetting['scope.writePhotosAlbum']) {
  581. that.setData({
  582. popup: true
  583. })
  584. }
  585. }
  586. })
  587. wx.hideLoading()
  588. }
  589. })
  590. },
  591. setting () {
  592. this.setData({
  593. popup: false
  594. })
  595. },
  596. roundRect (x, y, w, h, r, color) {
  597. ctx.beginPath()
  598. ctx.moveTo(x+r, y)
  599. ctx.arcTo(x+w, y, x+w, y+h, r)
  600. ctx.arcTo(x+w, y+h, x, y+h, r)
  601. ctx.arcTo(x, y+h, x, y, r)
  602. ctx.arcTo(x, y, x+w, y, r)
  603. ctx.closePath()
  604. ctx.fillStyle = color
  605. ctx.fill()
  606. },
  607. textAutoLine (str, initX, initY, lineHeight) {
  608. let lineWidth = 0
  609. let textWidth = this.dpi(580)
  610. let lastSubStrIndex= 0
  611. for (let i = 0; i < str.length; i++) {
  612. lineWidth += ctx.measureText(str[i]).width
  613. if (lineWidth > textWidth - initX) {
  614. ctx.fillText(str.substring(lastSubStrIndex, i), initX, initY)
  615. initY += lineHeight
  616. lineWidth = 0
  617. lastSubStrIndex = i
  618. }
  619. if (i === str.length - 1) {
  620. ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY)
  621. }
  622. }
  623. },
  624. stopPageScroll () {
  625. return
  626. },
  627. starttime () {
  628. var that = this
  629. that.setData({
  630. timer: setInterval(function () {
  631. // var date = new Date();
  632. // var now = date.getTime();
  633. // var endDate = new Date("2019-01-03 23:14:53");//设置截止时间
  634. // var end = that.data.clock * 1000;//设置截止时间
  635. // var end = endDate.getTime();
  636. // var leftTime = end - now; //时间差
  637. var t = Date.parse(new Date())
  638. t = t / 1000;
  639. var leftTime = that.data.clock - t
  640. var d, h, m, s, ms;
  641. if (leftTime >= 0) {
  642. // day = parseInt(ctime / 86400)
  643. h = parseInt(leftTime / 3600)
  644. m = parseInt(leftTime / 60 ) - parseInt(leftTime / 3600) * 60
  645. s = parseInt(leftTime) - parseInt(leftTime / 60 ) * 60
  646. // d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
  647. // h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
  648. // m = Math.floor(leftTime / 1000 / 60 % 60);
  649. // s = Math.floor(leftTime / 1000 % 60);
  650. // ms = Math.floor(leftTime % 1000);
  651. // ms = ms < 100 ? "0" + ms : ms
  652. s = s < 10 ? "0" + s : s
  653. m = m < 10 ? "0" + m : m
  654. h = h < 10 ? "0" + h : h
  655. that.setData({
  656. hour: h,
  657. min: m,
  658. second: s,
  659. // msecond: ms,
  660. leftTime: leftTime
  661. })
  662. }
  663. if (leftTime < 0) {
  664. clearInterval(that.data.timer);
  665. that.setData({
  666. leftTime: 0,
  667. hour: '00',
  668. min: '00',
  669. second: '00',
  670. // msecond: '00'
  671. })
  672. }
  673. },100),
  674. });
  675. },
  676. checkSize(event){
  677. var type = event.currentTarget.dataset.type;
  678. var id = event.currentTarget.dataset.id;
  679. var list = event.currentTarget.dataset.list;
  680. var that = this;
  681. var reproduct = this.data.productSize;
  682. // console.log('reproduct',reproduct)
  683. for(var i in reproduct[type]['attr_values']){
  684. // console.log('reproduct[type][i].is_select',reproduct[type]['attr_values'][i].is_select)
  685. reproduct[type]['attr_values'][i].is_select = false;
  686. if(reproduct[type]['attr_values'][i].id == id) reproduct[type]['attr_values'][i].is_select = true;
  687. }
  688. // console.log('reproduct',reproduct)
  689. that.setData({
  690. productSize :reproduct,
  691. [type]:id
  692. })
  693. // console.log('that.data[type]',that.data[type])
  694. that.getProductDetail();
  695. }
  696. })