index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // pages/my-account/index.js
  2. import { getProductHot } from '../../api/store.js'
  3. import { coinTransfer } from '../../api/mine.js'
  4. import { openRechargeSubscribe } from '../../utils/SubscribeMessage.js'
  5. import { getUserInfo, userActivity } from '../../api/user.js'
  6. import d from '../../utils/d.js'
  7. const app = getApp()
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. parameter: {
  14. navbar: '1',
  15. return: '1',
  16. title: '钱包',
  17. color: false
  18. },
  19. showPopup: false,
  20. transferAmount: 0, // int 转出数量
  21. transferSymbol: '', // 转的种类
  22. isHidden: false,
  23. userInfo: {},
  24. host_product: [],
  25. isClose: false,
  26. recharge_switch: 0,
  27. activity: {
  28. is_pink: false,
  29. is_seckill: false,
  30. is_bargin: false
  31. }
  32. },
  33. /**
  34. * 登录回调
  35. */
  36. onLoadFun: function () {
  37. this.getUserInfo()
  38. this.get_host_product()
  39. // this.get_activity();
  40. },
  41. /**
  42. * 生命周期函数--监听页面加载
  43. */
  44. onLoad: function (options) {
  45. if (app.globalData.token)
  46. this.setData({
  47. isHidden: true
  48. })
  49. },
  50. openSubscribe: function (e) {
  51. let page = e.currentTarget.dataset.url
  52. wx.showLoading({
  53. title: '正在加载'
  54. })
  55. openRechargeSubscribe()
  56. .then(res => {
  57. wx.hideLoading()
  58. wx.navigateTo({
  59. url: page
  60. })
  61. })
  62. .catch(() => {
  63. wx.hideLoading()
  64. })
  65. },
  66. // 点击币
  67. onTapCoinItem: function (ev) {
  68. // get amount
  69. let amount = 0
  70. for (let i = 0; i < this.data.userInfo.coins.length; i++) {
  71. if (this.data.userInfo.coins[i].symbol === ev.currentTarget.dataset.id) {
  72. amount = this.data.userInfo.coins[i].total
  73. }
  74. }
  75. // check total to be transfered
  76. if (amount < 1) {
  77. wx.showToast({
  78. title: '金额不足以转入'
  79. })
  80. return
  81. }
  82. this.setData({
  83. transferAmount: Math.floor(amount),
  84. transferSymbol: ev.currentTarget.dataset.id,
  85. showPopup: true
  86. })
  87. },
  88. onConfirmTransfer: function () {
  89. this.onCancelTransfer()
  90. // check if input value valid
  91. const regex = new RegExp('^[0-9]+$')
  92. if (!regex.test(this.data.transferAmount)) {
  93. wx.showToast({
  94. title: '您的输入有误'
  95. })
  96. return
  97. }
  98. // check if value great than total
  99. let total = 0
  100. for (let i = 0; i < this.data.userInfo.coins.length; i++) {
  101. if (this.data.userInfo.coins[i].symbol === this.data.transferSymbol) {
  102. total = this.data.userInfo.coins[i].total
  103. }
  104. }
  105. if (total < 1 || this.data.transferAmount > total) {
  106. wx.showToast({
  107. title: '输入错误'
  108. })
  109. return
  110. }
  111. // call API
  112. let that = this
  113. coinTransfer(this.data.transferAmount, this.data.transferSymbol)
  114. .then(res => {
  115. wx.showToast({
  116. title: '转出成功'
  117. })
  118. that.getUserInfo()
  119. })
  120. .catch(err => {
  121. wx.showToast({
  122. title: err
  123. })
  124. })
  125. },
  126. onCancelTransfer: function () {
  127. this.setData({
  128. showPopup: false
  129. })
  130. },
  131. /**
  132. * 获取用户详情
  133. */
  134. getUserInfo: function () {
  135. let that = this
  136. getUserInfo().then(res => {
  137. that.setData({
  138. userInfo: res.data,
  139. recharge_switch: res.data.recharge_switch
  140. })
  141. // app.globalData.unread = res.data.notice
  142. app.setUnread(res.data.notice)
  143. })
  144. },
  145. /**
  146. * 获取活动可参与否
  147. */
  148. get_activity: function () {
  149. var that = this
  150. userActivity().then(res => {
  151. that.setData({
  152. activity: res.data
  153. })
  154. })
  155. },
  156. /**
  157. * 获取我的推荐
  158. */
  159. get_host_product: function () {
  160. var that = this
  161. getProductHot().then(res => {
  162. that.setData({
  163. host_product: res.data
  164. })
  165. })
  166. },
  167. /**
  168. * 生命周期函数--监听页面显示
  169. */
  170. onShow: function () {
  171. if (app.globalData.isLog && this.data.isClose) {
  172. this.getUserInfo()
  173. this.get_host_product()
  174. // this.get_activity();
  175. this.setData({
  176. isHidden: true
  177. })
  178. }
  179. },
  180. /**
  181. * 生命周期函数--监听页面隐藏
  182. */
  183. onHide: function () {
  184. this.setData({
  185. isClose: true
  186. })
  187. }
  188. })