index.js 12 KB

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