index.js 12 KB

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