1
0

goods.js 12 KB

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