goods.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. comment: [],
  12. isSeller:false,
  13. seller: {},
  14. sellerDates: 0,
  15. sellerHistory: 0,
  16. relatedGoods: [],
  17. cartGoodsCount: 0,
  18. userHasCollect: 0,
  19. openComment: false,
  20. openDelete:false,
  21. replyId: '',
  22. replyUserId: '',
  23. replyUserName: '',
  24. page: 1,
  25. size: 10,
  26. commentContent: '',
  27. onLoadOption: {},
  28. noCollectImage: "/static/images/detail_star.png",
  29. hasCollectImage: "/static/images/detail_star_checked.png",
  30. collectBackImage: "/static/images/detail_star.png"
  31. },
  32. getGoodsInfo: function() {
  33. let that = this;
  34. util.request(api.GoodsDetail + '/' + that.data.id).then(function(res) {
  35. if (res.errno === 0) {
  36. if (res.data.info.isDelete) {
  37. util.showErrorToast('商品不存在')
  38. setTimeout(function callback() {
  39. wx.navigateBack({
  40. delta: 1
  41. })
  42. }, 1000)
  43. return
  44. }
  45. //计算卖家来平台第几天
  46. let registerTime = res.data.seller.registerTime
  47. let duration = new Date().getTime() - new Date(registerTime).getTime();
  48. let dates = parseInt(Math.floor(duration) / (1000 * 60 * 60 * 24));
  49. that.setData({
  50. goods: res.data.info,
  51. gallery: res.data.gallery,
  52. seller: res.data.seller,
  53. sellerHistory: res.data.sellerHistory,
  54. sellerDates: dates,
  55. comment: res.data.comment,
  56. userHasCollect: res.data.userHasCollect
  57. });
  58. if (res.data.userHasCollect == 1) {
  59. that.setData({
  60. 'collectBackImage': that.data.hasCollectImage
  61. });
  62. } else {
  63. that.setData({
  64. 'collectBackImage': that.data.noCollectImage
  65. });
  66. }
  67. if (that.data.seller.openId == wx.getStorageSync('userInfo').openId){
  68. console.log("当前用户是卖家")
  69. that.setData({
  70. isSeller: true
  71. });
  72. }
  73. WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
  74. that.getGoodsRelated();
  75. }
  76. });
  77. },
  78. getGoodsRelated: function() {
  79. let that = this;
  80. util.request(api.GoodsRelated + '/' + that.data.id, {
  81. page: this.data.page,
  82. size: this.data.size
  83. }).then(function(res) {
  84. if (res.errno === 0) {
  85. that.setData({
  86. relatedGoods: that.data.relatedGoods.concat(res.data)
  87. });
  88. }
  89. });
  90. },
  91. deleteGoods: function () {
  92. let that = this;
  93. util.request(api.GoodsDelete + '/' + that.data.id,{},'DELETE').then(function (res) {
  94. if (res.errno === 0) {
  95. setTimeout(function goback() {
  96. wx.reLaunch({
  97. url: '/pages/index/index'
  98. })
  99. }, 1000)
  100. wx.showToast({
  101. title: '删除成功'
  102. })
  103. }else{
  104. console.log(res.errmsg)
  105. }
  106. });
  107. },
  108. onLoad: function(options) {
  109. // 页面初始化 options为页面跳转所带来的参数
  110. this.setData({
  111. onLoadOption: options,
  112. id: parseInt(options.id),
  113. commentContent: ''
  114. // id: 1181000
  115. });
  116. var that = this;
  117. this.getGoodsInfo();
  118. // util.request(api.CartGoodsCount).then(function (res) {
  119. // if (res.errno === 0) {
  120. // that.setData({
  121. // cartGoodsCount: res.data.cartTotal.goodsCount
  122. // });
  123. // }
  124. // });
  125. },
  126. onReady: function() {
  127. // 页面渲染完成
  128. },
  129. onShow: function() {
  130. // 页面显示
  131. },
  132. onHide: function() {
  133. // 页面隐藏
  134. },
  135. onUnload: function() {
  136. // 页面关闭
  137. },
  138. switchCommentPop: function(event) {
  139. if(event.currentTarget.dataset.disable){
  140. console.log("disable")
  141. return
  142. }
  143. let that = this
  144. this.setData({
  145. replyId: event.currentTarget.dataset.replyId,
  146. replyUserId: event.currentTarget.dataset.replyUserId,
  147. replyUserName: event.currentTarget.dataset.replyUserName
  148. })
  149. user.checkLoginAndNav().then(() => {
  150. if (this.data.openComment == false) {
  151. this.setData({
  152. openComment: !this.data.openComment
  153. });
  154. }
  155. })
  156. },
  157. switchDeletetPop: function (event) {
  158. let that = this
  159. user.checkLoginAndNav().then(() => {
  160. if (this.data.openDelete == false) {
  161. this.setData({
  162. openDelete: !this.data.openDelete
  163. });
  164. }
  165. })
  166. },
  167. closeComment: function() {
  168. this.setData({
  169. openComment: false,
  170. });
  171. },
  172. closeDelete: function () {
  173. this.setData({
  174. openDelete: false,
  175. });
  176. },
  177. postComment: function(event) {
  178. let that = this
  179. if (event.detail.value.trim() == '') {
  180. util.showErrorToast('请填写内容')
  181. return false;
  182. }
  183. util.request(api.CommentPost + '/' + this.data.id, {
  184. replyCommentId: this.data.replyId,
  185. replyUserId: this.data.replyUserId,
  186. content: event.detail.value
  187. }, "POST").then(function(res) {
  188. if (res.errno === 0) {
  189. that.setData({
  190. commentContent: ''
  191. })
  192. wx.showToast({
  193. title: '留言成功'
  194. })
  195. //刷新
  196. that.onLoad(that.data.onLoadOption);
  197. }
  198. console.log(res)
  199. });
  200. },
  201. addCannelCollect: function() {
  202. let that = this;
  203. user.checkLoginAndNav().then(() => {
  204. //添加或是取消收藏
  205. util.request(api.CollectAddOrDelete + '/' + this.data.id + '/' + this.data.userHasCollect, {}, "POST")
  206. .then(function(res) {
  207. let _res = res;
  208. let collectState = !that.data.userHasCollect;
  209. if (_res.errno == 0) {
  210. that.setData({
  211. userHasCollect: collectState
  212. });
  213. if (that.data.userHasCollect) {
  214. that.setData({
  215. 'collectBackImage': that.data.hasCollectImage
  216. });
  217. } else {
  218. that.setData({
  219. 'collectBackImage': that.data.noCollectImage
  220. });
  221. }
  222. } else {
  223. wx.showToast({
  224. image: '/static/images/icon_error.png',
  225. title: _res.errmsg,
  226. mask: true
  227. });
  228. }
  229. });
  230. })
  231. },
  232. preview: function (event) {
  233. let url = event.currentTarget.dataset.url
  234. wx.previewImage({
  235. urls: [url] // 需要预览的图片http链接列表
  236. })
  237. console.log(url)
  238. },
  239. onReachBottom: function() {
  240. console.log("拉到底")
  241. this.setData({
  242. page: this.data.page + 1
  243. })
  244. this.getGoodsRelated()
  245. },
  246. })