goods.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. if(event.currentTarget.dataset.disable){
  246. console.log("disable")
  247. return
  248. }
  249. let that = this
  250. this.setData({
  251. replyId: event.currentTarget.dataset.replyId,
  252. replyUserId: event.currentTarget.dataset.replyUserId,
  253. replyUserName: event.currentTarget.dataset.replyUserName
  254. })
  255. user.checkLoginAndNav().then(() => {
  256. if (this.data.openComment == false) {
  257. this.setData({
  258. openComment: !this.data.openComment
  259. });
  260. }
  261. })
  262. },
  263. switchDeletetPop: function (event) {
  264. let that = this
  265. user.checkLoginAndNav().then(() => {
  266. if (this.data.openDelete == false) {
  267. this.setData({
  268. openDelete: !this.data.openDelete
  269. });
  270. }
  271. })
  272. },
  273. closeComment: function() {
  274. this.setData({
  275. openComment: false,
  276. });
  277. },
  278. closeDelete: function () {
  279. this.setData({
  280. openDelete: false,
  281. });
  282. },
  283. postComment: function(event) {
  284. let that = this
  285. if (event.detail.value.trim() == '') {
  286. util.showErrorToast('请填写内容')
  287. return false;
  288. }
  289. util.request(api.CommentPost + '/' + this.data.id, {
  290. replyCommentId: this.data.replyId,
  291. replyUserId: this.data.replyUserId,
  292. content: event.detail.value
  293. }, "POST").then(function(res) {
  294. if (res.errno === 0) {
  295. that.setData({
  296. commentContent: ''
  297. })
  298. wx.showToast({
  299. title: '留言成功'
  300. })
  301. //刷新
  302. that.onLoad(that.data.onLoadOption);
  303. }
  304. console.log(res)
  305. });
  306. },
  307. addCannelCollect: function() {
  308. let that = this;
  309. user.checkLoginAndNav().then(() => {
  310. //添加或是取消收藏
  311. util.request(api.CollectAddOrDelete + '/' + this.data.id + '/' + this.data.userHasCollect, {}, "POST")
  312. .then(function(res) {
  313. let _res = res;
  314. let collectState = !that.data.userHasCollect;
  315. if (_res.errno == 0) {
  316. that.setData({
  317. userHasCollect: collectState
  318. });
  319. if (that.data.userHasCollect) {
  320. that.setData({
  321. 'collectBackImage': that.data.hasCollectImage
  322. });
  323. } else {
  324. that.setData({
  325. 'collectBackImage': that.data.noCollectImage
  326. });
  327. }
  328. } else {
  329. wx.showToast({
  330. image: '/static/images/icon_error.png',
  331. title: _res.errmsg,
  332. mask: true
  333. });
  334. }
  335. });
  336. })
  337. },
  338. // openCartPage: function () {
  339. // wx.switchTab({
  340. // url: '/pages/cart/cart',
  341. // });
  342. // },
  343. // addToCart: function () {
  344. // var that = this;
  345. // if (this.data.openAttr === false) {
  346. // //打开规格选择窗口
  347. // this.setData({
  348. // openAttr: !this.data.openAttr
  349. // });
  350. // } else {
  351. // //提示选择完整规格
  352. // if (!this.isCheckedAllSpec()) {
  353. // wx.showToast({
  354. // image: '/static/images/icon_error.png',
  355. // title: '请选择规格',
  356. // mask: true
  357. // });
  358. // return false;
  359. // }
  360. // //根据选中的规格,判断是否有对应的sku信息
  361. // let checkedProduct = this.getCheckedProductItem(this.getCheckedSpecKey());
  362. // if (!checkedProduct || checkedProduct.length <= 0) {
  363. // //找不到对应的product信息,提示没有库存
  364. // wx.showToast({
  365. // image: '/static/images/icon_error.png',
  366. // title: '库存不足',
  367. // mask: true
  368. // });
  369. // return false;
  370. // }
  371. // //验证库存
  372. // if (checkedProduct.goods_number < this.data.number) {
  373. // //找不到对应的product信息,提示没有库存
  374. // wx.showToast({
  375. // image: '/static/images/icon_error.png',
  376. // title: '库存不足',
  377. // mask: true
  378. // });
  379. // return false;
  380. // }
  381. // //添加到购物车
  382. // util.request(api.CartAdd, { goodsId: this.data.goods.id, number: this.data.number, productId: checkedProduct[0].id }, "POST")
  383. // .then(function (res) {
  384. // let _res = res;
  385. // if (_res.errno == 0) {
  386. // wx.showToast({
  387. // title: '添加成功'
  388. // });
  389. // that.setData({
  390. // openAttr: !that.data.openAttr,
  391. // cartGoodsCount: _res.data.cartTotal.goodsCount
  392. // });
  393. // } else {
  394. // wx.showToast({
  395. // image: '/static/images/icon_error.png',
  396. // title: _res.errmsg,
  397. // mask: true
  398. // });
  399. // }
  400. // });
  401. // }
  402. // },
  403. // cutNumber: function () {
  404. // this.setData({
  405. // number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  406. // });
  407. // },
  408. // addNumber: function () {
  409. // this.setData({
  410. // number: this.data.number + 1
  411. // });
  412. // }
  413. onReachBottom: function() {
  414. console.log("拉到底")
  415. this.setData({
  416. page: this.data.page + 1
  417. })
  418. this.getGoodsRelated()
  419. },
  420. })