index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // pages/user/index.js
  2. const {
  3. serverName
  4. } = require('../../utils/services');
  5. const {
  6. request,
  7. newServerName
  8. } = require('../../utils/newServices.js');
  9. const {
  10. Toast
  11. } = require('../../utils/util.js');
  12. var app = getApp();
  13. Page({
  14. /**
  15. * 页面的初始数据
  16. */
  17. data: {
  18. testImg: '../../imgs/testImg/loginBg.png',
  19. avatar: '../../imgs/testImg/userIcon.png',
  20. likeImg: '../../imgs/icon/like.png',
  21. commentImg: '../../imgs/icon/comment.png',
  22. sawImg: '../../imgs/icon/saw.png',
  23. tabItem: [{
  24. name: '待付款',
  25. tag: "0",
  26. id: 1
  27. }, {
  28. name: '待使用',
  29. tag: "-1",
  30. id: 2
  31. }, {
  32. name: '待评论',
  33. tag: "-1",
  34. id: 3
  35. }],
  36. tabList: [{
  37. name: '收藏',
  38. img: "../../imgs/icon/like.png",
  39. id: 0
  40. }, {
  41. name: '评论',
  42. img: "../../imgs/icon/comment.png",
  43. id: 1
  44. }, {
  45. name: '足迹',
  46. img: "../../imgs/icon/saw.png",
  47. id: 2
  48. }],
  49. active: false,
  50. isLogin: ''
  51. },
  52. /**
  53. * 生命周期函数--监听页面加载
  54. */
  55. onLoad: function(options) {
  56. // this.isLongPolling()
  57. },
  58. isLongPolling: function() {
  59. let loginSessionKey = wx.getStorageSync("token");
  60. let requestTask = wx.request({
  61. url: newServerName + '/wx/user/longPolling',
  62. data: {
  63. loginSessionKey
  64. },
  65. header: {
  66. 'content-type': 'application/x-www-form-urlencoded'
  67. },
  68. method: "post",
  69. success: (res) => {
  70. if (res.data.code == 0) {
  71. if (res.data.data) {
  72. let tabList = this.data.tabList;
  73. tabList[1].img = "../../imgs/icon/comment_active.png";
  74. this.setData({
  75. tabList
  76. })
  77. } else {
  78. let tabList = this.data.tabList;
  79. tabList[1].img = "../../imgs/icon/comment.png";
  80. this.setData({
  81. tabList
  82. })
  83. }
  84. } else {
  85. return
  86. }
  87. this.isLongPolling()
  88. }
  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. if (loginSessionKey != " ") {
  168. wx.checkSession({
  169. success: res => {
  170. wx.request({
  171. url: newServerName + '/wx/user/checkLoginSessionKey',
  172. data: {
  173. loginSessionKey
  174. },
  175. header: {
  176. 'content-type': 'application/x-www-form-urlencoded'
  177. },
  178. method: "post",
  179. success: (res) => {
  180. if (res.data.code == 0) {
  181. this.setData({
  182. isLogin: true
  183. })
  184. app.globalData.isLogin = true;
  185. // app.globalData.cookieIDs = res.data.data.ids.split(',');
  186. } else {
  187. this.setData({
  188. isLogin: false,
  189. avatarUrl: ""
  190. })
  191. app.globalData.isLogin = false;
  192. }
  193. }
  194. })
  195. // request['checkLoginSessionKey']({
  196. // loginSessionKey
  197. // }, "post", res => {
  198. // if (res.data.code > -1) {
  199. // this.setData({
  200. // isLogin: true
  201. // })
  202. // } else {
  203. // this.setData({
  204. // isLogin: false
  205. // })
  206. // }
  207. // })
  208. },
  209. fail: res => {
  210. this.setData({
  211. isLogin: false,
  212. avatarUrl: ""
  213. })
  214. app.globalData.isLogin = false;
  215. }
  216. })
  217. } else {
  218. this.setData({
  219. isLogin: false,
  220. avatarUrl: ""
  221. })
  222. app.globalData.isLogin = false;
  223. }
  224. } else {
  225. this.setData({
  226. isLogin: false
  227. })
  228. app.globalData.isLogin = false;
  229. }
  230. // console.log("isLogin",this.data.isLogin);
  231. },
  232. getUserProfile:function(e){
  233. Toast.showToast2('loading');
  234. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  235. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  236. wx.getUserProfile({
  237. desc: '获取您的身份信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  238. success: (res) => {
  239. console.log(123456,res);
  240. let {
  241. encryptedData,
  242. iv,
  243. userInfo
  244. } = res;
  245. app.globalData.userInfo = userInfo;
  246. wx.setStorageSync("userInfo", userInfo)
  247. this.setData({
  248. avatarUrl: wx.getStorageSync('userInfo').avatarUrl
  249. })
  250. wx.login({
  251. success: res => {
  252. let {
  253. code
  254. } = res;
  255. console.log(789,res);
  256. if (code) {
  257. wx.request({
  258. url: newServerName + '/wx/user/getLoginSessionKey',
  259. data: {
  260. encryptedData,
  261. iv,
  262. code,
  263. },
  264. header: {
  265. 'content-type': 'application/x-www-form-urlencoded'
  266. },
  267. method: 'post',
  268. // dataType: 'json',
  269. // responseType: 'text',
  270. success: res => {
  271. if (res.data.code > -1) {
  272. let {
  273. loginSessionKey
  274. } = res.data.data;
  275. wx.setStorageSync('token', loginSessionKey)
  276. }
  277. else{
  278. Toast.showToast('warn', '登录失败,请重试');
  279. }
  280. },
  281. fail: res => {
  282. Toast.showToast('warn', '登录失败,请重试');
  283. },
  284. complete: res=> {
  285. this._isLogin();
  286. Toast.hideLoading();
  287. },
  288. })
  289. }
  290. }
  291. })
  292. // console.log(res.userInfo)
  293. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  294. // 所以此处加入 callback 以防止这种情况
  295. if (this.userInfoReadyCallback) {
  296. this.userInfoReadyCallback(res)
  297. }
  298. },
  299. complete:(res)=>{
  300. Toast.hideLoading();
  301. }
  302. })
  303. },
  304. _logout: function() {
  305. if (this.data.isLogin) {
  306. Toast.showToast2('loading');
  307. let loginSessionKey = wx.getStorageSync('token');
  308. request["logout"]({
  309. loginSessionKey
  310. }, "post", res => {
  311. if (res.data.code > -1) {
  312. wx.setStorageSync('userInfo', null);
  313. wx.setStorageSync('token', null);
  314. console.log(wx.getStorageSync('token'))
  315. this.setData({
  316. isLogin: false,
  317. avatarUrl: ""
  318. })
  319. app.globalData.isLogin = false;
  320. }
  321. }, err => {
  322. }, complete => {
  323. Toast.hideLoading();
  324. })
  325. }
  326. },
  327. tapToOrderByidx: function(e) {
  328. let {
  329. status,
  330. id
  331. } = e.currentTarget.dataset;
  332. this._isLogin();
  333. if (this.data.isLogin) {
  334. wx.navigateTo({
  335. url: `./my_order/index?status=${status}&&id=${id}`,
  336. success: function(res) {},
  337. fail: function(res) {},
  338. complete: function(res) {},
  339. })
  340. } else {
  341. Toast.showToast('tip', '请登录后查看', () => {
  342. });
  343. }
  344. // url="./my_order/index?status={{item.tag}}&&id={{item.id}}"
  345. },
  346. clickCell: function() {
  347. wx.navigateTo({
  348. url: './my_contact/index',
  349. })
  350. },
  351. /**
  352. * 生命周期函数--监听页面初次渲染完成
  353. */
  354. onReady: function() {
  355. },
  356. /**
  357. * 生命周期函数--监听页面显示
  358. */
  359. onShow: function() {
  360. let {
  361. avatar = ""
  362. } = this.data;
  363. console.log(wx.getStorageSync('userInfo'))
  364. let avatarUrl = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo').avatarUrl : "";
  365. let avatarName = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo').nickName : "";
  366. this.setData({
  367. avatarUrl: avatarUrl || avatar,
  368. avatarName: avatarName || '已登录'
  369. })
  370. this._isLogin()
  371. },
  372. /**
  373. * 生命周期函数--监听页面隐藏
  374. */
  375. onHide: function() {
  376. },
  377. /**
  378. * 生命周期函数--监听页面卸载
  379. */
  380. onUnload: function() {
  381. },
  382. /**
  383. * 页面相关事件处理函数--监听用户下拉动作
  384. */
  385. onPullDownRefresh: function() {
  386. },
  387. /**
  388. * 页面上拉触底事件的处理函数
  389. */
  390. onReachBottom: function() {
  391. },
  392. /**
  393. * 用户点击右上角分享
  394. */
  395. onShareAppMessage: function() {
  396. }
  397. })