index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const app = getApp()
  2. const WXAPI = require('apifm-wxapi')
  3. const AUTH = require('../../utils/auth')
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. uid: undefined
  10. },
  11. /**
  12. * 生命周期函数--监听页面加载
  13. */
  14. onLoad: function(options) {
  15. },
  16. /**
  17. * 生命周期函数--监听页面初次渲染完成
  18. */
  19. onReady: function() {
  20. },
  21. /**
  22. * 生命周期函数--监听页面显示
  23. */
  24. onShow: function() {
  25. AUTH.checkHasLogined().then(isLogined => {
  26. if (!isLogined) {
  27. wx.showModal({
  28. title: '提示',
  29. content: '本次操作需要您的登录授权',
  30. cancelText: '暂不登录',
  31. confirmText: '前往登录',
  32. success(res) {
  33. if (res.confirm) {
  34. wx.switchTab({
  35. url: "/pages/my/index"
  36. })
  37. } else {
  38. wx.navigateBack()
  39. }
  40. }
  41. })
  42. }
  43. })
  44. },
  45. bindSave: function(e) {
  46. var that = this;
  47. var amount = e.detail.value.amount;
  48. if (amount == "") {
  49. wx.showModal({
  50. title: '错误',
  51. content: '请填写正确的券号',
  52. showCancel: false
  53. })
  54. return
  55. }
  56. WXAPI.scoreExchange(wx.getStorageSync('token'), amount).then(function(res) {
  57. if (res.code == 700) {
  58. wx.showModal({
  59. title: '错误',
  60. content: '券号不正确',
  61. showCancel: false
  62. })
  63. return
  64. }
  65. if (res.code == 0) {
  66. wx.showModal({
  67. title: '成功',
  68. content: '恭喜您,成功兑换 ' + res.data.score + ' 积分',
  69. showCancel: false,
  70. success: function(res) {
  71. if (res.confirm) {
  72. that.bindCancel();
  73. }
  74. }
  75. })
  76. } else {
  77. wx.showModal({
  78. title: '错误',
  79. content: res.data.msg,
  80. showCancel: false
  81. })
  82. }
  83. })
  84. }
  85. })