login.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // pages/login/login.js
  2. const app=getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. showEyes: false,
  9. form: {
  10. phone: "",
  11. password: ''
  12. },
  13. phone: ''
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad: function (options) {
  19. const token = wx.getStorageSync('token')
  20. console.log(token);
  21. if (!token || token == '') {
  22. wx.setNavigationBarTitle({
  23. title: '物流管家',
  24. })
  25. }else{
  26. wx.navigateTo({
  27. url: '/pages/home/index',
  28. })
  29. }
  30. },
  31. onReady(){
  32. },
  33. close() {
  34. this.data.form.phone = ''
  35. this.setData({
  36. phone: ''
  37. })
  38. console.log(this.data.phone);
  39. },
  40. inputValue1(e) {
  41. let { value } = e.detail
  42. this.data.form.phone = value
  43. this.setData({
  44. phone: value
  45. })
  46. console.log(this.data.phone);
  47. },
  48. inputValue2(e) {
  49. let { value } = e.detail
  50. this.data.form.password = value
  51. },
  52. changeEyes() {
  53. this.setData({
  54. showEyes: !this.data.showEyes
  55. })
  56. },
  57. login() {
  58. var that=this;
  59. var phone=this.data.form.phone
  60. var password=this.data.form.password
  61. if(!phone){
  62. wx.showLoading({
  63. title: '手机号必填',
  64. })
  65. setTimeout(function () {
  66. wx.hideLoading();
  67. }, 1000);
  68. return false;
  69. }
  70. if(!password){
  71. wx.showLoading({
  72. title: '密码必填',
  73. })
  74. setTimeout(function () {
  75. wx.hideLoading();
  76. }, 1000);
  77. return false;
  78. }
  79. console.log(wx.getAccountInfoSync());
  80. app.api.useApi(app.globalData.baseAppUrl + "api/admin/login", {
  81. phone:this.data.form.phone,
  82. password: this.data.form.password,
  83. appid: wx.getAccountInfoSync().miniProgram.appId
  84. }, "post").then(function (res) {
  85. wx.showLoading({
  86. title: '登录中....',
  87. })
  88. console.log(res);
  89. if (res.code != 200) {
  90. wx.showLoading({
  91. title: res.message.msg,
  92. })
  93. setTimeout(function () {
  94. wx.hideLoading();
  95. }, 2000);
  96. } else {
  97. wx.hideLoading();
  98. wx.setStorageSync('token', res.message.data.access_token);
  99. wx.navigateTo({
  100. url: '/pages/home/index',
  101. })
  102. }
  103. }).catch(function (err) {
  104. console.log(222);
  105. })
  106. }
  107. })