placeOrder.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. app.api.useApi(app.globalData.baseAppUrl + "api/makeorder", {
  249. network_id: nid,
  250. }, "post").then(function (res) {
  251. console.log(res);
  252. if (res.code != 200) {
  253. wx.showLoading({
  254. title: res.message.msg,
  255. })
  256. wx.setTimeout(() => {
  257. wx.hideLoading();
  258. }, 1000);
  259. } else {
  260. var order_no = res.message.data.order_no;
  261. wx.navigateTo({
  262. url: '/pages/order/confirmOreder/confirmOreder?order_no=' + order_no,
  263. })
  264. }
  265. }).catch(function (err) {
  266. console.log(222);
  267. })
  268. },
  269. carPlus(e) {
  270. let items = e.currentTarget.dataset.item, list = this.data.carList;
  271. var carNum = this.data.carNum ? this.data.carNum : 0
  272. var carPrice = this.data.carPrice ? this.data.carPrice : 0
  273. list.map(item => {
  274. if (items.good_id == item.good_id) {
  275. item.stock++
  276. console.log(item);
  277. // this.changeShoppingList(item)
  278. //var price = (item.price.end_time && item.price.start_time) ? item.price.sale_price : item.price.price
  279. var price = item.money
  280. this.setData({
  281. carNum: carNum + 1,
  282. carPrice: parseFloat(carPrice) + parseFloat(price),
  283. })
  284. var good_id = items.good_id;
  285. var money = price;
  286. var stock = item.stock;
  287. this.cartChang(good_id, money, stock);
  288. }
  289. })
  290. this.setData({
  291. carList: list
  292. })
  293. //this.getCarAll()
  294. },
  295. carMinus(e) {
  296. let items = e.currentTarget.dataset.item, list = this.data.carList;
  297. var carNum = this.data.carNum ? this.data.carNum : 0
  298. var carPrice = this.data.carPrice ? this.data.carPrice : 0
  299. console.log(items);
  300. list.map(item => {
  301. if (items.good_id == item.good_id) {
  302. item.stock--
  303. if (item.stock <= 0) {
  304. item.stock = 0
  305. } else {
  306. item.stock--
  307. }
  308. console.log(item);
  309. var price = item.money
  310. this.setData({
  311. carNum: (carNum - 1) <= 0 ? 0 : carNum - 1,
  312. carPrice: (parseFloat(carPrice) - parseFloat(price)) <= 0 ? 0 : parseFloat(carPrice) - parseFloat(price)
  313. })
  314. // this.changeShoppingList(item)
  315. var good_id = items.good_id;
  316. var money = price;
  317. var stock = item.stock;
  318. this.cartChang(good_id, money, stock);
  319. if (item.num == 0) {
  320. list.splice(index, 1)
  321. }
  322. }
  323. })
  324. this.setData({
  325. carList: list
  326. })
  327. },
  328. clearCarAll() {
  329. this.setData({
  330. carList: []
  331. })
  332. let shoppingList = this.data.shoppingList
  333. shoppingList.map(item => {
  334. item.num = 0
  335. })
  336. this.setData({
  337. shoppingList
  338. })
  339. var that = this;
  340. var nid = this.data.worke.id;
  341. app.api.useApi(app.globalData.baseAppUrl + "api/DeleteCart", {
  342. network_id: nid,
  343. }, "DELETE").then(function (res) {
  344. console.log(res);
  345. if (res.code != 200) {
  346. wx.showLoading({
  347. title: res.message.msg,
  348. })
  349. } else {
  350. var data = res.message.data;
  351. wx.showLoading({
  352. title: res.message.msg,
  353. })
  354. }
  355. wx.hideLoading();
  356. }).catch(function (err) {
  357. console.log(222);
  358. })
  359. this.getCarAll()
  360. this.btnShow()
  361. this.getHeight()
  362. },
  363. closeNews() {
  364. console.log(this.data.height);
  365. this.setData({
  366. height: this.data.height + 40
  367. })
  368. },
  369. changeActive(e) {
  370. let { activekey } = e.currentTarget.dataset
  371. var id = e.currentTarget.dataset.id;
  372. var shoppingList=this.data.shop[id];
  373. if(typeof shoppingList=="undefined"){
  374. shoppingList=new Array();
  375. }
  376. this.setData({
  377. activeKey: activekey,
  378. shoppingList: shoppingList
  379. })
  380. },
  381. addressDetail() {
  382. this.setData({
  383. detailsShow: !this.data.detailsShow
  384. })
  385. },
  386. getHeight() {
  387. var clientHeight;
  388. wx.getSystemInfo({
  389. success: (res) => {
  390. clientHeight = res.windowHeight;
  391. }
  392. });
  393. var query1 = wx.createSelectorQuery()
  394. var query2 = wx.createSelectorQuery()
  395. var query3 = wx.createSelectorQuery()
  396. query1.select('#footerBar').boundingClientRect()
  397. query2.select('#topBox').boundingClientRect()
  398. query3.select('#scrollBox').boundingClientRect()
  399. query2.exec(result => {
  400. query3.exec(r => {
  401. query1.exec(res => {
  402. if (res && res[0] != null) {
  403. this.setData({
  404. height: clientHeight - res[0].height - result[0].height - r[0].height - 40
  405. })
  406. } else {
  407. this.setData({
  408. height: clientHeight - result[0].height - r[0].height - 40
  409. })
  410. }
  411. console.log(clientHeight, result[0].height, r[0].height);
  412. })
  413. })
  414. })
  415. },
  416. btnShow() {
  417. this.setData({
  418. show: !this.data.show
  419. })
  420. this.getcart()
  421. },
  422. leftList: function () {
  423. var that = this;
  424. var nid = this.data.worke.id;
  425. app.api.useApi(app.globalData.baseAppUrl + "api/good", {
  426. network_id: nid,
  427. }, "get").then(function (res) {
  428. console.log(res);
  429. if (res.code != 200) {
  430. wx.showLoading({
  431. title: res.message.status,
  432. })
  433. } else {
  434. var data = res.message.data;
  435. var shop = [];
  436. for (var index in data) {
  437. shop[data[index].id] = data[index].goods
  438. }
  439. that.setData({
  440. navList: res.message.data,
  441. shoppingList: res.message.data[0].goods,
  442. shop: shop
  443. })
  444. }
  445. wx.hideLoading();
  446. }).catch(function (err) {
  447. console.log(222);
  448. })
  449. },
  450. location() {
  451. wx.openLocation({
  452. latitude: Number(this.data.worke.lat),
  453. longitude: Number(this.data.worke.lng),
  454. });
  455. },
  456. /**
  457. * @name获取网点
  458. */
  459. getNwetwork(network_id){
  460. var that=this;
  461. app.api.useApi(app.globalData.baseAppUrl+'api/network/desc',{
  462. network_id:network_id>0?network_id:wx.getStorageSync('nid')
  463. }).then((res)=>{
  464. if(res.code!=200){
  465. wx.showToast({
  466. title: res.message.msg,
  467. })
  468. }else{
  469. console.log(res.message.data);
  470. that.setData({
  471. worke:res.message.data
  472. });
  473. this.getcart();
  474. }
  475. }).catch((err)=>{
  476. console.log(err.message)
  477. })
  478. }
  479. })