index.js 5.2 KB

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