goods.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. var app = getApp();
  2. var WxParse = require('../../lib/wxParse/wxParse.js');
  3. var util = require('../../utils/util.js');
  4. var api = require('../../config/api.js');
  5. var goodsUtil = require('../../utils/goods.js');
  6. Page({
  7. data: {
  8. winHeight: "",
  9. id: 0,
  10. referrer: 0,
  11. goods: {},
  12. gallery: [],
  13. attribute: [],
  14. issueList: [],
  15. comment: [],
  16. brand: {},
  17. specificationList: [],
  18. productList: [],
  19. relatedGoods: [],
  20. cartGoodsCount: 0,
  21. userHasCollect: 0,
  22. crashList: {},
  23. number: 1,
  24. checkedSpecText: '请选择规格数量',
  25. openAttr: false,
  26. noCollectImage: "/static/images/icon_collect.png",
  27. hasCollectImage: "/static/images/icon_collect_checked.png",
  28. collectBackImage: "/static/images/icon_collect.png",
  29. showNavList: false,
  30. stockNum: 0,
  31. cartNumber: 0,
  32. defaultFreight: 0
  33. },
  34. toggleNav() {
  35. this.setData({
  36. showNavList: !this.data.showNavList
  37. })
  38. },
  39. switchNav(event) {
  40. let name = event.currentTarget.dataset.name;
  41. wx.switchTab({
  42. url: `/pages/${name}/${name}`,
  43. });
  44. },
  45. getGoodsInfo: function () {
  46. wx.showLoading({
  47. title: '加载中...',
  48. });
  49. let that = this;
  50. util.request(api.GoodsDetail, { id: that.data.id, referrer: this.data.referrer }).then(function (res) {
  51. if (res.errno === 0) {
  52. that.setData({
  53. goods: res.data.info,
  54. gallery: res.data.gallery,
  55. attribute: res.data.attribute,
  56. issueList: res.data.issue,
  57. comment: res.data.comment,
  58. brand: res.data.brand,
  59. specificationList: res.data.specificationList,
  60. productList: res.data.productList,
  61. userHasCollect: res.data.userHasCollect,
  62. stockNum: res.data.stockNum,
  63. cartNumber: res.data.cartNumber,
  64. defaultFreight: res.data.defaultFreight
  65. });
  66. wx.hideLoading();
  67. if (res.data.userHasCollect == 1) {
  68. that.setData({
  69. 'collectBackImage': that.data.hasCollectImage
  70. });
  71. } else {
  72. that.setData({
  73. 'collectBackImage': that.data.noCollectImage
  74. });
  75. }
  76. WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
  77. that.getGoodsRelated();
  78. that.getGoodsCrashList();
  79. //
  80. let _specificationList = that.data.specificationList;
  81. for (let i = 0; i < _specificationList.length; i++) {
  82. if (_specificationList[i].valueList.length == 1) {
  83. //如果已经选中,则反选
  84. _specificationList[i].valueList[0].checked = false;
  85. }
  86. }
  87. that.setData({
  88. 'specificationList': _specificationList
  89. });
  90. }
  91. });
  92. if (null != that.data.referrer && that.data.referrer > 0) {
  93. util.request(api.GoodsTransferCoupon, {
  94. goods_id: that.data.id,
  95. referrer: that.data.referrer, send_type: 2
  96. }).then(function (res) {
  97. });
  98. }
  99. util.request(api.GoodsGallery, {
  100. goods_id: that.data.id
  101. }).then(function (res) {
  102. if (res.errno === 0) {
  103. that.setData({
  104. gallery: res.data
  105. })
  106. }
  107. ;
  108. });
  109. },
  110. getGoodsCrashList: function () {
  111. let that = this;
  112. util.request(api.GoodsCrashList, { goodsId: that.data.id }).then(function (res) {
  113. if (res.errno === 0) {
  114. that.setData({
  115. crashList: res.data,
  116. });
  117. that.data.crashList.length;
  118. }
  119. });
  120. },
  121. getGoodsRelated: function () {
  122. let that = this;
  123. util.request(api.GoodsRelated, { id: that.data.id }).then(function (res) {
  124. if (res.errno === 0) {
  125. that.setData({
  126. relatedGoods: res.data.goodsList,
  127. });
  128. }
  129. });
  130. },
  131. clickSkuValue: function (event) {
  132. let that = this;
  133. let specNameId = event.currentTarget.dataset.nameId;
  134. let specValueId = event.currentTarget.dataset.valueId;
  135. //
  136. let _specificationList = this.data.specificationList;
  137. for (let i = 0; i < _specificationList.length; i++) {
  138. if (_specificationList[i].specification_id == specNameId) {
  139. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  140. if (_specificationList[i].valueList[j].id == specValueId) {
  141. //如果已经选中,则反选
  142. if (_specificationList[i].valueList[j].checked) {
  143. _specificationList[i].valueList[j].checked = false;
  144. } else {
  145. _specificationList[i].valueList[j].checked = true;
  146. }
  147. } else {
  148. _specificationList[i].valueList[j].checked = false;
  149. }
  150. }
  151. }
  152. }
  153. this.setData({
  154. 'specificationList': _specificationList
  155. });
  156. //重新计算spec改变后的信息
  157. goodsUtil.changeSpecInfo(that);
  158. },
  159. onLoad: function (options) {
  160. // 页面初始化 options为页面跳转所带来的参数
  161. this.setData({
  162. id: parseInt(options.id)
  163. // id: 1181000
  164. });
  165. var that = this;
  166. this.getGoodsInfo();
  167. util.request(api.CartGoodsCount).then(function (res) {
  168. if (res.errno === 0) {
  169. that.setData({
  170. cartGoodsCount: res.data.cartTotal.goodsCount
  171. });
  172. }
  173. });
  174. var that = this;
  175. // 高度自适应
  176. wx.getSystemInfo({
  177. success: function (res) {
  178. var clientHeight = res.windowHeight,
  179. clientWidth = res.windowWidth,
  180. rpxR = 750 / clientWidth;
  181. var calc = clientHeight * rpxR - 100;
  182. console.log(calc)
  183. that.setData({
  184. winHeight: calc
  185. });
  186. }
  187. });
  188. },
  189. onReady: function () {
  190. let that = this;
  191. // 页面渲染完成
  192. // wx.setClipboardData({
  193. // data: '/pages/goods/goods?id=' + that.data.id,
  194. // success: function (res) {
  195. // wx.getClipboardData({
  196. // success: function (res) {
  197. // // console.log(res.data) // data
  198. // }
  199. // })
  200. // }
  201. // })
  202. },
  203. //购物车增加
  204. addNumber2: function (e) {
  205. let that = this;
  206. var goodsId = e.currentTarget.dataset.goodsId;
  207. var productId = e.currentTarget.dataset.productId;
  208. var hotGoods = that.data.hotGoods;
  209. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  210. if (res.errno === 0 && null != res.data) {
  211. util.request(api.CartGoodsCount).then(function (res) {
  212. if (res.errno === 0) {
  213. that.setData({
  214. cartGoodsCount: res.data.cartTotal.goodsCount
  215. });
  216. }
  217. });
  218. wx.showToast({
  219. title: '添加成功',
  220. icon: 'success',
  221. mask: true
  222. });
  223. } else {
  224. util.showErrorToast(res.errmsg)
  225. }
  226. });
  227. },
  228. onShow: function () {
  229. // 页面显示
  230. },
  231. onHide: function () {
  232. // 页面隐藏
  233. },
  234. onUnload: function () {
  235. // 页面关闭
  236. },
  237. switchAttrPop: function () {
  238. this.setData({
  239. openAttr: !this.data.openAttr
  240. })
  241. },
  242. hideSwitchAttrPop: function() {
  243. this.setData({
  244. openAttr: false
  245. })
  246. },
  247. closeAttrOrCollect: function () {
  248. let that = this;
  249. //添加或是取消收藏
  250. util.request(api.CollectAddOrDelete, { typeId: 0, valueId: this.data.id }, "POST")
  251. .then(function (res) {
  252. let _res = res;
  253. if (_res.errno == 0) {
  254. if (_res.data.type == 'add') {
  255. that.setData({
  256. 'collectBackImage': that.data.hasCollectImage
  257. });
  258. wx.showToast({
  259. title: "收藏成功",
  260. mask: true
  261. });
  262. } else {
  263. that.setData({
  264. 'collectBackImage': that.data.noCollectImage
  265. });
  266. wx.showToast({
  267. title: "收藏取消",
  268. mask: true
  269. });
  270. }
  271. } else {
  272. wx.showToast({
  273. image: '/static/images/icon_error.png',
  274. title: _res.errmsg,
  275. mask: true
  276. });
  277. }
  278. });
  279. },
  280. openCartPage: function () {
  281. wx.switchTab({
  282. url: '/pages/cart/cart',
  283. });
  284. },
  285. addToCart: function () {
  286. var that = this;
  287. if (this.data.openAttr == false) {
  288. //打开规格选择窗口
  289. this.setData({
  290. openAttr: !this.data.openAttr
  291. });
  292. } else {
  293. //提示选择完整规格
  294. if (!goodsUtil.isCheckedAllSpec(that)) {
  295. return false;
  296. }
  297. if (that.data.number + that.data.cartNumber > that.data.stockNum){
  298. // wx.showToast({
  299. // title: '库存不足',
  300. // mask: true
  301. // });
  302. util.showErrorToast('库存不足');
  303. //找不到对应的product信息,提示没有库存
  304. return false;
  305. }
  306. // //根据选中的规格,判断是否有对应的sku信息
  307. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  308. // if (!checkedProduct || checkedProduct.length <= 0) {
  309. // wx.showToast({
  310. // title: '库存不足',
  311. // mask: true
  312. // });
  313. // //找不到对应的product信息,提示没有库存
  314. // return false;
  315. // }
  316. //验证库存
  317. // if (checkedProduct.goods_number < this.data.number) {
  318. // //找不到对应的product信息,提示没有库存
  319. // return false;
  320. // }
  321. //添加到购物车
  322. util.request(api.CartAdd, {
  323. goodsId: this.data.goods.id,
  324. number: this.data.number,
  325. productId: checkedProduct[0].id
  326. }, "POST")
  327. .then(function (res) {
  328. let _res = res;
  329. if (_res.errno == 0) {
  330. wx.showToast({
  331. title: '添加成功'
  332. });
  333. that.setData({
  334. openAttr: !that.data.openAttr,
  335. cartGoodsCount: _res.data.cartTotal.goodsCount
  336. });
  337. if (that.data.userHasCollect == 1) {
  338. that.setData({
  339. 'collectBackImage': that.data.hasCollectImage
  340. });
  341. } else {
  342. that.setData({
  343. 'collectBackImage': that.data.noCollectImage
  344. });
  345. }
  346. } else {
  347. wx.showToast({
  348. title: _res.errmsg,
  349. icon: 'none'
  350. })
  351. }
  352. });
  353. }
  354. },
  355. cutNumber: function () {
  356. this.setData({
  357. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  358. });
  359. },
  360. addNumber: function () {
  361. this.setData({
  362. number: this.data.number + 1
  363. });
  364. },
  365. onShareAppMessage: function () {
  366. var that = this;
  367. // console.log("url:" + that.data.goods.list_pic_url);
  368. // var userId = wx.getStorageSync('userId');
  369. // console.log("userId:" + userId);
  370. return {
  371. title: '商业版',
  372. desc: null != that.data.goods.name ? that.data.goods.name : "商业版",
  373. imageUrl: that.data.goods.list_pic_url,
  374. path: '/pages/goods/goods?id=' + that.data.id + '&&referrer=' + wx.getStorageSync('userId'),
  375. success: function (res) {
  376. console.log("转发成功");
  377. // 转发成功
  378. },
  379. fail: function (res) {
  380. // 转发失败
  381. console.log("转发失败");
  382. }
  383. }
  384. },
  385. //购物车增加
  386. addCrashNumber: function (e) {
  387. let that = this;
  388. var goodsId = e.currentTarget.dataset.goodsId;
  389. var productId = e.currentTarget.dataset.productId;
  390. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  391. if (res.errno === 0 && null != res.data) {
  392. var hotGoods = that.data.hotGoods;
  393. hotGoods.forEach(function (val, index, arr) {
  394. if (val.id == goodsId) {
  395. val.cart_num = res.data;
  396. hotGoods[index] = val;
  397. that.setData({ hotGoods: hotGoods });
  398. }
  399. }, that);
  400. }
  401. });
  402. },
  403. previewPic(e) {
  404. let url = e.currentTarget.dataset.url;
  405. let urls = [];
  406. urls[0] = url;
  407. wx.previewImage({
  408. urls
  409. })
  410. },
  411. switchNav(event) {
  412. wx.switchTab({
  413. url: '/pages/index/index'
  414. });
  415. },
  416. })