index.js 15 KB

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