index.js 15 KB

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