huhaoguanli.js 8.5 KB

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