project-detail.js 16 KB

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