huhaoguanli.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. const app = getApp();
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. huHaoList: [],
  8. showUnbindModal: false,
  9. unbindId: null
  10. },
  11. onLoad: function (options) {
  12. console.log('页面加载时的全局数据:', {
  13. currentAccountInfo: app.globalData.currentAccountInfo,
  14. userWxInfo: app.globalData.userWxInfo
  15. });
  16. this.getAccountList();
  17. },
  18. // 过滤户号列表,只显示deleted=0的数据
  19. getAccountList: function () {
  20. const _this = this;
  21. wx.showLoading({
  22. title: '获取中...',
  23. mask: true,
  24. });
  25. wx.request({
  26. url: app.globalData.interfaceUrls.accountList,
  27. method: 'GET',
  28. header: {
  29. 'content-type': 'application/json',
  30. 'token': app.globalData.userWxInfo.token,
  31. 'source': "wc",
  32. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  33. },
  34. success(res) {
  35. wx.hideLoading();
  36. let apiReturnData = res.data;
  37. // 从全局获取当前账户的信息currentAccountInfo
  38. const currentUsernumber = app.globalData.currentAccountInfo.usernumber;
  39. console.log('当前用户信息:', {
  40. currentUsernumber: currentUsernumber,
  41. currentAccountInfo: app.globalData.currentAccountInfo
  42. });
  43. _this.setData({
  44. huHaoList: apiReturnData.data.map(item => {
  45. const isCurrent = item.usernumber === currentUsernumber;
  46. console.log('比较户号:', {
  47. itemNumber: item.usernumber,
  48. currentNumber: currentUsernumber,
  49. isCurrent: isCurrent,
  50. username: item.username
  51. });
  52. return {
  53. ...item,
  54. isCurrentUser: isCurrent
  55. };
  56. })
  57. });
  58. },
  59. fail(error) {
  60. wx.hideLoading();
  61. utils.simleInfo('获取数据失败,请稍后再试');
  62. }
  63. });
  64. },
  65. // 点击解除绑定按钮
  66. unbindHuHao: function (e) {
  67. const id = e.currentTarget.dataset.id;
  68. this.setData({
  69. showUnbindModal: true,
  70. unbindId: id
  71. });
  72. },
  73. // 取消解绑
  74. cancelUnbind: function () {
  75. this.setData({
  76. showUnbindModal: false,
  77. unbindId: null
  78. });
  79. },
  80. // 确认解绑
  81. confirmUnbind: function () {
  82. const id = this.data.unbindId;
  83. this.accountUnbind(id);
  84. },
  85. // 解绑方法
  86. accountUnbind:function (id) {
  87. debugger;
  88. const unbindInfo = this.data.huHaoList.filter(item => {
  89. return item.id === id;
  90. })[0];
  91. const otherInfo=this.data.huHaoList.filter(item => {
  92. return item.id!= id;
  93. });
  94. const _this = this;
  95. wx.showLoading({
  96. title: '获取中...',
  97. mask: true,
  98. });
  99. wx.request({
  100. url: app.globalData.interfaceUrls.accountUnbind,
  101. method: 'POST',
  102. data: {
  103. account: unbindInfo.usernumber,
  104. deKey: unbindInfo.dsKey,
  105. refresh: false,
  106. otherDsKeys: []
  107. },
  108. header: {
  109. 'content-type': 'application/json', // 默认值
  110. 'token': app.globalData.userWxInfo.token,
  111. },
  112. success(res) {
  113. wx.hideLoading();
  114. let apiReturnData = res.data;
  115. if(apiReturnData.code=='200'){
  116. wx.showToast({
  117. title: '解绑成功',
  118. icon: 'success',
  119. duration: 2000
  120. });
  121. _this.setData({
  122. huHaoList: otherInfo,
  123. showUnbindModal: false,
  124. currentHuHao: null
  125. });
  126. if(otherInfo.length==0){
  127. app.globalData.currentAccountInfo=[];
  128. app.globalData.userWxInfo.currentDsKey="";
  129. wx.redirectTo({
  130. url: '/pages/FirstBangDing/FirstBangDing',
  131. })
  132. }else{
  133. app.globalData.currentAccountInfo=otherInfo[0];
  134. app.globalData.userWxInfo.currentDsKey=otherInfo[0].deKey;
  135. }
  136. }else{
  137. wx.showToast({
  138. title: apiReturnData.msg,
  139. icon: 'error',
  140. duration: 2000
  141. })
  142. wx.hideLoading();
  143. }
  144. },
  145. fail(error) {
  146. wx.hideLoading();
  147. utils.simleInfo('户号解绑失败,请稍后再试');
  148. }
  149. });
  150. },
  151. // 跳转到绑定新户号页面
  152. goToBindNewHuHao: function () {
  153. wx.navigateTo({
  154. url: '/pages/huhaobangding/huhaobangding'
  155. });
  156. },
  157. /**
  158. * 返回上一页
  159. */
  160. goBack: function () {
  161. wx.navigateBack();
  162. },
  163. switchHuHao(e) {
  164. const huHaoInfo = e.currentTarget.dataset.item;
  165. // 更新全局数据
  166. app.globalData.userWxInfo = {
  167. ...app.globalData.userWxInfo,
  168. username: huHaoInfo.username,
  169. usernumber: huHaoInfo.usernumber,
  170. address: huHaoInfo.address,
  171. groupName: huHaoInfo.groupName,
  172. currentDsKey: huHaoInfo.dsKey // 更新 currentDsKey
  173. };
  174. // 更新当前账户信息
  175. app.globalData.currentAccountInfo = huHaoInfo;
  176. // 将新选择的户号信息保存到本地存储
  177. wx.setStorageSync('currentHuHao', huHaoInfo);
  178. wx.showToast({
  179. title: '户号切换成功',
  180. icon: 'success',
  181. duration: 1500
  182. });
  183. // 设置一个标记表示需要刷新首页
  184. wx.setStorageSync('needRefreshHomepage', true);
  185. // 延迟跳转到首页
  186. setTimeout(() => {
  187. wx.switchTab({
  188. url: '/pages/homepage/homepage'
  189. });
  190. }, 1500);
  191. },
  192. })