cartList.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // pages/cartList/cartList.js
  2. var app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. startX: 0, //开始坐标
  9. startY: 0,
  10. userInfo: wx.getStorageSync('userInfo'),
  11. checkAll: false,
  12. loading: false,
  13. load: false,
  14. checkNum: 0,
  15. checkPrice: 0.00,
  16. cartList: []
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad(options) {},
  22. /**
  23. * 生命周期函数--监听页面初次渲染完成
  24. */
  25. onReady() {
  26. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  27. // let userInfo = wx.getStorageSync('userInfo');
  28. // if (userInfo && userInfo.userId) {
  29. // app.globalData.tabbar.list[0].show = false;
  30. // app.globalData.tabbar.list[1].show = true;
  31. // } else {
  32. // app.globalData.tabbar.list[0].show = true;
  33. // }
  34. // this.getTabBar().setData({
  35. // selectIndex: 3,
  36. // tabbar: app.globalData.tabbar
  37. // })
  38. }
  39. },
  40. /**
  41. * 生命周期函数--监听页面显示
  42. */
  43. onShow() {
  44. let userInfo = wx.getStorageSync('userInfo');
  45. this.setData({
  46. userInfo,
  47. })
  48. this.setData({
  49. checkAll: false,
  50. checkNum: 0,
  51. checkPrice: 0.00,
  52. })
  53. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  54. // if (userInfo && userInfo.userId) {
  55. // app.globalData.tabbar.list[0].show = false;
  56. // app.globalData.tabbar.list[1].show = true;
  57. // } else {
  58. // app.globalData.tabbar.list[0].show = true;
  59. // }
  60. // this.getTabBar().setData({
  61. // selectIndex: 3,
  62. // tabbar: app.globalData.tabbar
  63. // })
  64. }
  65. wx.setNavigationBarTitle({
  66. title: app.globalData.tabbar.list[2].text,
  67. });
  68. if (userInfo && userInfo.userId)
  69. this.initData();
  70. },
  71. async initData() {
  72. this.setData({
  73. loading: !this.data.load
  74. })
  75. let {
  76. data
  77. } = await app.ajax.get({
  78. url: `/admin/bxdusercollection/query?userId=${this.data.userInfo.userId}&virtualInventoryId=${wx.getStorageSync('virtualInventoryId')}`,
  79. });
  80. let cartList = data.data.map(v => {
  81. v["picArr"] = v.pic ? v.pic.split(",") : null;
  82. return v;
  83. });
  84. this.setData({
  85. cartList: cartList,
  86. loading: false,
  87. load: true
  88. });
  89. this.switchChange({
  90. currentTarget: {
  91. dataset: {
  92. all: true
  93. }
  94. }
  95. });
  96. },
  97. switchChange(e) {
  98. let cartList = this.data.cartList;
  99. const allCHave = cartList.every(item => item.check == true);
  100. let checkAll = null;
  101. if (e.currentTarget.dataset.all) {
  102. if (!allCHave) {
  103. cartList.map(v => {
  104. v.check = true;
  105. });
  106. checkAll = true;
  107. } else {
  108. cartList.map(v => {
  109. v.check = false;
  110. });
  111. checkAll = true;
  112. }
  113. } else {
  114. cartList[e.currentTarget.dataset.index].check = !cartList[e.currentTarget.dataset.index].check;
  115. }
  116. this.setData({
  117. cartList,
  118. checkAll: cartList.every(item => item.check == true),
  119. checkNum: cartList.filter((item => item.check)).length,
  120. checkPrice: cartList.filter((item => item.check)).map(v => {
  121. return {
  122. price: app.currency(v.price).multiply(v.qty).value
  123. }
  124. }).reduce((price, item1) => app.currency(price).add(item1.price).value, 0),
  125. })
  126. },
  127. async jianNum(e) {
  128. let cartList = this.data.cartList;
  129. let cart = cartList[e.currentTarget.dataset.index];
  130. if (cart.qty <= 1) return;
  131. cart.qty = (cart.qty || 1) - 1;
  132. try {
  133. await this.updateSku({
  134. id: cart.id,
  135. qty: cart.qty
  136. });
  137. // this.setData({
  138. // cartList,
  139. // checkPrice: cartList.filter((item => item.check)).map(v => {
  140. // return {
  141. // price: app.currency(v.price).multiply(v.qty).value
  142. // }
  143. // }).reduce((price, item1) => app.currency(price).add(item1.price).value, 0),
  144. // })
  145. this.initData();
  146. } catch (e) {}
  147. },
  148. async jiaNum(e) {
  149. let cartList = this.data.cartList;
  150. let cart = cartList[e.currentTarget.dataset.index];
  151. if (cart.qty > cart.usableStocks || cart.qty == cart.limitQty) return;
  152. try {
  153. cart.qty = (cart.qty || 1) + 1;
  154. await this.updateSku({
  155. id: cart.id,
  156. qty: cart.qty
  157. });
  158. // this.setData({
  159. // cartList,
  160. // checkPrice: cartList.filter((item => item.check)).map(v => {
  161. // return {
  162. // price: app.currency(v.price).multiply(v.qty).value
  163. // }
  164. // }).reduce((price, item1) => app.currency(price).add(item1.price).value, 0),
  165. // });
  166. this.initData();
  167. } catch (e) {}
  168. },
  169. nextBtn() {
  170. let role = wx.getStorageSync('roleList').find(v => v.roleCode == 'User' || v.roleCode == 'SZMAdmin');
  171. if (!role) {
  172. wx.showToast({
  173. title: '您无下单权限!',
  174. icon: "error"
  175. })
  176. return;
  177. }
  178. let cartList = this.data.cartList.filter((item => item.check));
  179. if (cartList.length == 0) {
  180. wx.showToast({
  181. title: '请选择商品',
  182. icon: "error"
  183. });
  184. return;
  185. }
  186. wx.navigateTo({
  187. url: '/pages/order/create/create',
  188. success: function (res) {
  189. // 通过eventChannel向被打开页面传送数据
  190. res.eventChannel.emit('acceptDataFromOpenerPage', {
  191. goodsList: cartList,
  192. checkPrice: cartList.map(v => {
  193. return {
  194. price: app.currency(v.price).multiply(v.qty).value
  195. }
  196. }).reduce((price, item1) => app.currency(price).add(item1.price).value, 0),
  197. })
  198. }
  199. })
  200. },
  201. async updateSku(data) {
  202. await app.ajax.post({
  203. url: "/admin/bxdusercollection/updateQtyById",
  204. method: "PUT",
  205. data
  206. });
  207. },
  208. async delBtn() {
  209. let ids = this.data.cartList.filter(v => v.check).map(v => v.id).join(",");
  210. if (!ids) {
  211. return;
  212. }
  213. let that = this;
  214. wx.showModal({
  215. title: "删除提示",
  216. content: `确定删除这些商品吗?`,
  217. async success(res) {
  218. if (res.confirm) {
  219. await app.ajax.post({
  220. url: `/admin/bxdusercollection/removeByIds?ids=${ids}`,
  221. method: "DELETE"
  222. });
  223. wx.showToast({
  224. title: '删除成功'
  225. })
  226. that.setData({
  227. current: 0,
  228. cartList: []
  229. })
  230. setTimeout(async () => {
  231. await that.initData();
  232. }, 1000);
  233. }
  234. }
  235. })
  236. },
  237. async delBtn1(e) {
  238. let ids = e.currentTarget.dataset.item.id;
  239. if (!ids) {
  240. return;
  241. }
  242. let that = this;
  243. wx.showModal({
  244. title: "删除提示",
  245. content: `确定删除这个商品吗?`,
  246. async success(res) {
  247. if (res.confirm) {
  248. await app.ajax.post({
  249. url: `/admin/bxdusercollection/removeByIds?ids=${ids}`,
  250. method: "DELETE"
  251. });
  252. wx.showToast({
  253. title: '删除成功'
  254. })
  255. that.setData({
  256. current: 0,
  257. cartList: []
  258. })
  259. setTimeout(async () => {
  260. await that.initData();
  261. }, 1000);
  262. }
  263. }
  264. })
  265. },
  266. /**
  267. * 生命周期函数--监听页面隐藏
  268. */
  269. onHide() {
  270. // this.setData({
  271. // checkAll: false,
  272. // checkNum: 0,
  273. // checkPrice: 0.00,
  274. // })
  275. },
  276. toLogin() {
  277. wx.navigateTo({
  278. url: '/pages/login/login',
  279. })
  280. },
  281. toGoods(e) {
  282. wx.navigateTo({
  283. url: `/pages/goods/goods?prodId=${e.currentTarget.dataset.item.prodId}`,
  284. })
  285. },
  286. /**
  287. * 生命周期函数--监听页面卸载
  288. */
  289. onUnload() {
  290. },
  291. /**
  292. * 页面相关事件处理函数--监听用户下拉动作
  293. */
  294. onPullDownRefresh() {
  295. },
  296. /**
  297. * 页面上拉触底事件的处理函数
  298. */
  299. onReachBottom() {
  300. },
  301. /**
  302. * 用户点击右上角分享
  303. */
  304. onShareAppMessage() {
  305. },
  306. touchE: function (e) {
  307. // //console.log(e);
  308. var that = this
  309. if (e.changedTouches.length == 1) {
  310. //手指移动结束后触摸点位置的X坐标
  311. var endX = e.changedTouches[0].clientX;
  312. //触摸开始与结束,手指移动的距离
  313. var disX = that.data.startX - endX;
  314. var delBtnWidth = 120;
  315. //如果距离小于删除按钮的1/2,不显示删除按钮
  316. var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "rpx" : "left:0rpx";
  317. //获取手指触摸的是哪一项
  318. var index = e.currentTarget.dataset.index;
  319. var list = that.data.cartList;
  320. list[index].txtStyle = txtStyle;
  321. //更新列表的状态
  322. that.setData({
  323. cartList: list
  324. });
  325. }
  326. },
  327. //手指触摸动作开始 记录起点X坐标
  328. touchstart: function (e) {
  329. //开始触摸时 重置所有删除
  330. this.data.cartList.forEach(function (v, i) {
  331. if (v.isTouchMove) //只操作为true的
  332. v.isTouchMove = false;
  333. })
  334. this.setData({
  335. startX: e.changedTouches[0].clientX,
  336. startY: e.changedTouches[0].clientY,
  337. cartList: this.data.cartList
  338. })
  339. },
  340. //滑动事件处理
  341. touchmove: function (e) {
  342. var that = this,
  343. index = e.currentTarget.dataset.index, //当前索引
  344. startX = that.data.startX, //开始X坐标
  345. startY = that.data.startY, //开始Y坐标
  346. touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
  347. touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
  348. //获取滑动角度
  349. angle = that.angle({
  350. X: startX,
  351. Y: startY
  352. }, {
  353. X: touchMoveX,
  354. Y: touchMoveY
  355. });
  356. that.data.cartList.forEach(function (v, i) {
  357. v.isTouchMove = false
  358. //滑动超过30度角 return
  359. if (Math.abs(angle) > 30) return;
  360. if (i == index) {
  361. if (touchMoveX > startX) //右滑
  362. v.isTouchMove = false
  363. else //左滑
  364. v.isTouchMove = true
  365. }
  366. })
  367. //更新数据
  368. that.setData({
  369. cartList: that.data.cartList
  370. })
  371. },
  372. /**
  373. * 计算滑动角度
  374. * @param {Object} start 起点坐标
  375. * @param {Object} end 终点坐标
  376. */
  377. angle: function (start, end) {
  378. var _X = end.X - start.X,
  379. _Y = end.Y - start.Y
  380. //返回角度 /Math.atan()返回数字的反正切值
  381. return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  382. },
  383. })