helpfarm-detail.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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. if (res && res.errMsg && (res.errMsg.indexOf('cancel') > -1 || res.errMsg.indexOf('取消') > -1)) {
  564. wx.showToast({
  565. title: '用户取消支付',
  566. icon: 'none',
  567. duration: 2000
  568. })
  569. }
  570. },
  571. 'complete': function () {
  572. buttonClicked = true
  573. }
  574. })
  575. }
  576. _request.$post(url, params, success)
  577. },
  578. toBalancePay () {
  579. var that = this
  580. var url = 'v1/pay'
  581. var pay_way = 'cashbalance'
  582. var params = {
  583. order_id: that.data.productOid,
  584. pay_way: pay_way
  585. }
  586. var success = function (val) {
  587. that.closeModalDialog()
  588. buttonClicked = true
  589. wx.showToast({
  590. title: '支付成功',
  591. icon: 'success',
  592. duration: 1000
  593. })
  594. if (val) {
  595. wx.redirectTo({
  596. // url: that.data.path + '?orderId=' + that.data.orderId
  597. // url: '/packageUser/pages/user/farm/farm'
  598. url: '/pages/helpfarm/helpfarm-success/helpfarm-success?id=' + that.data.Inid
  599. })
  600. }
  601. }
  602. _request.$post(url, params, success)
  603. },
  604. // buttonClicked () {
  605. // var that = this
  606. // that.setData({
  607. // buttonClicked: true
  608. // })
  609. // setTimeout(function () {
  610. // that.setData({
  611. // buttonClicked: false
  612. // })
  613. // }, 500)
  614. // },
  615. onShareAppMessage: function (val) {
  616. return _request.share({
  617. title: this.data.project.share_title,
  618. path: '/pages/start/start?url=pages/helpfarm/helpfarm-detail/helpfarm-detail&id=' + this.data.projectId,
  619. imageUrl: '',
  620. sc: 'xcx_helpfarmproject',
  621. ri: this.data.projectId,
  622. rp: this.data.projectId
  623. })
  624. },
  625. showprogress() {
  626. var that = this
  627. var percent = that.data.percent
  628. if (percent <= 50) {
  629. that.setData({
  630. progress: percent * 3.6
  631. })
  632. } else if (percent > 100) {
  633. percent = 100
  634. that.setData({
  635. bgcolor: '#ff7655',
  636. progress: 0,
  637. progress1: (percent-50)*3.6
  638. })
  639. } else {
  640. that.setData({
  641. bgcolor: '#ff7655',
  642. progress: 0,
  643. progress1: (percent-50)*3.6
  644. })
  645. }
  646. },
  647. toCount () {
  648. let price = this.data.count * this.data.InvestWayList.invest_way[this.data.itemindex].price
  649. let count = this.data.count
  650. let sell = this.data.InvestWayList.invest_way[this.data.itemindex].product.user_sale_price
  651. let min = this.data.InvestWayList.invest_way[this.data.itemindex].self_use_min
  652. let retrieve = this.data.InvestWayList.invest_way[this.data.itemindex].harvest_rate / 100
  653. let limit = this.data.InvestWayList.invest_way[this.data.itemindex].gap_limit
  654. let remax = this.data.InvestWayList.invest_way[this.data.itemindex].buy_back_max_limit
  655. let reprice = this.data.InvestWayList.invest_way[this.data.itemindex].buy_back_price
  656. let minlimit = this.data.InvestWayList.invest_way[this.data.itemindex].min_limit
  657. wx.navigateTo({
  658. url: '/pages/helpfarm/count/count?price=' + price + '&count=' + count + '&sell=' + sell + '&min=' + min + '&retrieve=' + retrieve + '&limit=' + limit + '&remax=' + remax + '&minlimit=' + minlimit + '&reprice=' + reprice
  659. })
  660. },
  661. getIphoneX () {
  662. var that = this
  663. wx.getSystemInfo({
  664. success: function (res) {
  665. if (res.model.toLowerCase().indexOf('iphone x') > -1) {
  666. that.setData({
  667. value: 34
  668. })
  669. }
  670. }
  671. })
  672. },
  673. getCashTotal () {
  674. var that = this
  675. var url = 'v1/user/cash/balance/info'
  676. var params = {
  677. }
  678. var success = function (res) {
  679. that.data.cashTotal = res.data.available
  680. that.setData({
  681. cashTotal: res.data.available
  682. })
  683. }
  684. _request.$get(url, params, success)
  685. },
  686. projectImageLoading () {
  687. var that = this
  688. wx.getImageInfo({
  689. src: 'https://cfohow.hiwavo.com/xcx/poster5.jpg',
  690. success: function (res) {
  691. that.posterImage.bg = res
  692. that.posterLoading++
  693. if (that.posterLoading === 4) {
  694. that.createCtx()
  695. }
  696. }
  697. })
  698. if (this.data.imgUrls.length > 0) {
  699. wx.getImageInfo({
  700. src: this.data.imgUrls[0],
  701. success: function (res) {
  702. that.posterImage.project = res
  703. that.posterLoading++
  704. if (that.posterLoading === 4) {
  705. that.createCtx()
  706. }
  707. }
  708. })
  709. }
  710. var url = 'v1/poster/' + this.data.projectId
  711. var params = {
  712. type: 'poster'
  713. }
  714. var success = function (res) {
  715. wx.getImageInfo({
  716. src: res.data.qrcode_url,
  717. success: function (res) {
  718. that.posterImage.qcode = res
  719. that.posterLoading++
  720. if (that.posterLoading === 4) {
  721. that.createCtx()
  722. }
  723. },
  724. fail: function (res) {
  725. that.qcodeError = true
  726. if (that.data.posterPopup) {
  727. that.setData({
  728. posterPopup: false
  729. })
  730. wx.showToast({
  731. title: '海报生成失败,请稍后重试',
  732. icon: 'none',
  733. duration: 2000
  734. })
  735. wx.hideLoading()
  736. }
  737. }
  738. })
  739. wx.getImageInfo({
  740. src: res.data.wx_head,
  741. success: function (res) {
  742. that.posterImage.head = res
  743. that.posterLoading++
  744. if (that.posterLoading === 4) {
  745. that.createCtx()
  746. }
  747. }
  748. })
  749. }
  750. _request.$get(url, params, success)
  751. },
  752. createPoster () {
  753. if (!this.qcodeError) {
  754. if (!this.canvasLoading) {
  755. wx.showLoading({
  756. mask: true,
  757. title: ''
  758. })
  759. }
  760. this.setData({
  761. posterPopup: true
  762. })
  763. } else {
  764. wx.showToast({
  765. title: '海报生成失败,请稍后重试',
  766. icon: 'none',
  767. duration: 2000
  768. })
  769. }
  770. },
  771. posterClose () {
  772. this.setData({
  773. posterPopup: false
  774. })
  775. },
  776. createCtx () {
  777. var that = this
  778. ctx = wx.createCanvasContext('myCanvas')
  779. ctx.drawImage(this.posterImage.bg.path, 0, 0, this.dpi(this.posterImage.bg.width), this.dpi(this.posterImage.bg.height))
  780. ctx.drawImage(this.posterImage.qcode.path, this.dpi(501), this.dpi(1140), this.dpi(180), this.dpi(180))
  781. ctx.save()
  782. ctx.translate(this.dpi(44),this.dpi(129))
  783. ctx.save()
  784. ctx.arc(this.dpi(60), this.dpi(59), this.dpi(40), 0, Math.PI * 2, false)
  785. ctx.lineWidth = 1
  786. ctx.strokeStyle = '#d4d4d4'
  787. ctx.stroke()
  788. ctx.clip()
  789. ctx.drawImage(this.posterImage.head.path, this.dpi(21), this.dpi(20), this.dpi(78), this.dpi(78))
  790. ctx.restore()
  791. ctx.save()
  792. ctx.textBaseline = 'middle'
  793. ctx.font = this.dpi(24) + 'px Arial'
  794. ctx.fillStyle = '#eab86a'
  795. ctx.fillText('以农民最有尊严的方式扶贫,我为凤凰菁选商城助力。', this.dpi(136), this.dpi(59))
  796. ctx.restore()
  797. ctx.drawImage(this.posterImage.project.path, this.dpi(0), this.dpi(120), this.dpi(662), this.dpi(353))
  798. ctx.save()
  799. ctx.translate(this.dpi(434), this.dpi(485))
  800. ctx.textAlign="center"
  801. ctx.fillStyle = '#eab86a'
  802. ctx.font = 'bold ' + this.dpi(24) + 'px Arial'
  803. ctx.fillText(this.data.percent + '%', this.dpi(90), this.dpi(100))
  804. ctx.fillStyle = '#ffab34'
  805. ctx.font = this.dpi(24) + 'px Arial'
  806. ctx.fillText('完成度', this.dpi(90), this.dpi(195))
  807. this.drawArc()
  808. ctx.restore()
  809. ctx.beginPath()
  810. ctx.moveTo(this.dpi(0), this.dpi(119))
  811. ctx.lineTo(this.dpi(662), this.dpi(119))
  812. ctx.lineWidth = 1
  813. ctx.strokeStyle = '#eab86a'
  814. ctx.stroke()
  815. ctx.closePath()
  816. ctx.textBaseline = 'middle'
  817. ctx.font = this.dpi(28) + 'px Arial'
  818. ctx.fillStyle = '#bbbbbb'
  819. ctx.fillText('团购单价', this.dpi(64), this.dpi(540))
  820. ctx.fillStyle = '#eab86a'
  821. ctx.font = 'bold ' + this.dpi(48) + 'px Arial'
  822. ctx.fillText('¥', this.dpi(63), this.dpi(624))
  823. ctx.font = 'bold ' + this.dpi(72) + 'px Arial'
  824. ctx.fillText(util.formatMoney(this.data.project.min_project_way_price / 100, '', 2), this.dpi(110), this.dpi(624))
  825. ctx.translate(this.dpi(20),this.dpi(705))
  826. this.roundRect(this.dpi(0), this.dpi(0), this.dpi(622), this.dpi(140), this.dpi(6), '#f3f3f3')
  827. ctx.font = this.dpi(32) + 'px Arial'
  828. ctx.fillStyle = '#2b2b2b'
  829. this.textAutoLine(this.data.project.share_title, this.dpi(40), this.dpi(50), this.dpi(40))
  830. ctx.restore()
  831. ctx.draw(true, function callback () {
  832. that.createImage()
  833. })
  834. },
  835. createImage () {
  836. var that = this
  837. wx.canvasToTempFilePath({
  838. destWidth: 750,
  839. destHeight: 1334,
  840. canvasId: 'myCanvas',
  841. fileType: 'jpg',
  842. success: function (res) {
  843. console.log(res)
  844. that.setData({
  845. posterImage: res.tempFilePath
  846. })
  847. that.canvasLoading = true
  848. wx.hideLoading()
  849. }
  850. })
  851. },
  852. saveImage () {
  853. var that = this
  854. wx.showLoading({
  855. mask: true,
  856. title: ''
  857. })
  858. wx.saveImageToPhotosAlbum({
  859. filePath: this.data.posterImage,
  860. success (res) {
  861. wx.showToast({
  862. title: '已保存相册',
  863. icon: 'success',
  864. duration: 2000
  865. })
  866. },
  867. fail (res) {
  868. wx.getSetting({
  869. success (res) {
  870. if (!res.authSetting['scope.writePhotosAlbum']) {
  871. that.setData({
  872. popup: true
  873. })
  874. }
  875. }
  876. })
  877. wx.hideLoading()
  878. }
  879. })
  880. },
  881. setting () {
  882. this.setData({
  883. popup: false
  884. })
  885. },
  886. roundRect (x, y, w, h, r, color) {
  887. ctx.beginPath()
  888. ctx.moveTo(x+r, y)
  889. ctx.arcTo(x+w, y, x+w, y+h, r)
  890. ctx.arcTo(x+w, y+h, x, y+h, r)
  891. ctx.arcTo(x, y+h, x, y, r)
  892. ctx.arcTo(x, y, x+w, y, r)
  893. ctx.closePath()
  894. ctx.fillStyle = color
  895. ctx.fill()
  896. },
  897. textAutoLine (str, initX, initY, lineHeight) {
  898. let lineWidth = 0
  899. let textWidth = this.dpi(600)
  900. let lastSubStrIndex= 0
  901. for (let i = 0; i < str.length; i++) {
  902. lineWidth += ctx.measureText(str[i]).width
  903. if (lineWidth > textWidth - initX) {
  904. ctx.fillText(str.substring(lastSubStrIndex, i), initX, initY)
  905. initY += lineHeight
  906. lineWidth = 0
  907. lastSubStrIndex = i
  908. }
  909. if (i === str.length - 1) {
  910. ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY)
  911. }
  912. }
  913. },
  914. drawArc () {
  915. var rad = Math.PI * 2 / 100
  916. var arc = this.dpi(90)
  917. var lineHeight = this.dpi(30)
  918. var radius = arc - lineHeight
  919. ctx.save()
  920. ctx.beginPath()
  921. ctx.lineWidth = lineHeight
  922. ctx.lineCap = 'round'
  923. ctx.strokeStyle = '#eeeeee'
  924. ctx.arc(arc, arc, radius, 0, Math.PI * 2, false)
  925. ctx.stroke()
  926. ctx.closePath()
  927. ctx.restore()
  928. ctx.save()
  929. ctx.strokeStyle = '#ff7655'
  930. ctx.lineWidth = lineHeight
  931. ctx.beginPath()
  932. ctx.arc(arc, arc, radius , -Math.PI/2, -Math.PI/2 + this.data.percent * rad, false)
  933. ctx.stroke()
  934. ctx.closePath()
  935. ctx.restore()
  936. },
  937. stopPageScroll () {
  938. return
  939. }
  940. })