project-detail.js 19 KB

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