login.js 7.8 KB

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