material.js 16 KB

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