detail.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. const util = require('../../../utils/util.js');
  2. const app = getApp();
  3. Page({
  4. data: {
  5. nav: [{
  6. id: 0,
  7. title: '宝贝详情'
  8. }, {
  9. id: 1,
  10. title: '商品参数'
  11. }],
  12. current: 0,
  13. chatShow: false,
  14. skuShow: false,
  15. nodes: '',
  16. datas: {},
  17. comments: {},
  18. bottom: false,
  19. imgPath: util.config.imgPath,
  20. aid: '',
  21. title: '',
  22. score: 0,
  23. viode: [], //视频
  24. showBack: false, //是否显示返回顶部
  25. currentPages: 0,
  26. isColumn: false, //导航条是否到顶部
  27. onshow: true,
  28. skuHeight: '50%',
  29. },
  30. onLoad(options) {
  31. this.setData({
  32. aid: options.aid,
  33. currentPages: getCurrentPages().length,
  34. onshow: true
  35. });
  36. var pages = getCurrentPages() //获取加载的页面
  37. var currentPage = pages[pages.length - 1] //获取当前页面的对象
  38. },
  39. onShow() {
  40. let that = this;
  41. if (that.data.onshow) {
  42. if (!util.isObjEmpty(app.globalData.city)) {
  43. that.getData();
  44. } else {
  45. // var getLocation = util.promise(true);
  46. // getLocation.then(function (value) {
  47. that.getData();
  48. // }, function (error) {});
  49. }
  50. }
  51. },
  52. onUnload() {
  53. this.setData({
  54. skuShow: false
  55. });
  56. },
  57. onHide() {
  58. this.setData({
  59. skuShow: false
  60. });
  61. },
  62. onPageScroll(e) {
  63. var query = wx.createSelectorQuery();
  64. var isColumn = this.data.isColumn,
  65. that = this,
  66. showBack = this.data.showBack;
  67. query.select('#content').boundingClientRect();
  68. query.select('#notes').boundingClientRect();
  69. query.exec(function (res) {
  70. if (res && !that.data.isScroll) {
  71. if (res[1].top <= 0) {
  72. that.setData({
  73. current: 1
  74. });
  75. } else if (res[0].top <= 37) {
  76. that.setData({
  77. current: 0
  78. });
  79. }
  80. that.setData({
  81. isColumn: res[0].top <= 0 ? true : false
  82. });
  83. }
  84. });
  85. if (e.scrollTop > 500 && !showBack) {
  86. this.setData({
  87. showBack: true
  88. })
  89. } else if (e.scrollTop <= 500 && showBack) {
  90. this.setData({
  91. showBack: false
  92. })
  93. }
  94. },
  95. chooseNav(e) {
  96. let current = e.currentTarget.dataset.id,
  97. that = this,
  98. datas = this.data.datas;
  99. that.setData({
  100. current,
  101. isScroll: true
  102. });
  103. var arr = ['content', 'notes'];
  104. wx.pageScrollTo({
  105. selector: "#" + arr[current],
  106. success: () => {
  107. that.setData({
  108. isColumn: true
  109. });
  110. setTimeout(() => {
  111. that.setData({
  112. isScroll: false
  113. });
  114. }, 1000);
  115. }
  116. });
  117. if (current == 1) {
  118. wx.reportAnalytics('equip_parameter', {
  119. equip_aid: datas.aid,
  120. equip_title: datas.title,
  121. });
  122. } else if (current == 2) {
  123. wx.reportAnalytics('equip_comment', {
  124. equip_aid: datas.aid,
  125. equip_title: datas.title,
  126. });
  127. }
  128. },
  129. getData() {
  130. let that = this;
  131. util.ajax({
  132. func: "v2/article/detail",
  133. data: {
  134. "aid": that.data.aid
  135. }
  136. }, function (res) {
  137. if (res.code == 0) {
  138. let datas = res.data;
  139. // datas.content = datas.content.replace(/section/g, "div");
  140. // datas.content = datas.content.replace(/<img/g, "<img style='max-width:100%'");
  141. // let str = datas.content.match(/<source src="(\S*)"/g);
  142. // if (str) {
  143. // let viode = [];
  144. // for (let i in str) {
  145. // if (i < 3) {//只显示三个视频
  146. // let src = str[i].match(/<source src="(\S*)"/)[1];
  147. // viode.push(src.toString())
  148. // }
  149. // }
  150. // that.setData({ viode });
  151. // }
  152. if (datas.tags && datas.tags.length > 0) {
  153. let goodsGuarantee = [],
  154. keyTag = [];
  155. datas.tags.forEach(item => {
  156. if (item.tagKey == 'goodsGuarantee') {
  157. goodsGuarantee.push(item)
  158. }
  159. if (item.tagKey == 'keywords') {
  160. keyTag.push(item)
  161. }
  162. });
  163. res.data.goodsGuarantee = goodsGuarantee; //活动标签/电商卖点
  164. res.data.keyTag = keyTag; //电商保障
  165. // 分成两行
  166. if (res.data.keyTag && res.data.keyTag.length >= 5) {
  167. datas.keyTag1 = []
  168. datas.keyTag2 = []
  169. // 标签总字数
  170. let allNum = 0
  171. datas.keyTag.forEach(item => {
  172. allNum = allNum + item.tagValue.length
  173. })
  174. const middleIndex = Math.ceil(allNum / 2);
  175. // 已存入数组字数
  176. let nowNum = 0
  177. datas.keyTag.forEach(item => {
  178. if (nowNum >= middleIndex) {
  179. datas.keyTag2.push(item)
  180. } else {
  181. nowNum = nowNum + item.tagValue.length
  182. datas.keyTag1.push(item)
  183. }
  184. })
  185. } else {
  186. datas.keyTag1 = res.data.keyTag;
  187. }
  188. }
  189. that.setData({
  190. datas
  191. });
  192. that.getFeedbook();
  193. that.getContent();
  194. wx.reportAnalytics('enter_equipdetails', {
  195. equip_aid: that.data.aid,
  196. equip_title: datas.title,
  197. });
  198. } else {
  199. util.showTips(res.reason);
  200. }
  201. })
  202. },
  203. // 返回首页
  204. gohome() {
  205. wx.reLaunch({
  206. url: '/pages/home/index',
  207. })
  208. },
  209. // 获取富文本信息
  210. getContent() {
  211. let that = this;
  212. util.ajax({
  213. func: "v2/article/content",
  214. data: {
  215. "aid": that.data.aid
  216. },
  217. load: false
  218. }, function (res) {
  219. if (res.code == 0) {
  220. let datas = res.data;
  221. if (!util.isEmpty(datas.content)) {
  222. that.data.datas.content = datas.content.replace(/section/g, "div");
  223. that.data.datas.content = that.data.datas.content.replace(/<img/g, "<img style='max-width:100%;height:auto'");
  224. let str = datas.content.match(/<source src="(\S*)"/g);
  225. if (str) {
  226. let viode = [];
  227. for (let i in str) {
  228. if (i < 3) { //只显示三个视频
  229. let src = str[i].match(/<source src="(\S*)"/)[1];
  230. viode.push(src.toString())
  231. }
  232. }
  233. that.setData({
  234. viode
  235. });
  236. }
  237. }
  238. that.data.datas.notes = datas.notes ? datas.notes.replace(/section/g, "div") : '';
  239. that.data.datas.notes = that.data.datas.notes.replace(/<img/g, "<img style='max-width:100%'");
  240. that.data.datas.safeTrip = datas.safeTrip ? datas.safeTrip.replace(/section/g, "div") : '';
  241. that.data.datas.safeTrip = that.data.datas.safeTrip.replace(/<img/g, "<img style='max-width:100%'");
  242. that.setData({
  243. datas: that.data.datas
  244. });
  245. }
  246. })
  247. },
  248. getFeedbook() { //获取评论
  249. let that = this;
  250. util.ajax({
  251. func: "comments/list",
  252. data: {
  253. "aid": this.data.aid,
  254. "pageIndex": 1,
  255. "pageSize": 1,
  256. "sort": 1,
  257. "keyword": ''
  258. }
  259. }, function (res) {
  260. if (res.code == 0) {
  261. for (let i in res.data.comments) {
  262. res.data.comments[i].contented = res.data.comments[i].content.replace(/#([^#]+)#/g, "");
  263. res.data.comments[i].option = res.data.comments[i].content.match(/#([^#]+)#/g);
  264. }
  265. that.setData({
  266. comments: res.data,
  267. score: res.data.score
  268. });
  269. } else {
  270. util.showTips(res.reason);
  271. }
  272. })
  273. },
  274. goPay(e) {
  275. let datas = this.data.datas
  276. wx.reportAnalytics('equip_choose', {
  277. equip_aid: datas.aid,
  278. equip_title: datas.title,
  279. });
  280. this.setData({
  281. skuShow: true
  282. });
  283. },
  284. previewImage(e) {
  285. let index = e.currentTarget.dataset.index;
  286. let i = e.target.dataset.i;
  287. let comments = this.data.comments.comments;
  288. this.setData({
  289. onshow: false
  290. });
  291. wx.previewImage({
  292. current: comments[index].imageList[i], // 当前显示图片的http链接
  293. urls: comments[index].imageList // 需要预览的图片http链接列表
  294. })
  295. },
  296. toTop() {
  297. wx.pageScrollTo({
  298. scrollTop: 0,
  299. duration: 300
  300. })
  301. },
  302. chat() {
  303. this.setData({
  304. chatShow: true
  305. });
  306. },
  307. onShareAppMessage() {
  308. let that = this,
  309. datas = this.data.datas;
  310. wx.reportAnalytics('equip_share', {
  311. equip_aid: datas.aid,
  312. equip_title: datas.title,
  313. });
  314. return {
  315. title: datas.title,
  316. path: '/pages/product/goods/detail?aid=' + that.data.aid
  317. }
  318. }
  319. })