confirmOrder.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // pages/confirmOrder/confirmOrder.js
  2. var http = require('../../utils/http.js');
  3. var config = require("../../utils/config.js");
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. orderPath: '',
  10. fullItemObj: {},
  11. totalPrice: 0,
  12. goodsPrice: 0,
  13. transfee: 0,
  14. orderParam: {},
  15. userAddr: null,
  16. remarks: ""
  17. },
  18. onRemarksInput: function(e) {
  19. this.setData({
  20. remarks: e.detail.value
  21. });
  22. },
  23. // 获取orderPath
  24. getSeckill() {
  25. var _this = this
  26. http.request({
  27. url: '/p/seckill/orderPath',
  28. method: 'GET',
  29. callBack: (res) => {
  30. _this.setData({
  31. orderPath: res
  32. })
  33. _this.confirmOrder()
  34. }
  35. })
  36. },
  37. // 确认订单
  38. confirmOrder() {
  39. var secKillObj = wx.getStorageSync("secKillObj")
  40. if (this.data.userAddr) {
  41. secKillObj.addrId = this.data.userAddr.addrId
  42. }
  43. var orderPath = this.data.orderPath
  44. var _this = this
  45. http.request({
  46. url: `/p/seckill/${orderPath}/confirm`,
  47. method: 'POST',
  48. data: secKillObj,
  49. callBack: (res) => {
  50. _this.setData({
  51. fullItemObj: res,
  52. goodsPrice: res.shopCartItem.prodCount * res.shopCartItem.seckillPrice,
  53. transfee: res.transfee,
  54. totalPrice: res.transfee + res.shopCartItem.prodCount * res.shopCartItem.seckillPrice,
  55. userAddr: res.userAddr
  56. })
  57. }
  58. })
  59. },
  60. // 提交订单
  61. commitOrder() {
  62. this.createSocket()
  63. },
  64. // ws
  65. createSocket() {
  66. var _this = this
  67. wx.closeSocket()
  68. var st = wx.connectSocket({
  69. url: `${config.wsDomain}/seckill/websocket/${this.data.orderPath}`,
  70. method: 'get',
  71. fail: function(err) {
  72. wx.showToast({
  73. title: 'ws连接失败',
  74. })
  75. console.log(err)
  76. },
  77. success: function(res) {
  78. console.log("connect")
  79. console.log(res)
  80. }
  81. })
  82. st.onOpen(function(res) {
  83. console.log("open")
  84. console.log(res)
  85. wx.showLoading({
  86. title: '正在拼命抢购',
  87. mask: true
  88. })
  89. setTimeout(() => {
  90. var orderShopParam = {
  91. remarks: _this.data.remarks,
  92. shopId: _this.data.fullItemObj.shopCartItem.shopId
  93. }
  94. http.request({
  95. url: `/p/seckill/${_this.data.orderPath}/submit`,
  96. method: 'POST',
  97. data: orderShopParam
  98. })
  99. }, 1000)
  100. })
  101. st.onError(function(res) {
  102. console.log("error")
  103. console.log(res)
  104. })
  105. st.onClose(function(res) {
  106. console.log("close")
  107. console.log(res)
  108. })
  109. st.onMessage(function(res1) {
  110. console.log("mesg")
  111. console.log(res1)
  112. var orderParam = JSON.parse(res1.data)
  113. if (!orderParam.success) {
  114. wx.showToast({
  115. title: orderParam.msg,
  116. icon: "none"
  117. })
  118. return
  119. }
  120. if (orderParam.obj) {
  121. _this.calWeixinPay(orderParam.obj);
  122. }
  123. wx.closeSocket()
  124. })
  125. },
  126. /**
  127. * 唤起微信支付
  128. */
  129. calWeixinPay: function(orderNumbers) {
  130. wx.showLoading({
  131. mask: true
  132. });
  133. var params = {
  134. url: "/p/order/pay",
  135. method: "POST",
  136. data: {
  137. payType: 1,
  138. orderNumbers: orderNumbers
  139. },
  140. callBack: function(res) {
  141. wx.hideLoading();
  142. wx.requestPayment({
  143. timeStamp: res.timeStamp,
  144. nonceStr: res.nonceStr,
  145. package: res.packageValue,
  146. signType: res.signType,
  147. paySign: res.paySign,
  148. success: e => {
  149. wx.navigateTo({
  150. url: '/pages/pay-result/pay-result?sts=1&orderNumbers=' + orderNumbers + "&orderType=" + this.data.orderType,
  151. })
  152. },
  153. fail: err => {
  154. wx.navigateTo({
  155. url: '/pages/pay-result/pay-result?sts=0&orderNumbers=' + orderNumbers + "&orderType=" + this.data.orderType,
  156. })
  157. }
  158. })
  159. }
  160. };
  161. http.request(params);
  162. },
  163. /**
  164. * 选择地址
  165. */
  166. toAddrListPage: function() {
  167. wx.navigateTo({
  168. url: '/pages/delivery-address/delivery-address?order=0',
  169. })
  170. },
  171. /**
  172. * 生命周期函数--监听页面加载
  173. */
  174. onLoad: function(options) {
  175. },
  176. /**
  177. * 生命周期函数--监听页面初次渲染完成
  178. */
  179. onReady: function() {
  180. },
  181. /**
  182. * 生命周期函数--监听页面显示
  183. */
  184. onShow: function() {
  185. var pages = getCurrentPages();
  186. var currPage = pages[pages.length - 1];
  187. if (currPage.data.selAddress == "yes") {
  188. this.setData({ //将携带的参数赋值
  189. userAddr: currPage.data.item
  190. });
  191. }
  192. this.getSeckill()
  193. },
  194. /**
  195. * 生命周期函数--监听页面隐藏
  196. */
  197. onHide: function() {
  198. },
  199. /**
  200. * 生命周期函数--监听页面卸载
  201. */
  202. onUnload: function() {
  203. },
  204. /**
  205. * 页面相关事件处理函数--监听用户下拉动作
  206. */
  207. onPullDownRefresh: function() {
  208. },
  209. /**
  210. * 页面上拉触底事件的处理函数
  211. */
  212. onReachBottom: function() {
  213. },
  214. /**
  215. * 用户点击右上角分享
  216. */
  217. onShareAppMessage: function() {
  218. }
  219. })