me.js 8.9 KB

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