huhaoguanli.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. debugger
  143. // 更新全局数据
  144. app.globalData.userWxInfo = {
  145. ...app.globalData.userWxInfo,
  146. username: huHaoInfo.username,
  147. usernumber: huHaoInfo.usernumber,
  148. address: huHaoInfo.address,
  149. groupName: huHaoInfo.groupName,
  150. currentDsKey: huHaoInfo.dsKey // 更新 currentDsKey
  151. };
  152. // 更新当前账户信息
  153. app.globalData.currentAccountInfo = huHaoInfo;
  154. // 将新选择的户号信息保存到本地存储
  155. wx.setStorageSync('currentHuHao', huHaoInfo);
  156. // 设置标记表示已在用户管理中切换过账户
  157. wx.setStorageSync('hasUserSwitchedAccount', true);
  158. wx.showToast({
  159. title: '户号切换成功',
  160. icon: 'success',
  161. duration: 1500
  162. });
  163. // 设置一个标记表示需要刷新首页
  164. wx.setStorageSync('needRefreshHomepage', true);
  165. // 延迟跳转到首页
  166. setTimeout(() => {
  167. wx.switchTab({
  168. url: '/pages/homepage/homepage'
  169. });
  170. }, 1500);
  171. },
  172. // 设置默认户号
  173. setDefaultHuHao: function (e) {
  174. const id = e.currentTarget.dataset.id;
  175. this.setData({
  176. showDefaultModal: true,
  177. defaultId: id
  178. });
  179. },
  180. // 取消设置默认户号
  181. cancelDefault: function () {
  182. this.setData({
  183. showDefaultModal: false,
  184. defaultId: null
  185. });
  186. },
  187. // 确认设置默认户号
  188. confirmDefault: function () {
  189. const id = this.data.defaultId;
  190. this.setDefaultAccount(id);
  191. this.setData({
  192. showDefaultModal: false
  193. });
  194. },
  195. // 调用设置默认户号接口
  196. setDefaultAccount: function (id) {
  197. wx.showLoading({
  198. title: '设置中...',
  199. });
  200. const unbindInfo = this.data.huHaoList.filter(item => {
  201. return item.id === id;
  202. })[0];
  203. wx.request({
  204. url: app.globalData.interfaceUrls.setDefaultAccount,
  205. method: 'POST',
  206. data: {
  207. account: unbindInfo.usernumber,
  208. deKey: unbindInfo.dsKey,
  209. refresh: false,
  210. otherDsKeys: []
  211. },
  212. header: {
  213. 'content-type': 'application/json', // 默认值
  214. 'token': app.globalData.userWxInfo.token,
  215. },
  216. success: (res) => {
  217. if (res.data.code === "200") {
  218. wx.showToast({
  219. title: '设置成功',
  220. icon: 'success'
  221. });
  222. // 延迟一小段时间后刷新列表,确保后台数据已更新
  223. setTimeout(() => {
  224. this.getAccountList();
  225. }, 300);
  226. } else {
  227. wx.showToast({
  228. title: res.data.msg || '设置失败',
  229. icon: 'none'
  230. });
  231. }
  232. },
  233. fail: () => {
  234. wx.showToast({
  235. title: '网络异常',
  236. icon: 'none'
  237. });
  238. },
  239. complete: () => {
  240. wx.hideLoading();
  241. }
  242. });
  243. },
  244. // 过滤户号列表,只显示deleted=0的数据,并按默认户号排序
  245. getAccountList: function () {
  246. const _this = this;
  247. wx.showLoading({
  248. title: '获取中...',
  249. mask: true,
  250. });
  251. wx.request({
  252. url: app.globalData.interfaceUrls.accountList,
  253. method: 'GET',
  254. header: {
  255. 'content-type': 'application/json',
  256. 'token': app.globalData.userWxInfo.token,
  257. 'source': "wc",
  258. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  259. },
  260. success(res) {
  261. wx.hideLoading();
  262. let apiReturnData = res.data;
  263. // 从全局获取当前账户的信息currentAccountInfo
  264. const currentUsernumber = app.globalData.currentAccountInfo.usernumber;
  265. debugger
  266. // 对返回的数据进行排序,将默认户号(defaultAccount='0')排在前面
  267. const sortedData = apiReturnData.data.sort((a, b) => {
  268. // 先按defaultAccount排序('0'排在前面)
  269. if (a.defaultAccount !== b.defaultAccount) {
  270. return a.defaultAccount === '0' ? -1 : 1;
  271. }
  272. // 如果defaultAccount相同,则按是否是当前户号排序
  273. // if (a.usernumber === currentUsernumber) return -1;
  274. // if (b.usernumber === currentUsernumber) return 1;
  275. // 如果都不是当前户号,则按照户号(usernumber)排序,保证列表顺序稳定
  276. return a.usernumber.localeCompare(b.usernumber);
  277. });
  278. _this.setData({
  279. huHaoList: sortedData.map(item => {
  280. const isCurrent = item.usernumber === currentUsernumber;
  281. return {
  282. ...item,
  283. isCurrentUser: isCurrent
  284. };
  285. })
  286. });
  287. // 在获取列表后也检查是否为空
  288. if (_this.data.huHaoList.length === 0) {
  289. wx.navigateTo({
  290. url: '/pages/FirstBangDing/FirstBangDing'
  291. });
  292. }
  293. },
  294. fail(error) {
  295. wx.hideLoading();
  296. console.error('获取列表失败:', error);
  297. wx.showToast({
  298. title: '获取数据失败,请稍后再试',
  299. icon: 'none'
  300. });
  301. }
  302. });
  303. },
  304. // 阻止事件冒泡
  305. stopPropagation: function (e) {
  306. // 空函数,仅用于阻止事件冒泡
  307. },
  308. })