index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // pages/goods-details/index.js
  2. var app = getApp()
  3. var WxParse = require('../../wxParse/wxParse.js')
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. hideShopPopup: true, //规格弹出框
  10. indicatorDors: true,
  11. autoplay: true,
  12. interval: 4000,
  13. duration: 500,
  14. goodsDetail:{}, //商品详情对象
  15. buyNumMax:0, //最大购买数量
  16. buyNumMin: 0, //最小购买数量
  17. buyNumber: 0, //单个购买数量
  18. shopNum: 0, //全部购买数量
  19. propertyChildIds: '',//规格属性id
  20. propertyChildNames: '',//规格属性名
  21. selectSizePrice: 0,//单个选择商品价格
  22. selectSize: '选择: ',//选择规格
  23. hasMoreSelect: false,//是否有多种规格可选
  24. shopCarInfo: {},//设置在缓存的订单信息
  25. canSubmit: false,//是否能添加到订单
  26. imgurl: app.data.resturl,
  27. comments: [],
  28. id:'',
  29. zanstatus: 'N',
  30. znum: 0
  31. },
  32. onLoad:function(options){
  33. this.setData({
  34. id:options.id
  35. })
  36. },
  37. /**
  38. * 生命周期函数--监听页面加载
  39. */
  40. onShow: function (options) {
  41. var that = this
  42. //加载缓存,取得订单信息
  43. wx.getStorage({
  44. key: 'shopCarInfo',
  45. success:function(res){
  46. // console.log(`initshopCarInfo:${res.data}`)
  47. that.setData({
  48. shopCarInfo: res.data,
  49. shopNum: res.data.shopNum
  50. })
  51. }
  52. })
  53. let id = this.data.id;
  54. let openid = app.globalData.openid;
  55. //请求该商品详细数据
  56. wx.request({
  57. url: app.data.resturl + '/ginfo.action',
  58. data: {
  59. id: id,
  60. openid:openid
  61. },
  62. success:function(res){
  63. // console.log(res.data.data)
  64. let selectSizeTemp = ''
  65. that.setData({
  66. goodsDetail: res.data.dinfo,
  67. znum: res.data.dinfo.zan,
  68. zanstatus: res.data.dinfo.praiseStatus,
  69. comments: res.data.dlist,
  70. buyNumber:1
  71. })
  72. WxParse.wxParse('article', 'html', res.data.dinfo.goodsMiaoshu, that, 5)
  73. }
  74. })
  75. },
  76. addShopCar: function() {
  77. if (this.data.goodsDetail.properties && !this.data.canSubmit){
  78. this.bindGuiGeTap()
  79. return
  80. }
  81. let shopCarMap = {}
  82. shopCarMap.goodsId = this.data.goodsDetail.goodsId;
  83. shopCarMap.pic = this.data.imgurl + this.data.goodsDetail.goodsPic;
  84. shopCarMap.name = this.data.goodsDetail.goodsName;
  85. shopCarMap.label = this.data.goodsDetail.goodsName;
  86. shopCarMap.price = this.data.goodsDetail.goodsShichangjia
  87. shopCarMap.left = ''
  88. shopCarMap.active = true
  89. shopCarMap.number = this.data.buyNumber
  90. var shopCarInfo = this.data.shopCarInfo
  91. if (!shopCarInfo.shopNum){
  92. shopCarInfo.shopNum = 0
  93. }
  94. if(!shopCarInfo.shoplist){
  95. shopCarInfo.shoplist= []
  96. }
  97. var hasSameGoodsIndex = -1
  98. for (var i = 0; i < shopCarInfo.shoplist.length;i++){
  99. var tamShopCarMap = shopCarInfo.shoplist[i]
  100. if (tamShopCarMap.goodsId === shopCarMap.goodsId){
  101. shopCarMap.number = shopCarMap.number + tamShopCarMap.number
  102. hasSameGoodsIndex = i
  103. break
  104. }
  105. }
  106. shopCarInfo.shopNum = shopCarInfo.shopNum + this.data.buyNumber
  107. if (hasSameGoodsIndex > -1) {
  108. shopCarInfo.shoplist.splice(hasSameGoodsIndex, 1, shopCarMap)
  109. } else{
  110. shopCarInfo.shoplist.push(shopCarMap)
  111. }
  112. this.setData({
  113. shopCarInfo: shopCarInfo,
  114. shopNum: shopCarInfo.shopNum
  115. })
  116. this.closePopupTap()
  117. // wx.showToast({
  118. // title: '加入购物车成功',
  119. // icon: 'sucess',
  120. // duration: 2000
  121. // })
  122. wx.setStorage({
  123. key: 'shopCarInfo',
  124. data: shopCarInfo
  125. })
  126. },
  127. bindGuiGeTap(){
  128. this.setData({
  129. hideShopPopup: false
  130. })
  131. },
  132. closePopupTap(){
  133. this.setData({
  134. hideShopPopup: true
  135. })
  136. },
  137. numJiaTap(){
  138. let buyNumber = this.data.buyNumber
  139. if (buyNumber < this.data.buyNumMax){
  140. buyNumber++
  141. }
  142. console.log(buyNumber)
  143. this.setData({
  144. buyNumber: buyNumber
  145. })
  146. },
  147. numJianTap() {
  148. let buyNumber = this.data.buyNumber
  149. if (buyNumber > this.data.buyNumMin) {
  150. buyNumber--
  151. }
  152. this.setData({
  153. buyNumber: buyNumber
  154. })
  155. },
  156. goShopCar() {
  157. wx.reLaunch({
  158. //url: '../shop-cart/index'
  159. url: '../to-pay-order/index'
  160. })
  161. },
  162. tobuy(){
  163. if (this.data.goodsDetail.properties && !this.data.canSumbit){
  164. this.bindGuiGeTap()
  165. return
  166. }
  167. if (this.data.buyNumber<1){
  168. wx.showModal({
  169. title: '提示',
  170. concent:'暂时缺货哦~',
  171. showCancel:fasle
  172. })
  173. return
  174. }
  175. this.addShopCar()
  176. this.goShopCar()
  177. },
  178. /**
  179. * 生命周期函数--监听页面初次渲染完成
  180. */
  181. onReady: function () {
  182. },
  183. zan: function () {
  184. let openid = app.globalData.openid;
  185. if (openid == '') {
  186. wx.redirectTo({
  187. url: '../../pages/login/login',
  188. })
  189. return;
  190. }
  191. let id = this.data.goodsDetail.goodsId;
  192. let zstatus = this.data.zanstatus;
  193. let zcount = this.data.znum;
  194. let types = 1;
  195. if (zstatus == 'Y') {
  196. types = 2;
  197. }
  198. let param = "tcid=" + id + "&uid=" + openid + "&types=" + types;
  199. //console.log(param);
  200. let that = this;
  201. wx.request({
  202. url: app.data.resturl + '/praise.action',
  203. header: {
  204. 'content-type': 'application/x-www-form-urlencoded'
  205. },
  206. method: 'post',
  207. data: param,
  208. success: function (resp) {
  209. let rescode = resp.data.code;
  210. if (rescode == 200) {
  211. if (zstatus == 'N') {
  212. zstatus = 'Y';
  213. zcount = zcount + 1;
  214. } else {
  215. zstatus = 'N';
  216. zcount = zcount - 1;
  217. if (zcount < 0) { zcount = 0; }
  218. }
  219. that.setData({
  220. zanstatus: zstatus,
  221. znum: zcount
  222. });
  223. }
  224. }
  225. })
  226. },
  227. /**
  228. * 生命周期函数--监听页面隐藏
  229. */
  230. onHide: function () {
  231. },
  232. /**
  233. * 生命周期函数--监听页面卸载
  234. */
  235. onUnload: function () {
  236. },
  237. /**
  238. * 页面相关事件处理函数--监听用户下拉动作
  239. */
  240. onPullDownRefresh: function () {
  241. },
  242. /**
  243. * 页面上拉触底事件的处理函数
  244. */
  245. onReachBottom: function () {
  246. },
  247. /**
  248. * 用户点击右上角分享
  249. */
  250. onShareAppMessage: function () {
  251. }
  252. })