goods.js 11 KB

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