project-detail.js 18 KB

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