goods.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. } else {
  255. that.setData({
  256. 'collectBackImage': that.data.noCollectImage
  257. });
  258. }
  259. } else {
  260. wx.showToast({
  261. image: '/static/images/icon_error.png',
  262. title: _res.errmsg,
  263. mask: true
  264. });
  265. }
  266. });
  267. },
  268. openCartPage: function () {
  269. wx.switchTab({
  270. url: '/pages/cart/cart',
  271. });
  272. },
  273. addToCart: function () {
  274. var that = this;
  275. if (this.data.openAttr == false) {
  276. //打开规格选择窗口
  277. this.setData({
  278. openAttr: !this.data.openAttr
  279. });
  280. } else {
  281. //提示选择完整规格
  282. if (!goodsUtil.isCheckedAllSpec(that)) {
  283. return false;
  284. }
  285. if (that.data.number + that.data.cartNumber > that.data.stockNum){
  286. // wx.showToast({
  287. // title: '库存不足',
  288. // mask: true
  289. // });
  290. util.showErrorToast('库存不足');
  291. //找不到对应的product信息,提示没有库存
  292. return false;
  293. }
  294. // //根据选中的规格,判断是否有对应的sku信息
  295. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  296. // if (!checkedProduct || checkedProduct.length <= 0) {
  297. // wx.showToast({
  298. // title: '库存不足',
  299. // mask: true
  300. // });
  301. // //找不到对应的product信息,提示没有库存
  302. // return false;
  303. // }
  304. //验证库存
  305. // if (checkedProduct.goods_number < this.data.number) {
  306. // //找不到对应的product信息,提示没有库存
  307. // return false;
  308. // }
  309. //添加到购物车
  310. util.request(api.CartAdd, {
  311. goodsId: this.data.goods.id,
  312. number: this.data.number,
  313. productId: checkedProduct[0].id
  314. }, "POST")
  315. .then(function (res) {
  316. let _res = res;
  317. if (_res.errno == 0) {
  318. wx.showToast({
  319. title: '添加成功'
  320. });
  321. that.setData({
  322. openAttr: !that.data.openAttr,
  323. cartGoodsCount: _res.data.cartTotal.goodsCount
  324. });
  325. if (that.data.userHasCollect == 1) {
  326. that.setData({
  327. 'collectBackImage': that.data.hasCollectImage
  328. });
  329. } else {
  330. that.setData({
  331. 'collectBackImage': that.data.noCollectImage
  332. });
  333. }
  334. } else {
  335. wx.showToast({
  336. image: '/static/images/icon_error.png',
  337. title: _res.errmsg,
  338. mask: true
  339. });
  340. }
  341. });
  342. }
  343. },
  344. cutNumber: function () {
  345. this.setData({
  346. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  347. });
  348. },
  349. addNumber: function () {
  350. this.setData({
  351. number: this.data.number + 1
  352. });
  353. },
  354. onShareAppMessage: function () {
  355. var that = this;
  356. // console.log("url:" + that.data.goods.list_pic_url);
  357. // var userId = wx.getStorageSync('userId');
  358. // console.log("userId:" + userId);
  359. return {
  360. title: '商业版',
  361. desc: null != that.data.goods.name ? that.data.goods.name : "商业版",
  362. imageUrl: that.data.goods.list_pic_url,
  363. path: '/pages/goods/goods?id=' + that.data.id + '&&referrer=' + wx.getStorageSync('userId'),
  364. success: function (res) {
  365. console.log("转发成功");
  366. // 转发成功
  367. },
  368. fail: function (res) {
  369. // 转发失败
  370. console.log("转发失败");
  371. }
  372. }
  373. },
  374. //购物车增加
  375. addCrashNumber: function (e) {
  376. let that = this;
  377. var goodsId = e.currentTarget.dataset.goodsId;
  378. var productId = e.currentTarget.dataset.productId;
  379. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  380. if (res.errno === 0 && null != res.data) {
  381. var hotGoods = that.data.hotGoods;
  382. hotGoods.forEach(function (val, index, arr) {
  383. if (val.id == goodsId) {
  384. val.cart_num = res.data;
  385. hotGoods[index] = val;
  386. that.setData({ hotGoods: hotGoods });
  387. }
  388. }, that);
  389. }
  390. });
  391. },
  392. previewPic(e) {
  393. let url = e.currentTarget.dataset.url;
  394. let urls = [];
  395. urls[0] = url;
  396. wx.previewImage({
  397. urls
  398. })
  399. },
  400. switchNav(event) {
  401. wx.switchTab({
  402. url: '/pages/index/index'
  403. });
  404. },
  405. })