me.js 7.6 KB

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