login.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // pages/login/login.js
  2. const app = getApp();
  3. import {
  4. deepClone,
  5. encryption,
  6. randomLenNum
  7. } from "./../../utils/util";
  8. const accountInfo = wx.getAccountInfoSync();
  9. const current = accountInfo.miniProgram.envVersion;
  10. const pass = {
  11. //开发
  12. develop: "BXD123456",
  13. //体验版
  14. trial: "BXD123456",
  15. // 正式
  16. release: ""
  17. }
  18. const passwd = pass[current];
  19. const {
  20. requestPath
  21. } = require('./../../url')
  22. Page({
  23. /**
  24. * 页面的初始数据
  25. */
  26. data: {
  27. user: {
  28. username: "",
  29. password: passwd || "",
  30. // username: "SZMAdmin",
  31. // password: "BXD123456",
  32. mobile: "",
  33. code: '',
  34. wxCode: '',
  35. randomStr: '',
  36. code1: ''
  37. },
  38. smsTime: 0,
  39. eyes: false,
  40. straumann: false,
  41. ownerCodeList: [],
  42. virtualInventoryList: [],
  43. changeCompanyName: false,
  44. companyName: "",
  45. selectCompanyName: "",
  46. virtualInventoryId: "",
  47. virtualInventoryName: "",
  48. virtualInventoryValue: [],
  49. code: {
  50. src: "/code",
  51. value: "",
  52. len: 4,
  53. type: "image"
  54. },
  55. tokenData: {
  56. userData: null,
  57. user: null,
  58. company: null,
  59. },
  60. loginType: 0
  61. },
  62. /**
  63. * 生命周期函数--监听页面加载
  64. */
  65. onLoad(options) {
  66. // wx.setStorageSync('111', 111);
  67. // wx.enableAlertBeforeUnload({
  68. // message: "您确定取消登录吗?",
  69. // success: function (res) {
  70. // wx.clearStorageSync();
  71. // //console.log("成功", res);
  72. // },
  73. // fail: function (errMsg) {}
  74. // });
  75. let userInfo = wx.getStorageSync('userInfo');
  76. if (userInfo && userInfo.userId) {
  77. // app.globalData.tabbar.list[0].show = false;
  78. wx.switchTab({
  79. url: '/pages/index/index',
  80. })
  81. return;
  82. }
  83. this.getCode();
  84. this.renderCode()
  85. },
  86. /**
  87. * 生命周期函数--监听页面初次渲染完成
  88. */
  89. onReady() {
  90. },
  91. /**
  92. * 生命周期函数--监听页面显示
  93. */
  94. async onShow() {
  95. },
  96. async getCode() {
  97. let res = await wx.login();
  98. this.setData({
  99. wxCode: res.code
  100. })
  101. return res.code;
  102. },
  103. renderCode() {
  104. let codeV = this.data.code;
  105. let user = this.data.user;
  106. user.code = "";
  107. user.randomStr = randomLenNum(codeV.len, true);
  108. codeV.type === "text" ?
  109. (codeV.value = randomLenNum(codeV.len)) :
  110. (codeV.src = `${requestPath}/code?randomStr=${this.data.user.randomStr}`);
  111. this.setData({
  112. user,
  113. code: codeV
  114. })
  115. },
  116. async getSmsCode() {
  117. if (!this.data.user.mobile) {
  118. wx.showToast({
  119. title: '请输入正确的手机号',
  120. icon: "none"
  121. });
  122. return;
  123. }
  124. let {
  125. data
  126. } = await app.ajax.gets(`/admin/mobile/${this.data.user.mobile}`);
  127. if (!data.data) {
  128. wx.showToast({
  129. title: data.msg,
  130. icon: "none"
  131. });
  132. } else {}
  133. wx.showToast({
  134. title: '验证码发送成功',
  135. icon: "none"
  136. });
  137. this.setData({
  138. smsTime: 60
  139. });
  140. let that = this;
  141. let smsTime = setInterval(() => {
  142. if (that.data.smsTime - 1 == 0) {
  143. clearInterval(smsTime);
  144. }
  145. this.setData({
  146. smsTime: that.data.smsTime - 1
  147. });
  148. }, 1000);
  149. },
  150. setLoginType(e) {
  151. this.setData({
  152. loginType: Number(e.currentTarget.dataset.index)
  153. })
  154. },
  155. async submit(e) {
  156. let userInfo = e.detail.value;
  157. let userEnc = encryption({
  158. data: userInfo,
  159. key: "pigxpigxpigxpigx",
  160. param: ["password"]
  161. });
  162. // return;
  163. try {
  164. let data = {
  165. ...this.data.user,
  166. ...userEnc
  167. };
  168. let userData = {};
  169. if (this.data.loginType == 0) {
  170. if (!userInfo.mobile) {
  171. wx.showToast({
  172. title: '手机号不可为空',
  173. icon: "error"
  174. });
  175. return;
  176. }
  177. if (!userInfo.code1) {
  178. wx.showToast({
  179. title: '手机号不可为空',
  180. icon: "error"
  181. });
  182. return;
  183. }
  184. userData = await app.ajax.post({
  185. url: `/auth/mobile/token/sms?scope=server&mobile=SMS@${userInfo.mobile}&code=${userInfo.code1}`,
  186. data: {
  187. mobile: "SMS@" + userInfo.mobile,
  188. code: userInfo.code1,
  189. scope: "server"
  190. },
  191. header: {
  192. Authorization: "Basic bWluaTptaW5p",
  193. isToken: false
  194. }
  195. });
  196. } else if (this.data.loginType == 1) {
  197. userData = await app.ajax.post({
  198. url: `/auth/oauth/token?code=${data.code}&grant_type=password&password=${encodeURIComponent(data.password)}&randomStr=${data.randomStr}&scope=server&username=${data.username}`,
  199. data: {},
  200. header: {
  201. Authorization: "Basic bWluaTptaW5p",
  202. isToken: false
  203. }
  204. });
  205. }
  206. wx.clearStorageSync();
  207. wx.setStorageSync('access_token', userData.data.access_token);
  208. wx.setStorageSync('refresh_token', userData.data.refresh_token);
  209. wx.setStorageSync('expires_in', userData.data.expires_in);
  210. wx.setStorageSync('isLock', false);
  211. wx.setStorageSync('lockPasswd', '');
  212. //console.log(userData)
  213. // app.globalData.userInfo = userData;
  214. // wx.setStorageSync('userInfo', userData.data);
  215. let user = await app.ajax.get({
  216. url: "/admin/user/info",
  217. data: {}
  218. });
  219. user = user.data.data;
  220. let company = await app.ajax.get({
  221. url: `/admin/bxdClientCompany/${user.sysUser.companyId}`,
  222. data: {}
  223. });
  224. let isStraumann = company.data.data.corporationCode == 'straumann';
  225. wx.setStorageSync('isStraumann', isStraumann);
  226. if (isStraumann) {
  227. } else if (user.sysUser.roleType === "2") {
  228. this.renderCode();
  229. wx.clearStorageSync();
  230. wx.showToast({
  231. title: '您没下单或审批权限,请更换账号!',
  232. icon: 'none'
  233. });
  234. return;
  235. } else if (user.sysUser.roleType === "0") {
  236. let roles = user.roleList.find(v => v.roleCode == 'ROLE_ORDER_AUDIT' || v.roleCode == 'SZMAdmin');
  237. if (!roles) {
  238. this.renderCode();
  239. wx.clearStorageSync();
  240. wx.showToast({
  241. title: '您没下单或审批权限,请更换账号!',
  242. icon: 'none'
  243. });
  244. return;
  245. }
  246. }
  247. let xiadan = user.roleList.find(v => v.roleCode == 'User' || v.roleCode == 'ROLE_ORDER_AUDIT' || v.roleCode == 'SZMAdmin');
  248. if (!xiadan && !isStraumann) {
  249. this.renderCode();
  250. wx.clearStorageSync();
  251. wx.showToast({
  252. title: '您没下单或审批权限,请更换账号!',
  253. icon: 'none'
  254. });
  255. return;
  256. }
  257. await app.ajax.post({
  258. url: `/admin/social/bind?state=WX&code=${await this.getCode()}`,
  259. });
  260. company = company.data.data;
  261. app.globalData.userInfo = user;
  262. wx.setStorageSync('userInfo', user.sysUser);
  263. wx.setStorageSync('userRole', user.roles);
  264. wx.setStorageSync('roleList', user.roleList);
  265. wx.setStorageSync('companyId', user.sysUser.companyId);
  266. wx.setStorageSync('companyName', user.sysUser.companyName || '');
  267. wx.setStorageSync('selectCompanyName', user.sysUser.companyName || '');
  268. wx.setStorageSync('virtualInventoryId', user.sysUser.virtualInventoryId || 0);
  269. wx.setStorageSync('virtualInventoryName', user.sysUser.virtualInventoryName || '');
  270. wx.setStorageSync('virtualInventoryValue', []);
  271. wx.setStorageSync('virtualInventoryList', []);
  272. // wx.setStorageSync('ownerCodeList', company.ownerCodeList);
  273. wx.setStorageSync('ownerCodeList', user.sysUser.companyName.split(","));
  274. // app.globalData.tabbar.list[0].show = false;
  275. app.globalData.company = company;
  276. wx.setStorageSync('company', company);
  277. app.globalData.logout = false;
  278. wx.setStorageSync('logout', false);
  279. wx.setStorageSync('showChangeCompanyName', false);
  280. this.setData({
  281. companyName: company.companyName,
  282. tokenData: {
  283. userData: userData.data.data,
  284. user: user,
  285. company: company
  286. }
  287. })
  288. let role = user.roleList.find(v => v.roleCode == 'SZMAdmin');
  289. if (role) {
  290. wx.setStorageSync('companyName', '');
  291. wx.setStorageSync('selectCompanyName', '');
  292. wx.setStorageSync('showChangeCompanyName', true);
  293. app.globalData.changeCompanyName = true;
  294. } else {
  295. this.goHome();
  296. }
  297. } catch (e) {
  298. //console.log(e)
  299. this.renderCode();
  300. }
  301. },
  302. eyesTap() {
  303. this.setData({
  304. eyes: !this.data.eyes
  305. })
  306. },
  307. asyncValue(e) {
  308. this.setData({
  309. ["user." + e.target.dataset.name]: e.detail.value
  310. });
  311. },
  312. setCompanyName(e) {
  313. //console.log(e)
  314. this.setData({
  315. selectCompanyName: this.data.ownerCodeList[Number(e.detail.value)]
  316. })
  317. this.getVirtualInventoryList();
  318. },
  319. async changeCompany(userData, user, company) {
  320. this.setData({
  321. ownerCodeList: wx.getStorageSync('ownerCodeList'),
  322. changeCompanyName: true,
  323. });
  324. },
  325. async getVirtualInventoryList() {
  326. let companyId = this.data.tokenData.company.companyId;
  327. let selectCompanyName = this.data.selectCompanyName;
  328. let {
  329. data
  330. } = await app.ajax.get({
  331. url: `/admin/bxdVirtualInventory/page?companyId=${companyId}&companyName=${selectCompanyName}&current=1&size=10000`,
  332. noCompany: true
  333. });
  334. let virtualInventoryList = data.data.records.map(v => {
  335. v.children = [{
  336. virtualInventoryName: '一级'
  337. }].concat(v.children);
  338. return v;
  339. });
  340. this.setData({
  341. virtualInventoryValue: [0, 0],
  342. virtualInventoryList: [
  343. virtualInventoryList,
  344. virtualInventoryList[0].children
  345. ]
  346. })
  347. },
  348. setVirtualInventoryId(e) {
  349. //console.log(e)
  350. let virtualInventoryList = this.data.virtualInventoryList;
  351. let virtualInventoryName = virtualInventoryList[0][e.detail.value[0]].virtualInventoryName;
  352. let virtualInventoryId = virtualInventoryList[0][e.detail.value[0]].virtualInventoryId;
  353. if (virtualInventoryList[1][e.detail.value[1]].virtualInventoryId) {
  354. virtualInventoryName += "/" + virtualInventoryList[1][e.detail.value[1]].virtualInventoryName;
  355. virtualInventoryId = virtualInventoryList[1][e.detail.value[1]].virtualInventoryId;
  356. }
  357. this.setData({
  358. virtualInventoryValue: e.detail.value,
  359. virtualInventoryName,
  360. virtualInventoryId
  361. });
  362. },
  363. setVirtualInventoryChildren(e) {
  364. let virtualInventoryValue = this.data.virtualInventoryValue;
  365. let virtualInventoryList = this.data.virtualInventoryList;
  366. virtualInventoryValue[e.detail.column] = e.detail.value;
  367. if (e.detail.column == 0) {
  368. virtualInventoryValue[1] = 0;
  369. }
  370. this.setData({
  371. virtualInventoryValue
  372. });
  373. if (e.detail.column == 0) {
  374. virtualInventoryList[1] = virtualInventoryList[e.detail.column][e.detail.value].children;
  375. this.setData({
  376. virtualInventoryList
  377. });
  378. }
  379. },
  380. selectConfirm() {
  381. if (!this.data.selectCompanyName) {
  382. wx.showToast({
  383. title: '请选择货主',
  384. icon: 'error'
  385. });
  386. return;
  387. }
  388. if (!this.data.virtualInventoryName) {
  389. wx.showToast({
  390. title: '请选择成本中心',
  391. icon: 'error'
  392. });
  393. return;
  394. }
  395. this.goHome();
  396. },
  397. goHome() {
  398. let {
  399. userData,
  400. user,
  401. company
  402. } = this.data.tokenData;
  403. app.globalData.userInfo = user;
  404. wx.setStorageSync('userInfo', user.sysUser);
  405. wx.setStorageSync('userRole', user.roles);
  406. wx.setStorageSync('roleList', user.roleList);
  407. wx.setStorageSync('companyId', user.sysUser.companyId);
  408. wx.setStorageSync('companyName', this.data.selectCompanyName || user.sysUser.companyName);
  409. wx.setStorageSync('virtualInventoryId', this.data.virtualInventoryId || user.sysUser.virtualInventoryId || 0);
  410. wx.setStorageSync('virtualInventoryName', this.data.virtualInventoryName);
  411. wx.setStorageSync('virtualInventoryValue', this.data.virtualInventoryValue);
  412. wx.setStorageSync('virtualInventoryList', this.data.virtualInventoryList);
  413. wx.setStorageSync('ownerCodeList', this.data.ownerCodeList);
  414. // app.globalData.tabbar.list[0].show = false;
  415. app.globalData.company = company;
  416. wx.setStorageSync('company', company);
  417. wx.setStorageSync('isStraumann', company.corporationCode == 'straumann');
  418. app.globalData.logout = true;
  419. wx.setStorageSync('logout', false);
  420. setTimeout(() => {
  421. wx.showToast({
  422. title: '登录成功',
  423. icon: "success"
  424. });
  425. }, 200);
  426. setTimeout(() => {
  427. wx.switchTab({
  428. url: '/pages/index/index',
  429. })
  430. }, 1000)
  431. },
  432. /**
  433. * 生命周期函数--监听页面隐藏
  434. */
  435. onHide() {
  436. },
  437. /**
  438. * 生命周期函数--监听页面卸载
  439. */
  440. onUnload() {
  441. },
  442. /**
  443. * 页面相关事件处理函数--监听用户下拉动作
  444. */
  445. onPullDownRefresh() {
  446. },
  447. /**
  448. * 页面上拉触底事件的处理函数
  449. */
  450. onReachBottom() {
  451. },
  452. /**
  453. * 用户点击右上角分享
  454. */
  455. onShareAppMessage() {
  456. }
  457. })