index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // pages/login/index.js
  2. const app = getApp()
  3. const { sm2Encrypt } = require('../../utils/secret') //sm2Encrypt(formData.password)
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. account: '',
  10. password: '',
  11. showPass: false,
  12. code: '' //获取code用于传给后台获取OpenID
  13. },
  14. back: function () {
  15. wx.navigateBack()
  16. },
  17. toRegister: function () {
  18. wx.navigateTo({
  19. url: '../register/index',
  20. })
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function (options) {
  26. //this.getCodeFun()
  27. },
  28. /**
  29. * 生命周期函数--监听页面显示
  30. */
  31. onShow() {},
  32. // 密码显示隐藏
  33. toggleShowPass() {
  34. this.setData({
  35. showPass: !this.data.showPass
  36. })
  37. },
  38. //审核
  39. toLink: function (e) {
  40. let { url } = e.currentTarget.dataset
  41. wx.navigateTo({
  42. url: url
  43. })
  44. },
  45. //授权手机号注册
  46. getPhoneNumber: function (e) {
  47. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  48. app.request.POST({
  49. url: app.API.register,
  50. params: {
  51. 'code': e.detail.code
  52. },
  53. page: this,
  54. isLoadingTxt: '注册中...',
  55. isToken: false,
  56. successFun: true
  57. }).then(res => {
  58. wx.showModal({
  59. title: '注册成功!',
  60. content: '默认密码为123456',
  61. showCancel: false,
  62. success(res) {
  63. if (res.confirm) {
  64. console.log('用户点击确定')
  65. } else if (res.cancel) {
  66. console.log('用户点击取消')
  67. }
  68. }
  69. })
  70. })
  71. }
  72. },
  73. //提交登录事件
  74. formSubmit: function (e) {
  75. let formData = e.detail.value;
  76. var warn = ""; //弹框时提示的内容
  77. if (formData.account == '') {
  78. warn = "请输入账号!";
  79. } else if (formData.password == '') {
  80. warn = "请输入密码!";
  81. }
  82. if (warn != '') {
  83. app.util.checkForm(warn);
  84. } else {
  85. app.request.POST({
  86. url:app.API.login,
  87. params:{
  88. "account": formData.account,
  89. "password":sm2Encrypt(formData.password)
  90. },
  91. page:this,
  92. isToken:false,
  93. isLoadingTxt:'登录中...',
  94. successFun:true
  95. }).then(res=>{
  96. wx.setStorageSync('loginStatus', true); //登录状态
  97. wx.setStorageSync('Authorization', res.data.data); //token
  98. //获取字典数据
  99. app.request.GET({
  100. url:app.API.getTree,
  101. page:this,
  102. successFun:true
  103. }).then(res=>{
  104. app.globalData.treeArr = res.data.data
  105. })
  106. return app.request.GET({
  107. url:app.API.getUserInfo,
  108. isLoading:false,
  109. page:this,
  110. successFun:true
  111. })
  112. }).then(res=>{
  113. let { id, account, name, nickname, phone, unloadImg, roleCodeList, buttonCodeList } = res.data.data
  114. wx.setStorageSync('id', id); //用户id
  115. wx.setStorageSync('account', account); //用户姓名
  116. wx.setStorageSync('name', name); //用户姓名
  117. wx.setStorageSync('nickname', nickname); //企业名称
  118. wx.setStorageSync('phone', phone); //职位
  119. wx.setStorageSync('unloadImg', unloadImg); //证书图片
  120. wx.setStorageSync('roleCodeList', roleCodeList.join(',')); //用户类型
  121. wx.setStorageSync('buttonCodeList', buttonCodeList); //用户类型
  122. wx.switchTab({
  123. url: '/pages/workstand/index',
  124. })
  125. // if (roleCodeList.join(',')=='bizAdmin') {
  126. // wx.switchTab({
  127. // url: '/pages/queueCar/index',
  128. // })
  129. // } else if (roleCodeList.join(',')=='send') {
  130. // wx.switchTab({
  131. // url: '/pages/deliveryConfirm/index',
  132. // })
  133. // } else if (roleCodeList.join(',')=='sale') {
  134. // wx.switchTab({
  135. // url: '/pages/salesOrderMan/index',
  136. // })
  137. // } else{
  138. // wx.switchTab({
  139. // url: '/pages/index/index',
  140. // })
  141. // }
  142. })
  143. }
  144. },
  145. /**
  146. * 获取code
  147. * 发送 res.code 到后台换取 openId, sessionKey, unionId
  148. */
  149. getCodeFun: function () {
  150. let _this = this;
  151. wx.login({
  152. success: res => {
  153. if (res.code) {
  154. _this.setData({
  155. code: res.code
  156. })
  157. }
  158. }
  159. })
  160. },
  161. })