huhaoguanli.js 8.5 KB

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