buy.js 5.7 KB

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