index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. // pages/user/index.js
  2. const {
  3. serverName
  4. } = require('../../utils/services');
  5. const {
  6. newRequestFns,
  7. newServerName
  8. } = require('../../utils/newServices.js');
  9. const {
  10. Toast
  11. } = require('../../utils/util.js');
  12. // 添加默认头像
  13. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  14. var app = getApp();
  15. Page({
  16. /**
  17. * 页面的初始数据
  18. */
  19. data: {
  20. testImg: '../../imgs/testImg/loginBg.png',
  21. avatar: '../../imgs/testImg/userIcon.png',
  22. // 新增的用户信息相关数据
  23. userInfo: {
  24. avatarUrl: defaultAvatarUrl,
  25. nickName: '',
  26. },
  27. hasUserInfo: false,
  28. canIUseGetUserProfile: wx.canIUse('getUserProfile'),
  29. canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  30. likeImg: '../../imgs/icon/like.png',
  31. commentImg: '../../imgs/icon/comment.png',
  32. sawImg: '../../imgs/icon/saw.png',
  33. tabItem: [{
  34. name: '待付款',
  35. tag: "0",
  36. id: 1
  37. }, {
  38. name: '待使用',
  39. tag: "-1",
  40. id: 2
  41. }, {
  42. name: '待评论',
  43. tag: "-1",
  44. id: 3
  45. }],
  46. tabList: [{
  47. name: '收藏',
  48. img: "../../imgs/icon/like.png",
  49. id: 0
  50. }, {
  51. name: '评论',
  52. img: "../../imgs/icon/comment.png",
  53. id: 1
  54. }, {
  55. name: '足迹',
  56. img: "../../imgs/icon/saw.png",
  57. id: 2
  58. }],
  59. active: false,
  60. isLogin: ''
  61. },
  62. /**
  63. * 生命周期函数--监听页面加载
  64. */
  65. onLoad: function(options) {
  66. // 加载时初始化用户信息
  67. if(wx.getStorageSync('userInfo')){
  68. this.setData({
  69. userInfo: wx.getStorageSync('userInfo'),
  70. hasUserInfo: true
  71. })
  72. }
  73. // this.isLongPolling()
  74. },
  75. isLongPolling: function() {
  76. let loginSessionKey = wx.getStorageSync("token");
  77. let requestTask = wx.request({
  78. url: newServerName + '/wx/user/longPolling',
  79. data: {
  80. loginSessionKey
  81. },
  82. header: {
  83. 'content-type': 'application/x-www-form-urlencoded'
  84. },
  85. method: "post",
  86. success: (res) => {
  87. if (res.data.code == 0) {
  88. if (res.data.data) {
  89. let tabList = this.data.tabList;
  90. tabList[1].img = "../../imgs/icon/comment_active.png";
  91. this.setData({
  92. tabList
  93. })
  94. } else {
  95. let tabList = this.data.tabList;
  96. tabList[1].img = "../../imgs/icon/comment.png";
  97. this.setData({
  98. tabList
  99. })
  100. }
  101. } else {
  102. return
  103. }
  104. this.isLongPolling()
  105. }
  106. })
  107. },
  108. tapByIdx: function(e) {
  109. let {
  110. idx
  111. } = e.currentTarget.dataset;
  112. this._isLogin();
  113. if (this.data.isLogin) {
  114. switch (idx) {
  115. case 0:
  116. wx.navigateTo({
  117. url: './my_follow/index',
  118. success: function(res) {},
  119. fail: function(res) {},
  120. complete: function(res) {},
  121. })
  122. break;
  123. case 1:
  124. let tabList = this.data.tabList;
  125. tabList[1].img = "../../imgs/icon/comment.png";
  126. this.setData({
  127. tabList
  128. })
  129. wx.navigateTo({
  130. url: './my_comment/index',
  131. success: function(res) {},
  132. fail: function(res) {},
  133. complete: function(res) {},
  134. })
  135. break;
  136. case 2:
  137. wx.navigateTo({
  138. url: './my_saw/index',
  139. success: function(res) {},
  140. fail: function(res) {},
  141. complete: function(res) {},
  142. })
  143. break;
  144. }
  145. } else {
  146. Toast.showToast('tip', '请登录后查看', () => {
  147. });
  148. }
  149. },
  150. bindGetUserInfo: function(e) {
  151. console.log(e.detail.userInfo)
  152. },
  153. _myLikes: function() {
  154. this._isLogin();
  155. if (this.data.isLogin) {
  156. wx.navigateTo({
  157. url: './my_follow/index',
  158. success: function(res) {},
  159. fail: function(res) {},
  160. complete: function(res) {},
  161. })
  162. } else {
  163. Toast.showToast('tip', '请登录后查看', () => {
  164. });
  165. }
  166. },
  167. _myOrder: function() {
  168. this._isLogin();
  169. if (this.data.isLogin) {
  170. wx.navigateTo({
  171. url: './my_order/index',
  172. success: function(res) {},
  173. fail: function(res) {},
  174. complete: function(res) {},
  175. })
  176. } else {
  177. Toast.showToast('tip', '请登录后查看', () => {
  178. });
  179. }
  180. },
  181. _isLogin: function() {
  182. if (wx.getStorageSync('token')) {
  183. let loginSessionKey = wx.getStorageSync("token");
  184. if (loginSessionKey != " ") {
  185. wx.checkSession({
  186. success: res => {
  187. wx.request({
  188. url: newServerName + '/wx/user/checkLoginSessionKey',
  189. data: {
  190. loginSessionKey
  191. },
  192. header: {
  193. 'content-type': 'application/x-www-form-urlencoded'
  194. },
  195. method: "post",
  196. success: (res) => {
  197. if (res.data.code == 0) {
  198. this.setData({
  199. isLogin: true
  200. })
  201. app.globalData.isLogin = true;
  202. // app.globalData.cookieIDs = res.data.data.ids.split(',');
  203. } else {
  204. this.setData({
  205. isLogin: false,
  206. avatarUrl: ""
  207. })
  208. app.globalData.isLogin = false;
  209. }
  210. }
  211. })
  212. // request['checkLoginSessionKey']({
  213. // loginSessionKey
  214. // }, "post", res => {
  215. // if (res.data.code > -1) {
  216. // this.setData({
  217. // isLogin: true
  218. // })
  219. // } else {
  220. // this.setData({
  221. // isLogin: false
  222. // })
  223. // }
  224. // })
  225. },
  226. fail: res => {
  227. this.setData({
  228. isLogin: false,
  229. avatarUrl: ""
  230. })
  231. app.globalData.isLogin = false;
  232. }
  233. })
  234. } else {
  235. this.setData({
  236. isLogin: false,
  237. avatarUrl: ""
  238. })
  239. app.globalData.isLogin = false;
  240. }
  241. } else {
  242. this.setData({
  243. isLogin: false
  244. })
  245. app.globalData.isLogin = false;
  246. }
  247. // console.log("isLogin",this.data.isLogin);
  248. },
  249. getUserProfile:function(e){
  250. // 如果已经登录,直接返回
  251. if (this.data.isLogin) {
  252. return;
  253. }
  254. Toast.showToast2('loading');
  255. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  256. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  257. wx.getUserProfile({
  258. desc: '获取您的身份信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  259. success: (res) => {
  260. console.log(123456,res);
  261. let {
  262. encryptedData,
  263. iv,
  264. userInfo
  265. } = res;
  266. // 更新用户信息到新的数据结构
  267. app.globalData.userInfo = userInfo;
  268. wx.setStorageSync("userInfo", userInfo)
  269. this.setData({
  270. userInfo: userInfo,
  271. hasUserInfo: true,
  272. avatarUrl: userInfo.avatarUrl,
  273. avatarName: userInfo.nickName
  274. })
  275. wx.login({
  276. success: res => {
  277. let {
  278. code
  279. } = res;
  280. console.log(789,res);
  281. if (code) {
  282. wx.request({
  283. url: newServerName + '/wx/user/getLoginSessionKey',
  284. data: {
  285. encryptedData,
  286. iv,
  287. code,
  288. },
  289. header: {
  290. 'content-type': 'application/x-www-form-urlencoded'
  291. },
  292. method: 'post',
  293. // dataType: 'json',
  294. // responseType: 'text',
  295. success: res => {
  296. if (res.data.code > -1) {
  297. let {
  298. loginSessionKey
  299. } = res.data.data;
  300. wx.setStorageSync('token', loginSessionKey)
  301. wx.showToast({
  302. title: '登录成功',
  303. icon: 'success'
  304. })
  305. }
  306. else{
  307. Toast.showToast('warn', '登录失败,请重试');
  308. }
  309. },
  310. fail: res => {
  311. Toast.showToast('warn', '登录失败,请重试');
  312. },
  313. complete: res=> {
  314. this._isLogin();
  315. Toast.hideLoading();
  316. },
  317. })
  318. }
  319. }
  320. })
  321. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  322. // 所以此处加入 callback 以防止这种情况
  323. if (this.userInfoReadyCallback) {
  324. this.userInfoReadyCallback(res)
  325. }
  326. },
  327. fail: (res) => {
  328. // 用户取消授权时的处理逻辑
  329. console.log('用户取消授权:', res);
  330. // 仍然执行微信登录,但使用默认头像和昵称
  331. wx.login({
  332. success: loginRes => {
  333. let { code } = loginRes;
  334. console.log('静默登录 code:', code);
  335. if (code) {
  336. wx.request({
  337. url: newServerName + '/wx/user/getLoginSessionKey',
  338. data: {
  339. code,
  340. // 不传加密数据,只做基础登录
  341. },
  342. header: {
  343. 'content-type': 'application/x-www-form-urlencoded'
  344. },
  345. method: 'post',
  346. success: res => {
  347. if (res.data.code > -1) {
  348. let { loginSessionKey } = res.data.data;
  349. wx.setStorageSync('token', loginSessionKey)
  350. // 设置默认用户信息
  351. const defaultUserInfo = {
  352. avatarUrl: defaultAvatarUrl,
  353. nickName: '微信用户'
  354. };
  355. app.globalData.userInfo = defaultUserInfo;
  356. wx.setStorageSync("userInfo", defaultUserInfo);
  357. this.setData({
  358. userInfo: defaultUserInfo,
  359. hasUserInfo: true,
  360. avatarUrl: defaultAvatarUrl,
  361. avatarName: '微信用户'
  362. });
  363. wx.showToast({
  364. title: '登录成功',
  365. icon: 'success'
  366. });
  367. } else {
  368. Toast.showToast('warn', '登录失败,请重试');
  369. }
  370. },
  371. fail: res => {
  372. Toast.showToast('warn', '登录失败,请重试');
  373. },
  374. complete: res=> {
  375. this._isLogin();
  376. Toast.hideLoading();
  377. },
  378. })
  379. }
  380. },
  381. fail: () => {
  382. Toast.showToast('warn', '登录失败,请重试');
  383. Toast.hideLoading();
  384. }
  385. });
  386. },
  387. complete:(res)=>{
  388. Toast.hideLoading();
  389. }
  390. })
  391. },
  392. _logout: function() {
  393. if (this.data.isLogin) {
  394. wx.showModal({
  395. title: '提示',
  396. content: '确定要退出登录吗?',
  397. success: (res) => {
  398. if (res.confirm) {
  399. Toast.showToast2('loading');
  400. let loginSessionKey = wx.getStorageSync('token');
  401. newRequestFns["logout"]({
  402. loginSessionKey
  403. }, "post", res => {
  404. if (res.data.code > -1) {
  405. // 清除所有用户相关的存储信息
  406. wx.removeStorageSync('userInfo');
  407. wx.removeStorageSync('token');
  408. console.log(wx.getStorageSync('token'))
  409. // 重置页面状态到未登录
  410. this.setData({
  411. isLogin: false,
  412. avatarUrl: this.data.avatar,
  413. avatarName: '登录',
  414. userInfo: {
  415. avatarUrl: defaultAvatarUrl,
  416. nickName: '',
  417. },
  418. hasUserInfo: false
  419. })
  420. app.globalData.isLogin = false;
  421. app.globalData.userInfo = null;
  422. wx.showToast({
  423. title: '已退出登录',
  424. icon: 'success'
  425. });
  426. } else {
  427. Toast.showToast('warn', '退出失败,请重试');
  428. }
  429. }, err => {
  430. Toast.showToast('warn', '网络错误,请重试');
  431. }, complete => {
  432. Toast.hideLoading();
  433. })
  434. }
  435. }
  436. })
  437. } else {
  438. wx.showToast({
  439. title: '您还未登录',
  440. icon: 'none'
  441. });
  442. }
  443. },
  444. tapToOrderByidx: function(e) {
  445. let {
  446. status,
  447. id
  448. } = e.currentTarget.dataset;
  449. this._isLogin();
  450. if (this.data.isLogin) {
  451. wx.navigateTo({
  452. url: `./my_order/index?status=${status}&&id=${id}`,
  453. success: function(res) {},
  454. fail: function(res) {},
  455. complete: function(res) {},
  456. })
  457. } else {
  458. Toast.showToast('tip', '请登录后查看', () => {
  459. });
  460. }
  461. // url="./my_order/index?status={{item.tag}}&&id={{item.id}}"
  462. },
  463. clickCell: function() {
  464. wx.navigateTo({
  465. url: './my_contact/index',
  466. })
  467. },
  468. /**
  469. * 生命周期函数--监听页面初次渲染完成
  470. */
  471. onReady: function() {
  472. },
  473. /**
  474. * 生命周期函数--监听页面显示
  475. */
  476. onShow: function() {
  477. let {
  478. avatar = ""
  479. } = this.data;
  480. console.log(wx.getStorageSync('userInfo'))
  481. // 获取存储的用户信息
  482. const storedUserInfo = wx.getStorageSync('userInfo');
  483. let avatarUrl = storedUserInfo ? storedUserInfo.avatarUrl : "";
  484. let avatarName = storedUserInfo ? storedUserInfo.nickName : "";
  485. // 更新用户信息到新的数据结构
  486. if (storedUserInfo) {
  487. this.setData({
  488. userInfo: storedUserInfo,
  489. hasUserInfo: true,
  490. avatarUrl: avatarUrl || avatar,
  491. avatarName: avatarName || '已登录'
  492. })
  493. } else {
  494. this.setData({
  495. avatarUrl: avatar,
  496. avatarName: '登录'
  497. })
  498. }
  499. this._isLogin()
  500. },
  501. /**
  502. * 生命周期函数--监听页面隐藏
  503. */
  504. onHide: function() {
  505. },
  506. /**
  507. * 生命周期函数--监听页面卸载
  508. */
  509. onUnload: function() {
  510. },
  511. /**
  512. * 页面相关事件处理函数--监听用户下拉动作
  513. */
  514. onPullDownRefresh: function() {
  515. },
  516. /**
  517. * 页面上拉触底事件的处理函数
  518. */
  519. onReachBottom: function() {
  520. },
  521. /**
  522. * 用户点击右上角分享
  523. */
  524. onShareAppMessage: function() {
  525. },
  526. // 新增:选择头像功能
  527. onChooseAvatar(e) {
  528. const { avatarUrl } = e.detail
  529. const { nickName } = this.data.userInfo
  530. // 更新页面数据
  531. this.setData({
  532. "userInfo.avatarUrl": avatarUrl,
  533. hasUserInfo: true,
  534. avatarUrl: avatarUrl
  535. })
  536. // 更新本地存储和全局数据
  537. const updatedUserInfo = {
  538. ...this.data.userInfo,
  539. avatarUrl: avatarUrl,
  540. realUserInfo: true
  541. }
  542. wx.setStorageSync('userInfo', updatedUserInfo)
  543. if (app && app.globalData) {
  544. app.globalData.userInfo = updatedUserInfo
  545. }
  546. console.log('头像已更新:', avatarUrl)
  547. wx.showToast({
  548. title: '头像已更新',
  549. icon: 'success'
  550. })
  551. },
  552. // 新增:输入昵称功能
  553. onInputChange(e) {
  554. const nickName = e.detail.value
  555. const { avatarUrl } = this.data.userInfo
  556. // 更新页面数据
  557. this.setData({
  558. "userInfo.nickName": nickName || '微信用户',
  559. hasUserInfo: true,
  560. avatarName: nickName
  561. })
  562. // 更新本地存储和全局数据
  563. const updatedUserInfo = {
  564. ...this.data.userInfo,
  565. nickName: nickName,
  566. realUserInfo: true
  567. }
  568. wx.setStorageSync('userInfo', updatedUserInfo)
  569. if (app && app.globalData) {
  570. app.globalData.userInfo = updatedUserInfo
  571. }
  572. console.log('昵称已更新:', nickName)
  573. },
  574. // 新增:处理已登录状态下点击头像的逻辑
  575. handleAvatarClick() {
  576. if (this.data.canIUseNicknameComp) {
  577. wx.showToast({
  578. title: '请点击头像更换',
  579. icon: 'none'
  580. })
  581. } else {
  582. // 降级处理,重新获取用户信息
  583. this.getUserProfile()
  584. }
  585. },
  586. })