index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * Copyright (C) 2018-2019
  3. * All rights reserved, Designed By www.joolun.com
  4. * 注意:
  5. * 本软件为www.joolun.com开发研制,项目使用请保留此说明
  6. */
  7. const app = getApp()
  8. Page({
  9. data: {
  10. config: app.globalData.config,
  11. page: {
  12. current: 1,
  13. size: 50,
  14. ascs: '',//升序字段
  15. descs: 'create_time'
  16. },
  17. parameter: {},
  18. loadmore: true,
  19. operation: true,
  20. shoppingCartData: [],
  21. shoppingCartDataInvalid: [],
  22. isAllSelect: false,//全选
  23. selectValue: [],
  24. settlePrice: 0, //结算金额
  25. goodsSpu: [],
  26. shoppingCartSelect: []
  27. },
  28. onShow() {
  29. //更新tabbar购物车数量
  30. wx.setTabBarBadge({
  31. index: 2,
  32. text: app.globalData.shoppingCartCount + ''
  33. })
  34. app.initPage()
  35. .then(res => {
  36. this.shoppingCartPage()
  37. })
  38. },
  39. onLoad: function () {
  40. app.initPage()
  41. .then(res => {
  42. this.goodsRecom()
  43. })
  44. },
  45. //管理按键事件
  46. operation() {
  47. this.setData({
  48. operation: !this.data.operation
  49. })
  50. this.checkboxHandle(this.data.selectValue)
  51. },
  52. //加载数据
  53. shoppingCartPage(){
  54. app.api.shoppingCartPage(this.data.page)
  55. .then(res => {
  56. //更新购物车数量
  57. app.globalData.shoppingCartCount = res.data.total + ''
  58. wx.setTabBarBadge({
  59. index: 2,
  60. text: app.globalData.shoppingCartCount + ''
  61. })
  62. let shoppingCartData = []
  63. //过滤出失效商品
  64. let shoppingCartDataInvalid = []
  65. res.data.records.forEach(function (shoppingCart, index) {
  66. if (!shoppingCart.goodsSpu || shoppingCart.goodsSpu.shelf == '0'){//下架或删除了
  67. shoppingCartDataInvalid.push(shoppingCart)
  68. }else{
  69. shoppingCartData.push(shoppingCart)
  70. }
  71. })
  72. this.setData({
  73. shoppingCartData: shoppingCartData,
  74. shoppingCartDataInvalid: shoppingCartDataInvalid,
  75. loadmore: false
  76. })
  77. let selectValue = this.data.selectValue
  78. if (selectValue.length > 0) {
  79. this.checkboxHandle(selectValue)
  80. }
  81. })
  82. },
  83. //推荐商品
  84. goodsRecom() {
  85. app.api.goodsPage({
  86. searchCount: false,
  87. current: 1,
  88. size: 4,
  89. descs: 'create_time'
  90. })
  91. .then(res => {
  92. let goodsListRecom = res.data.records
  93. this.setData({
  94. goodsListRecom: goodsListRecom
  95. })
  96. })
  97. },
  98. //数量变化
  99. cartNumChang(e) {
  100. let index = e.target.dataset.index
  101. let shoppingCart = this.data.shoppingCartData[index]
  102. let quantity = Number(e.detail)
  103. this.setData({
  104. [`shoppingCartData[${index}].quantity`]: quantity
  105. })
  106. this.shoppingCartEdit({
  107. id: shoppingCart.id,
  108. quantity: quantity
  109. })
  110. this.countSelect()
  111. },
  112. shoppingCartEdit(parm){
  113. app.api.shoppingCartEdit(parm)
  114. },
  115. //收藏
  116. userCollectAdd(){
  117. },
  118. shoppingCartDel(){
  119. let selectValue = this.data.selectValue
  120. let that = this
  121. if (selectValue.length > 0){
  122. wx.showModal({
  123. content: '确认将这' + selectValue.length+'个宝贝删除',
  124. cancelText: '我再想想',
  125. confirmColor: '#ff0000',
  126. success(res) {
  127. if (res.confirm) {
  128. app.api.shoppingCartDel(selectValue)
  129. .then(res => {
  130. that.setData({
  131. selectValue: [],
  132. isAllSelect: false,
  133. settlePrice: 0
  134. })
  135. that.shoppingCartPage()
  136. })
  137. }
  138. }
  139. })
  140. }
  141. },
  142. clearInvalid(){
  143. let selectValue = []
  144. let that = this
  145. this.data.shoppingCartDataInvalid.forEach(function (shoppingCart, index) {
  146. selectValue.push(shoppingCart.id)
  147. })
  148. wx.showModal({
  149. content: '确认清空失效的宝贝吗',
  150. cancelText: '我再想想',
  151. confirmColor: '#ff0000',
  152. success(res) {
  153. if (res.confirm) {
  154. app.api.shoppingCartDel(selectValue)
  155. .then(res => {
  156. that.setData({
  157. shoppingCartDataInvalid: []
  158. })
  159. })
  160. }
  161. }
  162. })
  163. },
  164. checkboxHandle(selectValue){
  165. let that = this
  166. let shoppingCartData = this.data.shoppingCartData
  167. let isAllSelect = false
  168. if (shoppingCartData.length == selectValue.length) { isAllSelect = true }
  169. if (selectValue.length > 0) {
  170. let shoppingCartIds = []
  171. shoppingCartData.forEach(function (shoppingCart, index) {
  172. shoppingCartIds.push(shoppingCart.id)
  173. let selectValueIndex = selectValue.indexOf(shoppingCart.id)
  174. if (selectValueIndex > -1) {
  175. if (!that.data.operation){
  176. shoppingCart.checked = true
  177. }else{//如果是购买操作,需过滤不符商品
  178. if (shoppingCart.goodsSpu && shoppingCart.quantity <= shoppingCart.goodsSpu.stock) {
  179. shoppingCart.checked = true
  180. } else {
  181. shoppingCart.checked = false
  182. selectValue.splice(selectValueIndex, 1)
  183. }
  184. }
  185. } else {
  186. shoppingCart.checked = false
  187. }
  188. })
  189. selectValue.forEach(function (obj, index) {
  190. if (shoppingCartIds.indexOf(obj) <= -1) {
  191. selectValue.splice(index, 1)
  192. }
  193. })
  194. }else{
  195. shoppingCartData.forEach(function (shoppingCart, index) {
  196. shoppingCart.checked = false
  197. })
  198. }
  199. this.setData({
  200. shoppingCartData: shoppingCartData,
  201. isAllSelect: isAllSelect,
  202. selectValue: selectValue
  203. })
  204. this.countSelect()
  205. },
  206. checkboxChange: function (e) {
  207. this.checkboxHandle(e.detail.value)
  208. },
  209. checkboxAllChange(e) {
  210. var value = e.detail.value;
  211. if (value.length > 0) {
  212. this.setAllSelectValue(true)
  213. }else {
  214. this.setAllSelectValue(false)
  215. }
  216. },
  217. setAllSelectValue(status) {
  218. let shoppingCartData = this.data.shoppingCartData
  219. let selectValue = []
  220. let that = this
  221. if (shoppingCartData.length > 0) {
  222. if (status) {
  223. shoppingCartData.forEach(function (shoppingCart, index) {
  224. if (!that.data.operation) {
  225. selectValue.push(shoppingCart.id)
  226. } else {//如果是购买操作,需过滤不符商品
  227. if (shoppingCart.goodsSpu
  228. && shoppingCart.quantity <= shoppingCart.goodsSpu.stock) {
  229. selectValue.push(shoppingCart.id)
  230. }
  231. }
  232. })
  233. }
  234. this.checkboxHandle(selectValue)
  235. }
  236. },
  237. //计算结算值
  238. countSelect() {
  239. let selectValue = this.data.selectValue
  240. let settlePrice = 0
  241. if (selectValue.length <= 0) {
  242. this.setData({
  243. settlePrice: settlePrice
  244. })
  245. }else {
  246. this.data.shoppingCartData.forEach(function (shoppingCart, index) {
  247. if (selectValue.indexOf(shoppingCart.id) > -1
  248. && shoppingCart.goodsSpu
  249. && shoppingCart.quantity <= shoppingCart.goodsSpu.stock) {
  250. settlePrice = Number(settlePrice) + Number(shoppingCart.quantity) * Number(shoppingCart.goodsSpu.salesPrice)
  251. }
  252. })
  253. this.setData({
  254. settlePrice: settlePrice.toFixed(2)
  255. })
  256. }
  257. },
  258. //更换规格
  259. changeSpecs(e){
  260. },
  261. goodsGet(id) {
  262. app.api.goodsGet(id)
  263. .then(res => {
  264. let goodsSpu = res.data
  265. this.setData({
  266. goodsSpu: goodsSpu
  267. })
  268. })
  269. },
  270. operateCartEvent() {
  271. this.shoppingCartPage()
  272. },
  273. //结算
  274. orderConfirm(){
  275. let params = []
  276. let shoppingCartData = this.data.shoppingCartData
  277. shoppingCartData.forEach(function (shoppingCart, index) {
  278. if (shoppingCart.checked && shoppingCart.goodsSpu
  279. && shoppingCart.goodsSpu && shoppingCart.goodsSpu.shelf == '1'
  280. && shoppingCart.quantity <= shoppingCart.goodsSpu.stock){
  281. let param = {
  282. spuId: shoppingCart.spuId,
  283. quantity: shoppingCart.quantity,
  284. salesPrice: shoppingCart.goodsSpu.salesPrice,
  285. spuName: shoppingCart.goodsSpu.name,
  286. picUrl: shoppingCart.goodsSpu.picUrls?shoppingCart.goodsSpu.picUrls[0]:''
  287. }
  288. params.push(param)
  289. }
  290. })
  291. /* 把参数信息异步存储到缓存当中 */
  292. wx.setStorage({
  293. key: 'param-orderConfirm',
  294. data: params
  295. })
  296. wx.navigateTo({
  297. url: '/pages/order/order-confirm/index'
  298. })
  299. }
  300. })