order_addcart.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import { getCartList, getCartCounts, changeCartNum, cartDel} from '../../api/order.js';
  2. import { getProductHot, collectAll } from '../../api/store.js';
  3. const app = getApp();
  4. const util = require('../../utils/util.js');
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. parameter: {
  11. 'navbar': '1',
  12. 'return': '0',
  13. 'title': '购物车',
  14. 'color': false
  15. },
  16. navH: 0,
  17. cartCount:0,
  18. goodsHidden:true,
  19. footerswitch: true,
  20. host_product: [],
  21. cartList:[],
  22. isAllSelect:false,//全选
  23. selectValue:[],//选中的数据
  24. selectCountPrice:0.00,
  25. isGoIndex: true,
  26. iShidden: false,
  27. },
  28. /**
  29. * 生命周期函数--监听页面加载
  30. */
  31. onLoad: function (options) {
  32. var that = this;
  33. that.setData({
  34. navH: app.globalData.navHeight
  35. });
  36. if (app.globalData.token) that.setData({ iShidden:true});
  37. },
  38. /**
  39. * input阻止冒泡
  40. *
  41. */
  42. proventD: function () { },
  43. /**
  44. * 手动输入数量失焦事件
  45. */
  46. inputBlur(e) {
  47. if (e.detail.value < 1) {
  48. let index = e.currentTarget.dataset.index;
  49. let item = this.data.cartList.valid[index];
  50. item.cart_num = 1;
  51. if (item.cart_num) this.setCartNum(item.id, item.cart_num);
  52. let itemData = "cartList.valid[" + index + "]";
  53. this.setData({ [itemData]: item });
  54. this.switchSelect();
  55. }
  56. },
  57. /**
  58. * 关闭授权
  59. *
  60. */
  61. onCloseAuto: function () {
  62. this.setData({ iShidden: true });
  63. },
  64. subDel:function (event) {
  65. let that = this, selectValue = that.data.selectValue;
  66. if (selectValue.length > 0)
  67. cartDel(selectValue).then(res=>{
  68. that.getCartList();
  69. that.getCartNum();
  70. });
  71. else
  72. return app.Tips({ title:'请选择产品'});
  73. },
  74. getSelectValueProductId:function(){
  75. var that = this;
  76. var validList = that.data.cartList.valid;
  77. var selectValue = that.data.selectValue;
  78. var productId = [];
  79. if (selectValue.length > 0){ for (var index in validList){if(that.inArray(validList[index].id, selectValue)) { productId.push(validList[index].product_id);}}};
  80. return productId;
  81. },
  82. subCollect: function (event){
  83. let that = this, selectValue = that.data.selectValue;
  84. if (selectValue.length > 0) {
  85. var selectValueProductId = that.getSelectValueProductId();
  86. collectAll(that.getSelectValueProductId().join(',')).then(res=>{
  87. return app.Tips({title:res.msg,icon:'success'});
  88. }).catch(err=>{
  89. return app.Tips({ title: err });
  90. });
  91. } else {
  92. return app.Tips({ title:'请选择产品'});
  93. }
  94. },
  95. subOrder: function (event){
  96. let that = this, selectValue = that.data.selectValue;
  97. if (selectValue.length > 0){
  98. wx.navigateTo({url:'/pages/order_confirm/index?cartId=' + selectValue.join(',')});
  99. }else{
  100. return app.Tips({ title:'请选择产品'});
  101. }
  102. },
  103. checkboxAllChange: function (event){
  104. var value = event.detail.value;
  105. if (value.length > 0) { this.setAllSelectValue(1)}
  106. else { this.setAllSelectValue(0) }
  107. },
  108. setAllSelectValue:function(status){
  109. var that = this;
  110. var selectValue = [];
  111. var valid = that.data.cartList.valid;
  112. if (valid.length > 0) {
  113. for (var index in valid) {
  114. if (status == 1){
  115. valid[index].checked = true;
  116. selectValue.push(valid[index].id);
  117. }else valid[index].checked = false;
  118. }
  119. var validData = "cartList.valid";
  120. that.setData({
  121. [validData]: valid,
  122. selectValue: selectValue,
  123. });
  124. that.switchSelect();
  125. }
  126. },
  127. checkboxChange: function (event){
  128. var that = this;
  129. var value = event.detail.value;
  130. var valid = this.data.cartList.valid;
  131. for (var index in valid){
  132. if (that.inArray(valid[index].id, value)) valid[index].checked = true;
  133. else valid[index].checked = false;
  134. }
  135. var validData = "cartList.valid";
  136. this.setData({
  137. [validData]: valid,
  138. isAllSelect: value.length == this.data.cartList.valid.length,
  139. selectValue: value,
  140. })
  141. this.switchSelect();
  142. },
  143. inArray:function(search, array){
  144. for (var i in array) { if (array[i] == search) { return true; } }
  145. return false;
  146. },
  147. switchSelect:function(){
  148. var that = this;
  149. var validList = that.data.cartList.valid;
  150. var selectValue = that.data.selectValue;
  151. var selectCountPrice = 0.00;
  152. if (selectValue.length < 1) { that.setData({ selectCountPrice: selectCountPrice }); }
  153. else{
  154. for (var index in validList){
  155. if (that.inArray(validList[index].id, selectValue)){
  156. selectCountPrice = Number(selectCountPrice) + Number(validList[index].cart_num) * Number(validList[index].truePrice)
  157. }
  158. }
  159. that.setData({ selectCountPrice: selectCountPrice.toFixed(2) });
  160. }
  161. },
  162. subCart:function(event){
  163. var that = this;
  164. var status = false;
  165. var index = event.currentTarget.dataset.index;
  166. var item = that.data.cartList.valid[index];
  167. item.cart_num = item.cart_num - 1;
  168. if (item.cart_num < 1) status = true;
  169. if (item.cart_num <= 1) {
  170. item.cart_num = 1;
  171. item.numSub = true;
  172. } else { item.numSub = false;item.numAdd = false; }
  173. if (false == status) {
  174. that.setCartNum(item.id, item.cart_num, function (data) {
  175. var itemData = "cartList.valid[" + index + "]";
  176. that.setData({ [itemData]: item });
  177. that.switchSelect();
  178. });
  179. }
  180. },
  181. /**
  182. * 购物车手动填写
  183. *
  184. */
  185. iptCartNum: function (e) {
  186. let index = e.currentTarget.dataset.index;
  187. let item = this.data.cartList.valid[index];
  188. item.cart_num = e.detail.value;
  189. if (item.cart_num) this.setCartNum(item.id, item.cart_num);
  190. let itemData = "cartList.valid[" + index + "]";
  191. this.setData({ [itemData]: item });
  192. this.switchSelect();
  193. },
  194. addCart: function (event) {
  195. var that = this;
  196. var index = event.currentTarget.dataset.index;
  197. var item = that.data.cartList.valid[index];
  198. item.cart_num = parseInt(item.cart_num) + 1;
  199. var productInfo = item.productInfo;
  200. if (productInfo.hasOwnProperty('attrInfo') && item.cart_num >= item.productInfo.attrInfo.stock) {
  201. item.cart_num = item.productInfo.attrInfo.stock;
  202. item.numAdd = true;
  203. item.numSub = false;
  204. } else if (item.cart_num >= item.productInfo.stock) {
  205. item.cart_num = item.productInfo.stock;
  206. item.numAdd = true;
  207. item.numSub = false;
  208. } else { item.numAdd = false; item.numSub = false; }
  209. that.setCartNum(item.id, item.cart_num, function (data) {
  210. var itemData = "cartList.valid[" + index + "]";
  211. that.setData({ [itemData]: item });
  212. that.switchSelect();
  213. });
  214. },
  215. setCartNum(cartId, cartNum, successCallback) {
  216. var that = this;
  217. changeCartNum(cartId, cartNum).then(res=>{
  218. successCallback && successCallback(res.data);
  219. });
  220. },
  221. getCartNum: function () {
  222. var that = this;
  223. getCartCounts().then(res=>{
  224. that.setData({ cartCount: res.data.count });
  225. });
  226. },
  227. getCartList: function () {
  228. var that = this;
  229. getCartList().then(res=>{
  230. var cartList = res.data;
  231. var valid = cartList.valid;
  232. var numSub = [{ numSub: true }, { numSub: false }];
  233. var numAdd = [{ numAdd: true }, { numAdd: false }], selectValue = [];;
  234. if (valid.length > 0) {
  235. for (var index in valid) {
  236. if (valid[index].cart_num == 1) { valid[index].numSub = true; }
  237. else { valid[index].numSub = false; }
  238. var productInfo = valid[index].productInfo;
  239. if (productInfo.hasOwnProperty('attrInfo') && valid[index].cart_num == valid[index].productInfo.attrInfo.stock) {
  240. valid[index].numAdd = true;;
  241. } else if (valid[index].cart_num == valid[index].productInfo.stock) {
  242. valid[index].numAdd = true;;
  243. } else { valid[index].numAdd = false; }
  244. valid[index].checked = true;
  245. selectValue.push(valid[index].id);
  246. }
  247. }
  248. that.setData({
  249. cartList: cartList,
  250. goodsHidden: cartList.valid.length <= 0 ? false : true,
  251. selectValue: selectValue,
  252. isAllSelect: valid.length == selectValue.length && valid.length
  253. });
  254. that.switchSelect();
  255. });
  256. },
  257. getHostProduct: function () {
  258. var that = this;
  259. getProductHot().then(res=>{
  260. that.setData({ host_product: res.data });
  261. });
  262. },
  263. goodsOpen:function(){
  264. var that = this;
  265. that.setData({
  266. goodsHidden: !that.data.goodsHidden
  267. })
  268. },
  269. manage:function(){
  270. var that = this;
  271. that.setData({
  272. footerswitch: !that.data.footerswitch
  273. })
  274. },
  275. /**
  276. * 生命周期函数--监听页面初次渲染完成
  277. */
  278. onReady: function () {
  279. },
  280. onLoadFun: function () {
  281. this.getHostProduct();
  282. this.getCartList();
  283. this.getCartNum();
  284. },
  285. /**
  286. * 生命周期函数--监听页面显示
  287. */
  288. onShow: function () {
  289. if (app.globalData.isLog == true) {
  290. this.getHostProduct();
  291. this.getCartList();
  292. this.getCartNum();
  293. this.setData({
  294. goodsHidden: true,
  295. footerswitch: true,
  296. host_product: [],
  297. cartList: [],
  298. isAllSelect: false,//全选
  299. selectValue: [],//选中的数据
  300. selectCountPrice: 0.00,
  301. cartCount: 0,
  302. iShidden:true
  303. });
  304. }
  305. },
  306. unsetCart:function(){
  307. let that=this,ids=[];
  308. for (var i = 0, len = that.data.cartList.invalid.length;i < len;i++){
  309. ids.push(that.data.cartList.invalid[i].id);
  310. }
  311. cartDel(ids).then(res=>{
  312. app.Tips({ title: '清除成功' });
  313. that.setData({ 'cartList.invalid': [] });
  314. }).catch(res=>{
  315. });
  316. },
  317. /**
  318. * 生命周期函数--监听页面隐藏
  319. */
  320. onHide: function () {
  321. },
  322. /**
  323. * 生命周期函数--监听页面卸载
  324. */
  325. onUnload: function () {
  326. },
  327. /**
  328. * 页面相关事件处理函数--监听用户下拉动作
  329. */
  330. onPullDownRefresh: function () {
  331. },
  332. })