material.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. var _request = require('../../../../../utils/request.js')
  2. var util = require('../../../../../utils/accounting.min.js')
  3. var ctx
  4. Page({
  5. posterImage: {},
  6. posterLoad: 0,
  7. cType: '',
  8. cIndex: -1,
  9. page: 1,
  10. per_page: 4,
  11. more: true,
  12. data: {
  13. list: [],
  14. userInfo: {},
  15. textList: [],
  16. posterImage: null,
  17. posterPopup: false,
  18. textLoading: false,
  19. imgsLoading: false,
  20. posterLoading: false,
  21. popup: false,
  22. loadingPopup: false
  23. },
  24. onLoad: function (options) {
  25. this.getList()
  26. this.getInfo()
  27. this.readyImage()
  28. },
  29. dpi: function (val) {
  30. return val
  31. },
  32. getList: function () {
  33. var that = this
  34. var url = 'v1/share/material/list'
  35. var params = {
  36. page: this.page,
  37. per_page: this.per_page
  38. }
  39. var success = function (res) {
  40. var result = that.data.list.concat(res.data.list || [])
  41. that.setData({
  42. list: result
  43. })
  44. that.more = res.data.list_count > that.data.list.length
  45. }
  46. _request.$get(url, params, success)
  47. },
  48. onReachBottom: function () {
  49. if (this.more) {
  50. this.page++
  51. this.getList()
  52. }
  53. },
  54. getInfo: function () {
  55. var that = this
  56. wx.getUserInfo({
  57. success: function (res) {
  58. var userInfo = res.userInfo
  59. that.setData({
  60. userInfo: userInfo
  61. })
  62. }
  63. })
  64. },
  65. changeText: function (val) {
  66. let num = val.currentTarget.dataset.val
  67. let i = "textList[" + num + "]"
  68. if (this.data.textList[num]) {
  69. this.setData({
  70. [i]: false
  71. })
  72. } else {
  73. this.setData({
  74. [i]: true
  75. })
  76. }
  77. },
  78. getImage: function (val) {
  79. let index = val.currentTarget.dataset.index
  80. let i = val.currentTarget.dataset.i
  81. let list = this.data.list[i].image_list
  82. wx.previewImage({
  83. current: list[index],
  84. urls: list
  85. })
  86. },
  87. readyImage: function () {
  88. let that = this
  89. wx.getImageInfo({
  90. src: 'https://cfohow.hiwavo.com/xcx/poster6.jpg',
  91. success: function (res) {
  92. that.posterImage.product = res
  93. }
  94. })
  95. wx.getImageInfo({
  96. src: 'https://cfohow.hiwavo.com/xcx/poster5.jpg',
  97. success: function (res) {
  98. that.posterImage.project = res
  99. }
  100. })
  101. },
  102. copeText: function (index) {
  103. var that = this
  104. wx.setClipboardData({
  105. data: that.data.list[index].material_text,
  106. success (res) {
  107. that.setData({
  108. textLoading: true
  109. })
  110. }
  111. })
  112. },
  113. sharePoster: function (val) {
  114. let index = val.currentTarget.dataset.index
  115. let type = val.currentTarget.dataset.type
  116. this.chooseType(index, type)
  117. },
  118. allShare: function (val) {
  119. this.clearLoading()
  120. let index = val.currentTarget.dataset.index
  121. let type = val.currentTarget.dataset.type
  122. this.chooseType(index, type)
  123. this.copeText(index)
  124. },
  125. chooseType: function (index, type) {
  126. this.cType = type
  127. if (index === this.cIndex && this.posterImage) {
  128. if (type === 'one') {
  129. this.setData({
  130. posterPopup: true
  131. })
  132. } else if (type === 'all') {
  133. this.createImage()
  134. this.setData({
  135. loadingPopup: true
  136. })
  137. }
  138. } else {
  139. this.posterImage.qcode = null
  140. this.posterImage.img = null
  141. this.cIndex = index
  142. if (this.data.list[index].m_type === 'product') {
  143. this.getProductQcode(index, type)
  144. } else if (this.data.list[index].m_type === 'project') {
  145. this.getProjectQcode(index, type)
  146. }
  147. if (type === 'one') {
  148. } else if (type === 'all') {
  149. this.setData({
  150. loadingPopup: true
  151. })
  152. }
  153. }
  154. },
  155. getProductQcode: function (index, type) {
  156. var that = this
  157. var url = 'v1/poster/' + that.data.list[index].product_info.id
  158. var params = {
  159. type: 'project'
  160. }
  161. var success = function (res) {
  162. wx.getImageInfo({
  163. src: res.data.qrcode_url,
  164. success: function (res) {
  165. that.posterImage.qcode = res
  166. if (that.posterImage.qcode && that.posterImage.head && that.posterImage.img) {
  167. that.createProductCtx(index, type)
  168. }
  169. }
  170. })
  171. if (!that.posterImage.head) {
  172. wx.getImageInfo({
  173. src: res.data.wx_head,
  174. success: function (res) {
  175. that.posterImage.head = res
  176. if (that.posterImage.qcode && that.posterImage.head && that.posterImage.img) {
  177. that.createProductCtx(index, type)
  178. }
  179. }
  180. })
  181. }
  182. wx.getImageInfo({
  183. src: that.data.list[index].product_info.pics[0].img,
  184. success: function (res) {
  185. that.posterImage.img = res
  186. if (that.posterImage.qcode && that.posterImage.head && that.posterImage.img) {
  187. that.createProductCtx(index,type)
  188. }
  189. }
  190. })
  191. }
  192. _request.$get(url, params, success)
  193. },
  194. getProjectQcode: function (index, type) {
  195. var that = this
  196. var url = 'v1/poster/' + that.data.list[index].project_info.id
  197. var params = {
  198. type: 'poster'
  199. }
  200. var success = function (res) {
  201. wx.getImageInfo({
  202. src: res.data.qrcode_url,
  203. success: function (res) {
  204. that.posterImage.qcode = res
  205. if (that.posterImage.qcode && that.posterImage.head && that.posterImage.img) {
  206. that.createProjectCtx(index, type)
  207. }
  208. }
  209. })
  210. if (!that.posterImage.head) {
  211. wx.getImageInfo({
  212. src: res.data.wx_head,
  213. success: function (res) {
  214. that.posterImage.head = res
  215. if (that.posterImage.qcode && that.posterImage.head && that.posterImage.img) {
  216. that.createProjectCtx(index, type)
  217. }
  218. }
  219. })
  220. }
  221. wx.getImageInfo({
  222. src: that.data.list[index].project_info.introduce_pics[0].img,
  223. success: function (res) {
  224. that.posterImage.img = res
  225. if (that.posterImage.qcode && that.posterImage.head && that.posterImage.img) {
  226. that.createProjectCtx(index, type)
  227. }
  228. }
  229. })
  230. }
  231. _request.$get(url, params, success)
  232. },
  233. createProductCtx (index, type) {
  234. let that = this
  235. ctx = wx.createCanvasContext('myCanvas')
  236. ctx.drawImage(this.posterImage.product.path, 0, 0, this.dpi(this.posterImage.product.width), this.dpi(this.posterImage.product.height))
  237. ctx.drawImage(this.posterImage.qcode.path, this.dpi(501), this.dpi(1140), this.dpi(180), this.dpi(180))
  238. ctx.save()
  239. ctx.translate(this.dpi(44),this.dpi(46))
  240. ctx.save()
  241. ctx.arc(this.dpi(60), this.dpi(59), this.dpi(40), 0, Math.PI * 2, false)
  242. ctx.lineWidth = 1
  243. ctx.strokeStyle = '#d4d4d4'
  244. ctx.stroke()
  245. ctx.clip()
  246. ctx.drawImage(this.posterImage.head.path, this.dpi(21), this.dpi(20), this.dpi(78), this.dpi(78))
  247. ctx.restore()
  248. ctx.save()
  249. ctx.textBaseline = 'middle'
  250. ctx.font = this.dpi(24) + 'px Arial'
  251. ctx.fillStyle = '#eab86a'
  252. ctx.fillText('源处全球源产地的优质产品!', this.dpi(136), this.dpi(59))
  253. ctx.restore()
  254. ctx.drawImage(this.posterImage.img.path, this.dpi(20), this.dpi(140), this.dpi(622), this.dpi(622))
  255. ctx.beginPath()
  256. ctx.moveTo(this.dpi(0), this.dpi(119))
  257. ctx.lineTo(this.dpi(662), this.dpi(119))
  258. ctx.lineWidth = 1
  259. ctx.strokeStyle = '#eab86a'
  260. ctx.stroke()
  261. ctx.closePath()
  262. ctx.save()
  263. ctx.translate(this.dpi(20),this.dpi(784))
  264. ctx.textBaseline = 'middle'
  265. ctx.font = this.dpi(28) + 'px Arial'
  266. ctx.fillStyle = '#bbbbbb'
  267. ctx.fillText('购买单价', this.dpi(46), this.dpi(50))
  268. ctx.fillStyle = '#eab86a'
  269. ctx.setTextAlign('right')
  270. ctx.font = 'bold ' + this.dpi(54) + 'px Arial'
  271. ctx.fillText(util.formatMoney(this.data.list[index].product_info.price / 100, '¥', 2), this.dpi(584), this.dpi(50))
  272. ctx.setTextAlign('left')
  273. ctx.font = this.dpi(32) + 'px Arial'
  274. ctx.fillStyle = '#2b2b2b'
  275. this.textAutoLine(this.data.list[index].product_info.name, this.dpi(30), this.dpi(150), this.dpi(40))
  276. ctx.restore()
  277. ctx.restore()
  278. ctx.draw(true, function callback () {
  279. that.createImage(type)
  280. })
  281. },
  282. createProjectCtx (index, type) {
  283. var that = this
  284. let projects = this.data.list[index].project_info
  285. let percent = (projects.current_funding / projects.min_funding * 100).toFixed(2)
  286. ctx = wx.createCanvasContext('myCanvas')
  287. ctx.drawImage(this.posterImage.project.path, 0, 0, this.dpi(this.posterImage.project.width), this.dpi(this.posterImage.project.height))
  288. ctx.drawImage(this.posterImage.qcode.path, this.dpi(501), this.dpi(1140), this.dpi(180), this.dpi(180))
  289. ctx.save()
  290. ctx.translate(this.dpi(44),this.dpi(129))
  291. ctx.save()
  292. ctx.arc(this.dpi(60), this.dpi(59), this.dpi(40), 0, Math.PI * 2, false)
  293. ctx.lineWidth = 1
  294. ctx.strokeStyle = '#d4d4d4'
  295. ctx.stroke()
  296. ctx.clip()
  297. ctx.drawImage(this.posterImage.head.path, this.dpi(21), this.dpi(20), this.dpi(78), this.dpi(78))
  298. ctx.restore()
  299. ctx.save()
  300. ctx.textBaseline = 'middle'
  301. ctx.font = this.dpi(24) + 'px Arial'
  302. ctx.fillStyle = '#eab86a'
  303. ctx.fillText('源处全球源产地的优质产品!', this.dpi(136), this.dpi(59))
  304. ctx.restore()
  305. ctx.drawImage(this.posterImage.img.path, this.dpi(0), this.dpi(120), this.dpi(662), this.dpi(353))
  306. ctx.save()
  307. ctx.translate(this.dpi(434), this.dpi(485))
  308. ctx.textAlign="center"
  309. ctx.fillStyle = '#eab86a'
  310. ctx.font = 'bold ' + this.dpi(24) + 'px Arial'
  311. ctx.fillText(percent + '%', this.dpi(90), this.dpi(100))
  312. ctx.fillStyle = '#ffab34'
  313. ctx.font = this.dpi(24) + 'px Arial'
  314. ctx.fillText('完成度', this.dpi(90), this.dpi(195))
  315. this.drawArc(percent)
  316. ctx.restore()
  317. ctx.beginPath()
  318. ctx.moveTo(this.dpi(0), this.dpi(119))
  319. ctx.lineTo(this.dpi(662), this.dpi(119))
  320. ctx.lineWidth = 1
  321. ctx.strokeStyle = '#eab86a'
  322. ctx.stroke()
  323. ctx.closePath()
  324. ctx.textBaseline = 'middle'
  325. ctx.font = this.dpi(28) + 'px Arial'
  326. ctx.fillStyle = '#bbbbbb'
  327. ctx.fillText('团购单价', this.dpi(64), this.dpi(540))
  328. ctx.fillStyle = '#eab86a'
  329. ctx.font = 'bold ' + this.dpi(48) + 'px Arial'
  330. ctx.fillText('¥', this.dpi(63), this.dpi(624))
  331. ctx.font = 'bold ' + this.dpi(72) + 'px Arial'
  332. ctx.fillText(util.formatMoney(projects.min_project_way_price / 100, '', 2), this.dpi(110), this.dpi(624))
  333. ctx.translate(this.dpi(20),this.dpi(705))
  334. this.roundRect(this.dpi(0), this.dpi(0), this.dpi(622), this.dpi(140), this.dpi(6), '#f3f3f3')
  335. ctx.font = this.dpi(32) + 'px Arial'
  336. ctx.fillStyle = '#2b2b2b'
  337. this.textAutoLine(projects.title, this.dpi(40), this.dpi(50), this.dpi(40))
  338. ctx.restore()
  339. ctx.draw(true, function callback () {
  340. that.createImage(type)
  341. })
  342. },
  343. roundRect (x, y, w, h, r, color) {
  344. ctx.beginPath()
  345. ctx.moveTo(x+r, y)
  346. ctx.arcTo(x+w, y, x+w, y+h, r)
  347. ctx.arcTo(x+w, y+h, x, y+h, r)
  348. ctx.arcTo(x, y+h, x, y, r)
  349. ctx.arcTo(x, y, x+w, y, r)
  350. ctx.closePath()
  351. ctx.fillStyle = color
  352. ctx.fill()
  353. },
  354. textAutoLine (str, initX, initY, lineHeight) {
  355. let lineWidth = 0
  356. let textWidth = this.dpi(580)
  357. let lastSubStrIndex= 0
  358. for (let i = 0; i < str.length; i++) {
  359. lineWidth += ctx.measureText(str[i]).width
  360. if (lineWidth > textWidth - initX) {
  361. ctx.fillText(str.substring(lastSubStrIndex, i), initX, initY)
  362. initY += lineHeight
  363. lineWidth = 0
  364. lastSubStrIndex = i
  365. }
  366. if (i === str.length - 1) {
  367. ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY)
  368. }
  369. }
  370. },
  371. drawArc (percent) {
  372. var rad = Math.PI * 2 / 100
  373. var arc = this.dpi(90)
  374. var lineHeight = this.dpi(30)
  375. var radius = arc - lineHeight
  376. ctx.save()
  377. ctx.beginPath()
  378. ctx.lineWidth = lineHeight
  379. ctx.lineCap = 'round'
  380. ctx.strokeStyle = '#eeeeee'
  381. ctx.arc(arc, arc, radius, 0, Math.PI * 2, false)
  382. ctx.stroke()
  383. ctx.closePath()
  384. ctx.restore()
  385. ctx.save()
  386. ctx.strokeStyle = '#ff7655'
  387. ctx.lineWidth = lineHeight
  388. ctx.beginPath()
  389. ctx.arc(arc, arc, radius , -Math.PI/2, -Math.PI/2 + percent * rad, false)
  390. ctx.stroke()
  391. ctx.closePath()
  392. ctx.restore()
  393. },
  394. createImage () {
  395. var that = this
  396. wx.canvasToTempFilePath({
  397. destWidth: 750,
  398. destHeight: 1334,
  399. canvasId: 'myCanvas',
  400. fileType: 'jpg',
  401. success: function (res) {
  402. that.setData({
  403. posterImage: res.tempFilePath
  404. })
  405. if (that.cType === 'one') {
  406. that.setData({
  407. posterPopup: true
  408. })
  409. wx.hideLoading()
  410. } else if (that.cType === 'all') {
  411. that.downloadAll(res.tempFilePath)
  412. }
  413. }
  414. })
  415. },
  416. saveImage () {
  417. var that = this
  418. wx.saveImageToPhotosAlbum({
  419. filePath: that.data.posterImage,
  420. success (res) {
  421. wx.showToast({
  422. title: '已保存相册',
  423. icon: 'success',
  424. duration: 2000
  425. })
  426. },
  427. fail (res) {
  428. wx.getSetting({
  429. success (res) {
  430. if (!res.authSetting['scope.writePhotosAlbum']) {
  431. that.setData({
  432. popup: true
  433. })
  434. }
  435. }
  436. })
  437. }
  438. })
  439. },
  440. downloadAll (res) {
  441. var that = this
  442. wx.saveImageToPhotosAlbum({
  443. filePath: res,
  444. success (res) {
  445. that.setData({
  446. posterLoading: true
  447. })
  448. that.downloadImgs()
  449. },
  450. fail (res) {
  451. wx.getSetting({
  452. success (res) {
  453. if (!res.authSetting['scope.writePhotosAlbum']) {
  454. that.setData({
  455. popup: true
  456. })
  457. }
  458. }
  459. })
  460. }
  461. })
  462. },
  463. downloadImgs () {
  464. var that = this
  465. let num = 0
  466. let imgList = this.data.list[this.cIndex].image_list
  467. console.log('imgList',imgList);
  468. let save = []
  469. for (let i in imgList) {
  470. wx.getImageInfo({
  471. src: imgList[i],
  472. success: function (res) {
  473. num++
  474. save[i] = res.path
  475. if (num === imgList.length) {
  476. that.saveImages(save)
  477. }
  478. }
  479. })
  480. }
  481. },
  482. saveImages (save) {
  483. var that = this
  484. let num = 0
  485. for (let i in save) {
  486. wx.saveImageToPhotosAlbum({
  487. filePath: save[i],
  488. success: function (res) {
  489. num++
  490. if (num === save.length) {
  491. that.setData({
  492. imgsLoading: true,
  493. loadingPopup: false
  494. })
  495. wx.showToast({
  496. title: '一键保存成功',
  497. icon: 'success',
  498. duration: 2000
  499. })
  500. that.Statistics()
  501. }
  502. }
  503. })
  504. }
  505. },
  506. Statistics () {
  507. console.log(this.cIndex)
  508. let item = this.data.list[this.cIndex]
  509. let cc = 'share_material_' + item.m_type
  510. let ri = item.id
  511. let rp = item.type_id
  512. var that = this
  513. var url = 'v1/click/info'
  514. console.log(cc)
  515. console.log(ri)
  516. console.log(rp)
  517. var params = {
  518. cc: cc,
  519. ri: ri,
  520. rp: rp
  521. }
  522. var success = function (res) {
  523. }
  524. _request.$post(url, params, success)
  525. },
  526. posterClose () {
  527. this.setData({
  528. posterPopup: false
  529. })
  530. },
  531. setting () {
  532. this.setData({
  533. popup: false
  534. })
  535. this.clearLoading()
  536. this.createImage()
  537. },
  538. clearLoading () {
  539. this.setData({
  540. textLoading: false,
  541. imgsLoading: false,
  542. posterLoading: false
  543. })
  544. },
  545. stopPageScroll () {
  546. return
  547. },
  548. toProduct (val) {
  549. let id = val.currentTarget.dataset.id
  550. wx.navigateTo({
  551. url: '/pages/projects/project-detail/project-detail?id=' + id
  552. })
  553. },
  554. toProject (val) {
  555. let id = val.currentTarget.dataset.id
  556. wx.navigateTo({
  557. url: '/pages/helpfarm/helpfarm-detail/helpfarm-detail?id=' + id
  558. })
  559. },
  560. onShareAppMessage: function (val) {
  561. return _request.share({
  562. sc: 'xcx_user_material'
  563. })
  564. }
  565. })