index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. _getUserInfoToLogin: function(e) {
  230. Toast.showToast2('loading');
  231. if (e.detail.encryptedData) {
  232. // 可以将 res 发送给后台解码出 unionId
  233. // app.globalData.userInfo = res.userInfo;
  234. // app.globalData.encryptedData = res.encryptedData;
  235. // app.globalData.iv = res.iv;
  236. let {
  237. encryptedData,
  238. iv,
  239. userInfo
  240. } = e.detail;
  241. app.globalData.userInfo = userInfo;
  242. wx.setStorageSync("userInfo", userInfo)
  243. this.setData({
  244. avatarUrl: wx.getStorageSync('userInfo').avatarUrl
  245. })
  246. wx.login({
  247. success: res => {
  248. let {
  249. code
  250. } = res;
  251. if (code) {
  252. wx.request({
  253. url: serverName + '/wx/api/user/getLoginSessionKey',
  254. data: {
  255. encryptedData,
  256. iv,
  257. code,
  258. },
  259. header: {
  260. 'content-type': 'application/x-www-form-urlencoded'
  261. },
  262. method: 'post',
  263. // dataType: 'json',
  264. // responseType: 'text',
  265. success: res => {
  266. if (res.data.code > -1) {
  267. let {
  268. loginSessionKey
  269. } = res.data.data;
  270. wx.setStorageSync('token', loginSessionKey)
  271. }
  272. else{
  273. Toast.showToast('warn', '登录失败,请重试');
  274. }
  275. },
  276. fail: res => {
  277. Toast.showToast('warn', '登录失败,请重试');
  278. },
  279. complete: res=> {
  280. this._isLogin();
  281. Toast.hideLoading();
  282. },
  283. })
  284. // request["getLoginSessionKey"]({
  285. // encryptedData,
  286. // iv,
  287. // code,
  288. // loginSessionKey:''
  289. // }, "post", res => {
  290. // if (res.data.code > -1) {
  291. // let {
  292. // loginSessionKey
  293. // } = res.data.data;
  294. // wx.setStorageSync('token', loginSessionKey)
  295. // this.isLongPolling();
  296. // console.log(loginSessionKey)
  297. // }
  298. // }, err => {
  299. // }, complete => {
  300. // this._isLogin();
  301. // Toast.hideLoading();
  302. // })
  303. }
  304. }
  305. })
  306. // console.log(res.userInfo)
  307. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  308. // 所以此处加入 callback 以防止这种情况
  309. if (this.userInfoReadyCallback) {
  310. this.userInfoReadyCallback(res)
  311. }
  312. }
  313. },
  314. // _login:function(e){
  315. // console.log(e)
  316. // // if(!this.data.isLogin){
  317. // // Toast.showToast2('loading');
  318. // // wx.getSetting({
  319. // // success: res => {
  320. // // console.log(res.authSetting['scope.userInfo'])
  321. // // if (!res.authSetting['scope.userInfo'] == undefined || res.authSetting['scope.userInfo'] == false) {
  322. // // Toast.showToast('tip', '四维看展申请获取个人信息权限', () => {
  323. // // wx.openSetting({
  324. // // success: res => {
  325. // // this._getUserInfoToLogin();
  326. // // }
  327. // // });
  328. // // });
  329. // // } else {
  330. // // // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  331. // // this._getUserInfoToLogin();
  332. // // }
  333. // // }
  334. // // })
  335. // // }
  336. // },
  337. _logout: function() {
  338. if (this.data.isLogin) {
  339. Toast.showToast2('loading');
  340. let loginSessionKey = wx.getStorageSync('token');
  341. request["logout"]({
  342. loginSessionKey
  343. }, "post", res => {
  344. if (res.data.code > -1) {
  345. wx.setStorageSync('userInfo', null);
  346. wx.setStorageSync('token', null);
  347. console.log(wx.getStorageSync('token'))
  348. this.setData({
  349. isLogin: false,
  350. avatarUrl: ""
  351. })
  352. app.globalData.isLogin = false;
  353. }
  354. }, err => {
  355. }, complete => {
  356. Toast.hideLoading();
  357. })
  358. }
  359. },
  360. tapToOrderByidx: function(e) {
  361. let {
  362. status,
  363. id
  364. } = e.currentTarget.dataset;
  365. this._isLogin();
  366. if (this.data.isLogin) {
  367. wx.navigateTo({
  368. url: `./my_order/index?status=${status}&&id=${id}`,
  369. success: function(res) {},
  370. fail: function(res) {},
  371. complete: function(res) {},
  372. })
  373. } else {
  374. Toast.showToast('tip', '请登录后查看', () => {
  375. });
  376. }
  377. // url="./my_order/index?status={{item.tag}}&&id={{item.id}}"
  378. },
  379. clickCell: function() {
  380. wx.navigateTo({
  381. url: './my_contact/index',
  382. })
  383. },
  384. /**
  385. * 生命周期函数--监听页面初次渲染完成
  386. */
  387. onReady: function() {
  388. },
  389. /**
  390. * 生命周期函数--监听页面显示
  391. */
  392. onShow: function() {
  393. let {
  394. avatar = ""
  395. } = this.data;
  396. console.log(wx.getStorageSync('userInfo'))
  397. let avatarUrl = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo').avatarUrl : "";
  398. let avatarName = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo').nickName : "";
  399. this.setData({
  400. avatarUrl: avatarUrl || avatar,
  401. avatarName: avatarName || '已登录'
  402. })
  403. this._isLogin()
  404. },
  405. /**
  406. * 生命周期函数--监听页面隐藏
  407. */
  408. onHide: function() {
  409. },
  410. /**
  411. * 生命周期函数--监听页面卸载
  412. */
  413. onUnload: function() {
  414. },
  415. /**
  416. * 页面相关事件处理函数--监听用户下拉动作
  417. */
  418. onPullDownRefresh: function() {
  419. },
  420. /**
  421. * 页面上拉触底事件的处理函数
  422. */
  423. onReachBottom: function() {
  424. },
  425. /**
  426. * 用户点击右上角分享
  427. */
  428. onShareAppMessage: function() {
  429. }
  430. })