spellGoodsDetails.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // pages/snapUpDetail/snapUpDetail.js
  2. var http = require('../../utils/http.js');
  3. var config = require('../../utils/config.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. hideModal: true,
  10. prodCommData: {},
  11. littleCommPage: [],
  12. evaluate: -1,
  13. isCollection: false,
  14. prodCommPage: {
  15. current: 0,
  16. pages: 0,
  17. records: []
  18. },
  19. imgs: '', //商品图片
  20. /** swiper */
  21. indicatorDots: true,
  22. autoplay: true,
  23. indicatorColor: '#f2f2f2',
  24. interval: 3000,
  25. duration: 1000,
  26. indicatorActiveColor: '#eb2444',
  27. /** swiper */
  28. content: '', //商品详情介绍
  29. skuShow: false, //sku选择
  30. popupShow: false,
  31. commentShow: false,
  32. defaultSku: undefined,
  33. skuGroup: {},
  34. allProperties: [],
  35. prodNum: 1, //数量+-
  36. skuId: 0,
  37. prodId: 0,
  38. totalCartNum: 0,
  39. pic: "",
  40. imgs: '',
  41. prodName: '',
  42. price: 0,
  43. content: '',
  44. brief: '',
  45. picDomain: config.picDomain,
  46. groupSkuId:'',
  47. groupTeamId: 0
  48. },
  49. /**
  50. * 生命周期函数--监听页面加载
  51. */
  52. onLoad: function (options) {
  53. console.log(options);
  54. this.setData({
  55. prodId: options.prodid,
  56. });
  57. if (options.scene) {
  58. // console.log(options.scene)
  59. const scene = decodeURIComponent(options.scene);
  60. var params = scene.split(",");
  61. this.setData({
  62. prodId: params[0],
  63. });
  64. }
  65. // 加载商品信息
  66. this.getProdInfo();
  67. // 加载评论项
  68. this.getLittleProdComm();
  69. },
  70. /**
  71. * 获取商品信息
  72. */
  73. getProdInfo() {
  74. wx.showLoading();
  75. var params = {
  76. url: "/prod/prodInfo",
  77. method: "GET",
  78. data: {
  79. prodId: this.data.prodId,
  80. // userType: 0
  81. },
  82. callBack: (res) => {
  83. // console.log(res);
  84. var imgStrs = res.imgs;
  85. var imgs = imgStrs.split(",");
  86. var content = util.formatHtml(res.content);
  87. this.setData({
  88. imgs: imgs,
  89. content: content,
  90. price: res.price,
  91. prodName: res.prodName,
  92. prodId: res.prodId,
  93. brief: res.brief,
  94. // skuId: res.skuId
  95. skuList: res.skuList,
  96. pic: res.pic
  97. });
  98. // 获取优惠券
  99. this.getCouponList();
  100. // 组装sku
  101. this.groupSkuProp();
  102. wx.hideLoading();
  103. }
  104. };
  105. http.request(params);
  106. },
  107. showSku: function () {
  108. this.setData({
  109. skuShow: true
  110. });
  111. },
  112. /**
  113. * 根据sku的属性 分组
  114. */
  115. groupSkuProp: function () {
  116. var skuList = this.data.skuList;
  117. if (skuList.length == 1 && skuList[0].properties == "") {
  118. this.setData({
  119. defaultSku: skuList[0]
  120. });
  121. return;
  122. }
  123. var skuGroup = {};
  124. var allProperties = [];
  125. var propKeys = [];
  126. for (var i = 0; i < skuList.length; i++) {
  127. var defaultSku = this.data.defaultSku;
  128. var isDefault = false;
  129. if (!defaultSku && skuList[i].price == this.data.price) { //找到和商品价格一样的那个SKU,作为默认选中的SKU
  130. defaultSku = skuList[i];
  131. isDefault = true;
  132. this.setData({
  133. defaultSku: defaultSku
  134. });
  135. }
  136. var properties = skuList[i].properties; //版本:公开版;颜色:金色;内存:64GB
  137. allProperties.push(properties);
  138. var propList = properties.split(";"); // ["版本:公开版","颜色:金色","内存:64GB"]
  139. var selectedPropObj = this.data.selectedPropObj;
  140. for (var j = 0; j < propList.length; j++) {
  141. var propval = propList[j].split(":"); //["版本","公开版"]
  142. var props = skuGroup[propval[0]]; //先取出 版本对应的值数组
  143. //如果当前是默认选中的sku,把对应的属性值 组装到selectedProp
  144. if (isDefault) {
  145. propKeys.push(propval[0]);
  146. selectedPropObj[propval[0]] = propval[1];
  147. }
  148. if (props == undefined) {
  149. props = []; //假设还没有版本,新建个新的空数组
  150. props.push(propval[1]); //把 "公开版" 放进空数组
  151. } else {
  152. if (!this.array_contain(props, propval[1])) { //如果数组里面没有"公开版"
  153. props.push(propval[1]); //把 "公开版" 放进数组
  154. }
  155. }
  156. skuGroup[propval[0]] = props; //最后把数据 放回版本对应的值
  157. }
  158. this.setData({
  159. selectedPropObj: selectedPropObj,
  160. propKeys: propKeys
  161. });
  162. }
  163. this.parseSelectedObjToVals();
  164. this.setData({
  165. skuGroup: skuGroup,
  166. allProperties: allProperties
  167. });
  168. },
  169. /**
  170. * 将已选的 {key:val,key2:val2}转换成 [val,val2]
  171. */
  172. parseSelectedObjToVals: function () {
  173. var selectedPropObj = this.data.selectedPropObj;
  174. var selectedProperties = "";
  175. var selectedProp = [];
  176. for (var key in selectedPropObj) {
  177. selectedProp.push(selectedPropObj[key]);
  178. selectedProperties += key + ":" + selectedPropObj[key] + ";";
  179. }
  180. selectedProperties = selectedProperties.substring(0, selectedProperties.length - 1);
  181. // console.log(selectedProperties);
  182. this.setData({
  183. selectedProp: selectedProp
  184. });
  185. for (var i = 0; i < this.data.skuList.length; i++) {
  186. if (this.data.skuList[i].properties == selectedProperties) {
  187. this.setData({
  188. defaultSku: this.data.skuList[i]
  189. });
  190. break;
  191. }
  192. }
  193. },
  194. /**
  195. * 点击选择规格
  196. */
  197. toChooseItem: function (e) {
  198. var ok = e.currentTarget.dataset.ok;
  199. if (!ok) {
  200. return;
  201. }
  202. var val = e.currentTarget.dataset.val;
  203. var key = e.currentTarget.dataset.key;
  204. var selectedPropObj = this.data.selectedPropObj;
  205. selectedPropObj[key] = val;
  206. this.setData({
  207. selectedPropObj: selectedPropObj
  208. });
  209. this.parseSelectedObjToVals();
  210. },
  211. /**
  212. * 加数量
  213. */
  214. onCountPlus: function () {
  215. var prodNum = this.data.prodNum;
  216. if (prodNum < 1000) {
  217. this.setData({
  218. prodNum: prodNum + 1
  219. });
  220. }
  221. },
  222. /**
  223. * 减数量
  224. */
  225. onCountMinus: function () {
  226. var prodNum = this.data.prodNum;
  227. if (prodNum > 1) {
  228. this.setData({
  229. prodNum: prodNum - 1
  230. });
  231. }
  232. },
  233. /**
  234. * 关闭弹窗
  235. */
  236. closePopup: function () {
  237. this.setData({
  238. popupShow: false,
  239. skuShow: false,
  240. commentShow: false
  241. });
  242. },
  243. /**
  244. * 获取部分评论
  245. */
  246. getLittleProdComm() {
  247. if (this.data.prodCommPage.records.length) {
  248. return;
  249. }
  250. this.getProdCommPage();
  251. },
  252. getMoreCommPage(e) {
  253. this.getProdCommPage();
  254. },
  255. // 获取分页获取评论
  256. getProdCommPage(e) {
  257. if (e) {
  258. if (e.currentTarget.dataset.evaluate === this.data.evaluate) {
  259. return;
  260. }
  261. this.setData({
  262. prodCommPage: {
  263. current: 0,
  264. pages: 0,
  265. records: []
  266. },
  267. evaluate: e.currentTarget.dataset.evaluate
  268. })
  269. }
  270. http.request({
  271. url: "/p/prodComm/prodCommPageByProd",
  272. method: "GET",
  273. data: {
  274. prodId: this.data.prodId,
  275. size: 10,
  276. current: this.data.prodCommPage.current + 1,
  277. evaluate: this.data.evaluate
  278. },
  279. callBack: (res) => {
  280. res.records.forEach(item => {
  281. if (item.pics) {
  282. item.pics = item.pics.split(',')
  283. }
  284. })
  285. let records = this.data.prodCommPage.records
  286. records = records.concat(res.records)
  287. this.setData({
  288. prodCommPage: {
  289. current: res.current,
  290. pages: res.pages,
  291. records: records
  292. }
  293. })
  294. // 如果商品详情中没有评论的数据,截取两条到商品详情页商品详情
  295. if (!this.data.littleCommPage.length) {
  296. this.setData({
  297. littleCommPage: records.slice(0, 2)
  298. })
  299. }
  300. }
  301. })
  302. },
  303. showComment: function () {
  304. this.setData({
  305. commentShow: true
  306. });
  307. },
  308. /**
  309. * 立即购买
  310. */
  311. buyNow: function() {
  312. wx.setStorageSync("orderItem", JSON.stringify({
  313. prodId: this.data.prodId,
  314. skuId: this.data.defaultSku.skuId,
  315. prodCount: this.data.prodNum,
  316. shopId: this.data.shopId,
  317. distributionCardNo: this.data.distributionCardNo
  318. }));
  319. wx.navigateTo({
  320. url: '/pages/submit-order/submit-order?orderEntry=1',
  321. })
  322. },
  323. /**
  324. * 生命周期函数--监听页面初次渲染完成
  325. */
  326. onReady: function () {
  327. },
  328. /**
  329. * 生命周期函数--监听页面显示
  330. */
  331. onShow: function () {
  332. },
  333. /**
  334. * 生命周期函数--监听页面隐藏
  335. */
  336. onHide: function () {
  337. },
  338. /**
  339. * 生命周期函数--监听页面卸载
  340. */
  341. onUnload: function () {
  342. },
  343. /**
  344. * 页面相关事件处理函数--监听用户下拉动作
  345. */
  346. onPullDownRefresh: function () {
  347. },
  348. /**
  349. * 页面上拉触底事件的处理函数
  350. */
  351. onReachBottom: function () {
  352. },
  353. /**
  354. * 用户点击右上角分享
  355. */
  356. onShareAppMessage: function () {
  357. },
  358. /**
  359. * 显示遮罩层
  360. */
  361. showModal: function () {
  362. var that = this;
  363. that.setData({
  364. hideModal: false
  365. })
  366. var animation = wx.createAnimation({
  367. duration: 600,//动画的持续时间 默认400ms 数值越大,动画越慢 数值越小,动画越快
  368. timingFunction: 'ease',//动画的效果 默认值是linear
  369. })
  370. this.animation = animation
  371. setTimeout(function () {
  372. that.fadeIn();//调用显示动画
  373. }, 100)
  374. },
  375. /**
  376. * 隐藏遮罩层
  377. */
  378. hideModal: function () {
  379. var that = this;
  380. var animation = wx.createAnimation({
  381. duration: 800,//动画的持续时间 默认400ms 数值越大,动画越慢 数值越小,动画越快
  382. timingFunction: 'ease',//动画的效果 默认值是linear
  383. })
  384. this.animation = animation
  385. that.fadeDown();//调用隐藏动画
  386. setTimeout(function () {
  387. that.setData({ hideModal: true })
  388. }, 720)//先执行下滑动画,再隐藏模块
  389. },
  390. /**
  391. * 动画集
  392. */
  393. fadeIn: function () {
  394. this.animation.translateY(0).step()
  395. this.setData({
  396. animationData: this.animation.export()//动画实例的export方法导出动画数据传递给组件的animation属性
  397. })
  398. },
  399. fadeDown: function () {
  400. this.animation.translateY(300).step()
  401. this.setData({
  402. animationData: this.animation.export(),
  403. })
  404. },
  405. /**
  406. * 跳转到首页
  407. */
  408. toHomePage: function () {
  409. wx.switchTab({
  410. url: '/pages/index/index',
  411. })
  412. },
  413. /**
  414. * 跳转到购物车
  415. */
  416. toCartPage: function () {
  417. wx.switchTab({
  418. url: '/pages/basket/basket',
  419. })
  420. },
  421. /**
  422. * 查看拼团团队
  423. */
  424. toGroupTempInfoPage: function (e) {
  425. wx.navigateTo({
  426. url: '/pages/spellMembersDetails/spellMembersDetails?groupTeamId=' + e.currentTarget.dataset.groupTeamId,
  427. })
  428. },
  429. /**
  430. * 拼团确认订单
  431. */
  432. groupConfirmOrder: function(e){
  433. var ths = this;
  434. wx.showLoading({
  435. mask: true
  436. });
  437. // 查看是否授权
  438. wx.getSetting({
  439. success(res) {
  440. wx.hideLoading();
  441. if (res.authSetting['scope.userInfo']) {
  442. ths.setData({
  443. isAuthInfo: true
  444. });
  445. wx.setStorageSync("groupOrderItem", JSON.stringify({
  446. groupSkuId: 1,// this.data.groupSkuId,
  447. prodCount: this.data.prodNum,
  448. groupTeamId: 0 // this.data.groupTeamId
  449. }));
  450. wx.navigateTo({
  451. url: '/pages/groupConfirmOrder/groupConfirmOrder',
  452. })
  453. } else {
  454. ths.setData({
  455. isAuthInfo: false
  456. });
  457. }
  458. }
  459. })
  460. }
  461. })