index.js 12 KB

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