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. if (code) {
  280. wx.request({
  281. url: serverName + '/wx/api/user/getLoginSessionKey',
  282. data: {
  283. encryptedData,
  284. iv,
  285. code,
  286. },
  287. header: {
  288. 'content-type': 'application/x-www-form-urlencoded'
  289. },
  290. method: 'post',
  291. // dataType: 'json',
  292. // responseType: 'text',
  293. success: res => {
  294. if (res.data.code > -1) {
  295. let {
  296. loginSessionKey,
  297. sessionKey
  298. } = res.data.data;
  299. wx.setStorageSync('token', loginSessionKey)
  300. wx.setStorageSync('sessionKey', sessionKey)
  301. this.updateUserinfo();
  302. } else {
  303. Toast.showToast('warn', '登录失败,请重试');
  304. }
  305. },
  306. fail: res => {
  307. Toast.showToast('warn', '登录失败,请重试');
  308. },
  309. complete: res => {
  310. this._isLogin();
  311. Toast.hideLoading();
  312. },
  313. })
  314. // request["getLoginSessionKey"]({
  315. // encryptedData,
  316. // iv,
  317. // code,
  318. // loginSessionKey:''
  319. // }, "post", res => {
  320. // if (res.data.code > -1) {
  321. // let {
  322. // loginSessionKey
  323. // } = res.data.data;
  324. // wx.setStorageSync('token', loginSessionKey)
  325. // this.isLongPolling();
  326. // console.log(loginSessionKey)
  327. // }
  328. // }, err => {
  329. // }, complete => {
  330. // this._isLogin();
  331. // Toast.hideLoading();
  332. // })
  333. }
  334. }
  335. })
  336. // console.log(res.userInfo)
  337. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  338. // 所以此处加入 callback 以防止这种情况
  339. if (this.userInfoReadyCallback) {
  340. this.userInfoReadyCallback(res)
  341. }
  342. },
  343. complete: (res) => {
  344. Toast.hideLoading();
  345. }
  346. })
  347. },
  348. _logout: function () {
  349. if (this.data.isLogin) {
  350. Toast.showToast2('loading');
  351. let loginSessionKey = wx.getStorageSync('token');
  352. request["logout"]({
  353. loginSessionKey
  354. }, "post", res => {
  355. if (res.data.code > -1) {
  356. wx.setStorageSync('userInfo', null);
  357. wx.setStorageSync('token', null);
  358. wx.setStorageSync('sessionKey', null);
  359. console.log(wx.getStorageSync('token'))
  360. this.setData({
  361. isLogin: false,
  362. avatarUrl: "",
  363. })
  364. app.globalData.isLogin = false;
  365. }
  366. }, err => {
  367. }, complete => {
  368. Toast.hideLoading();
  369. })
  370. }
  371. },
  372. tapToOrderByidx: function (e) {
  373. let {
  374. status,
  375. id
  376. } = e.currentTarget.dataset;
  377. this._isLogin();
  378. if (this.data.isLogin) {
  379. wx.navigateTo({
  380. url: `./my_order/index?status=${status}&&id=${id}`
  381. })
  382. } else {
  383. Toast.showToast('tip', '请登录后查看', () => {
  384. });
  385. }
  386. // url="./my_order/index?status={{item.tag}}&&id={{item.id}}"
  387. },
  388. clickCell: function () {
  389. wx.navigateTo({
  390. url: './my_contact/index',
  391. })
  392. },
  393. toPersonal() {
  394. wx.navigateTo({
  395. url: '/pages/user/personal/personal',
  396. })
  397. },
  398. // newLogin() {
  399. // wx.login({
  400. // success: res => {
  401. // let {
  402. // code
  403. // } = res;
  404. // console.log(789, res);
  405. // if (code) {
  406. // wx.request({
  407. // url: serverName + '/wx/api/user/getLoginSessionKey',
  408. // data: {
  409. // code,
  410. // },
  411. // header: {
  412. // 'content-type': 'application/x-www-form-urlencoded'
  413. // },
  414. // method: 'post',
  415. // // dataType: 'json',
  416. // // responseType: 'text',
  417. // success: res => {
  418. // if (res.data.code > -1) {
  419. // let {
  420. // loginSessionKey
  421. // } = res.data.data;
  422. // wx.setStorageSync('token', loginSessionKey)
  423. // } else {
  424. // Toast.showToast('warn', '登录失败,请重试');
  425. // }
  426. // },
  427. // fail: res => {
  428. // Toast.showToast('warn', '登录失败,请重试');
  429. // },
  430. // complete: res => {
  431. // this._isLogin();
  432. // Toast.hideLoading();
  433. // },
  434. // })
  435. // }
  436. // }
  437. // })
  438. // },
  439. /**
  440. * 生命周期函数--监听页面初次渲染完成
  441. */
  442. onReady: function () {
  443. },
  444. /**
  445. * 生命周期函数--监听页面显示
  446. */
  447. onShow: async function () {
  448. let {
  449. avatar = ""
  450. } = this.data;
  451. const userInfo = wx.getStorageSync('userInfo')
  452. const sessionKey = wx.getStorageSync('sessionKey')
  453. let avatarUrl = userInfo ? userInfo.avatarUrl : "";
  454. let avatarName = userInfo ? userInfo.nickName : "";
  455. console.log('sessionKey', sessionKey)
  456. if (sessionKey) {
  457. this.updateUserinfo();
  458. }
  459. this.setData({
  460. avatarUrl: avatarUrl || avatar,
  461. avatarName: avatarName || '已登录'
  462. })
  463. this._isLogin()
  464. if (userInfo) {
  465. this.setData({
  466. userInfo: userInfo,
  467. })
  468. }
  469. },
  470. async updateUserinfo() {
  471. const sessionKey = wx.getStorageSync('sessionKey')
  472. console.log('sessionKey', sessionKey)
  473. if (sessionKey) {
  474. const res = await getWxUserInfo(sessionKey)
  475. const userInfo = wx.getStorageInfoSync('userInfo')
  476. if (res.data) {
  477. const mergeObj = {
  478. ...userInfo,
  479. ...res.data
  480. }
  481. console.log('userInfo-mergeObj', mergeObj)
  482. wx.setStorageSync('userInfo', mergeObj)
  483. }
  484. }
  485. },
  486. /**
  487. * 生命周期函数--监听页面隐藏
  488. */
  489. onHide: function () {
  490. },
  491. /**
  492. * 生命周期函数--监听页面卸载
  493. */
  494. onUnload: function () {
  495. },
  496. /**
  497. * 页面相关事件处理函数--监听用户下拉动作
  498. */
  499. onPullDownRefresh: function () {
  500. },
  501. /**
  502. * 页面上拉触底事件的处理函数
  503. */
  504. onReachBottom: function () {
  505. },
  506. /**
  507. * 用户点击右上角分享
  508. */
  509. onShareAppMessage: function () {
  510. }
  511. })