login.js 6.6 KB

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