placeOrder.js 18 KB

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