logoff.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // import { models, db, _ } from '../../utils/cloudbase.js'
  2. import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
  3. Page({
  4. data: {
  5. selectedReasons: [],
  6. feedback: '' // 新增 feedback 字段
  7. },
  8. onLoad(options) {
  9. // 页面加载时的初始化操作
  10. },
  11. handleCheckboxChange(e) {
  12. console.log('checkbox change:', e.detail.value); // e.detail.value 是一个数组
  13. this.setData({
  14. selectedReasons: e.detail.value
  15. });
  16. },
  17. handleFeedbackInput(e) {
  18. this.setData({
  19. feedback: e.detail.value
  20. });
  21. },
  22. async confirmLogout() {
  23. const { selectedReasons, feedback } = this.data;
  24. if (selectedReasons.length === 0) {
  25. wx.showToast({
  26. title: '请选择注销原因',
  27. icon: 'none'
  28. });
  29. return;
  30. }
  31. if (!feedback.trim()) {
  32. wx.showToast({
  33. title: '请输入意见或建议',
  34. icon: 'none'
  35. });
  36. return;
  37. }
  38. // 弹出确认框
  39. const that = this; // 保证作用域正确
  40. wx.showModal({
  41. title: '提示',
  42. content: '确定要注销吗?',
  43. success: async function (res) {
  44. if (res.confirm) {
  45. // 获取本地用户信息
  46. const userInfo = wx.getStorageSync('userInfo');
  47. if (!userInfo || !userInfo._id) {
  48. wx.showToast({
  49. title: '用户信息异常',
  50. icon: 'none'
  51. });
  52. return;
  53. }
  54. const reasonStr = that.data.selectedReasons.join(',');
  55. const models = await getModels()
  56. try {
  57. const { data } = await models.wx_user.update({
  58. data: {
  59. delete: 1, // 逻辑删除
  60. log_off: reasonStr,
  61. },
  62. filter: {
  63. where: {
  64. _id: {
  65. $eq: userInfo._id,
  66. }
  67. }
  68. },
  69. envType: "prod"
  70. });
  71. if (data.count > 0) {
  72. // 注销成功,清除本地缓存
  73. wx.removeStorageSync('userInfo');
  74. wx.showToast({
  75. title: '注销成功',
  76. icon: 'success',
  77. duration: 2000
  78. });
  79. // 可选:跳转到登录页
  80. setTimeout(() => {
  81. wx.redirectTo({
  82. url: '/pages/logins/logins'
  83. });
  84. }, 2000);
  85. } else {
  86. wx.showToast({
  87. title: '注销失败',
  88. icon: 'none'
  89. });
  90. }
  91. } catch (err) {
  92. console.error('注销失败:', err);
  93. wx.showToast({
  94. title: '服务器错误',
  95. icon: 'none'
  96. });
  97. }
  98. } else {
  99. console.log('用户取消注销');
  100. }
  101. }
  102. });
  103. }
  104. });