index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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: ''
  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. wx.navigateTo({
  141. url: "/pages/user/my_course/my_course"
  142. })
  143. break;
  144. case 4:
  145. wx.navigateTo({
  146. url: "/pages/user/my_host_course/index"
  147. })
  148. break;
  149. }
  150. } else {
  151. Toast.showToast('tip', '请登录后查看', () => {
  152. });
  153. }
  154. },
  155. bindGetUserInfo: function (e) {
  156. console.log(e.detail.userInfo)
  157. },
  158. _myLikes: function () {
  159. this._isLogin();
  160. if (this.data.isLogin) {
  161. wx.navigateTo({
  162. url: './my_follow/index',
  163. success: function (res) {},
  164. fail: function (res) {},
  165. complete: function (res) {},
  166. })
  167. } else {
  168. Toast.showToast('tip', '请登录后查看', () => {
  169. });
  170. }
  171. },
  172. _myOrder: function () {
  173. this._isLogin();
  174. if (this.data.isLogin) {
  175. wx.navigateTo({
  176. url: './my_order/index',
  177. success: function (res) {},
  178. fail: function (res) {},
  179. complete: function (res) {},
  180. })
  181. } else {
  182. Toast.showToast('tip', '请登录后查看', () => {
  183. });
  184. }
  185. },
  186. _isLogin: function () {
  187. if (wx.getStorageSync('token')) {
  188. let loginSessionKey = wx.getStorageSync("token");
  189. if (loginSessionKey != " ") {
  190. wx.checkSession({
  191. success: res => {
  192. wx.request({
  193. url: serverName + '/wx/api/user/checkLoginSessionKey',
  194. data: {
  195. loginSessionKey
  196. },
  197. header: {
  198. 'content-type': 'application/x-www-form-urlencoded'
  199. },
  200. method: "post",
  201. success: (res) => {
  202. if (res.data.code == 0) {
  203. this.setData({
  204. isLogin: true
  205. })
  206. app.globalData.isLogin = true;
  207. // app.globalData.cookieIDs = res.data.data.ids.split(',');
  208. } else {
  209. this.setData({
  210. isLogin: false,
  211. avatarUrl: ""
  212. })
  213. app.globalData.isLogin = false;
  214. }
  215. }
  216. })
  217. // request['checkLoginSessionKey']({
  218. // loginSessionKey
  219. // }, "post", res => {
  220. // if (res.data.code > -1) {
  221. // this.setData({
  222. // isLogin: true
  223. // })
  224. // } else {
  225. // this.setData({
  226. // isLogin: false
  227. // })
  228. // }
  229. // })
  230. },
  231. fail: res => {
  232. this.setData({
  233. isLogin: false,
  234. avatarUrl: ""
  235. })
  236. app.globalData.isLogin = false;
  237. }
  238. })
  239. } else {
  240. this.setData({
  241. isLogin: false,
  242. avatarUrl: ""
  243. })
  244. app.globalData.isLogin = false;
  245. }
  246. } else {
  247. this.setData({
  248. isLogin: false
  249. })
  250. app.globalData.isLogin = false;
  251. }
  252. // console.log("isLogin",this.data.isLogin);
  253. },
  254. getUserProfile: function (e) {
  255. Toast.showToast2('loading');
  256. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  257. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  258. wx.getUserProfile({
  259. desc: '获取您的身份信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  260. success: (res) => {
  261. console.log(123456, res);
  262. let {
  263. encryptedData,
  264. iv,
  265. userInfo
  266. } = res;
  267. app.globalData.userInfo = userInfo;
  268. wx.setStorageSync("userInfo", userInfo)
  269. this.setData({
  270. avatarUrl: wx.getStorageSync('userInfo').avatarUrl
  271. })
  272. wx.login({
  273. success: res => {
  274. let {
  275. code
  276. } = res;
  277. console.log(789, res);
  278. if (code) {
  279. wx.request({
  280. url: serverName + '/wx/api/user/getLoginSessionKey',
  281. data: {
  282. encryptedData,
  283. iv,
  284. code,
  285. },
  286. header: {
  287. 'content-type': 'application/x-www-form-urlencoded'
  288. },
  289. method: 'post',
  290. // dataType: 'json',
  291. // responseType: 'text',
  292. success: res => {
  293. if (res.data.code > -1) {
  294. let {
  295. loginSessionKey
  296. } = res.data.data;
  297. wx.setStorageSync('token', loginSessionKey)
  298. } else {
  299. Toast.showToast('warn', '登录失败,请重试');
  300. }
  301. },
  302. fail: res => {
  303. Toast.showToast('warn', '登录失败,请重试');
  304. },
  305. complete: res => {
  306. this._isLogin();
  307. Toast.hideLoading();
  308. },
  309. })
  310. // request["getLoginSessionKey"]({
  311. // encryptedData,
  312. // iv,
  313. // code,
  314. // loginSessionKey:''
  315. // }, "post", res => {
  316. // if (res.data.code > -1) {
  317. // let {
  318. // loginSessionKey
  319. // } = res.data.data;
  320. // wx.setStorageSync('token', loginSessionKey)
  321. // this.isLongPolling();
  322. // console.log(loginSessionKey)
  323. // }
  324. // }, err => {
  325. // }, complete => {
  326. // this._isLogin();
  327. // Toast.hideLoading();
  328. // })
  329. }
  330. }
  331. })
  332. // console.log(res.userInfo)
  333. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  334. // 所以此处加入 callback 以防止这种情况
  335. if (this.userInfoReadyCallback) {
  336. this.userInfoReadyCallback(res)
  337. }
  338. },
  339. complete: (res) => {
  340. Toast.hideLoading();
  341. }
  342. })
  343. },
  344. _logout: function () {
  345. if (this.data.isLogin) {
  346. Toast.showToast2('loading');
  347. let loginSessionKey = wx.getStorageSync('token');
  348. request["logout"]({
  349. loginSessionKey
  350. }, "post", res => {
  351. if (res.data.code > -1) {
  352. wx.setStorageSync('userInfo', null);
  353. wx.setStorageSync('token', null);
  354. console.log(wx.getStorageSync('token'))
  355. this.setData({
  356. isLogin: false,
  357. avatarUrl: ""
  358. })
  359. app.globalData.isLogin = false;
  360. }
  361. }, err => {
  362. }, complete => {
  363. Toast.hideLoading();
  364. })
  365. }
  366. },
  367. tapToOrderByidx: function (e) {
  368. let {
  369. status,
  370. id
  371. } = e.currentTarget.dataset;
  372. this._isLogin();
  373. if (this.data.isLogin) {
  374. wx.navigateTo({
  375. url: `./my_order/index?status=${status}&&id=${id}`,
  376. success: function (res) {},
  377. fail: function (res) {},
  378. complete: function (res) {},
  379. })
  380. } else {
  381. Toast.showToast('tip', '请登录后查看', () => {
  382. });
  383. }
  384. // url="./my_order/index?status={{item.tag}}&&id={{item.id}}"
  385. },
  386. clickCell: function () {
  387. wx.navigateTo({
  388. url: './my_contact/index',
  389. })
  390. },
  391. toPersonal() {
  392. wx.navigateTo({
  393. url: '/pages/user/personal/personal',
  394. })
  395. },
  396. newLogin() {
  397. wx.login({
  398. success: res => {
  399. let {
  400. code
  401. } = res;
  402. console.log(789, res);
  403. if (code) {
  404. wx.request({
  405. url: serverName + '/wx/api/user/getLoginSessionKey',
  406. data: {
  407. code,
  408. },
  409. header: {
  410. 'content-type': 'application/x-www-form-urlencoded'
  411. },
  412. method: 'post',
  413. // dataType: 'json',
  414. // responseType: 'text',
  415. success: res => {
  416. if (res.data.code > -1) {
  417. let {
  418. loginSessionKey
  419. } = res.data.data;
  420. wx.setStorageSync('token', loginSessionKey)
  421. } else {
  422. Toast.showToast('warn', '登录失败,请重试');
  423. }
  424. },
  425. fail: res => {
  426. Toast.showToast('warn', '登录失败,请重试');
  427. },
  428. complete: res => {
  429. this._isLogin();
  430. Toast.hideLoading();
  431. },
  432. })
  433. }
  434. }
  435. })
  436. },
  437. /**
  438. * 生命周期函数--监听页面初次渲染完成
  439. */
  440. onReady: function () {
  441. },
  442. /**
  443. * 生命周期函数--监听页面显示
  444. */
  445. onShow: function () {
  446. let {
  447. avatar = ""
  448. } = this.data;
  449. console.log(wx.getStorageSync('userInfo'))
  450. let avatarUrl = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo').avatarUrl : "";
  451. let avatarName = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo').nickName : "";
  452. this.setData({
  453. avatarUrl: avatarUrl || avatar,
  454. avatarName: avatarName || '已登录'
  455. })
  456. this._isLogin()
  457. getWxUserInfo('9fbbd7dfca6e49d9b20b77eb0f94e73a')
  458. // debugger
  459. },
  460. /**
  461. * 生命周期函数--监听页面隐藏
  462. */
  463. onHide: function () {
  464. },
  465. /**
  466. * 生命周期函数--监听页面卸载
  467. */
  468. onUnload: function () {
  469. },
  470. /**
  471. * 页面相关事件处理函数--监听用户下拉动作
  472. */
  473. onPullDownRefresh: function () {
  474. },
  475. /**
  476. * 页面上拉触底事件的处理函数
  477. */
  478. onReachBottom: function () {
  479. },
  480. /**
  481. * 用户点击右上角分享
  482. */
  483. onShareAppMessage: function () {
  484. }
  485. })