login.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. var util = require('../../utils/util.js')
  2. var app = getApp()
  3. Page({
  4. data: {
  5. isHiden: true,
  6. userurl: 'https://kptyun.cn/', //正式地址
  7. // userurl: 'http://192.168.1.57:8084/', //刘欢地址
  8. // userurl: 'http://210.16.189.72:8099/', //测试地址
  9. username: '',
  10. password: '',
  11. // username: '00020',
  12. // password: '123456',
  13. isChecked: true,
  14. openid: '',
  15. },
  16. onLaunch: function () {},
  17. onLoad: function (option) {
  18. var that = this
  19. var url2 = this.data.userurl + 'getData'
  20. wx.login({
  21. success: function (res) {
  22. if (res.code) {
  23. //发起网络请求
  24. wx.request({
  25. url: url2,
  26. data: {
  27. code: res.code,
  28. },
  29. method: 'GET',
  30. header: {
  31. 'Content-Type': 'json',
  32. },
  33. success: function (res) {
  34. console.log(res)
  35. var openid = res.data.openid //获取到的openid
  36. that.setData({
  37. openid: openid,
  38. })
  39. },
  40. fail: function (error) {
  41. console.log(error)
  42. },
  43. })
  44. } else {
  45. console.log('登录失败!' + res.errMsg)
  46. }
  47. },
  48. })
  49. var username = wx.getStorageSync('username')
  50. var password = wx.getStorageSync('password')
  51. if (username !== '' && password !== '') {
  52. this.setData({
  53. username: username,
  54. password: password,
  55. })
  56. }
  57. },
  58. //记住密码
  59. on_checked: function (e) {
  60. console.log(username, password)
  61. var check = e.detail.checked
  62. var username = this.data.username
  63. var password = this.data.password
  64. if (check) {
  65. console.log('选中')
  66. this.setData({ isChecked: true })
  67. wx.setStorageSync('username', username)
  68. wx.setStorageSync('password', password)
  69. } else {
  70. console.log('没选中')
  71. this.setData({ isChecked: false })
  72. wx.setStorageSync('username', '')
  73. wx.setStorageSync('password', '')
  74. }
  75. },
  76. formSubmit: function (e) {
  77. var username = e.detail.value.username
  78. var password = e.detail.value.password
  79. var userurl = this.data.userurl
  80. var openid = this.data.openid
  81. if (username == '' || password == '') {
  82. this.setData({
  83. tips: '用户名、密码或地址不能为空',
  84. })
  85. } else {
  86. var url = userurl + 'auth'
  87. var data = {
  88. username: username,
  89. password: password,
  90. openid: openid,
  91. }
  92. this.setData({
  93. username: username,
  94. password: password,
  95. })
  96. console.log('url:', url)
  97. util.ajax(url, data, 'POST', this.getUserData)
  98. }
  99. },
  100. getUserData: function (e) {
  101. console.log(e)
  102. wx.setStorageSync('usertoken', e.data.token)
  103. if (e.msg != 'ok') {
  104. var msg = e.data
  105. this.setData({
  106. tips: msg,
  107. })
  108. } else {
  109. var g_token = e.data.token
  110. // var g_infoname = e.data.info.userMsg["登录人名称"]
  111. var userurl = this.data.userurl
  112. var username = this.data.username
  113. var password = this.data.password
  114. var g_url = this.data.userurl
  115. app.globalData.g_url = g_url
  116. app.globalData.g_token = g_token
  117. // app.globalData.g_infoname = g_infoname
  118. wx.setStorageSync('userinfo', {
  119. userurl: g_url,
  120. username: this.data.username,
  121. password: this.data.password,
  122. })
  123. if (this.data.isChecked) {
  124. wx.setStorageSync('userurl', userurl)
  125. wx.setStorageSync('username', username)
  126. wx.setStorageSync('password', password)
  127. } else {
  128. wx.setStorageSync('username', '')
  129. wx.setStorageSync('password', '')
  130. }
  131. wx.setStorageSync('g_url', g_url)
  132. wx.setStorageSync('g_token', g_token)
  133. var that = this
  134. util.getDataByName(
  135. { name: 'findByUserPastureName', parammaps: { jwt_username: username } },
  136. function (e) {
  137. console.log(e)
  138. wx.setStorageSync('g_userId', e.data.list[0].employeId)
  139. var g_lgName = e.data.list[0].employeName
  140. var g_countName = e.data.list[0].username
  141. var g_depName = e.data.list[0].dname
  142. var g_lgNameId = e.data.list[0].empid
  143. var g_createrId = e.data.list[0].employeId
  144. var g_userId = e.data.list[0].employeId
  145. var g_roleId = e.data.list[0].roleId
  146. app.globalData.g_countName = g_countName
  147. app.globalData.g_depName = g_depName
  148. app.globalData.g_lgName = g_lgName
  149. app.globalData.g_lgNameId = g_lgNameId
  150. app.globalData.g_createrId = g_createrId
  151. app.globalData.g_userId = g_userId
  152. app.globalData.g_roleId = g_roleId
  153. that.linkJump()
  154. },
  155. )
  156. }
  157. },
  158. linkJump: function () {
  159. wx.switchTab({
  160. url: '../workbench/workbench',
  161. })
  162. },
  163. getUrl: function (e) {
  164. this.setData({
  165. userurl: e.detail.value,
  166. })
  167. },
  168. })