1
0

goods.js 12 KB

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