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