index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. // pages/user/index.js
  2. const {
  3. request,
  4. serverName
  5. } = require('../../utils/services');
  6. const {
  7. Toast
  8. } = require('../../utils/util.js');
  9. var app = getApp();
  10. import {
  11. getWxUserInfo
  12. } from '../../utils/request'
  13. Page({
  14. /**
  15. * 页面的初始数据
  16. */
  17. data: {
  18. testImg: '../../imgs/testImg/loginBg.png',
  19. avatar: '../../imgs/testImg/userIcon.png',
  20. likeImg: '../../imgs/icon/like.png',
  21. commentImg: '../../imgs/icon/comment.png',
  22. sawImg: '../../imgs/icon/saw.png',
  23. tabItem: [{
  24. name: '待付款',
  25. tag: "0",
  26. id: 1
  27. }, {
  28. name: '待使用',
  29. tag: "-1",
  30. id: 2
  31. }, {
  32. name: '待评论',
  33. tag: "-1",
  34. id: 3
  35. }],
  36. tabList: [{
  37. name: '购买',
  38. img: "../../imgs/icon/saw.png",
  39. id: 3
  40. },
  41. {
  42. name: '课程',
  43. img: "../../imgs/icon/saw.png",
  44. id: 4
  45. },
  46. {
  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. ],
  60. active: false,
  61. isLogin: false
  62. },
  63. /**
  64. * 生命周期函数--监听页面加载
  65. */
  66. onLoad: function (options) {
  67. // this.isLongPolling()
  68. // this.newLogin();
  69. },
  70. isLongPolling: function () {
  71. let loginSessionKey = wx.getStorageSync("token");
  72. let requestTask = wx.request({
  73. url: serverName + '/wx/api/user/longPolling',
  74. data: {
  75. loginSessionKey
  76. },
  77. header: {
  78. 'content-type': 'application/x-www-form-urlencoded'
  79. },
  80. method: "post",
  81. success: (res) => {
  82. if (res.data.code == 0) {
  83. if (res.data.data) {
  84. let tabList = this.data.tabList;
  85. tabList[1].img = "../../imgs/icon/comment_active.png";
  86. this.setData({
  87. tabList
  88. })
  89. } else {
  90. let tabList = this.data.tabList;
  91. tabList[1].img = "../../imgs/icon/comment.png";
  92. this.setData({
  93. tabList
  94. })
  95. }
  96. } else {
  97. return
  98. }
  99. this.isLongPolling()
  100. }
  101. })
  102. },
  103. tapByIdx: function (e) {
  104. let {
  105. idx
  106. } = e.currentTarget.dataset;
  107. this._isLogin();
  108. if (this.data.isLogin) {
  109. switch (idx) {
  110. case 0:
  111. wx.navigateTo({
  112. url: './my_follow/index',
  113. success: function (res) {},
  114. fail: function (res) {},
  115. complete: function (res) {},
  116. })
  117. break;
  118. case 1:
  119. let tabList = this.data.tabList;
  120. tabList[1].img = "../../imgs/icon/comment.png";
  121. this.setData({
  122. tabList
  123. })
  124. wx.navigateTo({
  125. url: './my_comment/index',
  126. success: function (res) {},
  127. fail: function (res) {},
  128. complete: function (res) {},
  129. })
  130. break;
  131. case 2:
  132. wx.navigateTo({
  133. url: './my_saw/index',
  134. success: function (res) {},
  135. fail: function (res) {},
  136. complete: function (res) {},
  137. })
  138. break;
  139. case 3:
  140. console.log('userId', this.data.userInfo.id)
  141. wx.navigateTo({
  142. url: `/pages/user/my_course/my_course?userId=${this.data.userInfo.id}`
  143. })
  144. break;
  145. case 4:
  146. wx.navigateTo({
  147. url: `/pages/user/my_host_course/index?userId=${this.data.userInfo.id}`
  148. })
  149. break;
  150. }
  151. } else {
  152. Toast.showToast('tip', '请登录后查看', () => {
  153. });
  154. }
  155. },
  156. bindGetUserInfo: function (e) {
  157. console.log(e.detail.userInfo)
  158. },
  159. _myLikes: function () {
  160. this._isLogin();
  161. if (this.data.isLogin) {
  162. wx.navigateTo({
  163. url: './my_follow/index',
  164. success: function (res) {},
  165. fail: function (res) {},
  166. complete: function (res) {},
  167. })
  168. } else {
  169. Toast.showToast('tip', '请登录后查看', () => {
  170. });
  171. }
  172. },
  173. _myOrder: function () {
  174. this._isLogin();
  175. if (this.data.isLogin) {
  176. wx.navigateTo({
  177. url: './my_order/index',
  178. success: function (res) {},
  179. fail: function (res) {},
  180. complete: function (res) {},
  181. })
  182. } else {
  183. Toast.showToast('tip', '请登录后查看', () => {
  184. });
  185. }
  186. },
  187. _isLogin: function () {
  188. if (wx.getStorageSync('token')) {
  189. let loginSessionKey = wx.getStorageSync("token");
  190. if (loginSessionKey != " ") {
  191. wx.checkSession({
  192. success: res => {
  193. wx.request({
  194. url: serverName + '/wx/api/user/checkLoginSessionKey',
  195. data: {
  196. loginSessionKey
  197. },
  198. header: {
  199. 'content-type': 'application/x-www-form-urlencoded'
  200. },
  201. method: "post",
  202. success: (res) => {
  203. if (res.data.code == 0) {
  204. this.setData({
  205. isLogin: true
  206. })
  207. app.globalData.isLogin = true;
  208. // app.globalData.cookieIDs = res.data.data.ids.split(',');
  209. } else {
  210. this.setData({
  211. isLogin: false,
  212. avatarUrl: ""
  213. })
  214. app.globalData.isLogin = false;
  215. }
  216. }
  217. })
  218. // request['checkLoginSessionKey']({
  219. // loginSessionKey
  220. // }, "post", res => {
  221. // if (res.data.code > -1) {
  222. // this.setData({
  223. // isLogin: true
  224. // })
  225. // } else {
  226. // this.setData({
  227. // isLogin: false
  228. // })
  229. // }
  230. // })
  231. },
  232. fail: res => {
  233. this.setData({
  234. isLogin: false,
  235. avatarUrl: ""
  236. })
  237. app.globalData.isLogin = false;
  238. }
  239. })
  240. } else {
  241. this.setData({
  242. isLogin: false,
  243. avatarUrl: ""
  244. })
  245. app.globalData.isLogin = false;
  246. }
  247. } else {
  248. this.setData({
  249. isLogin: false
  250. })
  251. app.globalData.isLogin = false;
  252. }
  253. // console.log("isLogin",this.data.isLogin);
  254. },
  255. getUserProfile: function (e) {
  256. Toast.showToast2('loading');
  257. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  258. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  259. wx.getUserProfile({
  260. desc: '获取您的身份信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  261. success: (res) => {
  262. console.log("getUserProfile", res);
  263. let {
  264. encryptedData,
  265. iv,
  266. userInfo
  267. } = res;
  268. app.globalData.userInfo = userInfo;
  269. wx.setStorageSync("userInfo", userInfo)
  270. this.setData({
  271. avatarUrl: wx.getStorageSync('userInfo').avatarUrl
  272. })
  273. wx.login({
  274. success: res => {
  275. let {
  276. code
  277. } = res;
  278. console.log(789, res);
  279. debugger
  280. if (code) {
  281. wx.request({
  282. url: serverName + '/wx/api/user/getLoginSessionKey',
  283. data: {
  284. encryptedData,
  285. iv,
  286. code,
  287. },
  288. header: {
  289. 'content-type': 'application/x-www-form-urlencoded'
  290. },
  291. method: 'post',
  292. // dataType: 'json',
  293. // responseType: 'text',
  294. success: res => {
  295. if (res.data.code > -1) {
  296. let {
  297. loginSessionKey,
  298. sessionKey
  299. } = res.data.data;
  300. wx.setStorageSync('token', loginSessionKey)
  301. wx.setStorageSync('sessionKey', sessionKey)
  302. this.updateUserinfo();
  303. } else {
  304. Toast.showToast('warn', '登录失败,请重试');
  305. }
  306. },
  307. fail: res => {
  308. Toast.showToast('warn', '登录失败,请重试');
  309. },
  310. complete: res => {
  311. this._isLogin();
  312. Toast.hideLoading();
  313. },
  314. })
  315. // request["getLoginSessionKey"]({
  316. // encryptedData,
  317. // iv,
  318. // code,
  319. // loginSessionKey:''
  320. // }, "post", res => {
  321. // if (res.data.code > -1) {
  322. // let {
  323. // loginSessionKey
  324. // } = res.data.data;
  325. // wx.setStorageSync('token', loginSessionKey)
  326. // this.isLongPolling();
  327. // console.log(loginSessionKey)
  328. // }
  329. // }, err => {
  330. // }, complete => {
  331. // this._isLogin();
  332. // Toast.hideLoading();
  333. // })
  334. }
  335. }
  336. })
  337. // console.log(res.userInfo)
  338. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  339. // 所以此处加入 callback 以防止这种情况
  340. if (this.userInfoReadyCallback) {
  341. this.userInfoReadyCallback(res)
  342. }
  343. },
  344. complete: (res) => {
  345. Toast.hideLoading();
  346. }
  347. })
  348. },
  349. _logout: function () {
  350. if (this.data.isLogin) {
  351. Toast.showToast2('loading');
  352. let loginSessionKey = wx.getStorageSync('token');
  353. request["logout"]({
  354. loginSessionKey
  355. }, "post", res => {
  356. if (res.data.code > -1) {
  357. wx.setStorageSync('userInfo', null);
  358. wx.setStorageSync('token', null);
  359. wx.setStorageSync('sessionKey', null);
  360. console.log(wx.getStorageSync('token'))
  361. this.setData({
  362. isLogin: false,
  363. avatarUrl: "",
  364. })
  365. app.globalData.isLogin = false;
  366. }
  367. }, err => {
  368. }, complete => {
  369. Toast.hideLoading();
  370. })
  371. }
  372. },
  373. tapToOrderByidx: function (e) {
  374. let {
  375. status,
  376. id
  377. } = e.currentTarget.dataset;
  378. this._isLogin();
  379. if (this.data.isLogin) {
  380. wx.navigateTo({
  381. url: `./my_order/index?status=${status}&&id=${id}`
  382. })
  383. } else {
  384. Toast.showToast('tip', '请登录后查看', () => {
  385. });
  386. }
  387. // url="./my_order/index?status={{item.tag}}&&id={{item.id}}"
  388. },
  389. clickCell: function () {
  390. wx.navigateTo({
  391. url: './my_contact/index',
  392. })
  393. },
  394. toPersonal() {
  395. wx.navigateTo({
  396. url: '/pages/user/personal/personal',
  397. })
  398. },
  399. // newLogin() {
  400. // wx.login({
  401. // success: res => {
  402. // let {
  403. // code
  404. // } = res;
  405. // console.log(789, res);
  406. // if (code) {
  407. // wx.request({
  408. // url: serverName + '/wx/api/user/getLoginSessionKey',
  409. // data: {
  410. // code,
  411. // },
  412. // header: {
  413. // 'content-type': 'application/x-www-form-urlencoded'
  414. // },
  415. // method: 'post',
  416. // // dataType: 'json',
  417. // // responseType: 'text',
  418. // success: res => {
  419. // if (res.data.code > -1) {
  420. // let {
  421. // loginSessionKey
  422. // } = res.data.data;
  423. // wx.setStorageSync('token', loginSessionKey)
  424. // } else {
  425. // Toast.showToast('warn', '登录失败,请重试');
  426. // }
  427. // },
  428. // fail: res => {
  429. // Toast.showToast('warn', '登录失败,请重试');
  430. // },
  431. // complete: res => {
  432. // this._isLogin();
  433. // Toast.hideLoading();
  434. // },
  435. // })
  436. // }
  437. // }
  438. // })
  439. // },
  440. /**
  441. * 生命周期函数--监听页面初次渲染完成
  442. */
  443. onReady: function () {
  444. },
  445. /**
  446. * 生命周期函数--监听页面显示
  447. */
  448. onShow: async function () {
  449. let {
  450. avatar = ""
  451. } = this.data;
  452. const userInfo = wx.getStorageSync('userInfo')
  453. const sessionKey = wx.getStorageSync('sessionKey')
  454. let avatarUrl = userInfo ? userInfo.avatarUrl : "";
  455. let avatarName = userInfo ? userInfo.nickName : "";
  456. console.log('sessionKey', sessionKey)
  457. if (sessionKey) {
  458. this.updateUserinfo();
  459. }
  460. this.setData({
  461. avatarUrl: avatarUrl || avatar,
  462. avatarName: avatarName || '已登录'
  463. })
  464. this._isLogin()
  465. if (userInfo) {
  466. this.setData({
  467. userInfo: userInfo,
  468. })
  469. }
  470. },
  471. async updateUserinfo() {
  472. const sessionKey = wx.getStorageSync('sessionKey')
  473. console.log('sessionKey', sessionKey)
  474. if (sessionKey) {
  475. const res = await getWxUserInfo(sessionKey)
  476. const userInfo = wx.getStorageInfoSync('userInfo')
  477. if (res.data) {
  478. const mergeObj = {
  479. ...userInfo,
  480. ...res.data
  481. }
  482. console.log('userInfo-mergeObj', mergeObj)
  483. wx.setStorageSync('userInfo', mergeObj)
  484. }
  485. }
  486. },
  487. /**
  488. * 生命周期函数--监听页面隐藏
  489. */
  490. onHide: function () {
  491. },
  492. /**
  493. * 生命周期函数--监听页面卸载
  494. */
  495. onUnload: function () {
  496. },
  497. /**
  498. * 页面相关事件处理函数--监听用户下拉动作
  499. */
  500. onPullDownRefresh: function () {
  501. },
  502. /**
  503. * 页面上拉触底事件的处理函数
  504. */
  505. onReachBottom: function () {
  506. },
  507. /**
  508. * 用户点击右上角分享
  509. */
  510. onShareAppMessage: function () {
  511. }
  512. })