goods.js 12 KB

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