goods.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. Page({
  6. data: {
  7. winHeight: "",
  8. id: 0,
  9. goods: {},
  10. gallery: [],
  11. attribute: [],
  12. issueList: [],
  13. comment: [],
  14. brand: {},
  15. specificationList: [],
  16. productList: [],
  17. relatedGoods: [],
  18. cartGoodsCount: 0,
  19. userHasCollect: 0,
  20. number: 1,
  21. checkedSpecText: '请选择规格数量',
  22. openAttr: false,
  23. noCollectImage: "/static/images/icon_collect.png",
  24. hasCollectImage: "/static/images/icon_collect_checked.png",
  25. collectBackImage: "/static/images/icon_collect.png"
  26. },
  27. getGoodsInfo: function () {
  28. let that = this;
  29. util.request(api.GoodsDetail, { id: that.data.id }).then(function (res) {
  30. if (res.errno === 0) {
  31. that.setData({
  32. goods: res.data.info,
  33. gallery: res.data.gallery,
  34. attribute: res.data.attribute,
  35. issueList: res.data.issue,
  36. comment: res.data.comment,
  37. brand: res.data.brand,
  38. specificationList: res.data.specificationList,
  39. productList: res.data.productList,
  40. userHasCollect: res.data.userHasCollect
  41. });
  42. //设置默认值
  43. that.setDefSpecInfo(that.data.specificationList);
  44. if (res.data.userHasCollect == 1) {
  45. that.setData({
  46. 'collectBackImage': that.data.hasCollectImage
  47. });
  48. } else {
  49. that.setData({
  50. 'collectBackImage': that.data.noCollectImage
  51. });
  52. }
  53. WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
  54. that.getGoodsRelated();
  55. }
  56. });
  57. },
  58. getGoodsRelated: function () {
  59. let that = this;
  60. util.request(api.GoodsRelated, { id: that.data.id }).then(function (res) {
  61. if (res.errno === 0) {
  62. that.setData({
  63. relatedGoods: res.data.goodsList,
  64. });
  65. }
  66. });
  67. },
  68. clickSkuValue: function (event) {
  69. let that = this;
  70. let specNameId = event.currentTarget.dataset.nameId;
  71. let specValueId = event.currentTarget.dataset.valueId;
  72. //判断是否可以点击
  73. //TODO 性能优化,可在wx:for中添加index,可以直接获取点击的属性名和属性值,不用循环
  74. let _specificationList = this.data.specificationList;
  75. for (let i = 0; i < _specificationList.length; i++) {
  76. if (_specificationList[i].specification_id == specNameId) {
  77. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  78. if (_specificationList[i].valueList[j].id == specValueId) {
  79. //如果已经选中,则反选
  80. if (_specificationList[i].valueList[j].checked) {
  81. _specificationList[i].valueList[j].checked = false;
  82. } else {
  83. _specificationList[i].valueList[j].checked = true;
  84. }
  85. } else {
  86. _specificationList[i].valueList[j].checked = false;
  87. }
  88. }
  89. }
  90. }
  91. this.setData({
  92. 'specificationList': _specificationList
  93. });
  94. //重新计算spec改变后的信息
  95. this.changeSpecInfo();
  96. //重新计算哪些值不可以点击
  97. },
  98. //获取选中的规格信息
  99. getCheckedSpecValue: function () {
  100. let checkedValues = [];
  101. let _specificationList = this.data.specificationList;
  102. for (let i = 0; i < _specificationList.length; i++) {
  103. let _checkedObj = {
  104. nameId: _specificationList[i].specification_id,
  105. valueId: 0,
  106. valueText: ''
  107. };
  108. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  109. if (_specificationList[i].valueList[j].checked) {
  110. _checkedObj.valueId = _specificationList[i].valueList[j].id;
  111. _checkedObj.valueText = _specificationList[i].valueList[j].value;
  112. }
  113. }
  114. checkedValues.push(_checkedObj);
  115. }
  116. return checkedValues;
  117. },
  118. //根据已选的值,计算其它值的状态
  119. setSpecValueStatus: function () {
  120. },
  121. //判断规格是否选择完整
  122. isCheckedAllSpec: function () {
  123. return !this.getCheckedSpecValue().some(function (v) {
  124. if (v.valueId == 0) {
  125. return true;
  126. }
  127. });
  128. },
  129. getCheckedSpecKey: function () {
  130. let checkedValue = this.getCheckedSpecValue().map(function (v) {
  131. return v.valueId;
  132. });
  133. return checkedValue.join('_');
  134. },
  135. changeSpecInfo: function () {
  136. let checkedNameValue = this.getCheckedSpecValue();
  137. //设置选择的信息
  138. let checkedValue = checkedNameValue.filter(function (v) {
  139. if (v.valueId != 0) {
  140. return true;
  141. } else {
  142. return false;
  143. }
  144. }).map(function (v) {
  145. return v.valueText;
  146. });
  147. if (checkedValue.length > 0) {
  148. this.setData({
  149. 'checkedSpecText': checkedValue.join(' ')
  150. });
  151. } else {
  152. this.setData({
  153. 'checkedSpecText': '请选择规格数量'
  154. });
  155. }
  156. },
  157. getCheckedProductItem: function (key) {
  158. return this.data.productList.filter(function (v) {
  159. if (v.goods_specification_ids.indexOf(key) > -1) {
  160. return true;
  161. } else {
  162. return false;
  163. }
  164. });
  165. },
  166. onLoad: function (options) {
  167. // 页面初始化 options为页面跳转所带来的参数
  168. this.setData({
  169. id: parseInt(options.id)
  170. // id: 1181000
  171. });
  172. var that = this;
  173. this.getGoodsInfo();
  174. util.request(api.CartGoodsCount).then(function (res) {
  175. if (res.errno === 0) {
  176. that.setData({
  177. cartGoodsCount: res.data.cartTotal.goodsCount
  178. });
  179. }
  180. });
  181. var that = this
  182. // 高度自适应
  183. wx.getSystemInfo({
  184. success: function (res) {
  185. var clientHeight = res.windowHeight,
  186. clientWidth = res.windowWidth,
  187. rpxR = 750 / clientWidth;
  188. var calc = clientHeight * rpxR - 100;
  189. that.setData({
  190. winHeight: calc
  191. });
  192. }
  193. });
  194. },
  195. onReady: function () {
  196. // 页面渲染完成
  197. },
  198. onShow: function () {
  199. // 页面显示
  200. },
  201. onHide: function () {
  202. // 页面隐藏
  203. },
  204. onUnload: function () {
  205. // 页面关闭
  206. },
  207. switchAttrPop: function () {
  208. if (this.data.openAttr == false) {
  209. this.setData({
  210. openAttr: !this.data.openAttr,
  211. collectBackImage: "/static/images/detail_back.png"
  212. });
  213. }
  214. },
  215. closeAttrOrCollect: function () {
  216. let that = this;
  217. if (this.data.openAttr) {
  218. this.setData({
  219. openAttr: false,
  220. });
  221. if (that.data.userHasCollect == 1) {
  222. that.setData({
  223. 'collectBackImage': that.data.hasCollectImage
  224. });
  225. } else {
  226. that.setData({
  227. 'collectBackImage': that.data.noCollectImage
  228. });
  229. }
  230. } else {
  231. //添加或是取消收藏
  232. util.request(api.CollectAddOrDelete, { typeId: 0, valueId: this.data.id }, "POST", "application/json")
  233. .then(function (res) {
  234. let _res = res;
  235. if (_res.errno == 0) {
  236. if ( _res.data.type == 'add') {
  237. that.setData({
  238. 'collectBackImage': that.data.hasCollectImage
  239. });
  240. } else {
  241. that.setData({
  242. 'collectBackImage': that.data.noCollectImage
  243. });
  244. }
  245. } else {
  246. wx.showToast({
  247. image: '/static/images/icon_error.png',
  248. title: _res.errmsg,
  249. mask: true
  250. });
  251. }
  252. });
  253. }
  254. },
  255. openCartPage: function () {
  256. wx.switchTab({
  257. url: '/pages/cart/cart',
  258. });
  259. },
  260. /**
  261. * 直接购买
  262. */
  263. buyGoods: function () {
  264. var that = this;
  265. if (this.data.openAttr == false) {
  266. //打开规格选择窗口
  267. this.setData({
  268. openAttr: !this.data.openAttr,
  269. collectBackImage: "/static/images/detail_back.png"
  270. });
  271. } else {
  272. //提示选择完整规格
  273. if (!this.isCheckedAllSpec()) {
  274. return false;
  275. }
  276. //根据选中的规格,判断是否有对应的sku信息
  277. let checkedProduct = this.getCheckedProductItem(this.getCheckedSpecKey());
  278. if (!checkedProduct || checkedProduct.length <= 0) {
  279. //找不到对应的product信息,提示没有库存
  280. return false;
  281. }
  282. //验证库存
  283. if (checkedProduct.goods_number < this.data.number) {
  284. //找不到对应的product信息,提示没有库存
  285. return false;
  286. }
  287. // 直接购买商品
  288. util.request(api.BuyAdd, { goodsId: this.data.goods.id, number: this.data.number, productId: checkedProduct[0].id }, "POST",'application/json')
  289. .then(function (res) {
  290. let _res = res;
  291. if (_res.errno == 0) {
  292. that.setData({
  293. openAttr: !that.data.openAttr,
  294. });
  295. wx.navigateTo({
  296. url: '/pages/shopping/checkout/checkout?isBuy=true',
  297. })
  298. } else {
  299. wx.showToast({
  300. image: '/static/images/icon_error.png',
  301. title: _res.errmsg,
  302. mask: true
  303. });
  304. }
  305. });
  306. }
  307. },
  308. /**
  309. * 添加到购物车
  310. */
  311. addToCart: function () {
  312. var that = this;
  313. if (this.data.openAttr == false) {
  314. //打开规格选择窗口
  315. this.setData({
  316. openAttr: !this.data.openAttr,
  317. collectBackImage: "/static/images/detail_back.png"
  318. });
  319. } else {
  320. //提示选择完整规格
  321. if (!this.isCheckedAllSpec()) {
  322. wx.showToast({
  323. title: '请选择完整规格'
  324. });
  325. return false;
  326. }
  327. //根据选中的规格,判断是否有对应的sku信息
  328. let checkedProduct = this.getCheckedProductItem(this.getCheckedSpecKey());
  329. if (!checkedProduct || checkedProduct.length <= 0) {
  330. //找不到对应的product信息,提示没有库存
  331. return false;
  332. }
  333. //验证库存
  334. if (checkedProduct.goods_number < this.data.number) {
  335. //找不到对应的product信息,提示没有库存
  336. return false;
  337. }
  338. //添加到购物车
  339. util.request(api.CartAdd, { goodsId: this.data.goods.id, number: this.data.number, productId: checkedProduct[0].id }, 'POST', 'application/json')
  340. .then(function (res) {
  341. let _res = res;
  342. if (_res.errno == 0) {
  343. wx.showToast({
  344. title: '添加成功'
  345. });
  346. that.setData({
  347. openAttr: !that.data.openAttr,
  348. cartGoodsCount: _res.data.cartTotal.goodsCount
  349. });
  350. if (that.data.userHasCollect == 1) {
  351. that.setData({
  352. 'collectBackImage': that.data.hasCollectImage
  353. });
  354. } else {
  355. that.setData({
  356. 'collectBackImage': that.data.noCollectImage
  357. });
  358. }
  359. } else {
  360. wx.showToast({
  361. image: '/static/images/icon_error.png',
  362. title: _res.errmsg,
  363. mask: true
  364. });
  365. }
  366. });
  367. }
  368. },
  369. cutNumber: function () {
  370. this.setData({
  371. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  372. });
  373. },
  374. addNumber: function () {
  375. this.setData({
  376. number: this.data.number + 1
  377. });
  378. },
  379. setDefSpecInfo: function (specificationList) {
  380. //未考虑规格联动情况
  381. let that = this;
  382. if (!specificationList)return;
  383. for (let i = 0; i < specificationList.length;i++){
  384. let specification = specificationList[i];
  385. let specNameId = specification.specification_id;
  386. //规格只有一个时自动选择规格
  387. if (specification.valueList && specification.valueList.length == 1){
  388. let specValueId = specification.valueList[0].id;
  389. that.clickSkuValue({ currentTarget: { dataset: { "nameId": specNameId, "valueId": specValueId } } });
  390. }
  391. }
  392. specificationList.map(function(item){
  393. });
  394. }
  395. })