index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // pages/shop-cart/index.js
  2. var app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. goodsList: {
  9. saveHidden: true,
  10. totalPrice: 0,
  11. allSelect: true,
  12. noSelect: false,
  13. list: []
  14. },
  15. },
  16. /**
  17. * 生命周期函数--监听页面显示
  18. */
  19. onShow: function () {
  20. this.initEleWidth();
  21. this.cartShow()
  22. },
  23. toIndexPage:function(){
  24. console.log('fff');
  25. wx.switchTab({
  26. url: '../../pages/classify/index',
  27. })
  28. },
  29. //获取元素自适应后的实际宽度
  30. getEleWidth: function (w) {
  31. var real = 0;
  32. try {
  33. var res = wx.getSystemInfoSync().windowWidth;
  34. var scale = (750 / 2) / (w / 2); //以宽度750px设计稿做宽度的自适应
  35. // console.log(scale);
  36. real = Math.floor(res / scale);
  37. return real;
  38. } catch (e) {
  39. return false;
  40. // Do something when catch error
  41. }
  42. },
  43. initEleWidth: function () {
  44. var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
  45. this.setData({
  46. delBtnWidth: delBtnWidth
  47. });
  48. },
  49. /**
  50. * 生命周期函数--监听页面加载
  51. */
  52. /*onLoad: function (options) {
  53. this.initEleWidth();
  54. this.onshow()
  55. },*/
  56. cartShow:function() {
  57. var shoplist = [];
  58. // 获取订单数据
  59. var shopCarInfoMem = wx.getStorageSync('shopCarInfo')
  60. if (shopCarInfoMem && shopCarInfoMem.shoplist) {
  61. shoplist = shopCarInfoMem.shoplist
  62. }
  63. // this.totalPrice()
  64. this.data.goodsList.list = shoplist;
  65. this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), shoplist);
  66. },
  67. getSaveHide: function () {
  68. var saveHidden = this.data.goodsList.saveHidden;
  69. return saveHidden;
  70. },
  71. allSelect:function() {
  72. var list = this.data.goodsList.list;
  73. var allSelect = false
  74. for (var i = 0; i < list.length; i++) {
  75. var curItem = list[i]
  76. if (curItem.active) {
  77. allSelect = true
  78. } else{
  79. allSelect = false
  80. break
  81. }
  82. }
  83. return allSelect
  84. },
  85. noSelect: function(){
  86. var list = this.data.goodsList.list;
  87. var noSelect = false
  88. for (var i = 0; i < list.length; i++) {
  89. var curItem = list[i]
  90. if (curItem.active) {
  91. noSelect = false
  92. break
  93. } else {
  94. noSelect = false
  95. }
  96. }
  97. return noSelect
  98. /*return list.forEach((item) => {
  99. if (item.active) {
  100. return false
  101. } else {
  102. return true
  103. }
  104. })*/
  105. },
  106. totalPrice: function () {
  107. var list = this.data.goodsList.list;
  108. var total = 0;
  109. for (var i = 0; i < list.length; i++) {
  110. var curItem = list[i];
  111. if (curItem.active) {
  112. total += parseFloat(curItem.price) * curItem.number;
  113. }
  114. }
  115. return total
  116. },
  117. toPayOrder(total) {
  118. this.setData({
  119. totalPrice: total
  120. })
  121. },
  122. selectTap(e) {
  123. var index = e.currentTarget.dataset.index
  124. var list = this.data.goodsList.list
  125. if (index!=='' && index!==null){
  126. list[parseInt(index)].active = !list[parseInt(index)].active
  127. this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list)
  128. }
  129. },
  130. bindAllSelect() {
  131. var list = this.data.goodsList.list;
  132. var currentAllSelect = this.data.goodsList.allSelect
  133. if (currentAllSelect) {
  134. list.forEach((item) => {
  135. item.active = false
  136. })
  137. } else {
  138. list.forEach((item) => {
  139. item.active = true
  140. })
  141. }
  142. this.setGoodsList(this.getSaveHide(), this.totalPrice(), !currentAllSelect, this.noSelect(), list);
  143. },
  144. saveHidden:function() {
  145. return this.data.goodsList.saveHidden
  146. },
  147. setGoodsList: function (saveHidden, total, allSelect, noSelect, list) {
  148. this.setData({
  149. goodsList: {
  150. saveHidden: saveHidden,
  151. totalPrice: total,
  152. allSelect: allSelect,
  153. noSelect: noSelect,
  154. list: list
  155. }
  156. });
  157. var shopCarInfo = {};
  158. var tempNumber = 0;
  159. shopCarInfo.shoplist = list;
  160. for (var i = 0; i < list.length; i++) {
  161. tempNumber = tempNumber + list[i].number
  162. }
  163. shopCarInfo.shopNum = tempNumber;
  164. wx.setStorage({
  165. key: "shopCarInfo",
  166. data: shopCarInfo
  167. })
  168. },
  169. jiaBtnTap(e){
  170. var index = parseInt(e.currentTarget.dataset.index)
  171. var list = this.data.goodsList.list
  172. if (index!==null && index !== '')
  173. if (list[index].number<10) {
  174. list[index].number++
  175. this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
  176. }
  177. },
  178. jianBtnTap(e) {
  179. var index = parseInt(e.currentTarget.dataset.index)
  180. var list = this.data.goodsList.list
  181. if (index !== null && index !== '')
  182. if (list[index].number > 1) {
  183. list[index].number--
  184. this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
  185. }
  186. },
  187. touchS(e) {
  188. if (e.touches.length == 1){
  189. this.setData({
  190. startX: e.touches[0].clientX
  191. })
  192. }
  193. },
  194. touchM(e) {
  195. var index = parseInt(e.currentTarget.dataset.index)
  196. var left = ''
  197. if(e.touches.length == 1){
  198. var moveX = e.touches[0].clientX
  199. var disX = this.data.startX - moveX
  200. var btnWidth = 120
  201. if (disX <= 0){
  202. left = 0
  203. } else if (disX>0 ){
  204. left = `-${disX}rpx`
  205. if (disX>=btnWidth){
  206. left = `-${btnWidth}rpx`
  207. }
  208. }
  209. var list = this.data.goodsList.list
  210. if (index !== '' && index!==null){
  211. list[index].left = left
  212. this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
  213. }
  214. }
  215. },
  216. touchE(e) {
  217. var left = ''
  218. var index = parseInt(e.currentTarget.dataset.index)
  219. if (e.changedTouches.length == 1){
  220. var endX = e.changedTouches[0].clientX
  221. var disX = this.data.startX - endX
  222. var btnWidth = 120
  223. disX >= btnWidth/2 ? left = `-${btnWidth}rpx` : left = 0
  224. }
  225. var list = this.data.goodsList.list
  226. if (index !== '' && index !== null) {
  227. list[index].left = left
  228. this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
  229. }
  230. },
  231. delItem(e) {
  232. var index = e.currentTarget.dataset.index
  233. var list = this.data.goodsList.list
  234. list.splice(index,1)
  235. this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
  236. },
  237. editTap(){
  238. var list = this.data.goodsList.list
  239. list.forEach((item)=>{
  240. item.active = false
  241. })
  242. this.setGoodsList(!this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
  243. },
  244. saveTap(){
  245. var list = this.data.goodsList.list
  246. list.forEach((item) => {
  247. item.active = true
  248. })
  249. this.setGoodsList(!this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), list);
  250. },
  251. deleteSelected(){
  252. var list = this.data.goodsList.list
  253. var newList = []
  254. list.forEach((item) => {
  255. if (!item.active) {
  256. newList.push(item)
  257. }
  258. })
  259. this.setGoodsList(this.getSaveHide(), this.totalPrice(), this.allSelect(), this.noSelect(), newList);
  260. },
  261. toPayOrder(){
  262. let that = this
  263. wx.showLoading()
  264. if (this.data.goodsList.noSelect){
  265. return
  266. }
  267. let shopList = []
  268. let shopListMap = wx.getStorageSync('shopCarInfo')
  269. if (shopListMap && shopListMap.shoplist){
  270. shopList = shopListMap.shoplist
  271. }
  272. if (shopList.length==0){
  273. return
  274. }
  275. wx.hideLoading();
  276. that.navToPayOrder();
  277. },
  278. navToPayOrder() {
  279. wx.hideLoading()
  280. wx.navigateTo({
  281. url: "/pages/to-pay-order/index",
  282. })
  283. },
  284. /**
  285. * 生命周期函数--监听页面初次渲染完成
  286. */
  287. onReady: function () {
  288. },
  289. /**
  290. * 生命周期函数--监听页面隐藏
  291. */
  292. onHide: function () {
  293. },
  294. /**
  295. * 生命周期函数--监听页面卸载
  296. */
  297. onUnload: function () {
  298. },
  299. /**
  300. * 页面相关事件处理函数--监听用户下拉动作
  301. */
  302. onPullDownRefresh: function () {
  303. },
  304. /**
  305. * 页面上拉触底事件的处理函数
  306. */
  307. onReachBottom: function () {
  308. },
  309. /**
  310. * 用户点击右上角分享
  311. */
  312. onShareAppMessage: function () {
  313. }
  314. })