goods.js 7.4 KB

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