index.js 13 KB

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