家政小程序
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.

index.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. address,
  60. contact: name,
  61. phone,
  62. products: [{
  63. id: this.data.detail.id,
  64. amount
  65. }]
  66. }).then((res) => {
  67. if (res.data) {
  68. this.callWXPay(res.data.id, res.data.order_type || 'product_order');
  69. }
  70. });
  71. },
  72. callWXPay(order_id, order_type) {
  73. $.request('payments/pay', 'POST', {
  74. order_id,
  75. order_type
  76. }).then((res) => {
  77. if (res.msg) {
  78. const {
  79. timestamp,
  80. paySign,
  81. nonceStr
  82. } = res.msg;
  83. const pck = res.msg.package;
  84. wx.requestPayment({
  85. timeStamp: timestamp + '',
  86. nonceStr,
  87. package: pck,
  88. signType: 'MD5',
  89. paySign,
  90. success(res) {
  91. console.log('支付成功', res);
  92. $.wx.showToast({
  93. title: '支付成功'
  94. });
  95. },
  96. fail() {
  97. $.wx.showToast({
  98. title: '支付失败'
  99. });
  100. },
  101. complete() {
  102. setTimeout(function () {
  103. $.router.goto({
  104. path: '/pages/order/index',
  105. type: $.ROUTER_TYPE.SWITCH_TAB
  106. });
  107. }, 2000);
  108. }
  109. });
  110. }
  111. });
  112. },
  113. toCart() {
  114. $.request('shopcar/add', 'POST', {
  115. product_id: this.data.detail.id,
  116. product_amount: this.data.amount || 1
  117. }).then((res) => {
  118. $.wx.showToast({
  119. title: '添加成功'
  120. });
  121. // setTimeout(function () {
  122. // $.router.goto({
  123. // path: '/pages/cart/index',
  124. // type: $.ROUTER_TYPE.SWITCH_TAB
  125. // });
  126. // }, 2000);
  127. });
  128. },
  129. // 页面分享
  130. onShareAppMessage() {
  131. let that = this;
  132. return {
  133. title: '玥子轩母婴家政',
  134. desc: that.data.detail.name,
  135. path: '/pages/goodsDetail/index?id=' + that.data.detail.id
  136. }
  137. }
  138. });