prod.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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.getCollection();
  64. },
  65. /**
  66. * 获取是否关注信息
  67. */
  68. getCollection() {
  69. if (!wx.getStorageSync('token')) {
  70. return
  71. }
  72. wx.showLoading();
  73. var params = {
  74. url: "/p/user/collection/isCollection",
  75. method: "GET",
  76. data: {
  77. prodId: this.data.prodId
  78. },
  79. callBack: (res) => {
  80. this.setData({
  81. isCollection: res
  82. })
  83. wx.hideLoading();
  84. }
  85. };
  86. http.request(params);
  87. },
  88. /**
  89. * 添加或者取消收藏商品
  90. */
  91. addOrCannelCollection() {
  92. wx.showLoading();
  93. var params = {
  94. url: "/p/user/collection/addOrCancel",
  95. method: "POST",
  96. data: this.data.prodId,
  97. callBack: (res) => {
  98. this.setData({
  99. isCollection: !this.data.isCollection
  100. })
  101. wx.hideLoading();
  102. }
  103. };
  104. http.request(params);
  105. },
  106. // 获取商品信息
  107. getProdInfo() {
  108. wx.showLoading();
  109. var params = {
  110. url: "/prod/prodInfo",
  111. method: "GET",
  112. data: {
  113. prodId: this.data.prodId,
  114. // userType: 0
  115. },
  116. callBack: (res) => {
  117. //console.log(res);
  118. var imgStrs = res.imgs;
  119. var imgs = imgStrs.split(",");
  120. var content = util.formatHtml(res.content);
  121. this.setData({
  122. imgs: imgs,
  123. content: content,
  124. price: res.price,
  125. prodName: res.prodName,
  126. prodId: res.prodId,
  127. brief: res.brief,
  128. // skuId: res.skuId
  129. skuList: res.skuList,
  130. pic: res.pic
  131. });
  132. // 获取优惠券
  133. //this.getCouponList();
  134. // 组装sku
  135. this.groupSkuProp();
  136. wx.hideLoading();
  137. }
  138. };
  139. http.request(params);
  140. },
  141. /**
  142. * 根据skuList进行数据组装
  143. */
  144. groupSkuProp: function() {
  145. var skuList = this.data.skuList;
  146. //当后台返回只有一个SKU时,且SKU属性值为空时,即该商品没有规格选项,该SKU直接作为默认选中SKU
  147. if (skuList.length == 1 && skuList[0].properties == "") {
  148. this.setData({
  149. defaultSku: skuList[0]
  150. });
  151. return;
  152. }
  153. var skuGroup = {};//所有的规格名(包含规格名下的规格值集合)对象,如 {"颜色":["金色","银色"],"内存":["64G","256G"]}
  154. var allProperties = [];//所有SKU的属性值集合,如 ["颜色:金色;内存:64GB","颜色:银色;内存:64GB"]
  155. var propKeys = [];//所有的规格名集合,如 ["颜色","内存"]
  156. for (var i = 0; i < skuList.length; i++) {
  157. //找到和商品价格一样的那个SKU,作为默认选中的SKU
  158. var defaultSku = this.data.defaultSku;
  159. var isDefault = false;
  160. if (!defaultSku && skuList[i].price == this.data.price) {
  161. defaultSku = skuList[i];
  162. isDefault = true;
  163. this.setData({
  164. defaultSku: defaultSku
  165. });
  166. }
  167. var properties = skuList[i].properties; //如:版本:公开版;颜色:金色;内存:64GB
  168. allProperties.push(properties);
  169. var propList = properties.split(";"); // 如:["版本:公开版","颜色:金色","内存:64GB"]
  170. var selectedPropObj = this.data.selectedPropObj;
  171. for (var j = 0; j < propList.length; j++) {
  172. var propval = propList[j].split(":"); //如 ["版本","公开版"]
  173. var props = skuGroup[propval[0]]; //先取出 规格名 对应的规格值数组
  174. //如果当前是默认选中的sku,把对应的属性值 组装到selectedProp
  175. if (isDefault) {
  176. propKeys.push(propval[0]);
  177. selectedPropObj[propval[0]] = propval[1];
  178. }
  179. if (props == undefined) {
  180. props = []; //假设还没有版本,新建个新的空数组
  181. props.push(propval[1]); //把 "公开版" 放进空数组
  182. } else {
  183. if (!this.array_contain(props, propval[1])) { //如果数组里面没有"公开版"
  184. props.push(propval[1]); //把 "公开版" 放进数组
  185. }
  186. }
  187. skuGroup[propval[0]] = props; //最后把数据 放回版本对应的值
  188. }
  189. this.setData({
  190. selectedPropObj: selectedPropObj,
  191. propKeys: propKeys
  192. });
  193. }
  194. this.parseSelectedObjToVals();
  195. this.setData({
  196. skuGroup: skuGroup,
  197. allProperties: allProperties
  198. });
  199. },
  200. //将已选的 {key:val,key2:val2}转换成 [val,val2]
  201. parseSelectedObjToVals: function() {
  202. var selectedPropObj = this.data.selectedPropObj;
  203. var selectedProperties = "";
  204. var selectedProp = [];
  205. for (var key in selectedPropObj) {
  206. selectedProp.push(selectedPropObj[key]);
  207. selectedProperties += key + ":" + selectedPropObj[key] + ";";
  208. }
  209. selectedProperties = selectedProperties.substring(0, selectedProperties.length - 1);
  210. this.setData({
  211. selectedProp: selectedProp
  212. });
  213. var findSku = false;
  214. for (var i = 0; i < this.data.skuList.length; i++) {
  215. if (this.data.skuList[i].properties == selectedProperties) {
  216. findSku = true;
  217. this.setData({
  218. defaultSku: this.data.skuList[i],
  219. });
  220. break;
  221. }
  222. }
  223. this.setData({
  224. findSku: findSku
  225. });
  226. },
  227. //点击选择规格
  228. toChooseItem: function(e) {
  229. var val = e.currentTarget.dataset.val;
  230. var key = e.currentTarget.dataset.key;
  231. var selectedPropObj = this.data.selectedPropObj;
  232. selectedPropObj[key] = val;
  233. this.setData({
  234. selectedPropObj: selectedPropObj
  235. });
  236. this.parseSelectedObjToVals();
  237. },
  238. //判断数组是否包含某对象
  239. array_contain: function(array, obj) {
  240. for (var i = 0; i < array.length; i++) {
  241. if (array[i] == obj) //如果要求数据类型也一致,这里可使用恒等号===
  242. return true;
  243. }
  244. return false;
  245. },
  246. /**
  247. * 生命周期函数--监听页面初次渲染完成
  248. */
  249. onReady: function() {
  250. },
  251. /**
  252. * 生命周期函数--监听页面显示
  253. */
  254. onShow: function() {
  255. this.setData({
  256. totalCartNum: app.globalData.totalCartCount
  257. });
  258. },
  259. /**
  260. * 生命周期函数--监听页面隐藏
  261. */
  262. onHide: function() {
  263. },
  264. /**
  265. * 生命周期函数--监听页面卸载
  266. */
  267. onUnload: function() {
  268. },
  269. /**
  270. * 页面相关事件处理函数--监听用户下拉动作
  271. */
  272. onPullDownRefresh: function() {
  273. },
  274. /**
  275. * 页面上拉触底事件的处理函数
  276. */
  277. onReachBottom: function() {
  278. },
  279. /**
  280. * 用户点击右上角分享
  281. */
  282. onShareAppMessage: function() {
  283. },
  284. /**
  285. * 跳转到首页
  286. */
  287. toHomePage: function() {
  288. wx.switchTab({
  289. url: '/pages/index/index',
  290. })
  291. },
  292. /**
  293. * 跳转到购物车
  294. */
  295. toCartPage: function() {
  296. wx.switchTab({
  297. url: '/pages/basket/basket',
  298. })
  299. },
  300. /**
  301. * 加入购物车
  302. */
  303. addToCart: function(event) {
  304. if (!this.data.findSku) {
  305. return;
  306. }
  307. var ths = this;
  308. wx.showLoading({
  309. mask: true
  310. });
  311. var params = {
  312. url: "/p/shopCart/changeItem",
  313. method: "POST",
  314. data: {
  315. basketId: 0,
  316. count: this.data.prodNum,
  317. prodId: this.data.prodId,
  318. shopId: this.data.shopId,
  319. skuId: this.data.defaultSku.skuId
  320. },
  321. callBack: function(res) {
  322. //console.log(res);
  323. ths.setData({
  324. totalCartNum: ths.data.totalCartNum + ths.data.prodNum
  325. });
  326. wx.hideLoading();
  327. wx.showToast({
  328. title: "加入购物车成功",
  329. icon: "none"
  330. })
  331. }
  332. };
  333. http.request(params);
  334. },
  335. /**
  336. * 立即购买
  337. */
  338. buyNow: function() {
  339. if (!this.data.findSku) {
  340. return;
  341. }
  342. wx.setStorageSync("orderItem", JSON.stringify({
  343. prodId: this.data.prodId,
  344. skuId: this.data.defaultSku.skuId,
  345. prodCount: this.data.prodNum,
  346. shopId: this.data.shopId
  347. }));
  348. wx.navigateTo({
  349. url: '/pages/submit-order/submit-order?orderEntry=1',
  350. })
  351. },
  352. /**
  353. * 减数量
  354. */
  355. onCountMinus: function() {
  356. var prodNum = this.data.prodNum;
  357. if (prodNum > 1) {
  358. this.setData({
  359. prodNum: prodNum - 1
  360. });
  361. }
  362. },
  363. /**
  364. * 加数量
  365. */
  366. onCountPlus: function() {
  367. var prodNum = this.data.prodNum;
  368. if (prodNum < 1000) {
  369. this.setData({
  370. prodNum: prodNum + 1
  371. });
  372. }
  373. },
  374. /**
  375. * 分享设置
  376. */
  377. onShareAppMessage: function(res) {
  378. return {
  379. title: this.data.prodName,
  380. path: '/pages/prod/prod?prodid=' + this.data.prodid
  381. }
  382. },
  383. showPopup: function() {
  384. if (this.data.loadCouponIds) {
  385. this.setData({
  386. popupShow: true
  387. });
  388. return;
  389. }
  390. http.request({
  391. url: "/p/myCoupon/listCouponIds",
  392. method: "GET",
  393. data: {},
  394. callBack: (couponIds) => {
  395. var couponList = this.data.couponList;
  396. console.log(couponList)
  397. couponList.forEach(coupon => {
  398. if (couponIds && couponIds.length) {
  399. // 领取该优惠券数量
  400. var couponLimit = 0;
  401. couponIds.forEach(couponId => {
  402. if (couponId == coupon.couponId) {
  403. couponLimit++;
  404. }
  405. });
  406. // 小于用户领取优惠券上限,可以领取优惠券
  407. if (couponLimit < coupon.limitNum) {
  408. coupon.canReceive = true;
  409. } else {
  410. coupon.canReceive = false;
  411. }
  412. } else {
  413. coupon.canReceive = true;
  414. }
  415. });
  416. this.setData({
  417. couponList: couponList,
  418. popupShow: true,
  419. loadCouponIds: true
  420. })
  421. }
  422. })
  423. },
  424. showSku: function() {
  425. this.setData({
  426. skuShow: true
  427. });
  428. },
  429. showComment: function() {
  430. this.setData({
  431. commentShow: true
  432. });
  433. },
  434. closePopup: function() {
  435. this.setData({
  436. popupShow: false,
  437. skuShow: false,
  438. commentShow: false
  439. });
  440. },
  441. })