prod.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. // pages/prod/prod.js
  2. const app = getApp()
  3. var http = require('../../utils/http.js');
  4. var config = require('../../utils/config.js');
  5. var util = require('../../utils/util.js');
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. shopId: 1,
  12. picDomain: config.picDomain,
  13. indicatorDots: true,
  14. indicatorColor: '#f2f2f2',
  15. indicatorActiveColor: '#eb2444',
  16. autoplay: true,
  17. interval: 3000,
  18. duration: 1000,
  19. prodNum: 1,
  20. totalCartNum: 0,
  21. pic: "",
  22. imgs: '',
  23. prodName: '',
  24. price: 0,
  25. content: '',
  26. prodId: 0,
  27. brief: '',
  28. skuId: 0,
  29. popupShow: false,
  30. // 是否获取过用户领取过的优惠券id
  31. loadCouponIds: false,
  32. skuShow: false,
  33. commentShow: false,
  34. couponList: [],
  35. skuList: [],
  36. skuGroup: {},
  37. findSku: true,
  38. defaultSku: undefined,
  39. selectedProp: [],
  40. selectedPropObj: {},
  41. propKeys: [],
  42. allProperties: [],
  43. prodCommData: {},
  44. prodCommPage: {
  45. current: 0,
  46. pages: 0,
  47. records: []
  48. },
  49. littleCommPage: [],
  50. evaluate: -1,
  51. isCollection: false
  52. },
  53. /**
  54. * 生命周期函数--监听页面加载
  55. */
  56. onLoad: function(options) {
  57. this.setData({
  58. prodId: options.prodid,
  59. });
  60. // 加载商品信息
  61. this.getProdInfo();
  62. // 加载评论数据
  63. this.getProdCommData();
  64. // 加载评论项
  65. this.getLittleProdComm();
  66. // 查看用户是否关注
  67. this.getCollection();
  68. },
  69. /**
  70. * 获取是否关注信息
  71. */
  72. getCollection() {
  73. wx.showLoading();
  74. var params = {
  75. url: "/p/user/collection/isCollection",
  76. method: "GET",
  77. data: {
  78. prodId: this.data.prodId
  79. },
  80. callBack: (res) => {
  81. this.setData({
  82. isCollection: res
  83. })
  84. wx.hideLoading();
  85. }
  86. };
  87. http.request(params);
  88. },
  89. /**
  90. * 添加或者取消收藏商品
  91. */
  92. addOrCannelCollection() {
  93. wx.showLoading();
  94. var params = {
  95. url: "/p/user/collection/addOrCancel",
  96. method: "POST",
  97. data: this.data.prodId,
  98. callBack: (res) => {
  99. this.setData({
  100. isCollection: !this.data.isCollection
  101. })
  102. wx.hideLoading();
  103. }
  104. };
  105. http.request(params);
  106. },
  107. // 获取商品信息
  108. getProdInfo() {
  109. wx.showLoading();
  110. var params = {
  111. url: "/prod/prodInfo",
  112. method: "GET",
  113. data: {
  114. prodId: this.data.prodId,
  115. // userType: 0
  116. },
  117. callBack: (res) => {
  118. //console.log(res);
  119. var imgStrs = res.imgs;
  120. var imgs = imgStrs.split(",");
  121. var content = util.formatHtml(res.content);
  122. this.setData({
  123. imgs: imgs,
  124. content: content,
  125. price: res.price,
  126. prodName: res.prodName,
  127. prodId: res.prodId,
  128. brief: res.brief,
  129. // skuId: res.skuId
  130. skuList: res.skuList,
  131. pic: res.pic
  132. });
  133. // 获取优惠券
  134. //this.getCouponList();
  135. // 组装sku
  136. this.groupSkuProp();
  137. wx.hideLoading();
  138. }
  139. };
  140. http.request(params);
  141. },
  142. getProdCommData() {
  143. http.request({
  144. url: "/prodComm/prodCommData",
  145. method: "GET",
  146. data: {
  147. prodId: this.data.prodId,
  148. },
  149. callBack: (res) => {
  150. this.setData({
  151. prodCommData: res
  152. })
  153. }
  154. })
  155. },
  156. // 获取部分评论
  157. getLittleProdComm() {
  158. if (this.data.prodCommPage.records.length) {
  159. return;
  160. }
  161. this.getProdCommPage();
  162. },
  163. getMoreCommPage(e) {
  164. this.getProdCommPage();
  165. },
  166. // 获取分页获取评论
  167. getProdCommPage(e) {
  168. if (e) {
  169. if (e.currentTarget.dataset.evaluate === this.data.evaluate) {
  170. return;
  171. }
  172. this.setData({
  173. prodCommPage: {
  174. current: 0,
  175. pages: 0,
  176. records: []
  177. },
  178. evaluate: e.currentTarget.dataset.evaluate
  179. })
  180. }
  181. http.request({
  182. url: "/prodComm/prodCommPageByProd",
  183. method: "GET",
  184. data: {
  185. prodId: this.data.prodId,
  186. size: 10,
  187. current: this.data.prodCommPage.current + 1,
  188. evaluate: this.data.evaluate
  189. },
  190. callBack: (res) => {
  191. res.records.forEach(item => {
  192. if (item.pics) {
  193. item.pics = item.pics.split(',')
  194. }
  195. })
  196. let records = this.data.prodCommPage.records
  197. records = records.concat(res.records)
  198. this.setData({
  199. prodCommPage: {
  200. current: res.current,
  201. pages: res.pages,
  202. records: records
  203. }
  204. })
  205. // 如果商品详情中没有评论的数据,截取两条到商品详情页商品详情
  206. if (!this.data.littleCommPage.length) {
  207. this.setData({
  208. littleCommPage: records.slice(0, 2)
  209. })
  210. }
  211. }
  212. })
  213. },
  214. getCouponList() {
  215. http.request({
  216. url: "/coupon/listByProdId",
  217. method: "GET",
  218. data: {
  219. prodId: this.data.prodId,
  220. shopId: this.data.shopId,
  221. },
  222. callBack: (res) => {
  223. this.setData({
  224. couponList: res
  225. })
  226. }
  227. })
  228. },
  229. /**
  230. * 根据skuList进行数据组装
  231. */
  232. groupSkuProp: function() {
  233. var skuList = this.data.skuList;
  234. //当后台返回只有一个SKU时,且SKU属性值为空时,即该商品没有规格选项,该SKU直接作为默认选中SKU
  235. if (skuList.length == 1 && skuList[0].properties == "") {
  236. this.setData({
  237. defaultSku: skuList[0]
  238. });
  239. return;
  240. }
  241. var skuGroup = {};//所有的规格名(包含规格名下的规格值集合)对象,如 {"颜色":["金色","银色"],"内存":["64G","256G"]}
  242. var allProperties = [];//所有SKU的属性值集合,如 ["颜色:金色;内存:64GB","颜色:银色;内存:64GB"]
  243. var propKeys = [];//所有的规格名集合,如 ["颜色","内存"]
  244. for (var i = 0; i < skuList.length; i++) {
  245. //找到和商品价格一样的那个SKU,作为默认选中的SKU
  246. var defaultSku = this.data.defaultSku;
  247. var isDefault = false;
  248. if (!defaultSku && skuList[i].price == this.data.price) {
  249. defaultSku = skuList[i];
  250. isDefault = true;
  251. this.setData({
  252. defaultSku: defaultSku
  253. });
  254. }
  255. var properties = skuList[i].properties; //如:版本:公开版;颜色:金色;内存:64GB
  256. allProperties.push(properties);
  257. var propList = properties.split(";"); // 如:["版本:公开版","颜色:金色","内存:64GB"]
  258. var selectedPropObj = this.data.selectedPropObj;
  259. for (var j = 0; j < propList.length; j++) {
  260. var propval = propList[j].split(":"); //如 ["版本","公开版"]
  261. var props = skuGroup[propval[0]]; //先取出 规格名 对应的规格值数组
  262. //如果当前是默认选中的sku,把对应的属性值 组装到selectedProp
  263. if (isDefault) {
  264. propKeys.push(propval[0]);
  265. selectedPropObj[propval[0]] = propval[1];
  266. }
  267. if (props == undefined) {
  268. props = []; //假设还没有版本,新建个新的空数组
  269. props.push(propval[1]); //把 "公开版" 放进空数组
  270. } else {
  271. if (!this.array_contain(props, propval[1])) { //如果数组里面没有"公开版"
  272. props.push(propval[1]); //把 "公开版" 放进数组
  273. }
  274. }
  275. skuGroup[propval[0]] = props; //最后把数据 放回版本对应的值
  276. }
  277. this.setData({
  278. selectedPropObj: selectedPropObj,
  279. propKeys: propKeys
  280. });
  281. }
  282. this.parseSelectedObjToVals();
  283. this.setData({
  284. skuGroup: skuGroup,
  285. allProperties: allProperties
  286. });
  287. },
  288. //将已选的 {key:val,key2:val2}转换成 [val,val2]
  289. parseSelectedObjToVals: function() {
  290. var selectedPropObj = this.data.selectedPropObj;
  291. var selectedProperties = "";
  292. var selectedProp = [];
  293. for (var key in selectedPropObj) {
  294. selectedProp.push(selectedPropObj[key]);
  295. selectedProperties += key + ":" + selectedPropObj[key] + ";";
  296. }
  297. selectedProperties = selectedProperties.substring(0, selectedProperties.length - 1);
  298. this.setData({
  299. selectedProp: selectedProp
  300. });
  301. var findSku = false;
  302. for (var i = 0; i < this.data.skuList.length; i++) {
  303. if (this.data.skuList[i].properties == selectedProperties) {
  304. findSku = true;
  305. this.setData({
  306. defaultSku: this.data.skuList[i],
  307. });
  308. break;
  309. }
  310. }
  311. this.setData({
  312. findSku: findSku
  313. });
  314. },
  315. //点击选择规格
  316. toChooseItem: function(e) {
  317. var val = e.currentTarget.dataset.val;
  318. var key = e.currentTarget.dataset.key;
  319. var selectedPropObj = this.data.selectedPropObj;
  320. selectedPropObj[key] = val;
  321. this.setData({
  322. selectedPropObj: selectedPropObj
  323. });
  324. this.parseSelectedObjToVals();
  325. },
  326. //判断数组是否包含某对象
  327. array_contain: function(array, obj) {
  328. for (var i = 0; i < array.length; i++) {
  329. if (array[i] == obj) //如果要求数据类型也一致,这里可使用恒等号===
  330. return true;
  331. }
  332. return false;
  333. },
  334. /**
  335. * 生命周期函数--监听页面初次渲染完成
  336. */
  337. onReady: function() {
  338. },
  339. /**
  340. * 生命周期函数--监听页面显示
  341. */
  342. onShow: function() {
  343. this.setData({
  344. totalCartNum: app.globalData.totalCartCount
  345. });
  346. },
  347. /**
  348. * 生命周期函数--监听页面隐藏
  349. */
  350. onHide: function() {
  351. },
  352. /**
  353. * 生命周期函数--监听页面卸载
  354. */
  355. onUnload: function() {
  356. },
  357. /**
  358. * 页面相关事件处理函数--监听用户下拉动作
  359. */
  360. onPullDownRefresh: function() {
  361. },
  362. /**
  363. * 页面上拉触底事件的处理函数
  364. */
  365. onReachBottom: function() {
  366. },
  367. /**
  368. * 用户点击右上角分享
  369. */
  370. onShareAppMessage: function() {
  371. },
  372. /**
  373. * 跳转到首页
  374. */
  375. toHomePage: function() {
  376. wx.switchTab({
  377. url: '/pages/index/index',
  378. })
  379. },
  380. /**
  381. * 跳转到购物车
  382. */
  383. toCartPage: function() {
  384. wx.switchTab({
  385. url: '/pages/basket/basket',
  386. })
  387. },
  388. /**
  389. * 加入购物车
  390. */
  391. addToCart: function(event) {
  392. if (!this.data.findSku) {
  393. return;
  394. }
  395. var ths = this;
  396. wx.showLoading({
  397. mask: true
  398. });
  399. var params = {
  400. url: "/p/shopCart/changeItem",
  401. method: "POST",
  402. data: {
  403. basketId: 0,
  404. count: this.data.prodNum,
  405. prodId: this.data.prodId,
  406. shopId: this.data.shopId,
  407. skuId: this.data.defaultSku.skuId
  408. },
  409. callBack: function(res) {
  410. //console.log(res);
  411. ths.setData({
  412. totalCartNum: ths.data.totalCartNum + ths.data.prodNum
  413. });
  414. wx.hideLoading();
  415. wx.showToast({
  416. title: "加入购物车成功",
  417. icon: "none"
  418. })
  419. }
  420. };
  421. http.request(params);
  422. },
  423. /**
  424. * 立即购买
  425. */
  426. buyNow: function() {
  427. if (!this.data.findSku) {
  428. return;
  429. }
  430. wx.setStorageSync("orderItem", JSON.stringify({
  431. prodId: this.data.prodId,
  432. skuId: this.data.defaultSku.skuId,
  433. prodCount: this.data.prodNum,
  434. shopId: this.data.shopId
  435. }));
  436. wx.navigateTo({
  437. url: '/pages/submit-order/submit-order?orderEntry=1',
  438. })
  439. },
  440. /**
  441. * 减数量
  442. */
  443. onCountMinus: function() {
  444. var prodNum = this.data.prodNum;
  445. if (prodNum > 1) {
  446. this.setData({
  447. prodNum: prodNum - 1
  448. });
  449. }
  450. },
  451. /**
  452. * 加数量
  453. */
  454. onCountPlus: function() {
  455. var prodNum = this.data.prodNum;
  456. if (prodNum < 1000) {
  457. this.setData({
  458. prodNum: prodNum + 1
  459. });
  460. }
  461. },
  462. /**
  463. * 分享设置
  464. */
  465. onShareAppMessage: function(res) {
  466. return {
  467. title: this.data.prodName,
  468. path: '/pages/prod/prod?prodid=' + this.data.prodid
  469. }
  470. },
  471. showPopup: function() {
  472. if (this.data.loadCouponIds) {
  473. this.setData({
  474. popupShow: true
  475. });
  476. return;
  477. }
  478. http.request({
  479. url: "/p/myCoupon/listCouponIds",
  480. method: "GET",
  481. data: {},
  482. callBack: (couponIds) => {
  483. var couponList = this.data.couponList;
  484. console.log(couponList)
  485. couponList.forEach(coupon => {
  486. if (couponIds && couponIds.length) {
  487. // 领取该优惠券数量
  488. var couponLimit = 0;
  489. couponIds.forEach(couponId => {
  490. if (couponId == coupon.couponId) {
  491. couponLimit++;
  492. }
  493. });
  494. // 小于用户领取优惠券上限,可以领取优惠券
  495. if (couponLimit < coupon.limitNum) {
  496. coupon.canReceive = true;
  497. } else {
  498. coupon.canReceive = false;
  499. }
  500. } else {
  501. coupon.canReceive = true;
  502. }
  503. });
  504. this.setData({
  505. couponList: couponList,
  506. popupShow: true,
  507. loadCouponIds: true
  508. })
  509. }
  510. })
  511. },
  512. showSku: function() {
  513. this.setData({
  514. skuShow: true
  515. });
  516. },
  517. showComment: function() {
  518. this.setData({
  519. commentShow: true
  520. });
  521. },
  522. closePopup: function() {
  523. this.setData({
  524. popupShow: false,
  525. skuShow: false,
  526. commentShow: false
  527. });
  528. },
  529. })