buy.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. app.globalData.useNumber = res.data
  42. })
  43. },
  44. //获取套餐列表
  45. getGoodsList () {
  46. wx.showLoading({
  47. title: '加载中...',
  48. mask: true
  49. })
  50. goodsList({}).then(res => {
  51. wx.hideLoading()
  52. var response = res.data
  53. var recommendIndx = 0
  54. var recommendItem = response.find((item, index) => {
  55. recommendIndx = index
  56. return item.isRecommend == true
  57. })
  58. console.log(recommendItem,recommendIndx,'recommendItem');
  59. if (recommendItem) {
  60. this.setData({
  61. activeIndex: recommendIndx,
  62. goodsId: recommendItem.goodsId,
  63. orderAmount: recommendItem.salePrice,
  64. num: recommendItem.donateNumber + recommendItem.useNumber
  65. })
  66. } else {
  67. var choseItem = response[0]
  68. this.setData({
  69. activeIndex: 0,
  70. goodsId: choseItem.goodsId,
  71. orderAmount: choseItem.salePrice,
  72. num: choseItem.donateNumber + choseItem.useNumber
  73. })
  74. }
  75. this.setData({
  76. goodsList: response
  77. })
  78. }).catch(e => {
  79. wx.hideLoading()
  80. wx.showModal({
  81. content: e,
  82. confirmColor: '#333',
  83. showCancel: false
  84. })
  85. })
  86. },
  87. handlePayType (e) {
  88. var index = Number(e.currentTarget.dataset.index)
  89. var goodsInfo = e.currentTarget.dataset.goodsinfo
  90. console.log(goodsInfo, 'goodsInfo');
  91. this.setData({
  92. activeIndex: index,
  93. goodsId: goodsInfo.goodsId,
  94. orderAmount: goodsInfo.salePrice,
  95. num: goodsInfo.donateNumber + goodsInfo.useNumber
  96. })
  97. },
  98. // 调用微信支付
  99. handleWXPay () {
  100. var that = this
  101. var data = {
  102. goodsId: this.data.goodsId,
  103. orderAmount: this.data.orderAmount,
  104. num: 1
  105. }
  106. wx.showLoading({
  107. title: '加载中...',
  108. mask: true
  109. })
  110. submitOrder(data).then(res => {
  111. wx.hideLoading()
  112. // 拉起微信支付
  113. wx.requestPayment({
  114. timeStamp: res.data.timeStamp,
  115. nonceStr: res.data.nonceStr,
  116. package: res.data.packageStr,
  117. signType: res.data.signType,
  118. paySign: res.data.paySign,
  119. success (res) {
  120. that.getAccountNmberFn()
  121. that.paySuccess()
  122. },
  123. fail (res) {
  124. wx.showToast({
  125. title: '支付失败',
  126. icon: "error"
  127. })
  128. }
  129. })
  130. }).catch(e => {
  131. wx.hideLoading()
  132. wx.showModal({
  133. content: e,
  134. confirmColor: '#333',
  135. showCancel: false
  136. })
  137. })
  138. },
  139. // 支付成功
  140. paySuccess () {
  141. var that = this
  142. wx.showModal({
  143. title: '支付成功,立即去预约',
  144. confirmText: '去预约',
  145. confirmColor: '#333',
  146. cancelColor: '#666',
  147. success (modalRes) {
  148. if (modalRes.confirm) {
  149. if (that.data.from == 'appointment') {
  150. wx.navigateBack()
  151. } else {
  152. that.getDocumentList()
  153. }
  154. }
  155. }
  156. })
  157. },
  158. // 获取用户档案信息
  159. getDocumentList () {
  160. wx.showLoading({
  161. title: '加载中...',
  162. mask: true
  163. })
  164. documentList({
  165. currentPage: 1
  166. }).then(res => {
  167. wx.hideLoading()
  168. const response = res.data.vos || []
  169. if (response.length == 0) {
  170. wx.showModal({
  171. content: '预约用户前需要先添加检测人员信息',
  172. cancelColor: '#666',
  173. confirmColor: '#333',
  174. success (res) {
  175. if (res.confirm) {
  176. wx.navigateTo({
  177. url: '/pages/createFile/createFile?from=buy'
  178. })
  179. }
  180. }
  181. })
  182. } else {
  183. wx.navigateTo({
  184. url: '/pages/appointment/appointment?from=buy'
  185. })
  186. }
  187. }).catch(e => {
  188. wx.hideLoading()
  189. wx.showModal({
  190. content: e,
  191. confirmColor: '#333',
  192. showCancel: false
  193. })
  194. })
  195. },
  196. })