goods.js 11 KB

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