placeOrder.js 17 KB

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