goods.js 11 KB

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