cps-taobao.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. const WXAPI = require('apifm-wxapi')
  2. const AUTH = require('../../../utils/auth')
  3. import Poster from 'wxa-plugin-canvas/poster/poster'
  4. Page({
  5. data: {
  6. beianPass: 0, // 0 未判断,1 未备案, 2 已备案
  7. },
  8. onLoad(e) {
  9. // e.id = 825894
  10. // 读取分享链接中的邀请人编号
  11. if (e && e.inviter_id) {
  12. wx.setStorageSync('referrer', e.inviter_id)
  13. }
  14. // 读取小程序码中的邀请人编号
  15. if (e && e.scene) {
  16. const scene = decodeURIComponent(e.scene) // 处理扫码进商品详情页面的逻辑
  17. if (scene && scene.split(',').length >= 2) {
  18. e.id = scene.split(',')[0]
  19. wx.setStorageSync('referrer', scene.split(',')[1])
  20. }
  21. }
  22. this.data.goodsId = e.id
  23. this.goodsDetail()
  24. },
  25. async goodsDetail() {
  26. const token = wx.getStorageSync('token')
  27. const res = await WXAPI.goodsDetail(this.data.goodsId, token ? token : '')
  28. if (res.code == 0) {
  29. this.setData({
  30. goodsDetail: res.data,
  31. })
  32. this.cpsTaobaoGoodsDetail(res.data.basicInfo.yyId)
  33. }
  34. },
  35. async cpsTaobaoGoodsDetail(goodsId) {
  36. const token = wx.getStorageSync('token')
  37. const res = await WXAPI.cpsTaobaoGoodsDetail({
  38. token,
  39. platform: 2,
  40. goodsIds: goodsId
  41. })
  42. if (res.code == 0) {
  43. this.setData({
  44. cpsTaobaoGoodsDetail: res.data[0]
  45. })
  46. }
  47. },
  48. onShow (){
  49. AUTH.checkHasLogined().then(isLogined => {
  50. if (isLogined) {
  51. this.goodsFavCheck()
  52. }
  53. })
  54. },
  55. async goodsFavCheck() {
  56. const res = await WXAPI.goodsFavCheck(wx.getStorageSync('token'), this.data.goodsId)
  57. if (res.code == 0) {
  58. this.setData({
  59. faved: true
  60. })
  61. } else {
  62. this.setData({
  63. faved: false
  64. })
  65. }
  66. },
  67. async addFav(){
  68. AUTH.checkHasLogined().then(isLogined => {
  69. this.setData({
  70. wxlogin: isLogined
  71. })
  72. if (isLogined) {
  73. if (this.data.faved) {
  74. // 取消收藏
  75. WXAPI.goodsFavDeleteV2({
  76. token: wx.getStorageSync('token'),
  77. goodsId: this.data.goodsId,
  78. type: 1
  79. }).then(res => {
  80. this.goodsFavCheck()
  81. })
  82. } else {
  83. const extJsonStr = {
  84. wxaurl: `/packageCps/pages/goods-details/cps-taobao?id=${this.data.goodsId}`,
  85. skuId: this.data.goodsId,
  86. pic: this.data.goodsDetail.basicInfo.pic,
  87. name: this.data.goodsDetail.basicInfo.name
  88. }
  89. // 加入收藏
  90. WXAPI.goodsFavAdd({
  91. token: wx.getStorageSync('token'),
  92. goodsId: this.data.goodsId,
  93. type: 1,
  94. extJsonStr: JSON.stringify(extJsonStr)
  95. }).then(res => {
  96. this.goodsFavCheck()
  97. })
  98. }
  99. }
  100. })
  101. },
  102. goShopCar: function() {
  103. wx.reLaunch({
  104. url: "/pages/shop-cart/index"
  105. });
  106. },
  107. toAddShopCar: function() {
  108. this.setData({
  109. shopType: "addShopCar"
  110. })
  111. this.bindGuiGeTap();
  112. },
  113. async tobuy() {
  114. const token = wx.getStorageSync('token')
  115. if (!token) {
  116. wx.showToast({
  117. title: '请先登陆',
  118. icon: 'none'
  119. })
  120. return
  121. }
  122. const res = await WXAPI.cpsPddGoodsShotUrl(token, this.data.goodsDetail.basicInfo.yyIdStr)
  123. if (res.code != 0) {
  124. wx.showToast({
  125. title: res.msg,
  126. icon: 'none'
  127. })
  128. return
  129. }
  130. wx.navigateToMiniProgram({
  131. appId: res.data.we_app_info.app_id,
  132. path: res.data.we_app_info.page_path
  133. })
  134. },
  135. toPingtuan: function(e) {
  136. let pingtuanopenid = 0
  137. if (e.currentTarget.dataset.pingtuanopenid) {
  138. pingtuanopenid = e.currentTarget.dataset.pingtuanopenid
  139. }
  140. this.setData({
  141. shopType: "toPingtuan",
  142. selectSizePrice: this.data.goodsDetail.basicInfo.pingtuanPrice,
  143. selectSizeOPrice: this.data.goodsDetail.basicInfo.originalPrice,
  144. pingtuanopenid: pingtuanopenid,
  145. hideShopPopup: false,
  146. skuGoodsPic: this.data.goodsDetail.basicInfo.pic
  147. });
  148. },
  149. /**
  150. * 规格选择弹出框
  151. */
  152. bindGuiGeTap: function() {
  153. this.setData({
  154. hideShopPopup: false
  155. })
  156. },
  157. /**
  158. * 规格选择弹出框隐藏
  159. */
  160. closePopupTap: function() {
  161. this.setData({
  162. hideShopPopup: true
  163. })
  164. },
  165. stepChange(event) {
  166. this.setData({
  167. buyNumber: event.detail
  168. })
  169. },
  170. /**
  171. * 选择商品规格
  172. */
  173. async labelItemTap(e) {
  174. const propertyindex = e.currentTarget.dataset.propertyindex
  175. const propertychildindex = e.currentTarget.dataset.propertychildindex
  176. const property = this.data.goodsDetail.properties[propertyindex]
  177. const child = property.childsCurGoods[propertychildindex]
  178. // 取消该分类下的子栏目所有的选中状态
  179. property.childsCurGoods.forEach(child => {
  180. child.active = false
  181. })
  182. // 设置当前选中状态
  183. property.optionValueId = child.id
  184. child.active = true
  185. // 获取所有的选中规格尺寸数据
  186. const needSelectNum = this.data.goodsDetail.properties.length
  187. let curSelectNum = 0;
  188. let propertyChildIds = "";
  189. let propertyChildNames = "";
  190. this.data.goodsDetail.properties.forEach(p => {
  191. p.childsCurGoods.forEach(c => {
  192. if (c.active) {
  193. curSelectNum++;
  194. propertyChildIds = propertyChildIds + p.id + ":" + c.id + ",";
  195. propertyChildNames = propertyChildNames + p.name + ":" + c.name + " ";
  196. }
  197. })
  198. })
  199. let canSubmit = false;
  200. if (needSelectNum == curSelectNum) {
  201. canSubmit = true;
  202. }
  203. let skuGoodsPic = this.data.skuGoodsPic
  204. if (this.data.goodsDetail.subPics && this.data.goodsDetail.subPics.length > 0) {
  205. const _subPic = this.data.goodsDetail.subPics.find(ele => {
  206. return ele.optionValueId == child.id
  207. })
  208. if (_subPic) {
  209. skuGoodsPic = _subPic.pic
  210. }
  211. }
  212. this.setData({
  213. goodsDetail: this.data.goodsDetail,
  214. canSubmit,
  215. skuGoodsPic,
  216. propertyChildIds,
  217. propertyChildNames,
  218. })
  219. this.calculateGoodsPrice()
  220. },
  221. async calculateGoodsPrice() {
  222. // 计算最终的商品价格
  223. let price = this.data.goodsDetail.basicInfo.minPrice
  224. let originalPrice = this.data.goodsDetail.basicInfo.originalPrice
  225. let totalScoreToPay = this.data.goodsDetail.basicInfo.minScore
  226. let buyNumMax = this.data.goodsDetail.basicInfo.stores
  227. let buyNumber = this.data.goodsDetail.basicInfo.minBuyNumber
  228. if (this.data.shopType == 'toPingtuan') {
  229. price = this.data.goodsDetail.basicInfo.pingtuanPrice
  230. }
  231. // 计算 sku 价格
  232. if (this.data.canSubmit) {
  233. const token = wx.getStorageSync('token')
  234. const res = await WXAPI.goodsPriceV2({
  235. token: token ? token : '',
  236. goodsId: this.data.goodsDetail.basicInfo.id,
  237. propertyChildIds: this.data.propertyChildIds
  238. })
  239. if (res.code == 0) {
  240. price = res.data.price
  241. if (this.data.shopType == 'toPingtuan') {
  242. price = res.data.pingtuanPrice
  243. }
  244. originalPrice = res.data.originalPrice
  245. totalScoreToPay = res.data.score
  246. buyNumMax = res.data.stores
  247. }
  248. }
  249. // 计算配件价格
  250. if (this.data.goodsAddition) {
  251. this.data.goodsAddition.forEach(big => {
  252. big.items.forEach(small => {
  253. if (small.active) {
  254. price = (price*100 + small.price*100) / 100
  255. }
  256. })
  257. })
  258. }
  259. this.setData({
  260. selectSizePrice: price,
  261. selectSizeOPrice: originalPrice,
  262. totalScoreToPay: totalScoreToPay,
  263. buyNumMax,
  264. buyNumber: (buyNumMax >= buyNumber) ? buyNumber : 0
  265. });
  266. },
  267. /**
  268. * 选择可选配件
  269. */
  270. async labelItemTap2(e) {
  271. const propertyindex = e.currentTarget.dataset.propertyindex
  272. const propertychildindex = e.currentTarget.dataset.propertychildindex
  273. const goodsAddition = this.data.goodsAddition
  274. const property = goodsAddition[propertyindex]
  275. const child = property.items[propertychildindex]
  276. if (child.active) {
  277. // 该操作为取消选择
  278. child.active = false
  279. this.setData({
  280. goodsAddition
  281. })
  282. this.calculateGoodsPrice()
  283. return
  284. }
  285. // 单选配件取消所有子栏目选中状态
  286. if (property.type == 0) {
  287. property.items.forEach(child => {
  288. child.active = false
  289. })
  290. }
  291. // 设置当前选中状态
  292. child.active = true
  293. this.setData({
  294. goodsAddition
  295. })
  296. this.calculateGoodsPrice()
  297. },
  298. /**
  299. * 组建立即购买信息
  300. */
  301. buliduBuyNowInfo: function(shoptype) {
  302. var shopCarMap = {};
  303. shopCarMap.goodsId = this.data.goodsId;
  304. shopCarMap.pic = this.data.imageDomain + this.data.price.pic;
  305. shopCarMap.name = this.data.price.skuName;
  306. shopCarMap.price = this.data.price.priceSale;
  307. shopCarMap.left = "";
  308. shopCarMap.active = true;
  309. shopCarMap.number = this.data.buyNumber;
  310. var buyNowInfo = {};
  311. buyNowInfo.shopNum = 0;
  312. buyNowInfo.shopList = [shopCarMap];
  313. return buyNowInfo;
  314. },
  315. onShareAppMessage() {
  316. let _data = {
  317. title: this.data.goodsDetail.basicInfo.name,
  318. path: '/packageCps/pages/goods-details/cps-taobao?id=' + this.data.goodsId + '&inviter_id=' + wx.getStorageSync('uid'),
  319. success: function(res) {
  320. // 转发成功
  321. },
  322. fail: function(res) {
  323. // 转发失败
  324. }
  325. }
  326. if (this.data.kjJoinUid) {
  327. _data.title = this.data.curKanjiaprogress.joiner.nick + '邀请您帮TA砍价'
  328. _data.path += '&kjJoinUid=' + this.data.kjJoinUid
  329. }
  330. return _data
  331. },
  332. reputation: function(goodsId) {
  333. var that = this;
  334. WXAPI.goodsReputation({
  335. goodsId: goodsId
  336. }).then(function(res) {
  337. if (res.code == 0) {
  338. res.data.forEach(ele => {
  339. if (ele.goods.goodReputation == 0) {
  340. ele.goods.goodReputation = 1
  341. } else if (ele.goods.goodReputation == 1) {
  342. ele.goods.goodReputation = 3
  343. } else if (ele.goods.goodReputation == 2) {
  344. ele.goods.goodReputation = 5
  345. }
  346. })
  347. that.setData({
  348. reputation: res.data
  349. });
  350. }
  351. })
  352. },
  353. pingtuanList: function(goodsId) {
  354. var that = this;
  355. WXAPI.pingtuanList({
  356. goodsId: goodsId,
  357. status: 0
  358. }).then(function(res) {
  359. if (res.code == 0) {
  360. that.setData({
  361. pingtuanList: res.data.result
  362. });
  363. }
  364. })
  365. },
  366. getVideoSrc: function(videoId) {
  367. var that = this;
  368. WXAPI.videoDetail(videoId).then(function(res) {
  369. if (res.code == 0) {
  370. that.setData({
  371. videoMp4Src: res.data.fdMp4
  372. });
  373. }
  374. })
  375. },
  376. joinKanjia(){
  377. AUTH.checkHasLogined().then(isLogined => {
  378. if (isLogined) {
  379. this.doneJoinKanjia();
  380. } else {
  381. this.setData({
  382. wxlogin: false
  383. })
  384. }
  385. })
  386. },
  387. doneJoinKanjia: function() { // 报名参加砍价活动
  388. const _this = this;
  389. if (!_this.data.curGoodsKanjia) {
  390. return;
  391. }
  392. wx.showLoading({
  393. title: '加载中',
  394. mask: true
  395. })
  396. WXAPI.kanjiaJoin(wx.getStorageSync('token'), _this.data.curGoodsKanjia.id).then(function(res) {
  397. wx.hideLoading()
  398. if (res.code == 0) {
  399. _this.setData({
  400. kjJoinUid: wx.getStorageSync('uid'),
  401. myHelpDetail: null
  402. })
  403. _this.getGoodsDetailAndKanjieInfo(_this.data.goodsDetail.basicInfo.id)
  404. } else {
  405. wx.showToast({
  406. title: res.msg,
  407. icon: 'none'
  408. })
  409. }
  410. })
  411. },
  412. joinPingtuan: function(e) {
  413. let pingtuanopenid = e.currentTarget.dataset.pingtuanopenid
  414. wx.navigateTo({
  415. url: "/pages/to-pay-order/index?orderType=buyNow&pingtuanOpenId=" + pingtuanopenid
  416. })
  417. },
  418. goIndex() {
  419. wx.switchTab({
  420. url: '/pages/index/index',
  421. });
  422. },
  423. helpKanjia() {
  424. const _this = this;
  425. AUTH.checkHasLogined().then(isLogined => {
  426. _this.setData({
  427. wxlogin: isLogined
  428. })
  429. if (isLogined) {
  430. _this.helpKanjiaDone()
  431. }
  432. })
  433. },
  434. helpKanjiaDone(){
  435. const _this = this;
  436. WXAPI.kanjiaHelp(wx.getStorageSync('token'), _this.data.kjId, _this.data.kjJoinUid, '').then(function (res) {
  437. if (res.code != 0) {
  438. wx.showToast({
  439. title: res.msg,
  440. icon: 'none'
  441. })
  442. return;
  443. }
  444. _this.setData({
  445. myHelpDetail: res.data
  446. });
  447. wx.showModal({
  448. title: '成功',
  449. content: '成功帮TA砍掉 ' + res.data.cutPrice + ' 元',
  450. showCancel: false
  451. })
  452. _this.getGoodsDetailAndKanjieInfo(_this.data.goodsDetail.basicInfo.id)
  453. })
  454. },
  455. cancelLogin() {
  456. this.setData({
  457. wxlogin: true
  458. })
  459. },
  460. closePop(){
  461. this.setData({
  462. posterShow: false
  463. })
  464. },
  465. async drawSharePic() {
  466. const _this = this
  467. const qrcodeRes = await WXAPI.wxaQrcode({
  468. scene: _this.data.goodsId + ',' + wx.getStorageSync('uid'),
  469. page: '/packageCps/pages/goods-details/cps-taobao',
  470. is_hyaline: true,
  471. autoColor: true,
  472. expireHours: 1
  473. })
  474. if (qrcodeRes.code != 0) {
  475. wx.showToast({
  476. title: qrcodeRes.msg,
  477. icon: 'none'
  478. })
  479. return
  480. }
  481. const qrcode = qrcodeRes.data
  482. const pic = _this.data.goodsDetail.basicInfo.pic
  483. wx.getImageInfo({
  484. src: pic,
  485. success(res) {
  486. const height = 490 * res.height / res.width
  487. _this.drawSharePicDone(height, qrcode)
  488. },
  489. fail(e) {
  490. console.error(e)
  491. }
  492. })
  493. },
  494. drawSharePicDone(picHeight, qrcode) {
  495. const _this = this
  496. const _baseHeight = 74 + (picHeight + 120)
  497. this.setData({
  498. posterConfig: {
  499. width: 750,
  500. height: picHeight + 660,
  501. backgroundColor: '#fff',
  502. debug: false,
  503. blocks: [
  504. {
  505. x: 76,
  506. y: 74,
  507. width: 604,
  508. height: picHeight + 120,
  509. borderWidth: 2,
  510. borderColor: '#c2aa85',
  511. borderRadius: 8
  512. }
  513. ],
  514. images: [
  515. {
  516. x: 133,
  517. y: 133,
  518. url: _this.data.goodsDetail.basicInfo.pic, // 商品图片
  519. width: 490,
  520. height: picHeight
  521. },
  522. {
  523. x: 76,
  524. y: _baseHeight + 199,
  525. url: qrcode, // 二维码
  526. width: 222,
  527. height: 222
  528. }
  529. ],
  530. texts: [
  531. {
  532. x: 375,
  533. y: _baseHeight + 80,
  534. width: 650,
  535. lineNum:2,
  536. text: _this.data.goodsDetail.basicInfo.name,
  537. textAlign: 'center',
  538. fontSize: 40,
  539. color: '#333'
  540. },
  541. {
  542. x: 375,
  543. y: _baseHeight + 180,
  544. text: '¥' + _this.data.goodsDetail.basicInfo.minPrice,
  545. textAlign: 'center',
  546. fontSize: 50,
  547. color: '#e64340'
  548. },
  549. {
  550. x: 352,
  551. y: _baseHeight + 320,
  552. text: '长按识别小程序码',
  553. fontSize: 28,
  554. color: '#999'
  555. }
  556. ],
  557. }
  558. }, () => {
  559. Poster.create();
  560. });
  561. },
  562. onPosterSuccess(e) {
  563. console.log('success:', e)
  564. this.setData({
  565. posterImg: e.detail,
  566. showposterImg: true
  567. })
  568. },
  569. onPosterFail(e) {
  570. console.error('fail:', e)
  571. },
  572. savePosterPic() {
  573. const _this = this
  574. wx.saveImageToPhotosAlbum({
  575. filePath: this.data.posterImg,
  576. success: (res) => {
  577. wx.showModal({
  578. content: '已保存到手机相册',
  579. showCancel: false,
  580. confirmText: '知道了',
  581. confirmColor: '#333'
  582. })
  583. },
  584. complete: () => {
  585. _this.setData({
  586. showposterImg: false
  587. })
  588. },
  589. fail: (res) => {
  590. wx.showToast({
  591. title: res.errMsg,
  592. icon: 'none',
  593. duration: 2000
  594. })
  595. }
  596. })
  597. },
  598. previewImage(e) {
  599. const url = e.currentTarget.dataset.url
  600. wx.previewImage({
  601. current: url, // 当前显示图片的http链接
  602. urls: [url] // 需要预览的图片http链接列表
  603. })
  604. },
  605. onTabsChange(e) {
  606. var index = e.detail.index
  607. this.setData({
  608. toView: this.data.tabs[index].view_id
  609. })
  610. },
  611. backToHome() {
  612. wx.switchTab({
  613. url: '/pages/index/index',
  614. })
  615. },
  616. async copyKouling() {
  617. const res = await WXAPI.cpsTaobaoGoodsKouling(wx.getStorageSync('token'), this.data.goodsDetail.content)
  618. if (res.code != 0) {
  619. wx.showToast({
  620. title: res.msg,
  621. icon: 'none'
  622. })
  623. return
  624. }
  625. wx.setClipboardData({
  626. data: res.data.model,
  627. success: () => {
  628. wx.showToast({
  629. title: '淘口令已复制'
  630. })
  631. }
  632. })
  633. },
  634. async copyLink() {
  635. const res = await WXAPI.cpsTaobaoGoodsShotUrl(wx.getStorageSync('token'), this.data.goodsDetail.content)
  636. if (res.code != 0) {
  637. wx.showToast({
  638. title: res.msg,
  639. icon: 'none'
  640. })
  641. return
  642. }
  643. wx.setClipboardData({
  644. data: res.data[0].content,
  645. success: () => {
  646. wx.showToast({
  647. title: '链接已复制'
  648. })
  649. }
  650. })
  651. }
  652. })