buy.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // pages/buy/buy.js
  2. import { goodsList } from '../../api/index'
  3. import { submitOrder } from '../../api/pay'
  4. import { getAccountNmber } from '../../api/appointment'
  5. import { documentList } from "../../api/document"
  6. const app = getApp()
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. count: 0,
  13. activeIndex: -1,
  14. goodsList: [],
  15. goodsId: '',
  16. orderAmount: '',
  17. num: '',
  18. appusername: '',
  19. appuserheadimg: '',
  20. from: '' // 从哪个页面进入的页面
  21. },
  22. /**
  23. * 生命周期函数--监听页面显示
  24. */
  25. onLoad(options) {
  26. this.getGoodsList()
  27. this.getAccountNmberFn()
  28. this.setData({
  29. from: options.from,
  30. appusername: app.globalData.userInfo.userName.length > 8 ? app.globalData.userInfo.userName.substring(0,4) : app.globalData.userInfo.userName,
  31. appuserheadimg: app.globalData.userInfo.headImg
  32. })
  33. },
  34. // 获取套餐次数
  35. getAccountNmberFn () {
  36. var that = this
  37. getAccountNmber({}).then(res => {
  38. that.setData({
  39. count: res.data
  40. })
  41. })
  42. },
  43. //获取套餐列表
  44. getGoodsList () {
  45. wx.showLoading({
  46. title: '加载中...',
  47. mask: true
  48. })
  49. goodsList({}).then(res => {
  50. wx.hideLoading()
  51. var response = res.data
  52. var recommendIndx = 0
  53. var recommendItem = response.find((item, index) => {
  54. recommendIndx = index
  55. return item.isRecommend == true
  56. })
  57. console.log(recommendItem,recommendIndx,'recommendItem');
  58. if (recommendItem) {
  59. this.setData({
  60. activeIndex: recommendIndx,
  61. goodsId: recommendItem.goodsId,
  62. orderAmount: recommendItem.salePrice,
  63. num: recommendItem.donateNumber + recommendItem.useNumber
  64. })
  65. } else {
  66. var choseItem = response[0]
  67. this.setData({
  68. activeIndex: 0,
  69. goodsId: choseItem.goodsId,
  70. orderAmount: choseItem.salePrice,
  71. num: choseItem.donateNumber + choseItem.useNumber
  72. })
  73. }
  74. this.setData({
  75. goodsList: response
  76. })
  77. }).catch(e => {
  78. wx.hideLoading()
  79. wx.showModal({
  80. content: e,
  81. confirmColor: '#333',
  82. showCancel: false
  83. })
  84. })
  85. },
  86. handlePayType (e) {
  87. var index = Number(e.currentTarget.dataset.index)
  88. var goodsInfo = e.currentTarget.dataset.goodsinfo
  89. console.log(goodsInfo, 'goodsInfo');
  90. this.setData({
  91. activeIndex: index,
  92. goodsId: goodsInfo.goodsId,
  93. orderAmount: goodsInfo.salePrice,
  94. num: goodsInfo.donateNumber + goodsInfo.useNumber
  95. })
  96. },
  97. // 调用微信支付
  98. handleWXPay () {
  99. var that = this
  100. var data = {
  101. goodsId: this.data.goodsId,
  102. orderAmount: this.data.orderAmount,
  103. num: 1
  104. }
  105. wx.showLoading({
  106. title: '加载中...',
  107. mask: true
  108. })
  109. submitOrder(data).then(res => {
  110. wx.hideLoading()
  111. // 拉起微信支付
  112. wx.requestPayment({
  113. timeStamp: res.data.timeStamp,
  114. nonceStr: res.data.nonceStr,
  115. package: res.data.packageStr,
  116. signType: res.data.signType,
  117. paySign: res.data.paySign,
  118. success (res) {
  119. that.getAccountNmberFn()
  120. that.paySuccess()
  121. },
  122. fail (res) {
  123. wx.showToast({
  124. title: '支付失败',
  125. icon: "error"
  126. })
  127. }
  128. })
  129. }).catch(e => {
  130. wx.hideLoading()
  131. wx.showModal({
  132. content: e,
  133. confirmColor: '#333',
  134. showCancel: false
  135. })
  136. })
  137. },
  138. // 支付成功
  139. paySuccess () {
  140. var that = this
  141. wx.showModal({
  142. title: '支付成功,立即去预约',
  143. confirmText: '去预约',
  144. confirmColor: '#333',
  145. cancelColor: '#666',
  146. success (modalRes) {
  147. if (modalRes.confirm) {
  148. if (that.data.from == 'appointment') {
  149. wx.navigateBack()
  150. } else {
  151. that.getDocumentList()
  152. }
  153. }
  154. }
  155. })
  156. },
  157. // 获取用户档案信息
  158. getDocumentList () {
  159. wx.showLoading({
  160. title: '加载中...',
  161. mask: true
  162. })
  163. documentList({
  164. currentPage: 1
  165. }).then(res => {
  166. wx.hideLoading()
  167. const response = res.data.vos || []
  168. if (response.length == 0) {
  169. wx.showModal({
  170. content: '预约用户前需要先添加检测人员信息',
  171. cancelColor: '#666',
  172. confirmColor: '#333',
  173. success (res) {
  174. if (res.confirm) {
  175. wx.navigateTo({
  176. url: '/pages/createFile/createFile?from=buy'
  177. })
  178. }
  179. }
  180. })
  181. } else {
  182. wx.navigateTo({
  183. url: '/pages/appointment/appointment?from=buy'
  184. })
  185. }
  186. }).catch(e => {
  187. wx.hideLoading()
  188. wx.showModal({
  189. content: e,
  190. confirmColor: '#333',
  191. showCancel: false
  192. })
  193. })
  194. },
  195. })