me.js 9.1 KB

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