login.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import store from '@/store';
  2. import $http from '@/config/requestConfig'
  3. import base from '@/config/baseUrl';
  4. // #ifdef H5
  5. import {
  6. h5Login
  7. } from '@/config/html5Utils';
  8. // #endif
  9. let code = "";
  10. let loginStart = true;
  11. let userInfo = {
  12. token: ""
  13. };
  14. let lastPageUrl = "";
  15. // 会员号密码一级手机号验证码登录
  16. async function login(options = {}) {
  17. console.log(options);
  18. // 请求登录
  19. let res = await $http.post(options.url, options.data);
  20. console.log(res);
  21. if (res.code != '200') {
  22. return Promise.resolve(res);
  23. } else {
  24. if (options.data.account) {
  25. uni.setStorageSync('username', options.data.account);
  26. uni.setStorageSync('password', options.data.password);
  27. let loginStatus = await getLoginUserInfo(res.data, options.data.account);
  28. if (loginStatus) {
  29. // 跳转到首页
  30. uni.switchTab({
  31. url: "/pages/index/index"
  32. })
  33. setTimeout(() => {
  34. uni.showToast({
  35. title: '登录成功',
  36. icon: 'none'
  37. });
  38. }, 500);
  39. }
  40. } else {
  41. uni.setStorageSync('username', res.data.userId);
  42. let loginStatus = await getLoginUserInfo(res.data.token, res.data.userId);
  43. if (loginStatus) {
  44. // 跳转到首页
  45. uni.switchTab({
  46. url: "/pages/index/index"
  47. })
  48. setTimeout(() => {
  49. uni.showToast({
  50. title: '登录成功',
  51. icon: 'none'
  52. });
  53. }, 500);
  54. }
  55. }
  56. }
  57. }
  58. // 微信小程序登录
  59. function onLogin(type = "judge", callback) {
  60. //判断登录状态
  61. if (loginStart) {
  62. lastPageUrl = "";
  63. loginStart = false;
  64. const _this = this;
  65. let platform;
  66. // #ifdef MP-WEIXIN
  67. platform = 'weixin';
  68. // #endif
  69. // #ifdef MP-ALIPAY
  70. platform = 'alipay';
  71. // #endif
  72. // #ifdef MP-BAIDU
  73. platform = 'baidu';
  74. // #endif
  75. uni.login({
  76. provider: platform,
  77. success: function(loginRes) {
  78. if (loginRes.errMsg == 'login:ok') {
  79. code = loginRes.code;
  80. // 获取用户信息
  81. uni.getUserInfo({
  82. provider: platform,
  83. success: function(infoRes) {
  84. getUserInfo(infoRes, "", callback);
  85. },
  86. fail() {
  87. if (type != "try") {
  88. store.commit('setLoginPopupShow', true);
  89. Object.defineProperty(userInfo, "token", {
  90. get: function(val) {
  91. return {};
  92. },
  93. set: function(newVal) {
  94. callback && callback();
  95. }
  96. });
  97. setTimeout(() => {
  98. loginStart = true;
  99. }, 2000);
  100. } else {
  101. loginStart = true;
  102. }
  103. }
  104. });
  105. }
  106. }
  107. });
  108. }
  109. }
  110. //微信小程序获取用户信息
  111. function getUserInfo(info, type, callback) {
  112. let httpData = {
  113. wxSmallCode: code, //小程序code
  114. iv: info.iv, //小程序加密算法的初始向量
  115. encryptedData: info.encryptedData //包括敏感数据在内的完整用户信息的加密数据
  116. };
  117. // store.state.chatScenesInfo里面是小程序二维码附带的信息
  118. if (store.state.chatScenesInfo.recommendCode) {
  119. // 推荐码
  120. httpData.recommendUid = store.state.chatScenesInfo.recommendCode;
  121. }
  122. $http.post('api/open/v1/login', httpData).then(res => {
  123. loginStart = true;
  124. store.commit('setUserInfo', res);
  125. if (type == "authorized") {
  126. userInfo.token = res.token;
  127. store.commit('setLoginPopupShow', false);
  128. } else {
  129. callback && callback();
  130. }
  131. uni.showToast({
  132. title: "登录成功"
  133. });
  134. }, err => {
  135. loginStart = true;
  136. });
  137. }
  138. //判断是否登录(所有端)
  139. function judgeLogin(callback, type = "judge") {
  140. if (store.state.chatScenesInfo.scene == 1154) {
  141. uni.showToast({
  142. title: '请前往小程序使用完整服务',
  143. icon: "none"
  144. });
  145. } else {
  146. let storeToken = store.state.token;
  147. if (!storeToken) { // nvue页面读取不到vuex里面数据,将取缓存
  148. storeToken = uni.getStorageSync("token");
  149. }
  150. if (type != "force" && storeToken) {
  151. callback();
  152. } else {
  153. // #ifdef MP
  154. // onLogin(type, callback);
  155. // #endif
  156. // #ifdef APP-PLUS
  157. uni.showModal({
  158. title: "登录提示",
  159. content: "此时此刻需要您登录喔~",
  160. confirmText: "去登录",
  161. cancelText: "再逛会",
  162. success: (res) => {
  163. if (res.confirm) {
  164. uni.reLaunch({
  165. url: "/pages/login/login"
  166. });
  167. }
  168. }
  169. });
  170. // #endif
  171. // #ifdef H5
  172. h5Login(type, () => {
  173. callback();
  174. });
  175. // #endif
  176. }
  177. }
  178. }
  179. //获取登录用户信息
  180. async function getLoginUserInfo(token, userId) {
  181. let Author = "Bearer" + " " + token;
  182. let userInfoRes = await $http.get('/user/loginUser', {}, {
  183. header: {
  184. Authorization: Author,
  185. }
  186. }); //用户信息
  187. store.commit('setUserModules', {
  188. title: 'userInfo',
  189. data: {
  190. sysUser: {
  191. ...userInfoRes.data
  192. }
  193. }
  194. })
  195. store.commit('setUserModules', {
  196. title: 'userLoginId',
  197. data: userInfoRes.data.id
  198. })
  199. store.commit('setUserModules', {
  200. title: 'token',
  201. data: Author
  202. })
  203. let Institutionalsources = await $http.get('/recruiting/queryType', {}, {
  204. header: {
  205. Authorization: Author,
  206. }
  207. });
  208. store.commit('setUserModules', {
  209. title: 'sources',
  210. data: Institutionalsources.data
  211. })
  212. // if (userInfoRes.data.name == '游客') {
  213. // uni.reLaunch({
  214. // url: "/pages/user/certify"
  215. // })
  216. // return false;
  217. // }
  218. // if (userInfoRes.data.sysUser.usertype == 'A') {
  219. // // 清除状态
  220. // uni.showToast({
  221. // title: '后线人员暂无登录权限',
  222. // icon: 'none',
  223. // duration: 2000
  224. // });
  225. // return false;
  226. // }
  227. // let userInfoCheckRes = await $http.get('/esmUserInternalcheck/findById?checkid=' +userId, {}, {
  228. // header: {
  229. // Authorization: Author
  230. // }
  231. // }); //用户临时信息
  232. // store.commit('setUserModules', {
  233. // title: 'userCheckInfo',
  234. // data: userInfoCheckRes.data
  235. // })
  236. // let avatar;
  237. // let avatars = await $http.get("/insTaskImage/findById?imgtype=avatar&taskid=" + userId, {}, {
  238. // header: {
  239. // Authorization: Author
  240. // }
  241. // })
  242. // if ((avatars.code == '200') && (avatars.data.imgList.length > 0)) {
  243. // avatar = avatars.data.imgList[0];
  244. // } else {
  245. // avatar = "/static/common/avatar.png";
  246. // }
  247. // store.commit('setUserModules', {
  248. // title: 'avatar',
  249. // data: avatar
  250. // })
  251. return true;
  252. // let loginStatus = await getStaffStatus(userId);
  253. // if (loginStatus) {
  254. // return true;
  255. // } else {
  256. // return false;
  257. // }
  258. }
  259. // 查询人员状态status,1为审核通过 2为新注册人员(新注册人员又分为5个状态 1:未认证 2:认证中 3:驳回认证 4:退回修改 5正常)
  260. async function getStaffStatus(userId) {
  261. // 人员状态:1正常 0删除 2新注册 3审批不通过
  262. //查询登录人的状态信息
  263. let res = await $http.get('/user/findByName?name=' + userId)
  264. //默认人员状态是未认证状态
  265. // var status = '1';
  266. // if (res.data.status == '1') {
  267. // status = '5';
  268. // let userInfoRes = await $http.get('/user/loginUser'); //用户信息
  269. // store.commit('setUserModules', {
  270. // title: 'userInfo',
  271. // data: {
  272. // sysUser:{...userInfoRes.data}
  273. // }
  274. // })
  275. // } else {
  276. // let userInfoRes = await $http.get('/user/loginUser'); //用户信息
  277. // store.commit('setUserModules', {
  278. // title: 'userInfo',
  279. // data: {
  280. // sysUser:{...userInfoRes.data}
  281. // }
  282. // })
  283. // let res1 = await $http.get('/esmUserInternalcheck/findById?checkid=' + userId);
  284. // store.commit('setUserModules', {
  285. // title: 'userCheckInfo',
  286. // data: res1.data
  287. // })
  288. // }
  289. store.commit('setUserModules', {
  290. title: 'userStatus',
  291. data: status
  292. })
  293. return true;
  294. }
  295. export {
  296. login,
  297. onLogin,
  298. getUserInfo,
  299. judgeLogin,
  300. getStaffStatus
  301. }