index.js 4.9 KB

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