me.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // pages/me/me.js
  2. import { models, db, _ } from '../../utils/cloudbase.js'
  3. Page({
  4. data: {
  5. userInfo: {},
  6. displayPhone: '',
  7. xxdata: {},
  8. collectFiles: [],
  9. historyItems: [],
  10. xiugaiimg: '',
  11. xiangjiimg: '',
  12. xiazi: '',
  13. souchangs: '',
  14. },
  15. onShow() {
  16. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  17. this.getTabBar().setSelected(3); // 比如首页就是 0
  18. }
  19. // 每次进入页面都会触发
  20. this.getUserInfo();
  21. this.getcollect();
  22. this.getpurchasehistory();
  23. },
  24. onLoad(options) {
  25. const fileIDs = [
  26. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xiugai.png',
  27. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xiangji.png',
  28. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xiazi.png',
  29. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/soucangs.png'
  30. ];
  31. // 并发下载多个 fileID
  32. Promise.all(
  33. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  34. ).then(results => {
  35. // 每个 result 对应一个下载结果
  36. const tempFilePaths = results.map(r => r.tempFilePath);
  37. console.log('全部下载成功:', tempFilePaths);
  38. this.setData({
  39. xiugaiimg: tempFilePaths[0],
  40. xiangjiimg: tempFilePaths[1],
  41. xiazi: tempFilePaths[2],
  42. souchangs: tempFilePaths[3],
  43. });
  44. }).catch(err => {
  45. console.error('有文件下载失败:', err);
  46. });
  47. },
  48. // 手机号加密
  49. hidePhone(phone) {
  50. if (!phone || phone.length !== 11) return phone || '未绑定';
  51. return phone.substr(0, 3) + '****' + phone.substr(7, 4);
  52. },
  53. // 用户信息
  54. async getUserInfo() {
  55. // 获取用户信息
  56. const userInfo = wx.getStorageSync('userInfo');
  57. this.setData({ userInfo }, () => {
  58. const phone = this.data.userInfo.phone;
  59. this.setData({
  60. displayPhone: this.hidePhone(phone)
  61. });
  62. });
  63. try {
  64. const { data } = await models.wx_school.get({
  65. filter: {
  66. where: {
  67. school_id: userInfo.school_id
  68. }
  69. },
  70. envType: "prod"
  71. });
  72. this.setData({
  73. xxdata: data
  74. });
  75. } catch (err) {
  76. console.error('获取学校数据失败:', err);
  77. }
  78. },
  79. // 下载历史
  80. async getpurchasehistory() {
  81. const { data } = await models.download_history.list({
  82. filter: {
  83. where: {
  84. user_id: this.data.userInfo._id
  85. }
  86. },
  87. pageSize: 2, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
  88. pageNumber: 1, // 第几页
  89. getCount: true, // 开启用来获取总数
  90. envType: "prod",
  91. });
  92. // 返回查询到的数据列表 records 和 总数 total
  93. console.log(data);
  94. const collectList = data.records || [];
  95. if (collectList.length === 0) {
  96. console.log('没有收藏记录');
  97. this.setData({ historyItems: [] });
  98. return;
  99. }
  100. // 第二步:提取 file_manage_id 列表
  101. const fileManagerIds = collectList.map(item => item.file_manage_id);
  102. // 第三步:逐个查询 file_manage 表详情
  103. const fileDetailPromises = fileManagerIds.map(id => {
  104. return models.file_manage.list({
  105. filter: {
  106. where: {
  107. _id: id
  108. }
  109. },
  110. envType: "prod"
  111. })
  112. .then(res => {
  113. const record = res.data?.records?.[0];
  114. if (!record) {
  115. console.warn(`未找到 _id 为 ${id} 的文件`);
  116. }
  117. return record || null;
  118. })
  119. .catch(err => {
  120. console.error(`获取文件 ${id} 失败`, err);
  121. return null;
  122. });
  123. });
  124. console.log(await Promise.all(fileDetailPromises), 'fileDetailPromises');
  125. // 等待所有查询完成
  126. const fileDetails = await Promise.all(fileDetailPromises);
  127. // 第四步:更新页面数据(过滤掉失败项)
  128. this.setData({
  129. historyItems: fileDetails.filter(Boolean)
  130. });
  131. },
  132. // 收藏列表
  133. async getcollect() {
  134. const { data } = await models.wx_collect.list({
  135. filter: {
  136. where: {
  137. wx_user_id: this.data.userInfo._id
  138. }
  139. },
  140. pageSize: 2, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
  141. pageNumber: 1, // 第几页
  142. getCount: true, // 开启用来获取总数
  143. envType: "prod",
  144. });
  145. // 返回查询到的数据列表 records 和 总数 total
  146. console.log(data);
  147. const collectList = data.records || [];
  148. if (collectList.length === 0) {
  149. console.log('没有收藏记录');
  150. this.setData({ collectFiles: [] });
  151. return;
  152. }
  153. // 第二步:提取 file_manager_id 列表
  154. const fileManagerIds = collectList.map(item => item.file_manager_id);
  155. // 第三步:批量查询 file_manage 表中对应的文件数据
  156. const fileRes = await models.file_manage.list({
  157. filter: {
  158. where: {
  159. _id: { $in: fileManagerIds } // 关键点:使用 $in 批量查询
  160. }
  161. },
  162. envType: "prod",
  163. });
  164. // 第四步:更新页面数据
  165. this.setData({
  166. collectFiles: fileRes.data.records || []
  167. });
  168. console.log('收藏的文件数据:', fileRes.data.records);
  169. },
  170. // 注销跳转
  171. handleLogout() {
  172. wx.navigateTo({
  173. url: `/subpackages/logoff/logoff`
  174. });
  175. },
  176. // 修改用户名跳转
  177. goToGoodsLists() {
  178. wx.navigateTo({
  179. url: '/subpackages/changename/changename?data=' + encodeURIComponent(JSON.stringify(this.data.userInfo))
  180. });
  181. },
  182. // 点击相机修改用户头像
  183. chooseAvatar() {
  184. wx.showActionSheet({
  185. itemList: ['拍照', '从相册选择'],
  186. success: (res) => {
  187. if (res.tapIndex === 0) {
  188. this.chooseImage('camera');
  189. } else if (res.tapIndex === 1) {
  190. this.chooseImage('album');
  191. }
  192. }
  193. });
  194. },
  195. chooseImage(sourceType) {
  196. wx.chooseMedia({
  197. count: 1,
  198. mediaType: ['image'],
  199. sourceType: [sourceType], // 'camera' or 'album'
  200. success: (res) => {
  201. const tempFilePath = res.tempFiles[0].tempFilePath;
  202. // 可选择上传到云存储,也可以直接更新 UI
  203. this.uploadAvatar(tempFilePath);
  204. },
  205. fail: (err) => {
  206. console.error('选择图片失败', err);
  207. }
  208. });
  209. },
  210. async uploadAvatar(filePath) {
  211. const cloudPath = 'avatar/' + Date.now() + '-' + Math.floor(Math.random() * 1000) + '.png';
  212. const uploadRes = await wx.cloud.uploadFile({
  213. cloudPath,
  214. filePath,
  215. });
  216. // 上传成功后更新用户头像
  217. const avatarUrl = uploadRes.fileID;
  218. console.log(avatarUrl, 'avatarUrl');
  219. // 更新头像显示
  220. this.setData({
  221. 'userInfo.img': avatarUrl
  222. });
  223. const cachedUserInfo = wx.getStorageSync('userInfo') || {};
  224. cachedUserInfo.img = avatarUrl;
  225. wx.setStorageSync('userInfo', cachedUserInfo);
  226. console.log('缓存更新成功:', cachedUserInfo);
  227. // TODO: 同步更新数据库中头像字段(可选)
  228. const { data } = await models.wx_teacher_user.update({
  229. data: {
  230. img: avatarUrl, // 头像
  231. },
  232. filter: {
  233. where: {
  234. _id: { $eq: this.data.userInfo._id }, // 推荐传入_id数据标识进行操作
  235. delete: { $eq: 1 },
  236. }
  237. },
  238. // envType: pre 体验环境, prod 正式环境
  239. envType: "prod",
  240. });
  241. if (data.count > 0) {
  242. wx.showToast({
  243. title: '头像修改成功',
  244. icon: 'success'
  245. });
  246. } else {
  247. wx.showToast({
  248. title: '头像修改失败',
  249. icon: 'none'
  250. });
  251. }
  252. }
  253. });