login.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. let util = require('../../utils/util.js')
  2. const api = require('../../utils/o2Api.js');
  3. const encrypt = require('../../utils/jsencrypt.js');
  4. Page({
  5. data: {
  6. isShowLogin: false,
  7. name: '',
  8. password: '',
  9. nameFocus: true,
  10. passwordFocus: false,
  11. registerEnable: false,
  12. rsa : {
  13. rsaEnable: false,
  14. publicKey: ''
  15. }
  16. },
  17. onLoad: function () {
  18. this.connectCenterServer();
  19. },
  20. //连接中心服务器获取应用连接地址
  21. connectCenterServer: function() {
  22. api.centerServer().then(data => {
  23. api.setDistribute(data);
  24. this.checkIsLogin();
  25. this.loadLoginRSAInfo();
  26. }).catch( err => {
  27. console.error('连接中心服务器失败', err);
  28. util.toast('连不上O2OA服务器,请检查网络!');
  29. });
  30. },
  31. //检查登录
  32. checkIsLogin: function() {
  33. var who = wx.getStorageSync('who');
  34. if (who && who.token) {
  35. //检查是否过期
  36. api.who().then(data => {
  37. if(data.token && data.token != "") {
  38. wx.setStorageSync('who', data);
  39. //跳转到首页
  40. wx.switchTab({
  41. url: '../index/index'
  42. })
  43. }else {
  44. wx.removeStorageSync('who');
  45. //过期
  46. this.setData({
  47. isShowLogin: true
  48. });
  49. }
  50. }).catch(err => {
  51. //已经过期?
  52. wx.removeStorageSync('who');
  53. this.setData({
  54. isShowLogin: true
  55. });
  56. });
  57. }else {
  58. this.setData({
  59. isShowLogin: true
  60. });
  61. }
  62. },
  63. // 获取 RSA 加密公开密钥
  64. loadLoginRSAInfo: function() {
  65. api.rsaPublishKey().then(data => {
  66. console.debug(data);
  67. if (data.rsaEnable && data.rsaEnable === true) {
  68. let rsa = {
  69. rsaEnable: true,
  70. publicKey: data.publicKey
  71. };
  72. this.setData({
  73. rsa: rsa
  74. });
  75. }
  76. }).catch(err => {
  77. console.error(err);
  78. });
  79. },
  80. checkRegisterMode: function() {
  81. api.registerMode().then(res => {
  82. if (res.value == 'code' ) {
  83. this.setData({
  84. registerEnable: true
  85. })
  86. }
  87. })
  88. },
  89. inputName: function(event) {
  90. this.data.name = event.detail.value;
  91. },
  92. inputPassword: function(event) {
  93. this.data.password = event.detail.value;
  94. },
  95. nameBindfocus: function() {
  96. this.setData({
  97. nameFocus: true,
  98. passwordFocus: false
  99. })
  100. },
  101. passwordBindfocus: function() {
  102. this.setData({
  103. nameFocus: false,
  104. passwordFocus: true
  105. })
  106. },
  107. login: function() {
  108. if (this.data.name.length == 0) {
  109. util.toast("请输入用户名!");
  110. return;
  111. }
  112. if (this.data.password.length == 0) {
  113. util.toast("请输入密码!");
  114. return;
  115. }
  116. let param = {
  117. credential: this.data.name,
  118. password: this.data.password
  119. }
  120. if (this.data.rsa.rsaEnable === true) {
  121. var et = new encrypt.JSEncrypt();
  122. et.setPublicKey("-----BEGIN PUBLIC KEY-----"+this.data.rsa.publicKey+"-----END PUBLIC KEY-----");
  123. param.password = et.encrypt(param.password);
  124. param.isEncrypted = "y";
  125. } else {
  126. param.isEncrypted = 'n';
  127. }
  128. api.login(param)
  129. .then(data => {
  130. wx.setStorageSync('who', data);
  131. util.toast('登录成功~');
  132. //跳转到首页
  133. wx.switchTab({
  134. url: '../index/index'
  135. })
  136. }).catch(err => {
  137. console.log('登录失败', err);
  138. api.o2Error(err, '登录失败');
  139. });
  140. },
  141. toRegister: function() {
  142. wx.navigateTo({
  143. url: './register',
  144. })
  145. },
  146. // 长按扫码
  147. openMPWxImage: function() {
  148. wx.previewImage({
  149. current: 'https://www.o2oa.net/imagex/experience/zoneland_mpweixin.jpg',
  150. urls: ['https://www.o2oa.net/imagex/experience/zoneland_mpweixin.jpg'],
  151. })
  152. },
  153. // 获取体验账号
  154. getDemoAccounts: function() {
  155. wx.hideKeyboard();
  156. api.wwwGetSampleServerAccounts('sample')
  157. .then(data => {
  158. console.log(data);
  159. if (data.value) {
  160. var list = [];
  161. if (data.value.password) {
  162. this.data.samplePassword = data.value.password;
  163. }
  164. if (data.value.accountList) {
  165. this.data.accountList = data.value.accountList;
  166. for (let index = 0; index < this.data.accountList.length; index++) {
  167. const element = this.data.accountList[index];
  168. list.push(element.name+': '+element.account);
  169. }
  170. }
  171. if (list.length > 0) {
  172. wx.showActionSheet({
  173. itemList: list,
  174. success: (res)=> {
  175. console.log(res.tapIndex)
  176. let account = this.data.accountList[res.tapIndex];
  177. console.log(account);
  178. this.setData({
  179. name: account.account,
  180. password: this.data.samplePassword
  181. });
  182. },
  183. fail: (res)=> {
  184. console.log(res.errMsg)
  185. }
  186. });
  187. } else {
  188. util.toast('连不上O2OA服务器,请检查网络!');
  189. }
  190. } else {
  191. util.toast('连不上O2OA服务器,请检查网络!');
  192. }
  193. }).catch(err => {
  194. console.error(err);
  195. api.o2Error(err, '获取账号失败,请到O2OA官网查询!');
  196. });
  197. },
  198. // 体验账号登录
  199. demoLogin: function(event) {
  200. let no = event.currentTarget.dataset.no;
  201. const param = {
  202. password: 'o2'
  203. };
  204. if (no == '1') {
  205. param['credential'] = '开发部经理'
  206. } else if (no == '2') {
  207. param['credential'] = '开发部员工1'
  208. } else if (no == '3') {
  209. param['credential'] = '公司领导1'
  210. } else if (no == '4') {
  211. param['credential'] = '办公室主任'
  212. }
  213. api.login(param)
  214. .then(data => {
  215. wx.setStorageSync('who', data);
  216. util.toast('登录成功~');
  217. //跳转到首页
  218. wx.switchTab({
  219. url: '../index/index'
  220. })
  221. }).catch(err => {
  222. console.log('登录失败', err);
  223. api.o2Error(err, '登录失败');
  224. });
  225. }
  226. })