buycar.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // import bm from '../../utils/baseMath.js'
  2. // // pages/buycar/buycar.js
  3. var app = getApp();
  4. // // var wx = require('../../utils/wx.js');
  5. Page({
  6. data: {
  7. isAttrInfo:'attrInfo',
  8. itemAttrInfo : '',
  9. foothidden:false,
  10. url: app.globalData.urlImages,
  11. countmoney:"",
  12. cartNum:"",
  13. isAllSelect:false,
  14. minusStatus: 'disabled',
  15. cartList:[],
  16. cartInvalid:[],
  17. cartIdsStr:''
  18. },
  19. setNumber: function (e) {
  20. var that = this;
  21. var index = e.currentTarget.dataset.item;
  22. var cartList = that.data.cartList;
  23. var num = parseInt(e.detail.value);
  24. var cart_num = num ? num : 1;
  25. if (cartList[index].productInfo.attrInfo.stock){
  26. if (cart_num > cartList[index].productInfo.stock) cart_num = cartList[index].productInfo.attrInfo.stock
  27. }else{
  28. if (cart_num > cartList[index].productInfo.attrInfo.stock) cart_num = cartList[index].productInfo.stock
  29. }
  30. cartList[index].cart_num = cart_num;
  31. that.setData({
  32. cartList: cartList
  33. })
  34. wx.request({
  35. url: app.globalData.url + '/routine/auth_api/set_buy_cart_num?uid=' + app.globalData.uid,
  36. method: 'GET',
  37. data:{
  38. cartId: cartList[index].id,
  39. cartNum: cart_num
  40. },
  41. success: function (res) {
  42. if (res.data.code == 400) {
  43. wx.showToast({
  44. title: res.data.msg,
  45. icon: 'none',
  46. duration: 1000,
  47. })
  48. }
  49. }
  50. })
  51. },
  52. onLoad: function (options) {
  53. app.setBarColor();
  54. app.setUserInfo();
  55. this.carnum();
  56. this.countmoney();
  57. this.getList();
  58. },
  59. btntap: function (e) {
  60. this.data.foothidden = !this.data.foothidden;
  61. this.setData({
  62. foothidden: this.data.foothidden
  63. })
  64. },
  65. getList:function(){
  66. var that = this;
  67. var header = {
  68. 'content-type': 'application/x-www-form-urlencoded',
  69. };
  70. wx.request({
  71. url: app.globalData.url + '/routine/auth_api/get_cart_list?uid=' + app.globalData.uid,
  72. method: 'POST',
  73. header: header,
  74. success: function (res) {
  75. if(res.data.code == 200){
  76. that.setData({
  77. cartList: res.data.data.valid,
  78. cartInvalid: res.data.data.invalid
  79. })
  80. }
  81. }
  82. })
  83. },
  84. //加
  85. numAddClick: function (event) {
  86. var index = event.currentTarget.dataset.index;
  87. this.data.cartList[index].cart_num = +this.data.cartList[index].cart_num + 1;
  88. var minusStatus = this.data.cartList[index].cart_num <= 1 ? 'disabled' : 'normal';
  89. this.setData({ cartList: this.data.cartList, minusStatus: minusStatus});
  90. this.carnum();
  91. this.countmoney();
  92. this.addCartNum(this.data.cartList[index].cart_num, this.data.cartList[index].id);
  93. },
  94. //减
  95. numDescClick: function (event) {
  96. var index = event.currentTarget.dataset.index;
  97. this.data.cartList[index].cart_num = this.data.cartList[index].cart_num <= 1 ? 1 : +this.data.cartList[index].cart_num - 1;
  98. var minusStatus = this.data.cartList[index].cart_num <= 1 ? 'disabled' : 'normal';
  99. this.setData({ cartList: this.data.cartList, minusStatus: minusStatus});
  100. this.carnum();
  101. this.countmoney();
  102. this.addCartNum(this.data.cartList[index].cart_num, this.data.cartList[index].id);
  103. },
  104. //单选;
  105. switchSelect:function(e){
  106. var index = e.currentTarget.dataset.index;
  107. this.data.cartList[index].checked = !this.data.cartList[index].checked;
  108. var len = this.data.cartList.length;
  109. var selectnum = [];
  110. for(var i = 0; i < len ;i++){
  111. if (this.data.cartList[i].checked==true){
  112. selectnum.push(true);
  113. }
  114. }
  115. if (selectnum.length==len){
  116. this.data.isAllSelect = true;
  117. }else{
  118. this.data.isAllSelect = false;
  119. }
  120. this.setData({
  121. cartList: this.data.cartList,
  122. isAllSelect: this.data.isAllSelect
  123. });
  124. this.carnum();
  125. this.countmoney();
  126. this.confirmOrder();
  127. },
  128. //全选
  129. allChecked:function(e){
  130. var selectAllStatus = this.data.isAllSelect;
  131. selectAllStatus = !selectAllStatus;
  132. var array = this.data.cartList;
  133. for (var i = 0; i < array.length;i++){
  134. array[i].checked = selectAllStatus;
  135. };
  136. this.setData({
  137. cartList:this.data.cartList,
  138. isAllSelect: selectAllStatus
  139. })
  140. this.carnum();
  141. this.countmoney();
  142. this.confirmOrder();
  143. },
  144. //数量
  145. carnum(){
  146. var carnum = 0;
  147. var array = this.data.cartList;
  148. for(var i = 0;i<array.length;i++){
  149. if (array[i].checked==true){
  150. carnum += parseInt(array[i].cart_num);
  151. }
  152. }
  153. this.setData({
  154. cartNum: carnum
  155. })
  156. },
  157. //总共价钱;
  158. countmoney(){
  159. var carmoney = 0;
  160. var array = this.data.cartList;
  161. for (var i = 0; i < array.length; i++) {
  162. if (array[i].checked == true) {
  163. if (array[i].productInfo.attrInfo){
  164. carmoney += parseFloat(array[i].cart_num * array[i].productInfo.attrInfo.price);
  165. }else{
  166. carmoney += parseFloat(array[i].cart_num * array[i].productInfo.price);
  167. }
  168. }
  169. }
  170. this.setData({
  171. countmoney: carmoney.toFixed(2)
  172. })
  173. },
  174. confirmOrder:function(){
  175. var array = this.data.cartList;
  176. var cartIds = [];
  177. for (var i = 0; i < array.length; i++) {
  178. if (array[i].checked == true) {
  179. cartIds.push(array[i].id);
  180. }
  181. }
  182. if (cartIds.length > 0) {
  183. this.goConfirm(cartIds);
  184. }else{
  185. this.setData({
  186. cartIdsStr: ''
  187. })
  188. }
  189. },
  190. goConfirm : function(cartIds){
  191. if (cartIds.length > 0) {
  192. this.setData({
  193. cartIdsStr: cartIds.join(',')
  194. })
  195. }
  196. // console.log(this);
  197. },
  198. addCartNum: function (cartNum, cartId){
  199. var header = {
  200. 'content-type': 'application/x-www-form-urlencoded',
  201. };
  202. wx.request({
  203. url: app.globalData.url + '/routine/auth_api/change_cart_num?uid=' + app.globalData.uid,
  204. method: 'GET',
  205. data: {
  206. cartNum: cartNum,
  207. cartId: cartId
  208. },
  209. header: header,
  210. success: function (res) {
  211. if (res.data.code == 200) {
  212. wx.showToast({
  213. title: '成功',
  214. icon: 'success',
  215. duration: 2000
  216. })
  217. } else {
  218. wx.showToast({
  219. title: res.data.msg,
  220. icon: 'none',
  221. duration: 2000
  222. })
  223. }
  224. }
  225. })
  226. },
  227. collectAll: function () {
  228. var array = this.data.cartList;
  229. var productIds = [];
  230. var that = this;
  231. for (var i = 0; i < array.length; i++) {
  232. if (array[i].checked == true) {
  233. productIds.push(array[i].product_id);
  234. }
  235. }
  236. if (productIds.length > 0) {
  237. var header = {
  238. 'content-type': 'application/x-www-form-urlencoded',
  239. };
  240. wx.request({
  241. url: app.globalData.url + '/routine/auth_api/collect_product_all?uid=' + app.globalData.uid,
  242. method: 'GET',
  243. data: {
  244. productId: productIds.join(',')
  245. },
  246. header: header,
  247. success: function (res) {
  248. if (res.data.code == 200) {
  249. wx.showToast({
  250. title: '收藏成功',
  251. icon: 'success',
  252. duration: 2000
  253. })
  254. } else {
  255. wx.showToast({
  256. title: res.data.msg,
  257. icon: 'none',
  258. duration: 2000
  259. })
  260. }
  261. }
  262. })
  263. }
  264. console.log(productIds);
  265. },
  266. cartDelAll:function(){
  267. var array = this.data.cartList;
  268. var ids = [];
  269. var that = this;
  270. for (var i = 0; i < array.length; i++) {
  271. if (array[i].checked == true) {
  272. ids.push(array[i].id);
  273. }
  274. }
  275. if (ids.length > 0){
  276. var header = {
  277. 'content-type': 'application/x-www-form-urlencoded',
  278. };
  279. wx.request({
  280. url: app.globalData.url + '/routine/auth_api/remove_cart?uid=' + app.globalData.uid,
  281. method: 'GET',
  282. data: {
  283. ids: ids.join(',')
  284. },
  285. header: header,
  286. success: function (res) {
  287. if (res.data.code == 200) {
  288. wx.showToast({
  289. title: '删除成功',
  290. icon: 'success',
  291. duration: 2000
  292. })
  293. for (var i = 0; i < ids.length; i++) {
  294. for (var j = 0; j < array.length; j++) {
  295. if (ids[i] == array[j].id) {
  296. array.splice(j, 1);
  297. }
  298. }
  299. }
  300. that.setData({
  301. cartList: array
  302. })
  303. } else {
  304. wx.showToast({
  305. title: res.data.msg,
  306. icon: 'none',
  307. duration: 2000
  308. })
  309. }
  310. }
  311. })
  312. }
  313. },
  314. cartDel:function(e){
  315. var that = this;
  316. if (e.currentTarget.dataset.id){
  317. var header = {
  318. 'content-type': 'application/x-www-form-urlencoded',
  319. };
  320. wx.request({
  321. url: app.globalData.url + '/routine/auth_api/remove_cart?uid=' + app.globalData.uid,
  322. method: 'GET',
  323. data: {
  324. ids: e.currentTarget.dataset.id
  325. },
  326. header: header,
  327. success: function (res) {
  328. if(res.data.code == 200){
  329. wx.showToast({
  330. title: '删除成功',
  331. icon: 'success',
  332. duration: 2000
  333. })
  334. var cartInvalid = that.data.cartInvalid;
  335. for (var i = 0; i < cartInvalid.length ; i++){
  336. if (e.currentTarget.dataset.id == cartInvalid[i].id){
  337. cartInvalid.splice(i,1);
  338. that.setData({
  339. cartInvalid: cartInvalid
  340. })
  341. }
  342. }
  343. }else{
  344. wx.showToast({
  345. title: res.data.msg,
  346. icon: 'none',
  347. duration: 2000
  348. })
  349. }
  350. }
  351. })
  352. }
  353. },
  354. /**
  355. * 生命周期函数--监听页面加载
  356. */
  357. /**
  358. * 生命周期函数--监听页面初次渲染完成
  359. */
  360. onReady: function () {
  361. },
  362. /**
  363. * 生命周期函数--监听页面显示
  364. */
  365. onShow: function () {
  366. this.carnum();
  367. this.countmoney();
  368. this.getList();
  369. },
  370. /**
  371. * 生命周期函数--监听页面隐藏
  372. */
  373. onHide: function () {
  374. },
  375. /**
  376. * 生命周期函数--监听页面卸载
  377. */
  378. onUnload: function () {
  379. },
  380. /**
  381. * 页面相关事件处理函数--监听用户下拉动作
  382. */
  383. onPullDownRefresh: function () {
  384. },
  385. /**
  386. * 页面上拉触底事件的处理函数
  387. */
  388. onReachBottom: function () {
  389. },
  390. /**
  391. * 用户点击右上角分享
  392. */
  393. onShareAppMessage: function () {
  394. }
  395. })