goods.js 11 KB

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