index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. const WXAPI = require('apifm-wxapi')
  2. const TOOLS = require('../../utils/tools.js')
  3. const AUTH = require('../../utils/auth')
  4. const app = getApp()
  5. Page({
  6. data: {
  7. shopCarType: 0, //0自营 1云货架
  8. saveHidden: true,
  9. allSelect: true,
  10. delBtnWidth: 120, //删除按钮宽度单位(rpx)
  11. },
  12. //获取元素自适应后的实际宽度
  13. getEleWidth: function (w) {
  14. var real = 0;
  15. try {
  16. var res = wx.getSystemInfoSync().windowWidth
  17. var scale = (750 / 2) / (w / 2)
  18. // console.log(scale);
  19. real = Math.floor(res / scale);
  20. return real;
  21. } catch (e) {
  22. return false;
  23. // Do something when catch error
  24. }
  25. },
  26. initEleWidth: function () {
  27. var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
  28. this.setData({
  29. delBtnWidth: delBtnWidth
  30. });
  31. },
  32. onLoad: function () {
  33. this.initEleWidth();
  34. this.onShow();
  35. this.setData({
  36. shopping_cart_vop_open: wx.getStorageSync('shopping_cart_vop_open')
  37. })
  38. },
  39. onShow: function () {
  40. this.shippingCarInfo()
  41. },
  42. async shippingCarInfo() {
  43. const token = wx.getStorageSync('token')
  44. if (!token) {
  45. return
  46. }
  47. if (this.data.shopCarType == 0) { //自营购物车
  48. var res = await WXAPI.shippingCarInfo(token)
  49. } else if (this.data.shopCarType == 1) { //云货架购物车
  50. var res = await WXAPI.jdvopCartInfo(token)
  51. }
  52. if (res.code == 0) {
  53. if (this.data.shopCarType == 0) //自营商品
  54. {
  55. res.data.items.forEach(ele => {
  56. if (!ele.stores || ele.status == 1) {
  57. ele.selected = false
  58. }
  59. })
  60. }
  61. this.setData({
  62. shippingCarInfo: res.data
  63. })
  64. } else {
  65. this.setData({
  66. shippingCarInfo: null
  67. })
  68. }
  69. },
  70. toIndexPage: function () {
  71. wx.switchTab({
  72. url: "/pages/index/index"
  73. });
  74. },
  75. touchS: function (e) {
  76. if (e.touches.length == 1) {
  77. this.setData({
  78. startX: e.touches[0].clientX
  79. });
  80. }
  81. },
  82. touchM: function (e) {
  83. const index = e.currentTarget.dataset.index;
  84. if (e.touches.length == 1) {
  85. var moveX = e.touches[0].clientX;
  86. var disX = this.data.startX - moveX;
  87. var delBtnWidth = this.data.delBtnWidth;
  88. var left = "";
  89. if (disX == 0 || disX < 0) { //如果移动距离小于等于0,container位置不变
  90. left = "margin-left:0px";
  91. } else if (disX > 0) { //移动距离大于0,container left值等于手指移动距离
  92. left = "margin-left:-" + disX + "px";
  93. if (disX >= delBtnWidth) {
  94. left = "left:-" + delBtnWidth + "px";
  95. }
  96. }
  97. this.data.shippingCarInfo.items[index].left = left
  98. this.setData({
  99. shippingCarInfo: this.data.shippingCarInfo
  100. })
  101. }
  102. },
  103. touchE: function (e) {
  104. var index = e.currentTarget.dataset.index;
  105. if (e.changedTouches.length == 1) {
  106. var endX = e.changedTouches[0].clientX;
  107. var disX = this.data.startX - endX;
  108. var delBtnWidth = this.data.delBtnWidth;
  109. //如果距离小于删除按钮的1/2,不显示删除按钮
  110. var left = disX > delBtnWidth / 2 ? "margin-left:-" + delBtnWidth + "px" : "margin-left:0px";
  111. this.data.shippingCarInfo.items[index].left = left
  112. this.setData({
  113. shippingCarInfo: this.data.shippingCarInfo
  114. })
  115. }
  116. },
  117. async delItem(e) {
  118. const key = e.currentTarget.dataset.key
  119. this.delItemDone(key)
  120. },
  121. async delItemDone(key) {
  122. const token = wx.getStorageSync('token')
  123. if(this.data.shopCarType == 0){
  124. var res = await WXAPI.shippingCarInfoRemoveItem(token, key)
  125. }
  126. if(this.data.shopCarType == 1){
  127. var res = await WXAPI.jdvopCartRemove(token, key)
  128. }
  129. if (res.code != 0 && res.code != 700) {
  130. wx.showToast({
  131. title: res.msg,
  132. icon: 'none'
  133. })
  134. } else {
  135. this.shippingCarInfo()
  136. TOOLS.showTabBarBadge()
  137. }
  138. },
  139. async jiaBtnTap(e) {
  140. const index = e.currentTarget.dataset.index;
  141. const item = this.data.shippingCarInfo.items[index]
  142. const number = item.number + 1
  143. const token = wx.getStorageSync('token')
  144. if(this.data.shopCarType == 0){
  145. var res = await WXAPI.shippingCarInfoModifyNumber(token, item.key, number)
  146. }
  147. else if(this.data.shopCarType == 1){
  148. var res = await WXAPI.jdvopCartModifyNumber(token, item.key, number)
  149. }
  150. this.shippingCarInfo()
  151. },
  152. async jianBtnTap(e) {
  153. const index = e.currentTarget.dataset.index;
  154. const item = this.data.shippingCarInfo.items[index]
  155. const number = item.number - 1
  156. if (number <= 0) {
  157. // 弹出删除确认
  158. wx.showModal({
  159. content: '确定要删除该商品吗?',
  160. success: (res) => {
  161. if (res.confirm) {
  162. this.delItemDone(item.key)
  163. }
  164. }
  165. })
  166. return
  167. }
  168. const token = wx.getStorageSync('token')
  169. if(this.data.shopCarType == 0)
  170. {
  171. var res = await WXAPI.shippingCarInfoModifyNumber(token, item.key, number)
  172. }
  173. if(this.data.shopCarType == 1)
  174. {
  175. var res = await WXAPI.jdvopCartModifyNumber(token, item.key, number)
  176. }
  177. this.shippingCarInfo()
  178. },
  179. changeCarNumber(e) {
  180. const key = e.currentTarget.dataset.key
  181. const num = e.detail.value
  182. const token = wx.getStorageSync('token')
  183. if(this.data.shopCarType == 0){
  184. WXAPI.shippingCarInfoModifyNumber(token, key, num).then(res => {
  185. this.shippingCarInfo()
  186. })}
  187. else if(this.data.shopCarType == 1){
  188. WXAPI.jdvopCartModifyNumber(token, key, num).then(res => {
  189. this.shippingCarInfo()
  190. })
  191. }
  192. },
  193. async radioClick(e) {
  194. var index = e.currentTarget.dataset.index;
  195. var item = this.data.shippingCarInfo.items[index]
  196. const token = wx.getStorageSync('token')
  197. if (this.data.shopCarType == 0) { //自营购物车
  198. if (!item.stores || item.status == 1) {
  199. return
  200. }
  201. var res = await WXAPI.shippingCartSelected(token, item.key, !item.selected)
  202. } else if (this.data.shopCarType == 1) { //云货架购物车
  203. var res = await WXAPI.jdvopCartSelect(token, item.key, !item.selected)
  204. }
  205. this.shippingCarInfo()
  206. },
  207. onChange(event) {
  208. this.setData({
  209. shopCarType: event.detail.name
  210. })
  211. this.shippingCarInfo()
  212. }
  213. })