login.js 6.9 KB

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