helpfarm-detail.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. var width = 0
  2. var height = 0
  3. var ctx
  4. var buttonClicked = true
  5. var _request = require('../../../utils/request.js')
  6. var _handle = require('../../../utils/handle.js')
  7. var WxParse = require('../../../utils/wxParse/wxParse.js')
  8. var util = require('../../../utils/accounting.min.js')
  9. var fomldsJSON = ''
  10. Page({
  11. posterImage: {},
  12. posterLoading: 0,
  13. canvasLoading: false,
  14. qcodeError: false,
  15. data: {
  16. indicatorDots: false,
  17. autoplay: false,
  18. interval: 5000,
  19. duration: 1000,
  20. circular: false,
  21. imgUrls: [
  22. // 'http://fohow-test.oss-cn-shenzhen.aliyuncs.com/ad_item/8b472c80.jpg',
  23. // 'http://fohow-test.oss-cn-shenzhen.aliyuncs.com/ad_item/7ab455d0.jpg',
  24. // 'http://fohow-test.oss-cn-shenzhen.aliyuncs.com/ad_item/70eabcc0.jpg'
  25. ],
  26. imgindex: 1,
  27. currentTab: 0,
  28. project: null,
  29. percent: '0',
  30. step: '0',
  31. personList: [],
  32. page: 1,
  33. per_page: 10,
  34. InvestWayList: {},
  35. day: '00',
  36. hour: '00',
  37. min: '00',
  38. second: '00',
  39. timer: '',
  40. ctime: '0',
  41. btn: true,
  42. animationData: {},
  43. showDialog: false,
  44. showModalDialog: false,
  45. bindTel: false,
  46. itemindex: 0,
  47. InvestWayList1:[],
  48. changenum: 0,
  49. count: 1,
  50. count1: '',
  51. // hidden: false,
  52. title: '',
  53. ctime: '',
  54. progress: 0,
  55. progress1: 0,
  56. bgcolor: '#eee',
  57. cashTotal: 0,
  58. posterPopup: false,
  59. productOid: '',
  60. posterImage: null,
  61. popup: false,
  62. Inid: ''
  63. },
  64. onLoad: function (options) {
  65. this.getIphoneX()
  66. var info = wx.getSystemInfoSync()
  67. width = info.windowWidth
  68. height = info.windowHeight
  69. this.data.projectId = options.id
  70. this.getProjectDetail()
  71. this.getPersonList()
  72. this.getInvestWayList()
  73. // this.check()
  74. this.showprogress()
  75. this.getCashTotal()
  76. },
  77. onShow: function () {
  78. },
  79. onReady: function () {
  80. },
  81. dpi: function (val) {
  82. return val
  83. },
  84. onSlideChangeEnd: function(e) {
  85. var that = this;
  86. that.setData({
  87. imgindex: e.detail.current + 1
  88. })
  89. },
  90. check () {
  91. var that = this
  92. var url = 'v1/user/check'
  93. var params = {
  94. }
  95. var success = function (res) {
  96. if (res.data.user_id > 0) {
  97. that.setData({
  98. bindTel: true
  99. })
  100. }
  101. }
  102. _request.$get(url, params, success)
  103. },
  104. drawProgressbg () {
  105. // 使用 wx.createContext 获取绘图上下文 context
  106. var ctx = wx.createCanvasContext('canvasProgressbg')
  107. ctx.setLineWidth(this.dpi(10));// 设置圆环的宽度
  108. ctx.setStrokeStyle('#eeeeee'); // 设置圆环的颜色
  109. ctx.setLineCap('butt') // 设置圆环端点的形状
  110. ctx.beginPath();//开始一个新的路径
  111. ctx.arc(this.dpi(55), this.dpi(38), this.dpi(28), this.dpi(0), 2 * Math.PI, false);
  112. //设置一个原点(100,100),半径为90的圆的路径到当前路径
  113. ctx.stroke();//对当前路径进行描边
  114. ctx.draw();
  115. },
  116. drawCircle: function (step){
  117. var context = wx.createCanvasContext('canvasProgress');
  118. // 设置渐变
  119. var gradient = context.createLinearGradient(this.dpi(200), this.dpi(100), this.dpi(100), this.dpi(100));
  120. gradient.addColorStop("0", "#ffc06c");
  121. gradient.addColorStop("0.5", "#ff9e61");
  122. gradient.addColorStop("1.0", "#ff7655");
  123. context.setLineWidth(this.dpi(10));
  124. context.setStrokeStyle(gradient);
  125. context.setLineCap('butt')
  126. context.beginPath();
  127. // 参数step 为绘制的圆环周长,从0到2为一周 。 -Math.PI / 2 将起始角设在12点钟位置 ,结束角 通过改变 step 的值确定
  128. context.arc(this.dpi(55), this.dpi(38), this.dpi(28), -Math.PI / 2, step * Math.PI - Math.PI / 2, false);
  129. context.stroke();
  130. context.draw()
  131. },
  132. swichNav:function(e){
  133. var cur=e.target.dataset.current;
  134. if (this.data.currentTaB==cur) {
  135. return false;
  136. }
  137. else {
  138. this.setData({
  139. currentTab:cur
  140. })
  141. }
  142. },
  143. getProjectDetail () {
  144. var that = this
  145. var url = "v1/project/" + that.data.projectId
  146. var params = {}
  147. var success = function (val) {
  148. var p = val.data.detail
  149. var f = val.data.finance_plan
  150. var countArr = []
  151. var percent = val.data.current_funding / val.data.min_funding * 100
  152. var percent1 = (percent).toFixed(2)
  153. var step = val.data.current_funding / val.data.min_funding * 2
  154. for (var index in val.data.pics) {
  155. var imgs = val.data.pics[index].img
  156. countArr.push(imgs)
  157. }
  158. that.setData({
  159. timer: setInterval(function () {
  160. var t = Date.parse(new Date())
  161. t = t / 1000;
  162. var ctime = val.data.stop_time - t
  163. var day = parseInt(ctime / 86400)
  164. var hour = parseInt(ctime / 3600) - day *24
  165. var min = parseInt(ctime / 60 ) - parseInt(ctime / 3600) * 60
  166. var second = parseInt(ctime) - parseInt(ctime / 60 ) * 60
  167. that.setData({
  168. day: day,
  169. hour: hour,
  170. min: min,
  171. second: second,
  172. ctime: ctime
  173. })
  174. if (ctime <= 0) {
  175. clearInterval(that.data.timer);
  176. that.setData({
  177. ctime: 0
  178. })
  179. }
  180. },100),
  181. project: val.data,
  182. imgUrls: countArr,
  183. percent: percent1,
  184. step: step,
  185. });
  186. that.showprogress()
  187. WxParse.wxParse('parse', 'html', p, that, 10)
  188. // WxParse.wxParse('parse1', 'html', f, that, 10)
  189. that.projectImageLoading()
  190. // that.drawProgressbg();
  191. // that.drawCircle(step)
  192. }
  193. _request.$get(url, params, success)
  194. },
  195. getPersonList() {
  196. var that = this
  197. var url = 'v1/project/' + that.data.projectId + '/invests'
  198. var params = {
  199. page: that.data.page,
  200. per_page: that.data.per_page
  201. }
  202. var success = function (res) {
  203. if (res.data.investor_list) {
  204. var newPersonList = that.data.personList.concat(res.data.investor_list)
  205. that.setData({
  206. personList : newPersonList,
  207. page: that.data.page + 1,
  208. per_page: that.data.per_page
  209. })
  210. } else {
  211. that.setData({
  212. btn: false
  213. })
  214. }
  215. }
  216. _request.$get(url, params, success)
  217. },
  218. getInvestWayList() {
  219. var that = this
  220. var url = 'v1/project/' + that.data.projectId + '/invest_ways'
  221. var params = {
  222. }
  223. var success = function (res) {
  224. if (res.data.invest_way.length > 0) {
  225. var left = res.data.invest_way[that.data.itemindex].left_copies
  226. var min = res.data.invest_way[that.data.itemindex].min_limit
  227. var owned = res.data.invest_way[that.data.itemindex].owned_copies
  228. var gap = res.data.invest_way[that.data.itemindex].gap_limit
  229. var Inid = res.data.invest_way[that.data.itemindex].id
  230. if (left > min || left == min) {
  231. if (owned > min || owned == min) {
  232. if (gap > left) {
  233. that.setData({
  234. count: left
  235. })
  236. } else {
  237. that.setData({
  238. count: gap
  239. })
  240. }
  241. } else {
  242. that.setData({
  243. count: min
  244. })
  245. }
  246. } else {
  247. if (owned > min || owned == min) {
  248. if (gap > left) {
  249. that.setData({
  250. count: left
  251. })
  252. } else {
  253. that.setData({
  254. count: gap
  255. })
  256. }
  257. } else {
  258. that.setData({
  259. count: left
  260. })
  261. }
  262. }
  263. that.setData({
  264. InvestWayList : res.data,
  265. InvestWayList1 : res.data.invest_way,
  266. // title: res.data.invest_way[that.data.itemindex].title,
  267. price: res.data.invest_way[that.data.itemindex].price,
  268. count1: min,
  269. Inid: Inid
  270. })
  271. var itemindex = that.data.itemindex
  272. var g = that.data.InvestWayList1[itemindex].desc
  273. WxParse.wxParse('parse2', 'html', g, that, 10)
  274. }
  275. }
  276. _request.$get(url, params, success)
  277. },
  278. // 显示弹框
  279. showModal: function () {
  280. var animation = wx.createAnimation({
  281. duration: 300,
  282. timingFunction: "ease-out"
  283. })
  284. this.animation = animation
  285. animation.translateY(500).step()
  286. this.setData({
  287. animationData: animation.export(),
  288. showDialog: true
  289. })
  290. setTimeout(function () {
  291. animation.translateY(0).step()
  292. this.setData({
  293. animationData: animation.export()
  294. })
  295. }.bind(this), 300)
  296. },
  297. // 隐藏弹框
  298. hideModal: function () {
  299. var animation = wx.createAnimation({
  300. duration: 300,
  301. timingFunction: "ease-out"
  302. })
  303. this.animation = animation
  304. animation.translateY(500).step()
  305. this.setData({
  306. animationData: animation.export(),
  307. })
  308. setTimeout(function () {
  309. animation.translateY(0).step()
  310. this.setData({
  311. animationData: animation.export(),
  312. showDialog: false
  313. })
  314. }.bind(this), 300)
  315. },
  316. toPay: function (e) {
  317. _handle.setFormId(e)
  318. if (this.data.bindTel) {
  319. var that = this
  320. // that.buttonClicked()
  321. that.data.showDialog = !that.data.showDialog
  322. if (that.data.showDialog) {
  323. that.showModal()
  324. } else {
  325. that.hideModal()
  326. }
  327. } else {
  328. wx.navigateTo({
  329. url: '/packageUser/pages/user/bind/bind'
  330. })
  331. }
  332. },
  333. closePay: function () {
  334. this.hideModal()
  335. },
  336. changeCount: function(e) {
  337. var that = this
  338. var itemindex = e.target.dataset.item;
  339. var left = that.data.InvestWayList1[itemindex].left_copies
  340. var min = that.data.InvestWayList1[itemindex].min_limit
  341. var owned = that.data.InvestWayList1[itemindex].owned_copies
  342. var gap = that.data.InvestWayList1[itemindex].gap_limit
  343. var Inid = that.data.InvestWayList1[itemindex].id
  344. if (left > min || left == min) {
  345. if (owned > min || owned == min) {
  346. if (gap > left) {
  347. that.setData({
  348. count: left
  349. })
  350. } else {
  351. that.setData({
  352. count: gap
  353. })
  354. }
  355. } else {
  356. that.setData({
  357. count: min
  358. })
  359. }
  360. } else {
  361. if (owned > min || owned == min) {
  362. if (gap > left) {
  363. that.setData({
  364. count: left
  365. })
  366. } else {
  367. that.setData({
  368. count: gap
  369. })
  370. }
  371. } else {
  372. that.setData({
  373. count: left
  374. })
  375. }
  376. }
  377. that.setData({
  378. itemindex: itemindex,
  379. changenum: itemindex,
  380. // title: that.data.InvestWayList1[itemindex].title,
  381. price: that.data.InvestWayList1[itemindex].price,
  382. count1: min,
  383. Inid: Inid
  384. })
  385. var g = that.data.InvestWayList1[itemindex].desc
  386. WxParse.wxParse('parse2', 'html', g, that, 10)
  387. },
  388. add: function () {
  389. var left = this.data.InvestWayList1[this.data.itemindex].left_copies
  390. var min = this.data.InvestWayList1[this.data.itemindex].min_limit
  391. var owned = this.data.InvestWayList1[this.data.itemindex].owned_copies
  392. var gap = this.data.InvestWayList1[this.data.itemindex].gap_limit
  393. var max = this.data.InvestWayList1[this.data.itemindex].max_limit
  394. var count = this.data.count
  395. if (left > min) {
  396. if (owned > min || owned == min) {
  397. if (gap > left) {
  398. this.setData({
  399. count: left
  400. })
  401. } else {
  402. this.setData({
  403. count: count + gap
  404. })
  405. }
  406. } else {
  407. if (gap > left) {
  408. this.setData({
  409. count: left
  410. })
  411. } else {
  412. this.setData({
  413. count: count + gap
  414. })
  415. }
  416. }
  417. } else {
  418. this.setData({
  419. count: left
  420. })
  421. }
  422. },
  423. mius: function () {
  424. var left = this.data.InvestWayList1[this.data.itemindex].left_copies
  425. var min = this.data.InvestWayList1[this.data.itemindex].min_limit
  426. var owned = this.data.InvestWayList1[this.data.itemindex].owned_copies
  427. var gap = this.data.InvestWayList1[this.data.itemindex].gap_limit
  428. var count = this.data.count
  429. if (left > min) {
  430. if (owned > min || owned == min) {
  431. if (gap > left) {
  432. this.setData({
  433. count: left
  434. })
  435. } else {
  436. if (count - gap < 0 || count - gap == 0) {
  437. this.setData({
  438. count: count
  439. })
  440. } else {
  441. this.setData({
  442. count: count - gap
  443. })
  444. }
  445. }
  446. } else {
  447. if (gap > left) {
  448. this.setData({
  449. count: left
  450. })
  451. } else {
  452. if (count - gap < 0 || count - gap == 0) {
  453. this.setData({
  454. count: count
  455. })
  456. } else {
  457. this.setData({
  458. count: count - gap
  459. })
  460. }
  461. }
  462. }
  463. } else {
  464. this.setData({
  465. count: left
  466. })
  467. }
  468. },
  469. changeSum: function(val) {
  470. var count = val.detail.value
  471. if (count <= 0 ) {
  472. this.setData({
  473. count: 1
  474. })
  475. } else {
  476. this.setData({
  477. count: count
  478. })
  479. }
  480. },
  481. pay: function(e) {
  482. _handle.setFormId(e)
  483. if (buttonClicked) {
  484. buttonClicked = false
  485. var that = this
  486. var url = "v1/project/" + that.data.projectId + "/invest/" + that.data.count
  487. var params = {
  488. invest_way_id: that.data.InvestWayList1[that.data.itemindex].id
  489. }
  490. var success = function (res) {
  491. that.data.productOid = res.data.order_id
  492. that.topay1(res)
  493. buttonClicked = true
  494. }
  495. var fail = function () {
  496. buttonClicked = true
  497. }
  498. _request.$post(url, params, success, fail)
  499. }
  500. },
  501. topay1 () {
  502. var that = this
  503. var price = that.data.count * that.data.InvestWayList1[that.data.itemindex].price
  504. if (that.data.cashTotal >= price) {
  505. // wx.showModal({
  506. // title: '提示',
  507. // content: '是否使用余额支付?',
  508. // confirmText: '是',
  509. // cancelText: '微信支付',
  510. // success: function(res) {
  511. // if (res.confirm) {
  512. // that.toBalancePay(value.data.order_id)
  513. // } else if (res.cancel) {
  514. // that.toWeixinPay(value.data.order_id)
  515. // }
  516. // }
  517. // })
  518. that.setData({
  519. showModalDialog: true
  520. })
  521. } else {
  522. that.toWeixinPay()
  523. }
  524. },
  525. closeModalDialog () {
  526. var that = this
  527. that.data.showModalDialog = false
  528. that.setData({
  529. showModalDialog: false
  530. })
  531. },
  532. toWeixinPay () {
  533. var that = this
  534. var url = 'v1/pay'
  535. var pay_way = 'weixinpay'
  536. var params = {
  537. order_id: that.data.productOid,
  538. pay_way: pay_way
  539. }
  540. var success = function (val) {
  541. wx.requestPayment({
  542. 'timeStamp': val.data.pay_data.timeStamp,
  543. 'nonceStr': val.data.pay_data.nonceStr,
  544. 'package': val.data.pay_data.package,
  545. 'signType': val.data.pay_data.signType,
  546. 'paySign': val.data.pay_data.paySign,
  547. 'success':function(res){
  548. wx.showToast({
  549. title: '支付成功',
  550. icon: 'success',
  551. duration: 1000
  552. })
  553. that.closeModalDialog()
  554. // wx.navigateTo({
  555. // url: that.data.path + '?orderId=' + that.data.orderId
  556. // url: '/packageUser/pages/user/farm/farm'
  557. // })
  558. wx.redirectTo({
  559. url: '/pages/helpfarm/helpfarm-success/helpfarm-success?id=' + that.data.Inid
  560. })
  561. },
  562. 'fail':function(res){
  563. },
  564. 'complete': function () {
  565. buttonClicked = true
  566. }
  567. })
  568. }
  569. _request.$post(url, params, success)
  570. },
  571. toBalancePay () {
  572. var that = this
  573. var url = 'v1/pay'
  574. var pay_way = 'cashbalance'
  575. var params = {
  576. order_id: that.data.productOid,
  577. pay_way: pay_way
  578. }
  579. var success = function (val) {
  580. that.closeModalDialog()
  581. buttonClicked = true
  582. wx.showToast({
  583. title: '支付成功',
  584. icon: 'success',
  585. duration: 1000
  586. })
  587. if (val) {
  588. wx.redirectTo({
  589. // url: that.data.path + '?orderId=' + that.data.orderId
  590. // url: '/packageUser/pages/user/farm/farm'
  591. url: '/pages/helpfarm/helpfarm-success/helpfarm-success?id=' + that.data.Inid
  592. })
  593. }
  594. }
  595. _request.$post(url, params, success)
  596. },
  597. // buttonClicked () {
  598. // var that = this
  599. // that.setData({
  600. // buttonClicked: true
  601. // })
  602. // setTimeout(function () {
  603. // that.setData({
  604. // buttonClicked: false
  605. // })
  606. // }, 500)
  607. // },
  608. onShareAppMessage: function (val) {
  609. return _request.share({
  610. title: this.data.project.share_title,
  611. path: '/pages/start/start?url=pages/helpfarm/helpfarm-detail/helpfarm-detail&id=' + this.data.projectId,
  612. imageUrl: '',
  613. sc: 'xcx_helpfarmproject',
  614. ri: this.data.projectId,
  615. rp: this.data.projectId
  616. })
  617. },
  618. showprogress() {
  619. var that = this
  620. var percent = that.data.percent
  621. if (percent <= 50) {
  622. that.setData({
  623. progress: percent * 3.6
  624. })
  625. } else if (percent > 100) {
  626. percent = 100
  627. that.setData({
  628. bgcolor: '#ff7655',
  629. progress: 0,
  630. progress1: (percent-50)*3.6
  631. })
  632. } else {
  633. that.setData({
  634. bgcolor: '#ff7655',
  635. progress: 0,
  636. progress1: (percent-50)*3.6
  637. })
  638. }
  639. },
  640. toCount () {
  641. let price = this.data.count * this.data.InvestWayList.invest_way[this.data.itemindex].price
  642. let count = this.data.count
  643. let sell = this.data.InvestWayList.invest_way[this.data.itemindex].product.user_sale_price
  644. let min = this.data.InvestWayList.invest_way[this.data.itemindex].self_use_min
  645. let retrieve = this.data.InvestWayList.invest_way[this.data.itemindex].harvest_rate / 100
  646. let limit = this.data.InvestWayList.invest_way[this.data.itemindex].gap_limit
  647. let remax = this.data.InvestWayList.invest_way[this.data.itemindex].buy_back_max_limit
  648. let reprice = this.data.InvestWayList.invest_way[this.data.itemindex].buy_back_price
  649. let minlimit = this.data.InvestWayList.invest_way[this.data.itemindex].min_limit
  650. wx.navigateTo({
  651. url: '/pages/helpfarm/count/count?price=' + price + '&count=' + count + '&sell=' + sell + '&min=' + min + '&retrieve=' + retrieve + '&limit=' + limit + '&remax=' + remax + '&minlimit=' + minlimit + '&reprice=' + reprice
  652. })
  653. },
  654. getIphoneX () {
  655. var that = this
  656. wx.getSystemInfo({
  657. success: function (res) {
  658. if (res.model.toLowerCase().indexOf('iphone x') > -1) {
  659. that.setData({
  660. value: 34
  661. })
  662. }
  663. }
  664. })
  665. },
  666. getCashTotal () {
  667. var that = this
  668. var url = 'v1/user/cash/balance/info'
  669. var params = {
  670. }
  671. var success = function (res) {
  672. that.data.cashTotal = res.data.available
  673. that.setData({
  674. cashTotal: res.data.available
  675. })
  676. }
  677. _request.$get(url, params, success)
  678. },
  679. projectImageLoading () {
  680. var that = this
  681. wx.getImageInfo({
  682. src: 'https://cfohow.hiwavo.com/xcx/poster5.jpg',
  683. success: function (res) {
  684. that.posterImage.bg = res
  685. that.posterLoading++
  686. if (that.posterLoading === 4) {
  687. that.createCtx()
  688. }
  689. }
  690. })
  691. if (this.data.imgUrls.length > 0) {
  692. wx.getImageInfo({
  693. src: this.data.imgUrls[0],
  694. success: function (res) {
  695. that.posterImage.project = res
  696. that.posterLoading++
  697. if (that.posterLoading === 4) {
  698. that.createCtx()
  699. }
  700. }
  701. })
  702. }
  703. var url = 'v1/poster/' + this.data.projectId
  704. var params = {
  705. type: 'poster'
  706. }
  707. var success = function (res) {
  708. wx.getImageInfo({
  709. src: res.data.qrcode_url,
  710. success: function (res) {
  711. that.posterImage.qcode = res
  712. that.posterLoading++
  713. if (that.posterLoading === 4) {
  714. that.createCtx()
  715. }
  716. },
  717. fail: function (res) {
  718. that.qcodeError = true
  719. if (that.data.posterPopup) {
  720. that.setData({
  721. posterPopup: false
  722. })
  723. wx.showToast({
  724. title: '海报生成失败,请稍后重试',
  725. icon: 'none',
  726. duration: 2000
  727. })
  728. wx.hideLoading()
  729. }
  730. }
  731. })
  732. wx.getImageInfo({
  733. src: res.data.wx_head,
  734. success: function (res) {
  735. that.posterImage.head = res
  736. that.posterLoading++
  737. if (that.posterLoading === 4) {
  738. that.createCtx()
  739. }
  740. }
  741. })
  742. }
  743. _request.$get(url, params, success)
  744. },
  745. createPoster () {
  746. if (!this.qcodeError) {
  747. if (!this.canvasLoading) {
  748. wx.showLoading({
  749. mask: true,
  750. title: ''
  751. })
  752. }
  753. this.setData({
  754. posterPopup: true
  755. })
  756. } else {
  757. wx.showToast({
  758. title: '海报生成失败,请稍后重试',
  759. icon: 'none',
  760. duration: 2000
  761. })
  762. }
  763. },
  764. posterClose () {
  765. this.setData({
  766. posterPopup: false
  767. })
  768. },
  769. createCtx () {
  770. var that = this
  771. ctx = wx.createCanvasContext('myCanvas')
  772. ctx.drawImage(this.posterImage.bg.path, 0, 0, this.dpi(this.posterImage.bg.width), this.dpi(this.posterImage.bg.height))
  773. ctx.drawImage(this.posterImage.qcode.path, this.dpi(501), this.dpi(1140), this.dpi(180), this.dpi(180))
  774. ctx.save()
  775. ctx.translate(this.dpi(44),this.dpi(129))
  776. ctx.save()
  777. ctx.arc(this.dpi(60), this.dpi(59), this.dpi(40), 0, Math.PI * 2, false)
  778. ctx.lineWidth = 1
  779. ctx.strokeStyle = '#d4d4d4'
  780. ctx.stroke()
  781. ctx.clip()
  782. ctx.drawImage(this.posterImage.head.path, this.dpi(21), this.dpi(20), this.dpi(78), this.dpi(78))
  783. ctx.restore()
  784. ctx.save()
  785. ctx.textBaseline = 'middle'
  786. ctx.font = this.dpi(24) + 'px Arial'
  787. ctx.fillStyle = '#eab86a'
  788. ctx.fillText('以农民最有尊严的方式扶贫,我为凤凰菁选商城助力。', this.dpi(136), this.dpi(59))
  789. ctx.restore()
  790. ctx.drawImage(this.posterImage.project.path, this.dpi(0), this.dpi(120), this.dpi(662), this.dpi(353))
  791. ctx.save()
  792. ctx.translate(this.dpi(434), this.dpi(485))
  793. ctx.textAlign="center"
  794. ctx.fillStyle = '#eab86a'
  795. ctx.font = 'bold ' + this.dpi(24) + 'px Arial'
  796. ctx.fillText(this.data.percent + '%', this.dpi(90), this.dpi(100))
  797. ctx.fillStyle = '#ffab34'
  798. ctx.font = this.dpi(24) + 'px Arial'
  799. ctx.fillText('完成度', this.dpi(90), this.dpi(195))
  800. this.drawArc()
  801. ctx.restore()
  802. ctx.beginPath()
  803. ctx.moveTo(this.dpi(0), this.dpi(119))
  804. ctx.lineTo(this.dpi(662), this.dpi(119))
  805. ctx.lineWidth = 1
  806. ctx.strokeStyle = '#eab86a'
  807. ctx.stroke()
  808. ctx.closePath()
  809. ctx.textBaseline = 'middle'
  810. ctx.font = this.dpi(28) + 'px Arial'
  811. ctx.fillStyle = '#bbbbbb'
  812. ctx.fillText('团购单价', this.dpi(64), this.dpi(540))
  813. ctx.fillStyle = '#eab86a'
  814. ctx.font = 'bold ' + this.dpi(48) + 'px Arial'
  815. ctx.fillText('¥', this.dpi(63), this.dpi(624))
  816. ctx.font = 'bold ' + this.dpi(72) + 'px Arial'
  817. ctx.fillText(util.formatMoney(this.data.project.min_project_way_price / 100, '', 2), this.dpi(110), this.dpi(624))
  818. ctx.translate(this.dpi(20),this.dpi(705))
  819. this.roundRect(this.dpi(0), this.dpi(0), this.dpi(622), this.dpi(140), this.dpi(6), '#f3f3f3')
  820. ctx.font = this.dpi(32) + 'px Arial'
  821. ctx.fillStyle = '#2b2b2b'
  822. this.textAutoLine(this.data.project.share_title, this.dpi(40), this.dpi(50), this.dpi(40))
  823. ctx.restore()
  824. ctx.draw(true, function callback () {
  825. that.createImage()
  826. })
  827. },
  828. createImage () {
  829. var that = this
  830. wx.canvasToTempFilePath({
  831. destWidth: 750,
  832. destHeight: 1334,
  833. canvasId: 'myCanvas',
  834. fileType: 'jpg',
  835. success: function (res) {
  836. console.log(res)
  837. that.setData({
  838. posterImage: res.tempFilePath
  839. })
  840. that.canvasLoading = true
  841. wx.hideLoading()
  842. }
  843. })
  844. },
  845. saveImage () {
  846. var that = this
  847. wx.showLoading({
  848. mask: true,
  849. title: ''
  850. })
  851. wx.saveImageToPhotosAlbum({
  852. filePath: this.data.posterImage,
  853. success (res) {
  854. wx.showToast({
  855. title: '已保存相册',
  856. icon: 'success',
  857. duration: 2000
  858. })
  859. },
  860. fail (res) {
  861. wx.getSetting({
  862. success (res) {
  863. if (!res.authSetting['scope.writePhotosAlbum']) {
  864. that.setData({
  865. popup: true
  866. })
  867. }
  868. }
  869. })
  870. wx.hideLoading()
  871. }
  872. })
  873. },
  874. setting () {
  875. this.setData({
  876. popup: false
  877. })
  878. },
  879. roundRect (x, y, w, h, r, color) {
  880. ctx.beginPath()
  881. ctx.moveTo(x+r, y)
  882. ctx.arcTo(x+w, y, x+w, y+h, r)
  883. ctx.arcTo(x+w, y+h, x, y+h, r)
  884. ctx.arcTo(x, y+h, x, y, r)
  885. ctx.arcTo(x, y, x+w, y, r)
  886. ctx.closePath()
  887. ctx.fillStyle = color
  888. ctx.fill()
  889. },
  890. textAutoLine (str, initX, initY, lineHeight) {
  891. let lineWidth = 0
  892. let textWidth = this.dpi(600)
  893. let lastSubStrIndex= 0
  894. for (let i = 0; i < str.length; i++) {
  895. lineWidth += ctx.measureText(str[i]).width
  896. if (lineWidth > textWidth - initX) {
  897. ctx.fillText(str.substring(lastSubStrIndex, i), initX, initY)
  898. initY += lineHeight
  899. lineWidth = 0
  900. lastSubStrIndex = i
  901. }
  902. if (i === str.length - 1) {
  903. ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY)
  904. }
  905. }
  906. },
  907. drawArc () {
  908. var rad = Math.PI * 2 / 100
  909. var arc = this.dpi(90)
  910. var lineHeight = this.dpi(30)
  911. var radius = arc - lineHeight
  912. ctx.save()
  913. ctx.beginPath()
  914. ctx.lineWidth = lineHeight
  915. ctx.lineCap = 'round'
  916. ctx.strokeStyle = '#eeeeee'
  917. ctx.arc(arc, arc, radius, 0, Math.PI * 2, false)
  918. ctx.stroke()
  919. ctx.closePath()
  920. ctx.restore()
  921. ctx.save()
  922. ctx.strokeStyle = '#ff7655'
  923. ctx.lineWidth = lineHeight
  924. ctx.beginPath()
  925. ctx.arc(arc, arc, radius , -Math.PI/2, -Math.PI/2 + this.data.percent * rad, false)
  926. ctx.stroke()
  927. ctx.closePath()
  928. ctx.restore()
  929. },
  930. stopPageScroll () {
  931. return
  932. }
  933. })