login.js 7.2 KB

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