goods.js 11 KB

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