huhaoguanli.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. this.getAccountList();
  13. },
  14. // 过滤户号列表,只显示deleted=0的数据
  15. getAccountList: function () {
  16. const _this = this;
  17. wx.showLoading({
  18. title: '获取中...',
  19. mask: true,
  20. });
  21. wx.request({
  22. url: app.globalData.interfaceUrls.accountList,
  23. method: 'GET',
  24. header: {
  25. 'content-type': 'application/json',
  26. 'token': app.globalData.userWxInfo.token,
  27. 'source': "wc",
  28. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  29. },
  30. success(res) {
  31. wx.hideLoading();
  32. let apiReturnData = res.data;
  33. // 从全局获取当前账户的信息currentAccountInfo
  34. const currentUsernumber = app.globalData.currentAccountInfo.usernumber;
  35. _this.setData({
  36. huHaoList: apiReturnData.data.map(item => {
  37. const isCurrent = item.usernumber === currentUsernumber;
  38. return {
  39. ...item,
  40. isCurrentUser: isCurrent
  41. };
  42. })
  43. }, () => {
  44. });
  45. debugger
  46. },
  47. fail(error) {
  48. wx.hideLoading();
  49. console.error('获取列表失败:', error);
  50. wx.showToast({
  51. title: '获取数据失败,请稍后再试',
  52. icon: 'none'
  53. });
  54. }
  55. });
  56. },
  57. // 点击解除绑定按钮
  58. unbindHuHao: function (e) {
  59. const id = e.currentTarget.dataset.id;
  60. this.setData({
  61. showUnbindModal: true,
  62. unbindId: id
  63. });
  64. },
  65. // 取消解绑
  66. cancelUnbind: function () {
  67. this.setData({
  68. showUnbindModal: false,
  69. unbindId: null
  70. });
  71. },
  72. // 确认解绑
  73. confirmUnbind: function () {
  74. const id = this.data.unbindId;
  75. this.accountUnbind(id);
  76. },
  77. // 解绑方法
  78. accountUnbind:function (id) {
  79. debugger;
  80. const unbindInfo = this.data.huHaoList.filter(item => {
  81. return item.id === id;
  82. })[0];
  83. const otherInfo=this.data.huHaoList.filter(item => {
  84. return item.id!= id;
  85. });
  86. const _this = this;
  87. wx.showLoading({
  88. title: '获取中...',
  89. mask: true,
  90. });
  91. wx.request({
  92. url: app.globalData.interfaceUrls.accountUnbind,
  93. method: 'POST',
  94. data: {
  95. account: unbindInfo.usernumber,
  96. deKey: unbindInfo.dsKey,
  97. refresh: false,
  98. otherDsKeys: []
  99. },
  100. header: {
  101. 'content-type': 'application/json', // 默认值
  102. 'token': app.globalData.userWxInfo.token,
  103. },
  104. success(res) {
  105. wx.hideLoading();
  106. let apiReturnData = res.data;
  107. if(apiReturnData.code=='200'){
  108. wx.showToast({
  109. title: '解绑成功',
  110. icon: 'success',
  111. duration: 2000
  112. });
  113. _this.setData({
  114. huHaoList: otherInfo,
  115. showUnbindModal: false,
  116. currentHuHao: null
  117. });
  118. if(otherInfo.length==0){
  119. app.globalData.currentAccountInfo=[];
  120. app.globalData.userWxInfo.currentDsKey="";
  121. wx.redirectTo({
  122. url: '/pages/FirstBangDing/FirstBangDing',
  123. })
  124. }else{
  125. app.globalData.currentAccountInfo=otherInfo[0];
  126. app.globalData.userWxInfo.currentDsKey=otherInfo[0].deKey;
  127. }
  128. }else{
  129. wx.showToast({
  130. title: apiReturnData.msg,
  131. icon: 'error',
  132. duration: 2000
  133. })
  134. wx.hideLoading();
  135. }
  136. },
  137. fail(error) {
  138. wx.hideLoading();
  139. utils.simleInfo('户号解绑失败,请稍后再试');
  140. }
  141. });
  142. },
  143. // 跳转到绑定新户号页面
  144. goToBindNewHuHao: function () {
  145. wx.navigateTo({
  146. url: '/pages/huhaobangding/huhaobangding'
  147. });
  148. },
  149. /**
  150. * 返回上一页
  151. */
  152. goBack: function () {
  153. wx.navigateBack();
  154. },
  155. switchHuHao(e) {
  156. debugger;
  157. const huHaoInfo = e.currentTarget.dataset.item;
  158. // 更新全局数据
  159. app.globalData.userWxInfo = {
  160. ...app.globalData.userWxInfo,
  161. username: huHaoInfo.username,
  162. usernumber: huHaoInfo.usernumber,
  163. address: huHaoInfo.address,
  164. groupName: huHaoInfo.groupName,
  165. currentDsKey: huHaoInfo.dsKey // 更新 currentDsKey
  166. };
  167. // 更新当前账户信息
  168. app.globalData.currentAccountInfo = huHaoInfo;
  169. // 将新选择的户号信息保存到本地存储
  170. wx.setStorageSync('currentHuHao', huHaoInfo);
  171. wx.showToast({
  172. title: '户号切换成功',
  173. icon: 'success',
  174. duration: 1500
  175. });
  176. // 设置一个标记表示需要刷新首页
  177. wx.setStorageSync('needRefreshHomepage', true);
  178. // 延迟跳转到首页
  179. setTimeout(() => {
  180. wx.switchTab({
  181. url: '/pages/homepage/homepage'
  182. });
  183. }, 1500);
  184. },
  185. // 设置默认户号
  186. setDefaultHuHao: function(e) {
  187. const id = e.currentTarget.dataset.id;
  188. wx.showModal({
  189. title: '提示',
  190. content: '是否将此户号设为默认户号?',
  191. success: (res) => {
  192. debugger
  193. if (res.confirm) {
  194. this.setDefaultAccount(id);
  195. }
  196. }
  197. });
  198. },
  199. // 调用设置默认户号接口
  200. setDefaultAccount: function(id) {
  201. wx.showLoading({
  202. title: '设置中...',
  203. });
  204. const unbindInfo = this.data.huHaoList.filter(item => {
  205. return item.id === id;
  206. })[0];
  207. wx.request({
  208. url: app.globalData.interfaceUrls.setDefaultAccount,
  209. method: 'POST',
  210. data: {
  211. account: unbindInfo.usernumber,
  212. deKey: unbindInfo.dsKey,
  213. refresh: false,
  214. otherDsKeys: []
  215. },
  216. header: {
  217. 'content-type': 'application/json', // 默认值
  218. 'token': app.globalData.userWxInfo.token,
  219. },
  220. success: (res) => {
  221. debugger
  222. if (res.data.code === "200") {
  223. wx.showToast({
  224. title: '切换成功',
  225. icon: 'success'
  226. });
  227. this.getAccountList();
  228. } else {
  229. wx.showToast({
  230. title: res.data.msg || '设置失败',
  231. icon: 'none'
  232. });
  233. }
  234. },
  235. fail: () => {
  236. wx.showToast({
  237. title: '网络异常',
  238. icon: 'none'
  239. });
  240. },
  241. complete: () => {
  242. wx.hideLoading();
  243. }
  244. });
  245. },
  246. })