goods.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. id: 0,
  8. goods: {},
  9. gallery: [],
  10. // attribute: [],
  11. // issueList: [],
  12. comment: [],
  13. // brand: {},
  14. // specificationList: [],
  15. // productList: [],
  16. relatedGoods: [],
  17. cartGoodsCount: 0,
  18. userHasCollect: 0,
  19. number: 1,
  20. checkedSpecText: '请选择规格数量',
  21. openAttr: false,
  22. noCollectImage: "/static/images/detail_star.png",
  23. hasCollectImage: "/static/images/detail_star_checked.png",
  24. collectBackImage: "/static/images/detail_star.png"
  25. },
  26. getGoodsInfo: function () {
  27. let that = this;
  28. util.request(api.GoodsDetail + '/' + that.data.id).then(function (res) {
  29. if (res.errno === 0) {
  30. that.setData({
  31. goods: res.data.info,
  32. gallery: res.data.gallery,
  33. // attribute: res.data.attribute,
  34. // issueList: res.data.issue,
  35. comment: res.data.comment,
  36. // brand: res.data.brand,
  37. // specificationList: res.data.specificationList,
  38. // productList: res.data.productList,
  39. userHasCollect: res.data.userHasCollect
  40. });
  41. if (res.data.userHasCollect == 1) {
  42. that.setData({
  43. 'collectBackImage': that.data.hasCollectImage
  44. });
  45. } else {
  46. that.setData({
  47. 'collectBackImage': that.data.noCollectImage
  48. });
  49. }
  50. WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
  51. that.getGoodsRelated();
  52. }
  53. });
  54. },
  55. getGoodsRelated: function () {
  56. let that = this;
  57. util.request(api.GoodsRelated + '/' + that.data.id,).then(function (res) {
  58. if (res.errno === 0) {
  59. that.setData({
  60. relatedGoods: res.data.goodsList,
  61. });
  62. }
  63. });
  64. },
  65. // clickSkuValue: function (event) {
  66. // let that = this;
  67. // let specNameId = event.currentTarget.dataset.nameId;
  68. // let specValueId = event.currentTarget.dataset.valueId;
  69. // //判断是否可以点击
  70. // //TODO 性能优化,可在wx:for中添加index,可以直接获取点击的属性名和属性值,不用循环
  71. // let _specificationList = this.data.specificationList;
  72. // for (let i = 0; i < _specificationList.length; i++) {
  73. // if (_specificationList[i].specification_id == specNameId) {
  74. // for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  75. // if (_specificationList[i].valueList[j].id == specValueId) {
  76. // //如果已经选中,则反选
  77. // if (_specificationList[i].valueList[j].checked) {
  78. // _specificationList[i].valueList[j].checked = false;
  79. // } else {
  80. // _specificationList[i].valueList[j].checked = true;
  81. // }
  82. // } else {
  83. // _specificationList[i].valueList[j].checked = false;
  84. // }
  85. // }
  86. // }
  87. // }
  88. // this.setData({
  89. // 'specificationList': _specificationList
  90. // });
  91. // //重新计算spec改变后的信息
  92. // this.changeSpecInfo();
  93. // //重新计算哪些值不可以点击
  94. // },
  95. //获取选中的规格信息
  96. // getCheckedSpecValue: function () {
  97. // let checkedValues = [];
  98. // let _specificationList = this.data.specificationList;
  99. // for (let i = 0; i < _specificationList.length; i++) {
  100. // let _checkedObj = {
  101. // nameId: _specificationList[i].specification_id,
  102. // valueId: 0,
  103. // valueText: ''
  104. // };
  105. // for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  106. // if (_specificationList[i].valueList[j].checked) {
  107. // _checkedObj.valueId = _specificationList[i].valueList[j].id;
  108. // _checkedObj.valueText = _specificationList[i].valueList[j].value;
  109. // }
  110. // }
  111. // checkedValues.push(_checkedObj);
  112. // }
  113. // return checkedValues;
  114. // },
  115. // //根据已选的值,计算其它值的状态
  116. // setSpecValueStatus: function () {
  117. // },
  118. // //判断规格是否选择完整
  119. // isCheckedAllSpec: function () {
  120. // return !this.getCheckedSpecValue().some(function (v) {
  121. // if (v.valueId == 0) {
  122. // return true;
  123. // }
  124. // });
  125. // },
  126. // getCheckedSpecKey: function () {
  127. // let checkedValue = this.getCheckedSpecValue().map(function (v) {
  128. // return v.valueId;
  129. // });
  130. // return checkedValue.join('_');
  131. // },
  132. // changeSpecInfo: function () {
  133. // let checkedNameValue = this.getCheckedSpecValue();
  134. // //设置选择的信息
  135. // let checkedValue = checkedNameValue.filter(function (v) {
  136. // if (v.valueId != 0) {
  137. // return true;
  138. // } else {
  139. // return false;
  140. // }
  141. // }).map(function (v) {
  142. // return v.valueText;
  143. // });
  144. // if (checkedValue.length > 0) {
  145. // this.setData({
  146. // 'checkedSpecText': checkedValue.join(' ')
  147. // });
  148. // } else {
  149. // this.setData({
  150. // 'checkedSpecText': '请选择规格数量'
  151. // });
  152. // }
  153. // },
  154. // getCheckedProductItem: function (key) {
  155. // return this.data.productList.filter(function (v) {
  156. // if (v.goods_specification_ids == key) {
  157. // return true;
  158. // } else {
  159. // return false;
  160. // }
  161. // });
  162. // },
  163. onLoad: function (options) {
  164. // 页面初始化 options为页面跳转所带来的参数
  165. this.setData({
  166. id: parseInt(options.id)
  167. // id: 1181000
  168. });
  169. var that = this;
  170. this.getGoodsInfo();
  171. // util.request(api.CartGoodsCount).then(function (res) {
  172. // if (res.errno === 0) {
  173. // that.setData({
  174. // cartGoodsCount: res.data.cartTotal.goodsCount
  175. // });
  176. // }
  177. // });
  178. },
  179. onReady: function () {
  180. // 页面渲染完成
  181. },
  182. onShow: function () {
  183. // 页面显示
  184. },
  185. onHide: function () {
  186. // 页面隐藏
  187. },
  188. onUnload: function () {
  189. // 页面关闭
  190. },
  191. // switchAttrPop: function () {
  192. // if (this.data.openAttr == false) {
  193. // this.setData({
  194. // openAttr: !this.data.openAttr
  195. // });
  196. // }
  197. // },
  198. // closeAttr: function () {
  199. // this.setData({
  200. // openAttr: false,
  201. // });
  202. // },
  203. addCannelCollect: function () {
  204. let that = this;
  205. //添加或是取消收藏
  206. util.request(api.CollectAddOrDelete + '/' + this.data.id + '/' + this.data.userHasCollect,{}, "POST")
  207. .then(function (res) {
  208. let _res = res;
  209. let collectState = !that.data.userHasCollect;
  210. if (_res.errno == 0) {
  211. that.setData({
  212. userHasCollect: collectState
  213. });
  214. if (that.data.userHasCollect) {
  215. that.setData({
  216. 'collectBackImage': that.data.hasCollectImage
  217. });
  218. } else {
  219. that.setData({
  220. 'collectBackImage': that.data.noCollectImage
  221. });
  222. }
  223. } else {
  224. wx.showToast({
  225. image: '/static/images/icon_error.png',
  226. title: _res.errmsg,
  227. mask: true
  228. });
  229. }
  230. });
  231. },
  232. // openCartPage: function () {
  233. // wx.switchTab({
  234. // url: '/pages/cart/cart',
  235. // });
  236. // },
  237. // addToCart: function () {
  238. // var that = this;
  239. // if (this.data.openAttr === false) {
  240. // //打开规格选择窗口
  241. // this.setData({
  242. // openAttr: !this.data.openAttr
  243. // });
  244. // } else {
  245. // //提示选择完整规格
  246. // if (!this.isCheckedAllSpec()) {
  247. // wx.showToast({
  248. // image: '/static/images/icon_error.png',
  249. // title: '请选择规格',
  250. // mask: true
  251. // });
  252. // return false;
  253. // }
  254. // //根据选中的规格,判断是否有对应的sku信息
  255. // let checkedProduct = this.getCheckedProductItem(this.getCheckedSpecKey());
  256. // if (!checkedProduct || checkedProduct.length <= 0) {
  257. // //找不到对应的product信息,提示没有库存
  258. // wx.showToast({
  259. // image: '/static/images/icon_error.png',
  260. // title: '库存不足',
  261. // mask: true
  262. // });
  263. // return false;
  264. // }
  265. // //验证库存
  266. // if (checkedProduct.goods_number < this.data.number) {
  267. // //找不到对应的product信息,提示没有库存
  268. // wx.showToast({
  269. // image: '/static/images/icon_error.png',
  270. // title: '库存不足',
  271. // mask: true
  272. // });
  273. // return false;
  274. // }
  275. // //添加到购物车
  276. // util.request(api.CartAdd, { goodsId: this.data.goods.id, number: this.data.number, productId: checkedProduct[0].id }, "POST")
  277. // .then(function (res) {
  278. // let _res = res;
  279. // if (_res.errno == 0) {
  280. // wx.showToast({
  281. // title: '添加成功'
  282. // });
  283. // that.setData({
  284. // openAttr: !that.data.openAttr,
  285. // cartGoodsCount: _res.data.cartTotal.goodsCount
  286. // });
  287. // } else {
  288. // wx.showToast({
  289. // image: '/static/images/icon_error.png',
  290. // title: _res.errmsg,
  291. // mask: true
  292. // });
  293. // }
  294. // });
  295. // }
  296. // },
  297. // cutNumber: function () {
  298. // this.setData({
  299. // number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  300. // });
  301. // },
  302. // addNumber: function () {
  303. // this.setData({
  304. // number: this.data.number + 1
  305. // });
  306. // }
  307. })