index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const app = getApp()
  2. Page({
  3. data: {
  4. frameStyle: { useTop: true, name: '系统', leftArrow: false, useBar: false },
  5. },
  6. toCommon: function (e) {
  7. const { route } = e.currentTarget.dataset;
  8. if (route) wx.navigateTo({ url: `/pages/${route}` });
  9. },
  10. // 重置
  11. async toMoney() {
  12. let obj = {
  13. openid: 'oH0y05UqPfQYt7eBWGMzhkfDwMMA',
  14. money: 0.1,
  15. school_id: '6355f251cb0f85380e83520b',
  16. student_id: '635629fecb0f85380e8354e5'
  17. }
  18. let res = await app.$post(`/payOrder/toCharge`, obj);
  19. if (res.errcode == '0') {
  20. // 调取支付窗口
  21. let wxSign = res.data.wxSign;
  22. console.log(wxSign);
  23. wx.requestPayment({
  24. timeStamp: wxSign.timestamp,
  25. nonceStr: wxSign.nonceStr,
  26. package: `prepay_id=${wxSign.prepay_id}`,
  27. signType: wxSign.signType,
  28. paySign: wxSign.paySign,
  29. async success(payRes) {
  30. console.log('充值成功');
  31. },
  32. async fail(payErr) {
  33. console.log(payErr);
  34. wx.showToast({ title: '充值不成功', icon: 'none' })
  35. }
  36. })
  37. } else {
  38. wx.showToast({ title: res.errmsg, icon: 'none' })
  39. }
  40. },
  41. /**
  42. * 生命周期函数--监听页面加载
  43. */
  44. onLoad: function (options) { },
  45. // 监听用户是否登录
  46. watchLogin: async function () {
  47. // 监听用户是否登录,
  48. wx.getStorage({
  49. key: 'user',
  50. success: async res => {
  51. // if (res.data) wx.redirectTo({ url: '/pages/school/index' })
  52. // /pages/school/index
  53. // /pagesSchool/common/lessoninfo
  54. // /pagesSchool/schAdmin/course/coachlist
  55. },
  56. fail: async res => {
  57. wx.login({
  58. success: async (arr) => {
  59. if (app.globalData?.wxInfo?.openid) { await this.toLogin(app.globalData?.wxInfo?.openid); return; }
  60. const { code: js_code } = arr;
  61. const aee = await app.$get('/wechat/api/login/app', { js_code: js_code, config: 'newCourtApp' });
  62. if (aee.errcode === 0 && aee.data?.openid) {
  63. const openid = aee.data.openid;
  64. app.globalData.wxInfo = { openid };
  65. await this.toLogin(openid);
  66. } else {
  67. wx.showToast({ title: `${aee.errmsg}`, icon: 'fail', duration: 2000 });
  68. wx.redirectTo({ url: '/pages/register/index' });
  69. }
  70. },
  71. });
  72. }
  73. })
  74. },
  75. async toLogin(openid) {
  76. const res = await app.$post('/user/wxAppLogin', { openid });
  77. if (app.$checkRes(res)) {
  78. const { data } = res;
  79. // 没有用户,去注册
  80. if (!data) wx.redirectTo({ url: '/pages/register/index' });
  81. // 有用户,存起来,跳转
  82. else {
  83. wx.setStorageSync('user', data);
  84. wx.redirectTo({ url: '/pages/school/index' });
  85. }
  86. }
  87. },
  88. /**
  89. * 生命周期函数--监听页面初次渲染完成
  90. */
  91. onReady: function () {
  92. },
  93. /**
  94. * 生命周期函数--监听页面显示
  95. */
  96. onShow: function () {
  97. const that = this;
  98. // 监听用户是否登录
  99. that.watchLogin();
  100. },
  101. /**
  102. * 生命周期函数--监听页面隐藏
  103. */
  104. onHide: function () {
  105. },
  106. /**
  107. * 生命周期函数--监听页面卸载
  108. */
  109. onUnload: function () {
  110. },
  111. /**
  112. * 页面相关事件处理函数--监听用户下拉动作
  113. */
  114. onPullDownRefresh: function () {
  115. },
  116. /**
  117. * 页面上拉触底事件的处理函数
  118. */
  119. onReachBottom: function () {
  120. },
  121. /**
  122. * 用户点击右上角分享
  123. */
  124. onShareAppMessage: function () {
  125. }
  126. })