placeOrder.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // pages/order/placeOrder/placeOrder.js
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. activeKey: 0,
  9. navList: [],
  10. height: '',
  11. show: false,
  12. detailsShow: false,
  13. carList: [
  14. ],
  15. carNum: 0,
  16. carPrice: '',
  17. shoppingList: [],
  18. shop: [],
  19. worke: "",
  20. status: ""
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function (options) {
  26. var worke = JSON.parse(options.parmise)
  27. console.log(worke)
  28. this.setData({
  29. worke: worke,
  30. status: app.globalData.status
  31. })
  32. this.getHeight()
  33. this.getCarAll()
  34. this.leftList()
  35. },
  36. shoppingMinus(e) {
  37. let items = e.currentTarget.dataset.item, list = this.data.shoppingList;
  38. list.map(item => {
  39. if (items.id == item.id) {
  40. item.num--
  41. console.log(item);
  42. this.changeCarList(item)
  43. var good_id = items.id;
  44. var money = items.price.sale_price;
  45. var stock = item.num;
  46. this.cartChang(good_id, money, stock);
  47. }
  48. })
  49. this.setData({
  50. shoppingList: list
  51. })
  52. this.getCarAll()
  53. },
  54. shoppingPlus(e) {
  55. let items = e.currentTarget.dataset.item, list = this.data.shoppingList;
  56. list.map(item => {
  57. if (items.id == item.id) {
  58. item.num++
  59. this.changeCarList(item)
  60. var good_id = items.id;
  61. var money = items.price.sale_price;
  62. var stock = item.num;
  63. this.cartChang(good_id, money, stock);
  64. }
  65. })
  66. this.setData({
  67. shoppingList: list
  68. })
  69. this.getCarAll()
  70. },
  71. changeCarList(item) {
  72. let carList = this.data.carList
  73. if (carList.length == 0) {
  74. carList.push(item)
  75. this.setData({
  76. carList
  77. })
  78. return
  79. }
  80. console.log(item);
  81. var type = false;
  82. carList.map((i, index) => {
  83. if (i.id === item.id) {
  84. i.num = item.num
  85. if (item.num == 0) {
  86. carList.splice(index, 1)
  87. }
  88. this.setData({
  89. carList
  90. })
  91. type = false
  92. } else {
  93. type = true
  94. }
  95. })
  96. if (type) {
  97. carList.push(item)
  98. }
  99. console.log(carList);
  100. this.setData({
  101. carList
  102. })
  103. },
  104. //修改购物车
  105. cartChang(good_id, money, stock) {
  106. var nid = this.data.worke.id;
  107. app.api.useApi(app.globalData.baseAppUrl + "api/change_cart", {
  108. network_id: nid,
  109. good_id: good_id,
  110. money: money,
  111. stock: stock
  112. }, "post").then(function (res) {
  113. console.log(res);
  114. if (res.code != 200) {
  115. wx.showLoading({
  116. title: res.message.status,
  117. })
  118. } else {
  119. var data = res.message.data;
  120. var shop = [];
  121. for (var index in data) {
  122. shop[data[index].id] = data[index].goods
  123. }
  124. that.setData({
  125. navList: res.message.data,
  126. shoppingList: res.message.data[0].goods,
  127. shop: shop
  128. })
  129. }
  130. wx.hideLoading();
  131. }).catch(function (err) {
  132. console.log(222);
  133. })
  134. },
  135. changeShoppingList(item) {
  136. console.log(item);
  137. let shoppingList = this.data.shoppingList;
  138. shoppingList.map(i => {
  139. if (item.id == i.id) {
  140. i.num = item.num
  141. }
  142. })
  143. this.setData({
  144. shoppingList
  145. })
  146. },
  147. getCarAll() {
  148. var nid = this.data.worke.id;
  149. var that=this;
  150. app.api.useApi(app.globalData.baseAppUrl + "api/cart_info", {
  151. network_id: nid,
  152. }, "get").then(function (res) {
  153. console.log(res);
  154. if (res.code != 200) {
  155. wx.showLoading({
  156. title: res.message.msg,
  157. })
  158. } else {
  159. console.log(res.message.data.carPrice)
  160. that.setData({
  161. carList:res.message.data.cart.data,
  162. carNum: res.message.data.carNum,
  163. carPrice: res.message.data.carPrice
  164. })
  165. }
  166. wx.hideLoading();
  167. }).catch(function (err) {
  168. console.log(222);
  169. })
  170. let carList = this.data.carList
  171. carList.map(item => {
  172. wx.nextTick(() => {
  173. this.setData({
  174. carNum: this.data.carNum + item.num,
  175. carPrice: this.data.carPrice + item.price.sale_price * item.num
  176. })
  177. })
  178. })
  179. },
  180. getOrder() {
  181. var nid = this.data.worke.id;
  182. app.api.useApi(app.globalData.baseAppUrl + "api/makeorder", {
  183. network_id: nid,
  184. }, "post").then(function (res) {
  185. console.log(res);
  186. if (res.code != 200) {
  187. wx.showLoading({
  188. title: res.message.msg,
  189. })
  190. wx.setTimeout(() => {
  191. wx.hideLoading();
  192. }, 1000);
  193. } else {
  194. var order_no = res.message.data.order_no;
  195. wx.navigateTo({
  196. url: '/pages/order/confirmOreder/confirmOreder?order_no='+order_no,
  197. })
  198. }
  199. }).catch(function (err) {
  200. console.log(222);
  201. })
  202. },
  203. carPlus(e) {
  204. let items = e.currentTarget.dataset.item, list = this.data.carList
  205. console.log(items);
  206. list.map((item, index) => {
  207. if (items.id == item.id) {
  208. item.num++
  209. this.changeShoppingList(item)
  210. }
  211. })
  212. this.setData({
  213. carList: list
  214. })
  215. this.getCarAll()
  216. },
  217. carMinus(e) {
  218. let items = e.currentTarget.dataset.item, list = this.data.carList
  219. list.map((item, index) => {
  220. if (items.id == item.id) {
  221. item.num--
  222. this.changeShoppingList(item)
  223. if (items.num - 1 <= 0) {
  224. list.splice(index, 1)
  225. }
  226. }
  227. })
  228. this.setData({
  229. carList: list
  230. })
  231. this.getCarAll()
  232. },
  233. clearCarAll() {
  234. this.setData({
  235. carList: []
  236. })
  237. let shoppingList = this.data.shoppingList
  238. shoppingList.map(item => {
  239. item.num = 0
  240. })
  241. this.setData({
  242. shoppingList
  243. })
  244. this.getCarAll()
  245. this.btnShow()
  246. this.getHeight()
  247. },
  248. closeNews() {
  249. console.log(this.data.height);
  250. this.setData({
  251. height: this.data.height + 40
  252. })
  253. },
  254. changeActive(e) {
  255. let { activekey } = e.currentTarget.dataset
  256. var id = e.target.dataset.id;
  257. this.setData({
  258. activeKey: activekey,
  259. shoppingList: this.data.shop[id]
  260. })
  261. },
  262. addressDetail() {
  263. this.setData({
  264. detailsShow: !this.data.detailsShow
  265. })
  266. },
  267. getHeight() {
  268. var clientHeight;
  269. wx.getSystemInfo({
  270. success: (res) => {
  271. clientHeight = res.windowHeight;
  272. }
  273. });
  274. var query1 = wx.createSelectorQuery()
  275. var query2 = wx.createSelectorQuery()
  276. var query3 = wx.createSelectorQuery()
  277. query1.select('#footerBar').boundingClientRect()
  278. query2.select('#topBox').boundingClientRect()
  279. query3.select('#scrollBox').boundingClientRect()
  280. query2.exec(result => {
  281. query3.exec(r => {
  282. query1.exec(res => {
  283. if (res && res[0] != null) {
  284. this.setData({
  285. height: clientHeight - res[0].height - result[0].height - r[0].height - 40
  286. })
  287. } else {
  288. this.setData({
  289. height: clientHeight - result[0].height - r[0].height - 40
  290. })
  291. }
  292. console.log(clientHeight, result[0].height, r[0].height);
  293. })
  294. })
  295. })
  296. },
  297. btnShow() {
  298. this.setData({
  299. show: !this.data.show
  300. })
  301. },
  302. leftList: function () {
  303. var that = this;
  304. var nid = this.data.worke.id;
  305. app.api.useApi(app.globalData.baseAppUrl + "api/good", {
  306. network_id: nid,
  307. }, "get").then(function (res) {
  308. console.log(res);
  309. if (res.code != 200) {
  310. wx.showLoading({
  311. title: res.message.status,
  312. })
  313. } else {
  314. var data = res.message.data;
  315. var shop = [];
  316. for (var index in data) {
  317. shop[data[index].id] = data[index].goods
  318. }
  319. that.setData({
  320. navList: res.message.data,
  321. shoppingList: res.message.data[0].goods,
  322. shop: shop
  323. })
  324. }
  325. wx.hideLoading();
  326. }).catch(function (err) {
  327. console.log(222);
  328. })
  329. },
  330. })