goods.js 11 KB

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