buy.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // pages/buy/buy.js
  2. import { goodsList } from '../../api/index'
  3. import { submitOrder } from '../../api/pay'
  4. import { getAccountNmber, appointmentList } 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. onShow () {
  23. this.getGoodsList()
  24. this.getAccountNmberFn()
  25. },
  26. /**
  27. * 生命周期函数--监听页面显示
  28. */
  29. onLoad(options) {
  30. this.setData({
  31. from: options.from,
  32. appusername: app.globalData.userInfo.userName.length > 8 ? app.globalData.userInfo.userName.substring(0,4) : app.globalData.userInfo.userName,
  33. appuserheadimg: app.globalData.userInfo.headImg
  34. })
  35. },
  36. // 获取套餐次数
  37. getAccountNmberFn () {
  38. var that = this
  39. getAccountNmber({}).then(res => {
  40. that.setData({
  41. count: res.data
  42. })
  43. app.globalData.useNumber = res.data
  44. })
  45. },
  46. //获取套餐列表
  47. getGoodsList () {
  48. wx.showLoading({
  49. title: '加载中...',
  50. mask: true
  51. })
  52. goodsList({}).then(res => {
  53. wx.hideLoading()
  54. var response = res.data
  55. var recommendIndx = 0
  56. var recommendItem = response.find((item, index) => {
  57. recommendIndx = index
  58. return item.isRecommend == true
  59. })
  60. if (recommendItem) {
  61. this.setData({
  62. activeIndex: recommendIndx,
  63. goodsId: recommendItem.goodsId,
  64. orderAmount: recommendItem.salePrice,
  65. num: recommendItem.donateNumber + recommendItem.useNumber
  66. })
  67. } else {
  68. var choseItem = response[0]
  69. this.setData({
  70. activeIndex: 0,
  71. goodsId: choseItem.goodsId,
  72. orderAmount: choseItem.salePrice,
  73. num: choseItem.donateNumber + choseItem.useNumber
  74. })
  75. }
  76. this.setData({
  77. goodsList: response
  78. })
  79. }).catch(e => {
  80. wx.hideLoading()
  81. wx.showModal({
  82. content: e,
  83. confirmColor: '#333',
  84. showCancel: false
  85. })
  86. })
  87. },
  88. handlePayType (e) {
  89. var index = Number(e.currentTarget.dataset.index)
  90. var goodsInfo = e.currentTarget.dataset.goodsinfo
  91. console.log(goodsInfo, 'goodsInfo');
  92. this.setData({
  93. activeIndex: index,
  94. goodsId: goodsInfo.goodsId,
  95. orderAmount: goodsInfo.salePrice,
  96. num: goodsInfo.donateNumber + goodsInfo.useNumber
  97. })
  98. },
  99. // 调用微信支付
  100. handleWXPay () {
  101. var that = this
  102. var data = {
  103. goodsId: this.data.goodsId,
  104. orderAmount: this.data.orderAmount,
  105. num: 1
  106. }
  107. wx.showLoading({
  108. title: '加载中...',
  109. mask: true
  110. })
  111. submitOrder(data).then(res => {
  112. wx.hideLoading()
  113. // 拉起微信支付
  114. wx.requestPayment({
  115. timeStamp: res.data.timeStamp,
  116. nonceStr: res.data.nonceStr,
  117. package: res.data.packageStr,
  118. signType: res.data.signType,
  119. paySign: res.data.paySign,
  120. success (res) {
  121. that.getAccountNmberFn()
  122. that.paySuccess()
  123. },
  124. fail (res) {
  125. wx.showToast({
  126. title: '支付失败',
  127. icon: "error"
  128. })
  129. }
  130. })
  131. }).catch(e => {
  132. wx.hideLoading()
  133. wx.showModal({
  134. content: e,
  135. confirmColor: '#333',
  136. showCancel: false
  137. })
  138. })
  139. },
  140. // 支付成功
  141. paySuccess () {
  142. var that = this
  143. wx.showModal({
  144. title: '支付成功,立即去预约',
  145. confirmText: '去预约',
  146. confirmColor: '#333',
  147. cancelColor: '#666',
  148. success (modalRes) {
  149. if (modalRes.confirm) {
  150. if (that.data.from == 'appointment') {
  151. wx.navigateBack()
  152. } else {
  153. that.hasAppointmentFn()
  154. }
  155. }
  156. }
  157. })
  158. },
  159. // 获取用户预约信息
  160. hasAppointmentFn (flag = true) {
  161. var that = this
  162. wx.showNavigationBarLoading()
  163. appointmentList({
  164. status: 1,
  165. currentPage: 1
  166. }).then(res => {
  167. wx.hideNavigationBarLoading()
  168. if (res.data.vos.length !== 0) {
  169. app.globalData.hasAppointment = true
  170. that.navigateAppointment()
  171. } else {
  172. app.globalData.hasAppointment = false
  173. that.getDocumentList()
  174. }
  175. }).catch(e => {
  176. wx.hideNavigationBarLoading()
  177. })
  178. },
  179. navigateAppointment () {
  180. wx.showModal({
  181. content: '您预约的健康筛查还未体验,请先体验',
  182. cancelColor: '#666',
  183. cancelText: '取消',
  184. confirmText: '我的预约',
  185. confirmColor: '#333',
  186. success (res) {
  187. if (res.confirm) {
  188. wx.navigateTo({
  189. url: '/pages/myAppointment/myAppointment'
  190. })
  191. }
  192. }
  193. })
  194. },
  195. // 获取用户档案信息
  196. getDocumentList () {
  197. wx.showLoading({
  198. title: '加载中...',
  199. mask: true
  200. })
  201. documentList({
  202. currentPage: 1
  203. }).then(res => {
  204. wx.hideLoading()
  205. const response = res.data.vos || []
  206. if (response.length == 0) {
  207. wx.showModal({
  208. content: '预约用户前需要先添加检测人员信息',
  209. cancelColor: '#666',
  210. confirmColor: '#333',
  211. success (res) {
  212. if (res.confirm) {
  213. wx.navigateTo({
  214. url: '/pages/createFile/createFile?from=buy'
  215. })
  216. }
  217. }
  218. })
  219. } else {
  220. wx.navigateTo({
  221. url: '/pages/appointment/appointment?from=buy'
  222. })
  223. }
  224. }).catch(e => {
  225. wx.hideLoading()
  226. wx.showModal({
  227. content: e,
  228. confirmColor: '#333',
  229. showCancel: false
  230. })
  231. })
  232. },
  233. })