giftBagOrder.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // subPackagesE/pages/giftBagOrder/giftBagOrder.js
  2. import {
  3. get,
  4. post
  5. } from '../../../utils/http'
  6. const app = getApp()
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. store_id: '', //店铺ID
  13. storeInfo: '', //店铺信息
  14. giftInfo: "", //礼盒信息
  15. goodsDetailsList: '', //美妆产品详情
  16. showMoneyGood: '', //商品价格
  17. payNow: false, //控制提交订单弹出框
  18. inserllAmount: '', // 我的储值
  19. pay_methon: 1, //支付方式 1微信2储值
  20. orderId: '', //订单ID
  21. exchangeCondition: '', //加购规则
  22. exchangeList: [], //加购产品列表
  23. exchangeSum: 0, //加购总次数
  24. exchangeSelect: false, //是否选中加购产品
  25. toExchangePrice: '', //选中加购产品的总价格
  26. exchangePop: true, //加购弹窗
  27. exchangeCountDown: 5, //加购弹窗倒计时
  28. timer: null, //倒计时器
  29. },
  30. /**
  31. * 生命周期函数--监听页面加载
  32. */
  33. onLoad(options) {
  34. // 弹窗倒计时
  35. this.countDown()
  36. // 计算产品总数量
  37. let sum = 0
  38. if (options.productList) {
  39. let list = JSON.parse(options.productList)
  40. list.forEach(item => {
  41. sum = item.num + sum
  42. })
  43. }
  44. this.setData({
  45. store_id: options.store_id || wx.getStorageSync('store_id'), //店铺名称
  46. goodsDetailsList: JSON.parse(options.productList), //产品信息
  47. giftID: options.giftID, //礼盒ID
  48. total: sum
  49. })
  50. console.log(JSON.parse(options.productList));
  51. // 获取店铺信息
  52. this.getStoreInfo()
  53. // 获取礼盒信息
  54. this.getGiftBagDetail()
  55. },
  56. // 加购弹窗倒计时
  57. countDown: function () {
  58. let that = this;
  59. let exchangeCountDown = that.data.exchangeCountDown; //获取倒计时初始值
  60. that.setData({
  61. timer: setInterval(function () {
  62. exchangeCountDown--;
  63. that.setData({
  64. exchangeCountDown: exchangeCountDown
  65. })
  66. if (exchangeCountDown == 0) {
  67. clearInterval(that.data.timer);
  68. that.setData({
  69. exchangePop: false
  70. })
  71. }
  72. }, 1000)
  73. })
  74. },
  75. // 关闭弹窗
  76. onCloseLandscape() {
  77. clearInterval(this.data.timer);
  78. this.setData({
  79. exchangePop: false
  80. })
  81. },
  82. /**
  83. * 生命周期函数--监听页面初次渲染完成
  84. */
  85. onReady() {
  86. },
  87. /**
  88. * 生命周期函数--监听页面显示
  89. */
  90. onShow() {
  91. },
  92. /**
  93. * 生命周期函数--监听页面隐藏
  94. */
  95. onHide() {
  96. },
  97. /**
  98. * 生命周期函数--监听页面卸载
  99. */
  100. onUnload() {
  101. },
  102. /**
  103. * 页面相关事件处理函数--监听用户下拉动作
  104. */
  105. onPullDownRefresh() {
  106. },
  107. /**
  108. * 页面上拉触底事件的处理函数
  109. */
  110. onReachBottom() {
  111. },
  112. /**
  113. * 用户点击右上角分享
  114. */
  115. onShareAppMessage() {
  116. },
  117. // 提交订单
  118. onNewPay() {
  119. // 判断加购产品是否选择完成
  120. if (this.data.exchangeSelect) {
  121. if (this.data.exchangeSum < this.data.exchangeCondition.time) {
  122. wx.showToast({
  123. title: '您还有剩余次数未选',
  124. icon: 'none'
  125. })
  126. return
  127. }
  128. }
  129. // 筛选礼包信息
  130. let item_data = []
  131. this.data.goodsDetailsList.forEach(item => {
  132. let list = {}
  133. list.item_id = item.item_id
  134. list.goods_id = item.goods_id
  135. list.num = item.num
  136. item_data.push(list)
  137. })
  138. // 筛选添加换购产品
  139. let plus = []
  140. this.data.exchangeList.list.forEach(item => {
  141. if (item.num != 0) {
  142. plus.push(item.id)
  143. }
  144. })
  145. let params = {
  146. store_id: this.data.store_id || wx.getStorageSync('store_id'),
  147. id: this.data.giftID, //礼盒ID
  148. item_data: JSON.stringify(item_data), //配置数据,json字符串,格式如[{item_id:"",goods_id:"",num:""}],item_id配置ID,goods_id商品ID,num商品对应数量
  149. plus_ids: plus.toString()
  150. }
  151. post('v2/api/package/add', params, res => {
  152. if (res.code == 200) {
  153. if (res.data.pay_status == 1) {
  154. wx.showToast({
  155. title: '支付成功',
  156. icon: 'success'
  157. })
  158. // 只有项目购买,需要订阅消息
  159. wx.reLaunch({
  160. url: `/pages/paySuccess/paySuccess?current=1&source=goods&order_id=${res.data.order_id}`
  161. })
  162. } else {
  163. this.setData({
  164. payNow: true,
  165. orderId: res.data.order_id
  166. })
  167. this.getAmount()
  168. }
  169. }
  170. })
  171. },
  172. /**
  173. * 订单支付
  174. */
  175. payOrder() {
  176. let that = this
  177. post(
  178. 'v2/api/order/goods/pay', {
  179. pay_way: this.data.pay_methon == 1 ? 'weixin' : 'amount',
  180. order_id: this.data.orderId
  181. },
  182. (res) => {
  183. that.setData({
  184. payNow: false
  185. })
  186. if (that.data.pay_methon == 2) {
  187. that.goToOrder('支付成功')
  188. return
  189. }
  190. wx.requestPayment({
  191. timeStamp: res.data.pay_data.timeStamp,
  192. nonceStr: res.data.pay_data.nonceStr,
  193. package: res.data.pay_data.package,
  194. signType: res.data.pay_data.signType,
  195. paySign: res.data.pay_data.paySign,
  196. success(res) {
  197. if (res.errMsg == 'requestPayment:ok') {
  198. that.goToOrder('支付成功')
  199. }
  200. },
  201. fail(res) {
  202. that.goToOrder('支付失败')
  203. }
  204. })
  205. }
  206. )
  207. },
  208. /**
  209. * 跳转订单列表
  210. */
  211. goToOrder(text) {
  212. let that = this
  213. wx.showToast({
  214. title: text,
  215. icon: text == '支付成功' ? 'success' : 'error'
  216. })
  217. if (text == '支付成功') {
  218. // 只有项目购买,需要订阅消息
  219. wx.reLaunch({
  220. url: `/pages/paySuccess/paySuccess?current=1&source=goods&order_id=${that.data.orderId}`
  221. })
  222. // 获取下发权限
  223. wx.requestSubscribeMessage({
  224. tmplIds: ['RNife3ZhTYYpUuT26ylGQj8W0v07IPo2TGe941iR3y8'],
  225. success(res) {
  226. let status = ''
  227. if (res['RNife3ZhTYYpUuT26ylGQj8W0v07IPo2TGe941iR3y8'] == 'reject') {
  228. status = 2
  229. } else {
  230. status = 1
  231. }
  232. get('/v2/api/activity/push', {
  233. type: 2,
  234. status,
  235. }, res => {})
  236. },
  237. })
  238. return
  239. } else {
  240. let failUrl = `/pages/goods/orderList/orderList?current=0`
  241. setTimeout(() => {
  242. wx.reLaunch({
  243. url: failUrl
  244. })
  245. }, 1000)
  246. }
  247. },
  248. // 关闭提交订单框
  249. onClosePay() {
  250. this.setData({
  251. payNow: false
  252. })
  253. },
  254. // 选择微信支付
  255. pay_methonw() {
  256. this.setData({
  257. pay_methon: 1
  258. })
  259. },
  260. // 选择储值支付
  261. pay_methonc() {
  262. this.setData({
  263. pay_methon: 2
  264. })
  265. },
  266. /**
  267. * 获取我的储值
  268. * api/user
  269. */
  270. getAmount() {
  271. get('api/user/amount', {}, (res) => {
  272. if (res.data) {
  273. this.setData({
  274. inserllAmount: res.data.amount
  275. })
  276. }
  277. })
  278. },
  279. // 获取店铺信息
  280. getStoreInfo() {
  281. get('api/store/info', {
  282. store_id: this.data.store_id
  283. }, (res => {
  284. this.setData({
  285. storeInfo: res.data
  286. })
  287. }))
  288. },
  289. // 获取礼包详情
  290. getGiftBagDetail() {
  291. get('v2/api/package/info', {
  292. id: this.data.giftID
  293. }, (res => {
  294. this.setData({
  295. giftInfo: res.data,
  296. showMoneyGood: res.data.price
  297. })
  298. // 获取加购列表
  299. this.getNewPlus()
  300. }))
  301. },
  302. //获取新加购列表
  303. getNewPlus() {
  304. post('v2/api/car/plus', {}, (res => {
  305. if (res.code == 200) {
  306. let flag = true
  307. res.data.time = res.data.time.reverse()
  308. res.data.time.forEach(item => {
  309. if (Number(this.data.showMoneyGood) >= item.price && flag) {
  310. flag = false
  311. this.setData({
  312. exchangeCondition: item
  313. })
  314. }
  315. })
  316. res.data.list.forEach(item => {
  317. item.num = 0
  318. })
  319. this.setData({
  320. exchangeList: res.data,
  321. })
  322. }
  323. }))
  324. },
  325. // 加购加产品
  326. add(e) {
  327. let index = e.currentTarget.dataset.index
  328. // 如果加购产品数量大于总次数就返回
  329. if (this.data.exchangeSum + 1 > this.data.exchangeCondition.time) {
  330. return
  331. }
  332. let exchangeList = `exchangeList.list[${index}].num`
  333. this.setData({
  334. [exchangeList]: this.data.exchangeList.list[index].num + 1
  335. })
  336. // 遍历计算出总共加购多少次
  337. let sum = 0
  338. this.data.exchangeList.list.forEach(item => {
  339. sum = item.num + sum
  340. })
  341. this.setData({
  342. exchangeSum: sum
  343. })
  344. // 判断是否选中加购产品
  345. if (sum >= 1) {
  346. this.setData({
  347. exchangeSelect: true,
  348. toExchangePrice: 99,
  349. toExchangePrice: (Number(this.data.showMoneyGood) + 99).toFixed(2)
  350. })
  351. }
  352. },
  353. // 加购减产品
  354. reduce(e) {
  355. let index = e.currentTarget.dataset.index
  356. if (this.data.exchangeList.list[index].num == 0) {
  357. return
  358. }
  359. let exchangeList = `exchangeList.list[${index}].num`
  360. this.setData({
  361. [exchangeList]: this.data.exchangeList.list[index].num - 1
  362. })
  363. // 遍历计算出总共加购多少次
  364. let sum = 0
  365. this.data.exchangeList.list.forEach(item => {
  366. sum = item.num + sum
  367. })
  368. this.setData({
  369. exchangeSum: sum
  370. })
  371. // 判断是否选中加购产品
  372. if (sum < 1) {
  373. this.setData({
  374. exchangeSelect: false,
  375. toExchangePrice: 0,
  376. })
  377. }
  378. },
  379. })