goods.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/icon_collect.png",
  23. hasCollectImage: "/static/images/icon_collect_checked.png",
  24. collectBackImage: "/static/images/icon_collect.png"
  25. },
  26. getGoodsInfo: function () {
  27. let that = this;
  28. util.request(api.GoodsDetail, { id: 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, { id: 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, { typeId: 0, valueId: this.data.id }, "POST")
  207. .then(function (res) {
  208. let _res = res;
  209. if (_res.errno == 0) {
  210. if (_res.data.type == 'add') {
  211. that.setData({
  212. 'collectBackImage': that.data.hasCollectImage
  213. });
  214. } else {
  215. that.setData({
  216. 'collectBackImage': that.data.noCollectImage
  217. });
  218. }
  219. } else {
  220. wx.showToast({
  221. image: '/static/images/icon_error.png',
  222. title: _res.errmsg,
  223. mask: true
  224. });
  225. }
  226. });
  227. },
  228. openCartPage: function () {
  229. wx.switchTab({
  230. url: '/pages/cart/cart',
  231. });
  232. },
  233. addToCart: function () {
  234. var that = this;
  235. if (this.data.openAttr === false) {
  236. //打开规格选择窗口
  237. this.setData({
  238. openAttr: !this.data.openAttr
  239. });
  240. } else {
  241. //提示选择完整规格
  242. if (!this.isCheckedAllSpec()) {
  243. wx.showToast({
  244. image: '/static/images/icon_error.png',
  245. title: '请选择规格',
  246. mask: true
  247. });
  248. return false;
  249. }
  250. //根据选中的规格,判断是否有对应的sku信息
  251. let checkedProduct = this.getCheckedProductItem(this.getCheckedSpecKey());
  252. if (!checkedProduct || checkedProduct.length <= 0) {
  253. //找不到对应的product信息,提示没有库存
  254. wx.showToast({
  255. image: '/static/images/icon_error.png',
  256. title: '库存不足',
  257. mask: true
  258. });
  259. return false;
  260. }
  261. //验证库存
  262. if (checkedProduct.goods_number < this.data.number) {
  263. //找不到对应的product信息,提示没有库存
  264. wx.showToast({
  265. image: '/static/images/icon_error.png',
  266. title: '库存不足',
  267. mask: true
  268. });
  269. return false;
  270. }
  271. //添加到购物车
  272. util.request(api.CartAdd, { goodsId: this.data.goods.id, number: this.data.number, productId: checkedProduct[0].id }, "POST")
  273. .then(function (res) {
  274. let _res = res;
  275. if (_res.errno == 0) {
  276. wx.showToast({
  277. title: '添加成功'
  278. });
  279. that.setData({
  280. openAttr: !that.data.openAttr,
  281. cartGoodsCount: _res.data.cartTotal.goodsCount
  282. });
  283. } else {
  284. wx.showToast({
  285. image: '/static/images/icon_error.png',
  286. title: _res.errmsg,
  287. mask: true
  288. });
  289. }
  290. });
  291. }
  292. },
  293. cutNumber: function () {
  294. this.setData({
  295. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  296. });
  297. },
  298. addNumber: function () {
  299. this.setData({
  300. number: this.data.number + 1
  301. });
  302. }
  303. })