goods.js 7.1 KB

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