cps-pdd.js 17 KB

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