huhaoguanli.js 7.3 KB

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