submitOrder.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // pages/groupmeallist/submitOrder/submitOrder.js
  2. import storeGoods from '../../../api/storeGoods'
  3. const cache = require('../../../utils/cache.js');
  4. import Activity from '../../../api/activity';
  5. const user = require('../../../utils/user.js');
  6. import base from '../../../utils/base'
  7. const app = getApp()
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. orderGoodList: [{}, {}, {}, {}],
  14. store: {},
  15. phone: '',
  16. storeWay: '',
  17. addressvaule: '',
  18. addressId: '',
  19. coupon: '',
  20. couponSn: '',
  21. hallFood: '',
  22. invoiceValue: '',
  23. invoiceId: '',
  24. hidden: true,
  25. inputValue: '',
  26. remark: '',
  27. showAllSku: false,
  28. isShowPannel: false,
  29. forSelectList: [],
  30. discoutTitle: '',
  31. couponSn: '',
  32. orderMode: '',
  33. discountPriceY: ''
  34. },
  35. //全部显示和收起
  36. handleFlodExtend() {
  37. this.setData({
  38. showAllSku: !this.data.showAllSku
  39. })
  40. },
  41. /**
  42. * 生命周期函数--监听页面加载
  43. */
  44. onLoad: function (options) {
  45. let coupon = options.coupon
  46. if (coupon != null) {
  47. let jsoncoupon = JSON.parse(coupon)
  48. this.setData({
  49. coupon: jsoncoupon,
  50. couponSn: jsoncoupon.sn
  51. })
  52. }
  53. let invoiceValue = options.invoiceValue
  54. let invoiceId = options.invoiceId
  55. if (invoiceValue != null && invoiceId != null) {
  56. this.setData({
  57. invoiceValue: invoiceValue,
  58. invoiceId: invoiceId
  59. })
  60. }
  61. let addressvaule = options.addressvalue
  62. let addressId = options.addressId
  63. if (addressvaule != null && addressId != null) {
  64. let address = {
  65. addressvaule: addressvaule,
  66. addressId: addressId
  67. }
  68. // 设置地址缓存
  69. cache.setaddress(address)
  70. this.setData({
  71. addressvaule: addressvaule,
  72. addressId: addressId
  73. })
  74. }
  75. },
  76. /**
  77. * 取消
  78. */
  79. cancel: function () {
  80. this.setData({
  81. hidden: true,
  82. inputValue: '',
  83. remark: '',
  84. });
  85. },
  86. /**
  87. * 提交
  88. */
  89. confirm: function (e) {
  90. let inputValue = e.detail.value
  91. this.setData({
  92. hidden: true,
  93. inputValue: inputValue,
  94. remark: inputValue,
  95. });
  96. },
  97. /**
  98. * 备注
  99. */
  100. getReason: function (e) {
  101. this.setData({
  102. inputValue: e.detail.value
  103. })
  104. },
  105. remarkShow() {
  106. this.setData({
  107. hidden: false,
  108. // inputValue: '',
  109. // remark: '',
  110. });
  111. },
  112. /**
  113. * 生命周期函数--监听页面初次渲染完成
  114. */
  115. onReady: function () {
  116. },
  117. listenInput(e) {
  118. this.setData({
  119. phone: e.detail.value
  120. })
  121. },
  122. // 提交订单
  123. submitOrder() {
  124. console.log("进入创建订单方法")
  125. let parms = {}
  126. if (!this.data.isValid) {
  127. app.showToast("购物车无商品")
  128. return
  129. }
  130. // parms={
  131. // mobile:cache.getMobile(),
  132. // storeId:cache.getStoreId(),
  133. // couponSn:this.data.couponSn,
  134. // dinnerType:this.data.storeWay,
  135. // addressId:this.data.addressId,
  136. // invoiceId:this.data.invoiceId,
  137. // phone:this.data.phone,
  138. // remark:this.data.remark
  139. // }
  140. storeGoods.creatOrder({
  141. mobile: cache.getMobile(),
  142. storeId: cache.getStoreId(),
  143. couponSn: this.data.couponSn,
  144. dinnerType: this.data.storeWay,
  145. addressId: this.data.addressId,
  146. invoiceId: this.data.invoiceId,
  147. phone: this.data.phone,
  148. remark: this.data.remark
  149. }).then(res => {
  150. if (res.code == 200 && res.data.state != -2) {
  151. // 唤起收银台
  152. this.goToPay(res.data)
  153. } else if (res.data.state != -2) {
  154. wx.redirectTo({
  155. url: '/pages/userOrderList/userOrderList',
  156. })
  157. }
  158. }).catch(_ => {
  159. // this.setData({
  160. // isShowPannel: true
  161. // })
  162. this.tapSex()
  163. console.log(_)
  164. })
  165. },
  166. tapSex:function(res){
  167. var that = this
  168. wx.showModal({
  169. title: '提示',
  170. content: '订单创建失败,确认之后返回购物栏',
  171. showCancel: false,
  172. confirmText:'确认',
  173. success(res){
  174. if(res.confirm){
  175. that.goToStoreGoods()
  176. }
  177. }
  178. })
  179. },
  180. goToPay(order) {
  181. const storeInfo = cache.getCurrStore()
  182. // console.log(user.getUserInfo().openid,)
  183. console.log(storeInfo)
  184. storeGoods.getPayParams({
  185. orderSn: order.orderSn,
  186. payCode: storeInfo.payCode,
  187. openId: Activity.getOpenId(),
  188. isOrderPayment: true,
  189. orderInfoPath: "../orderDetail/orderDetail?sn=" + order.orderSn,
  190. productDetailPath: "../storeGoods/storeGoods",
  191. }).then(res => {
  192. if (res.code == 200) {
  193. this.rousePayment(res.data, order)
  194. }
  195. }).catch(_ => {
  196. console.log(_)
  197. })
  198. },
  199. rousePayment(payParam, order) {
  200. wx.requestOrderPayment({
  201. timeStamp: payParam.wxPrePay.timeStamp,
  202. package: payParam.wxPrePay.pack,
  203. nonceStr: payParam.wxPrePay.nonceStr,
  204. signType: payParam.wxPrePay.signType,
  205. paySign: payParam.wxPrePay.paySign,
  206. orderInfo:payParam.orderInfo,
  207. success: function (res) {
  208. //支付成功,跳转领取成功页面
  209. console.log("支付成功,跳到订单详情页面")
  210. storeGoods.getPayStatus(order.orderSn);
  211. app.showToast("支付成功")
  212. wx.redirectTo({
  213. url: '../orderDetail/orderDetail?sn=' + order.orderSn,
  214. })
  215. },
  216. fail: function (res) {
  217. console.log("未支付")
  218. app.showToast("支付失败")
  219. wx.redirectTo({
  220. url: '../orderDetail/orderDetail?sn=' + order.orderSn,
  221. })
  222. }
  223. })
  224. },
  225. /**
  226. * 生命周期函数--监听页面显示
  227. */
  228. onShow: function () {
  229. let store = cache.getCurrStore();
  230. console.log(store)
  231. let storeWay = cache.getStoreWay();
  232. let address = cache.getaddress();
  233. if (address != null) {
  234. this.setData({
  235. addressvaule: address.addressvaule,
  236. addressId: address.addressId,
  237. })
  238. }
  239. if (store != null) {
  240. this.setData({
  241. store: store,
  242. storeWay: storeWay,
  243. hallFood: store.hallFood
  244. })
  245. }
  246. this.loadCartData();
  247. this.getUseConfirmOrderCoupon()
  248. },
  249. //发票列表
  250. gotoinvoice() {
  251. wx.redirectTo({
  252. url: '../invoicesList/invoicesList?way=' + 'order',
  253. })
  254. },
  255. // 自动获取手机号
  256. getPhone() {
  257. let phone = cache.getMobile()
  258. this.setData({
  259. phone: phone
  260. })
  261. },
  262. // 选择收货地址
  263. chooseAdress() {
  264. wx.redirectTo({
  265. url: '../address/address?isreturn=' + 'true',
  266. })
  267. },
  268. getUseConfirmOrderCoupon() {
  269. storeGoods.orderCouponForSelect({
  270. storeId: cache.getStoreId(),
  271. mobile: cache.getMobile(),
  272. // tableId: 1
  273. }).then(res => {
  274. if (res.code == 200) {
  275. this.setData({
  276. forSelectList: res.data
  277. })
  278. }
  279. }).catch(_ => {
  280. // // this.setData({
  281. // // isShowPannel: true
  282. // // })
  283. // this.tapSex()
  284. console.log(_)
  285. })
  286. },
  287. //选择卡券
  288. gotoselectcoupon() {
  289. wx.navigateTo({
  290. url: '../couponlist/couponlist',
  291. })
  292. },
  293. /**
  294. * 加载购物车数据
  295. */
  296. loadCartData() {
  297. storeGoods.getCartData(cache.getStoreId(), cache.getMobile(), null, this.data.couponSn, ).then(res => {
  298. if (res.code == 200) {
  299. this.cartDataMapToView(res.data)
  300. }
  301. }).catch(_ => {
  302. console.log(_)
  303. })
  304. },
  305. cartDataMapToView(data) {
  306. var isShowCartPannel = true;
  307. if (base.isEmpty(data.skuList) || data.skuList.length == 0) {
  308. isShowCartPannel = false
  309. data.totalPriceY = "0.00"
  310. data.totalNum = 0
  311. }
  312. // let skuList = data.skuList || []
  313. // skuList.forEach(sku => {
  314. // if (!base.isEmpty(sku.specList)) {
  315. // let specValueList = []
  316. // sku.specList.forEach(spec => {
  317. // specValueList.push(spec.specValue)
  318. // })
  319. // sku.specDes = specValueList.join("、")
  320. // } else {
  321. // sku.specDes = ""
  322. // }
  323. // sku.allFee = base.fenToYuan(sku.num * sku.price)
  324. // sku.allOriFee = base.fenToYuan(sku.num * sku.oriPrice)
  325. // })
  326. this.setData({
  327. isValid: data.skuList.length > 0,
  328. skuList: data.skuList,
  329. areaName: data.areaName,
  330. tableCode: data.tableCode,
  331. totalNum: data.totalNum,
  332. totalOriPriceY: data.totalOriPriceY,
  333. totalPriceY: data.totalPriceY - data.discountPriceY,
  334. totalPrice: data.totalPrice,
  335. totalOriPrice: data.totalOriPrice,
  336. discountPriceY: data.discountPriceY
  337. })
  338. },
  339. // 详情和收起
  340. changeCollapseOrDetail: function (e) {
  341. let idx = e.currentTarget.dataset.idx;
  342. var that = this;
  343. that.data.skuList[idx].show = !that.data.skuList[idx].show
  344. that.setData({
  345. skuList: that.data.skuList
  346. });
  347. console.log("购物车详情");
  348. console.log(that.data.skuList);
  349. },
  350. /**
  351. * 生命周期函数--监听页面隐藏
  352. */
  353. onHide: function () {
  354. },
  355. /**
  356. * 生命周期函数--监听页面卸载
  357. */
  358. onUnload: function () {
  359. },
  360. /**
  361. * 页面相关事件处理函数--监听用户下拉动作
  362. */
  363. onPullDownRefresh: function () {
  364. },
  365. /**
  366. * 页面上拉触底事件的处理函数
  367. */
  368. onReachBottom: function () {
  369. },
  370. /**
  371. * 用户点击右上角分享
  372. */
  373. onShareAppMessage: function () {
  374. }
  375. })