cart.js 11 KB

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