家政小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const WxParse = require('../../wxParse/wxParse.js');
  2. const $ = global;
  3. new $.Page({
  4. data: {
  5. amount: 1
  6. },
  7. onLoad(options) {
  8. const id = options.id;
  9. if (id) {
  10. $.wx.showToast({
  11. title: '加载中'
  12. }, 'loading');
  13. $.request(`product/${id}`, 'GET', {}).then((res) => {
  14. if (res.data) {
  15. $.wx.hideToast('loading');
  16. const article = res.data.info;
  17. this.setData({
  18. detail: res.data,
  19. unitPrice: res.data.price
  20. });
  21. WxParse.wxParse('article', 'html', article, this, 5)
  22. }
  23. });
  24. }
  25. },
  26. minusAmount() {
  27. if (this.data.amount > 1) {
  28. this.setData({
  29. amount: this.data.amount - 1
  30. });
  31. }
  32. },
  33. addAmount() {
  34. this.setData({
  35. amount: this.data.amount + 1
  36. });
  37. },
  38. toPay() {
  39. const {
  40. name,
  41. phone,
  42. address,
  43. amount
  44. } = this.data;
  45. if (!name || !phone || !address || !amount) {
  46. return $.wx.showToast({
  47. title: '请把信息填写完整'
  48. });
  49. }
  50. if (!this._isPhone(phone)) {
  51. return $.wx.showToast({
  52. title: '请输入正确的手机号码'
  53. });
  54. }
  55. $.wx.showToast({
  56. title: '购买中'
  57. }, 'loading');
  58. $.request('product_order/store', 'POST', {
  59. amount,
  60. address,
  61. contact: name,
  62. phone,
  63. product_id: this.data.detail.id
  64. }).then((res) => {
  65. if (res.data) {
  66. this.callWXPay(res.data.id, res.data.order_type || 'product_order');
  67. }
  68. });
  69. },
  70. callWXPay(order_id, order_type) {
  71. $.request('payments/pay', 'POST', {
  72. order_id,
  73. order_type
  74. }).then((res) => {
  75. if (res.msg) {
  76. const {
  77. timestamp,
  78. paySign,
  79. nonceStr
  80. } = res.msg;
  81. const pck = res.msg.package;
  82. wx.requestPayment({
  83. timeStamp: timestamp + '',
  84. nonceStr,
  85. package: pck,
  86. signType: 'MD5',
  87. paySign,
  88. success(res) {
  89. console.log('支付成功', res);
  90. $.wx.showToast({
  91. title: '支付成功'
  92. });
  93. },
  94. fail() {
  95. $.wx.showToast({
  96. title: '支付失败'
  97. });
  98. },
  99. complete() {
  100. setTimeout(function () {
  101. $.router.goto({
  102. path: '/pages/order/index',
  103. type: $.ROUTER_TYPE.SWITCH_TAB
  104. });
  105. }, 2000);
  106. }
  107. });
  108. }
  109. });
  110. },
  111. toCart() {
  112. $.request('shopcar/add', 'POST', {
  113. product_id: this.data.detail.id,
  114. product_amount: this.data.amount || 1
  115. }).then((res) => {
  116. $.wx.showToast({
  117. title: '添加成功'
  118. });
  119. setTimeout(function () {
  120. $.router.goto({
  121. path: '/pages/cart/index',
  122. type: $.ROUTER_TYPE.SWITCH_TAB
  123. });
  124. }, 2000);
  125. });
  126. }
  127. });