huhaoguanli.js 9.3 KB

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