login.js 6.6 KB

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