details.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // pages/details/details.js
  2. const getReq = require('./../../config.js').getReq;
  3. const app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. imgUrls: [],
  10. datas: {},
  11. getOneSummary: [],
  12. webViewUrl: '',
  13. indicatorDots: true,
  14. vertical: false,
  15. autoplay: true,
  16. interval: 2000,
  17. duration: 500,
  18. animation_flag: false,
  19. sec_index: 0,
  20. goodsNumber: 1,
  21. cartOrBuy: '',
  22. getgift: '',
  23. options_goods_id: ''
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad: function (options) {
  29. let webViewUrl = `https://passport.lrlz.com/mobile/index.php?act=goods_common&op=detail&goods_id=${options.goods_id}&client_type=ios`
  30. this.setData({
  31. webViewUrl,
  32. options_goods_id: options.goods_id
  33. })
  34. this.getDatas(options.goods_id)
  35. },
  36. getDatas(goods_id) {
  37. wx.showLoading({
  38. title: '加载中',
  39. })
  40. var self = this
  41. getReq({
  42. act: 'goods_common',
  43. op: 'index',
  44. goods_id
  45. }, function (res) {
  46. wx.hideLoading()
  47. if (res.code == 200) {
  48. let oneSummary = self.getOneSummary(res.datas.summary, goods_id)
  49. let getgift = self.getgift(res.datas.summary, goods_id)
  50. let differencePrice = (oneSummary.goods_price - oneSummary.bonus_price).toFixed(2)
  51. oneSummary = Object.assign({}, { differencePrice }, oneSummary)
  52. self.setData({
  53. datas: res.datas,
  54. getOneSummary: oneSummary,
  55. imgUrls: res.datas.common_info.images,
  56. getgift
  57. })
  58. }
  59. else {
  60. wx.showToast({
  61. icon: 'none',
  62. title: res.message,
  63. duration: 1500
  64. })
  65. }
  66. })
  67. },
  68. getOneSummary(sumarys, goods_id) {
  69. let getOneSummary = sumarys.filter((item, index) => {
  70. return item.goods_id == goods_id
  71. })
  72. return getOneSummary[0]
  73. },
  74. secSku(e) {
  75. let goodsId = e.currentTarget.dataset.goodsid
  76. let sec_index = e.currentTarget.dataset.secindex
  77. if (this.data.sec_index == sec_index) {
  78. return
  79. }
  80. let getOneSummary = this.getOneSummary(this.data.datas.summary, goodsId)
  81. let getgift = this.getgift(this.data.datas.summary, goodsId)
  82. if (getOneSummary.goods_storage < 1) {
  83. wx.showToast({
  84. title: '客官!暂时没有库存!',
  85. icon: 'none',
  86. duration: 1500
  87. })
  88. return
  89. }
  90. this.setData({
  91. sec_index,
  92. getOneSummary,
  93. goodsNumber: 1,
  94. getgift
  95. })
  96. },
  97. goodsNumHandle(e) {
  98. let type = e.currentTarget.dataset.type
  99. if (type == 'minus') {
  100. if (this.data.goodsNumber <= 1) {
  101. wx.showToast({
  102. title: '客官!不能再少了!',
  103. icon: 'none',
  104. duration: 1500
  105. })
  106. return
  107. }
  108. else {
  109. this.setData({
  110. goodsNumber: --this.data.goodsNumber
  111. })
  112. }
  113. }
  114. else {
  115. if (this.data.goodsNumber >= this.data.getOneSummary['goods_storage']) {
  116. wx.showToast({
  117. title: '客官!只有这么多了!',
  118. icon: 'none',
  119. duration: 1500
  120. })
  121. return
  122. }
  123. else {
  124. this.setData({
  125. goodsNumber: ++this.data.goodsNumber
  126. })
  127. }
  128. }
  129. },
  130. checkSubmit() {
  131. if (!app.globalData.userInfo) {
  132. wx.navigateTo({
  133. url: '/pages/login/login'
  134. });
  135. return;
  136. }
  137. let cartOrBuy = this.data.cartOrBuy
  138. this.setData({
  139. animation_flag: false
  140. })
  141. if (cartOrBuy == 'isCart') {
  142. wx.showLoading({
  143. title: '加载中',
  144. })
  145. getReq({
  146. act: 'cart',
  147. op: 'addex',
  148. quantity: this.data.goodsNumber,
  149. goods_id: this.data.getOneSummary.goods_id
  150. }, function (res) {
  151. wx.hideLoading()
  152. if (res.code == 200) {
  153. wx.showToast({
  154. icon: 'none',
  155. title: '添加成功!',
  156. duration: 1500
  157. })
  158. }
  159. else {
  160. wx.showToast({
  161. icon: 'none',
  162. title: res.message,
  163. duration: 1500
  164. })
  165. }
  166. })
  167. }
  168. else if (cartOrBuy == 'isBuy') {
  169. if (this.data.getOneSummary.goods_storage < 1) {
  170. wx.showToast({
  171. title: '客官!暂时没有库存!',
  172. icon: 'none',
  173. duration: 1500
  174. })
  175. return
  176. }
  177. wx.navigateTo({
  178. url: `/pages/confirmOrder/confirmOrder?goods_id=${this.data.getOneSummary.goods_id}&iscart=0&num=${this.data.goodsNumber}`
  179. })
  180. }
  181. else {
  182. return
  183. }
  184. },
  185. toWebView() {
  186. let webViewUrl = encodeURIComponent(this.data.webViewUrl)
  187. wx.navigateTo({
  188. url: `/pages/webView/webView?url=${webViewUrl}`
  189. })
  190. },
  191. animation_flag(e) {
  192. let cartOrBuy = e.currentTarget.dataset.cartorbuy || 'none'
  193. let flag = e.currentTarget.dataset.flag
  194. let animation_flag = flag == 'true' ? true : false
  195. this.setData({
  196. animation_flag,
  197. cartOrBuy
  198. })
  199. },
  200. getgift(sumarys, goods_id) {
  201. let goods = this.getOneSummary(sumarys, goods_id)
  202. let giftSummary = ''
  203. if (goods.have_gift) {
  204. giftSummary = this.getGiftSummary(sumarys, goods.gifts)
  205. }
  206. return giftSummary
  207. },
  208. getGiftSummary(sumarys, gifts) {
  209. let arr = []
  210. gifts.map(item => {
  211. sumarys.filter(sum => {
  212. if (item.gift_goods_id == sum.goods_id) {
  213. arr.push({
  214. sum,
  215. gifts: item
  216. })
  217. }
  218. })
  219. })
  220. return arr
  221. },
  222. /**
  223. * 生命周期函数--监听页面初次渲染完成
  224. */
  225. onReady: function () {
  226. },
  227. /**
  228. * 生命周期函数--监听页面显示
  229. */
  230. onShow: function () {
  231. if (app.globalData.fcodeErr) {
  232. wx.showToast({
  233. icon: 'none',
  234. title: app.globalData.fcodeErr,
  235. duration: 1500
  236. })
  237. app.globalData.fcodeErr = ''
  238. this.setData({
  239. animation_flag: false
  240. })
  241. setTimeout(() => {
  242. this.getDatas(this.data.options_goods_id)
  243. }, 1500)
  244. }
  245. if (app.globalData.backLogin) {
  246. this.getDatas(this.data.getOneSummary.goods_id);
  247. }
  248. },
  249. /**
  250. * 生命周期函数--监听页面隐藏
  251. */
  252. onHide: function () {
  253. },
  254. /**
  255. * 生命周期函数--监听页面卸载
  256. */
  257. onUnload: function () {
  258. },
  259. /**
  260. * 页面相关事件处理函数--监听用户下拉动作
  261. */
  262. onPullDownRefresh: function () {
  263. },
  264. /**
  265. * 页面上拉触底事件的处理函数
  266. */
  267. onReachBottom: function () {
  268. },
  269. /**
  270. * 用户点击右上角分享
  271. */
  272. onShareAppMessage: function () {
  273. }
  274. })