login.js 8.1 KB

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