placeOrder.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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: 0,
  17. shoppingList: [],
  18. shop: [],
  19. worke: "",
  20. status: "",
  21. order_num:""
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. console.log(options);
  28. const q = decodeURIComponent(options.q) // 获取到二维码原始链接内容
  29. if(typeof options.parmise!="undefined"){
  30. var worke = JSON.parse(options.parmise);
  31. this.setData({
  32. worke:worke
  33. })
  34. this.getcart();
  35. }else if(typeof options.network_id!="undefined"){
  36. this.getNwetwork(options.network_id)
  37. }else{
  38. var network_id=app.getQueryVariable(q,"network_id")
  39. var order_num=app.getQueryVariable(q,"order_num")
  40. console.log(network_id);
  41. this.setData({
  42. order_num:order_num
  43. })
  44. this.getNwetwork(network_id)
  45. }
  46. this.setData({
  47. status: app.globalData.status
  48. })
  49. this.getHeight()
  50. this.getCarAll()
  51. this.leftList()
  52. },
  53. onShow() {
  54. this.getcart();
  55. },
  56. goto(){
  57. wx.navigateTo({
  58. url: '/pages/order/access/access?network_id='+this.data.worke.id,
  59. })
  60. },
  61. shoppingMinus(e) {
  62. let items = e.currentTarget.dataset.item, list = this.data.shoppingList;
  63. var carNum = this.data.carNum ? this.data.carNum : 0
  64. var carPrice = this.data.carPrice ? this.data.carPrice : 0
  65. list.map(item => {
  66. if (items.id == item.id) {
  67. if (item.num <= 0) {
  68. item.num = 0
  69. } else {
  70. item.num--
  71. }
  72. console.log(item);
  73. var price = (item.price.end_time == 0 && item.price.start_time == 0) ? item.price.price : item.price.sale_price
  74. this.setData({
  75. carNum: (carNum - 1) <= 0 ? 0 : carNum - 1,
  76. carPrice: (parseFloat(carPrice) - parseFloat(price)) <= 0 ? 0 : parseFloat(carPrice) - parseFloat(price)
  77. })
  78. this.changeCarList(item)
  79. var good_id = items.id;
  80. var money = price;
  81. var stock = item.num;
  82. this.cartChang(good_id, money, stock);
  83. }
  84. })
  85. this.setData({
  86. shoppingList: list
  87. })
  88. // this.getCarAll()
  89. },
  90. shoppingPlus(e) {
  91. let items = e.currentTarget.dataset.item, list = this.data.shoppingList;
  92. var carNum = this.data.carNum ? this.data.carNum : 0
  93. var carPrice = this.data.carPrice ? this.data.carPrice : 0
  94. list.map(item => {
  95. if (items.id == item.id) {
  96. item.num++
  97. console.log(item);
  98. this.changeCarList(item)
  99. var price = (item.price.end_time == 0 && item.price.start_time == 0) ? item.price.price : item.price.sale_price
  100. this.setData({
  101. carNum: carNum + 1,
  102. carPrice: parseFloat(carPrice) + parseFloat(price)
  103. })
  104. var type = items.good_type
  105. var good_id = items.id;
  106. var money = price;
  107. var stock = item.num;
  108. this.cartChang(good_id, money, stock, type);
  109. }
  110. })
  111. this.setData({
  112. // carList:items,
  113. shoppingList: list
  114. })
  115. //this.getCarAll()
  116. },
  117. changeCarList(item) {
  118. let carList = this.data.carList
  119. if (carList == {} || carList.length == 0 || !carList || carList !== []) {
  120. // carList.push(item)
  121. this.setData({
  122. carList: item
  123. })
  124. return false;
  125. } else {
  126. console.log(item);
  127. var type = false;
  128. carList.map((i, index) => {
  129. if (i.id === item.id) {
  130. i.stock = item.num
  131. if (item.num == 0) {
  132. carList.splice(index, 1)
  133. }
  134. this.setData({
  135. carList
  136. })
  137. type = false
  138. } else {
  139. type = true
  140. }
  141. })
  142. if (type) {
  143. carList.push(item)
  144. }
  145. console.log(carList);
  146. this.setData({
  147. carList
  148. })
  149. }
  150. },
  151. //修改购物车
  152. cartChang(good_id, money, stock, type = 1) {
  153. var nid = this.data.worke.id;
  154. app.api.useApi(app.globalData.baseAppUrl + "api/change_cart", {
  155. network_id: nid,
  156. good_id: good_id,
  157. money: money,
  158. stock: stock
  159. }, "post").then(function (res) {
  160. console.log(res);
  161. if (res.code != 200) {
  162. wx.showLoading({
  163. title: res.message.status,
  164. })
  165. } else {
  166. var data = res.message.data;
  167. var shop = [];
  168. for (var index in data) {
  169. shop[data[index].id] = data[index].goods
  170. }
  171. that.setData({
  172. navList: res.message.data,
  173. shoppingList: res.message.data[0].goods,
  174. shop: shop
  175. })
  176. if (type != 1) {
  177. that.getOrder();
  178. }
  179. }
  180. wx.hideLoading();
  181. }).catch(function (err) {
  182. console.log(222);
  183. })
  184. },
  185. changeShoppingList(item) {
  186. console.log(item);
  187. let shoppingList = this.data.shoppingList;
  188. shoppingList.map(i => {
  189. if (item.id == i.id) {
  190. i.num = item.num
  191. }
  192. })
  193. this.setData({
  194. shoppingList
  195. })
  196. },
  197. getCarAll() {
  198. let carList = this.data.carList
  199. var carNum = this.data.carNum ? this.data.carNum : 0
  200. var carPrice = this.data.carPrice ? this.data.carPrice : 0
  201. carList.map(item => {
  202. wx.nextTick(() => {
  203. var price = (item.price.end_time && item.price.start_time) ? item.price.sale_price : item.price.price
  204. carPrice = carPrice + price * item.num;
  205. this.setData({
  206. carNum: carNum + item.num,
  207. carPrice: carPrice
  208. })
  209. })
  210. })
  211. },
  212. getcart() {
  213. var nid = this.data.worke.id;
  214. var that = this;
  215. app.api.useApi(app.globalData.baseAppUrl + "api/cart_info", {
  216. network_id: nid,
  217. }, "get").then(function (res) {
  218. console.log(res);
  219. if (res.code != 200) {
  220. wx.showLoading({
  221. title: res.message.msg,
  222. })
  223. } else {
  224. console.log(res.message.msg)
  225. if (res.message.msg == "空购物车") {
  226. console.log(111)
  227. that.setData({
  228. carList: {},
  229. carNum: 0,
  230. carPrice: 0
  231. })
  232. } else {
  233. console.log(444)
  234. that.setData({
  235. carList: res.message.data.cart.data,
  236. carNum: res.message.data.carNum,
  237. carPrice: res.message.data.carPrice
  238. })
  239. }
  240. }
  241. wx.hideLoading();
  242. }).catch(function (err) {
  243. console.log(222);
  244. })
  245. },
  246. getOrder() {
  247. var nid = this.data.worke.id;
  248. if(this.data.carNum>0){
  249. app.api.useApi(app.globalData.baseAppUrl + "api/makeorder", {
  250. network_id: nid,
  251. }, "post").then(function (res) {
  252. console.log(res);
  253. if (res.code != 200) {
  254. wx.showLoading({
  255. title: res.message.msg,
  256. })
  257. wx.setTimeout(() => {
  258. wx.hideLoading();
  259. }, 1000);
  260. } else {
  261. var order_no = res.message.data.order_no;
  262. wx.navigateTo({
  263. url: '/pages/order/confirmOreder/confirmOreder?order_no=' + order_no,
  264. })
  265. }
  266. }).catch(function (err) {
  267. console.log(222);
  268. })
  269. }else{
  270. wx.showToast({
  271. title: '购物车不能为空',
  272. icon: 'none',
  273. duration: 2000
  274. })
  275. }
  276. },
  277. getOrderone(e) {
  278. var gid = e.currentTarget.dataset.gid;
  279. var nid = this.data.worke.id;
  280. app.api.useApi(app.globalData.baseAppUrl + "api/CreateOrder", {
  281. network_id: nid,
  282. good_id: gid,
  283. num: 1
  284. }, "post").then(function (res) {
  285. console.log(res);
  286. if (res.code != 200) {
  287. wx.showLoading({
  288. title: res.message.msg,
  289. })
  290. wx.setTimeout(() => {
  291. wx.hideLoading();
  292. }, 1000);
  293. } else {
  294. var order_no = res.message.data.order_no;
  295. wx.navigateTo({
  296. url: '/pages/order/confirmOreder/confirmOreder?order_no=' + order_no,
  297. })
  298. }
  299. }).catch(function (err) {
  300. console.log(222);
  301. })
  302. },
  303. carPlus(e) {
  304. let items = e.currentTarget.dataset.item, list = this.data.carList;
  305. var carNum = this.data.carNum ? this.data.carNum : 0
  306. var carPrice = this.data.carPrice ? this.data.carPrice : 0
  307. list.map(item => {
  308. if (items.good_id == item.good_id) {
  309. item.stock++
  310. console.log(item);
  311. // this.changeShoppingList(item)
  312. //var price = (item.price.end_time && item.price.start_time) ? item.price.sale_price : item.price.price
  313. var price = item.money
  314. this.setData({
  315. carNum: carNum + 1,
  316. carPrice: parseFloat(carPrice) + parseFloat(price),
  317. })
  318. var good_id = items.good_id;
  319. var money = price;
  320. var stock = item.stock;
  321. this.cartChang(good_id, money, stock);
  322. }
  323. })
  324. this.setData({
  325. carList: list
  326. })
  327. //this.getCarAll()
  328. },
  329. carMinus(e) {
  330. let items = e.currentTarget.dataset.item, list = this.data.carList;
  331. var carNum = this.data.carNum ? this.data.carNum : 0
  332. var carPrice = this.data.carPrice ? this.data.carPrice : 0
  333. console.log(items);
  334. list.map(item => {
  335. if (items.good_id == item.good_id) {
  336. item.stock--
  337. if (item.stock <= 0) {
  338. item.stock = 0
  339. } else {
  340. item.stock--
  341. }
  342. console.log(item);
  343. var price = item.money
  344. this.setData({
  345. carNum: (carNum - 1) <= 0 ? 0 : carNum - 1,
  346. carPrice: (parseFloat(carPrice) - parseFloat(price)) <= 0 ? 0 : parseFloat(carPrice) - parseFloat(price)
  347. })
  348. // this.changeShoppingList(item)
  349. var good_id = items.good_id;
  350. var money = price;
  351. var stock = item.stock;
  352. this.cartChang(good_id, money, stock);
  353. if (item.num == 0) {
  354. list.splice(index, 1)
  355. }
  356. }
  357. })
  358. this.setData({
  359. carList: list
  360. })
  361. },
  362. clearCarAll() {
  363. this.setData({
  364. carList: []
  365. })
  366. let shoppingList = this.data.shoppingList
  367. shoppingList.map(item => {
  368. item.num = 0
  369. })
  370. this.setData({
  371. shoppingList
  372. })
  373. var that = this;
  374. var nid = this.data.worke.id;
  375. app.api.useApi(app.globalData.baseAppUrl + "api/DeleteCart", {
  376. network_id: nid,
  377. }, "DELETE").then(function (res) {
  378. console.log(res);
  379. if (res.code != 200) {
  380. wx.showLoading({
  381. title: res.message.msg,
  382. })
  383. } else {
  384. var data = res.message.data;
  385. wx.showLoading({
  386. title: res.message.msg,
  387. })
  388. }
  389. wx.hideLoading();
  390. }).catch(function (err) {
  391. console.log(222);
  392. })
  393. this.getCarAll()
  394. this.btnShow()
  395. this.getHeight()
  396. },
  397. closeNews() {
  398. console.log(this.data.height);
  399. this.setData({
  400. height: this.data.height + 40
  401. })
  402. },
  403. changeActive(e) {
  404. let { activekey } = e.currentTarget.dataset
  405. var id = e.currentTarget.dataset.id;
  406. var shoppingList=this.data.shop[id];
  407. if(typeof shoppingList=="undefined"){
  408. shoppingList=new Array();
  409. }
  410. this.setData({
  411. activeKey: activekey,
  412. shoppingList: shoppingList
  413. })
  414. },
  415. addressDetail() {
  416. this.setData({
  417. detailsShow: !this.data.detailsShow
  418. })
  419. },
  420. getHeight() {
  421. var clientHeight;
  422. wx.getSystemInfo({
  423. success: (res) => {
  424. clientHeight = res.windowHeight;
  425. }
  426. });
  427. var query1 = wx.createSelectorQuery()
  428. var query2 = wx.createSelectorQuery()
  429. var query3 = wx.createSelectorQuery()
  430. query1.select('#footerBar').boundingClientRect()
  431. query2.select('#topBox').boundingClientRect()
  432. query3.select('#scrollBox').boundingClientRect()
  433. query2.exec(result => {
  434. query3.exec(r => {
  435. query1.exec(res => {
  436. if (res && res[0] != null) {
  437. this.setData({
  438. height: clientHeight - res[0].height - result[0].height - r[0].height - 40
  439. })
  440. } else {
  441. this.setData({
  442. height: clientHeight - result[0].height - r[0].height - 40
  443. })
  444. }
  445. console.log(clientHeight, result[0].height, r[0].height);
  446. })
  447. })
  448. })
  449. },
  450. btnShow() {
  451. this.setData({
  452. show: !this.data.show
  453. })
  454. this.getcart()
  455. },
  456. leftList: function () {
  457. var that = this;
  458. var nid = this.data.worke.id;
  459. app.api.useApi(app.globalData.baseAppUrl + "api/good", {
  460. network_id: nid,
  461. }, "get").then(function (res) {
  462. console.log(res);
  463. if (res.code != 200) {
  464. wx.showLoading({
  465. title: res.message.status,
  466. })
  467. } else {
  468. var data = res.message.data;
  469. var shop = [];
  470. for (var index in data) {
  471. shop[data[index].id] = data[index].goods
  472. }
  473. that.setData({
  474. navList: res.message.data,
  475. shoppingList: res.message.data[0].goods,
  476. shop: shop
  477. })
  478. }
  479. wx.hideLoading();
  480. }).catch(function (err) {
  481. console.log(222);
  482. })
  483. },
  484. location() {
  485. wx.openLocation({
  486. latitude: Number(this.data.worke.lat),
  487. longitude: Number(this.data.worke.lng),
  488. });
  489. },
  490. /**
  491. * @name获取网点
  492. */
  493. getNwetwork(network_id){
  494. var that=this;
  495. app.api.useApi(app.globalData.baseAppUrl+'api/network/desc',{
  496. network_id:network_id>0?network_id:wx.getStorageSync('nid')
  497. }).then((res)=>{
  498. if(res.code!=200){
  499. wx.showToast({
  500. title: res.message.msg,
  501. })
  502. }else{
  503. console.log(res.message.data);
  504. that.setData({
  505. worke:res.message.data
  506. });
  507. this.getcart();
  508. }
  509. }).catch((err)=>{
  510. console.log(err.message)
  511. })
  512. }
  513. })