login.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // pages/login/login.js
  2. const utils = require('../../utils/util.js')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. loginCode: ''
  9. },
  10. /**
  11. * 生命周期函数--监听页面加载
  12. */
  13. onLoad: function (options) {
  14. },
  15. onShow: function () {
  16. utils.getLoginCode().then(res => {
  17. this.setData({
  18. loginCode: res.code
  19. })
  20. console.log('wx.login=>code=>', res.code)
  21. }).catch(res => {
  22. console.log('login=>wx.login登录失败=>' + res)
  23. })
  24. },
  25. //员工授权登录
  26. login() {
  27. let params = {
  28. code: this.data.loginCode,
  29. reqChannel: 5
  30. }
  31. utils.axios({
  32. method: 'post',
  33. url: '/auth/wx/login',
  34. data: params,
  35. sendBefore() {
  36. wx.showLoading({
  37. title: '登录中...',
  38. mask: true
  39. })
  40. },
  41. complete() {
  42. wx.hideLoading()
  43. },
  44. success: res => {
  45. let result = res.data
  46. if (result.loginFlag) {
  47. this.loginSuccess(result)
  48. } else {
  49. wx.navigateTo({
  50. url: '/pages/loginBind/loginBind?uid=' + result.unionId
  51. })
  52. }
  53. }
  54. })
  55. },
  56. //登录成功后的执行函数
  57. loginSuccess(result) {
  58. utils.toast('登录成功')
  59. let userInfo = {
  60. roles: result.usertype,
  61. mobile: result.account,
  62. userName: result.userName,
  63. roleName: result.roleName
  64. }
  65. wx.setStorageSync('userInfo', userInfo)
  66. wx.setStorageSync('token', result.token)
  67. wx.setStorageSync('expirationTime', result.expirationTime)
  68. // utils.getCompany().then(res => {
  69. // wx.setStorageSync('company', res.data)
  70. // })
  71. wx.reLaunch({
  72. url: '/pages/index/index'
  73. })
  74. },
  75. //单纯的获取uid
  76. getUid() {
  77. wx.navigateTo({
  78. url: '/pages/loginBind/loginBind'
  79. })
  80. },
  81. //返回首页
  82. toHome() {
  83. wx.switchTab({
  84. url: '/pages/index/index'
  85. })
  86. },
  87. /**
  88. * 用户点击右上角分享
  89. */
  90. onShareAppMessage: function (res) {
  91. return {
  92. title: '诺信云',
  93. path: '/pages/index/index'
  94. }
  95. }
  96. })