login.js 6.7 KB

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