goods.js 12 KB

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