index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <template>
  2. <header class="login">
  3. <van-image class="login_logo" width="100" height="100" :src="require('../../assets/img/login_logo.png')"/>
  4. <div class="login_title">工时管家</div>
  5. <div class="login_subtitle">欢迎使用移动端工时管家</div>
  6. <van-form class="login_form" @submit="login">
  7. <van-field class="form_input" v-model="form.username" name="username" label="账号" placeholder="请输入账号" :rules="rules.username" />
  8. <van-field class="form_input" v-model="form.password" type="password" name="password" label="密码" placeholder="请输入密码" :rules="rules.password" />
  9. <div class="form_btn" style="margin: 16px;">
  10. <van-button round block type="info" native-type="submit"> 登录 </van-button>
  11. </div>
  12. <div class="form_jump" v-on:click="jumpTo" v-if="!isCorpWX && !isDingding">企业注册</div>
  13. </van-form>
  14. </header>
  15. </template>
  16. <script>
  17. import { constants } from "crypto";
  18. import * as dd from 'dingtalk-jsapi';
  19. import { obtainCorrespondingConfigurationInformation } from '../../utils/appidConfiguration';
  20. export default {
  21. data() {
  22. return {
  23. isDingding: false,
  24. isCorpWX:false,
  25. isWX:false,
  26. defaultHeight: '0', //默认屏幕高度
  27. nowHeight: '0', //实时屏幕高度
  28. form: {
  29. username: "",
  30. password: "",
  31. },
  32. rules: {
  33. username: [{ required: true, message: '请输入账号' }],
  34. password: [{ required: true, message: '请输入密码' }]
  35. },
  36. };
  37. },
  38. methods: {
  39. login() {
  40. const toast = this.$toast.loading({
  41. forbidClick: true,
  42. duration: 0
  43. });
  44. this.$axios.post("/user/loginAdmin", this.form)
  45. .then(res => {
  46. if(res.code == "ok") {
  47. this.$toast.clear();
  48. this.$toast.success('登录成功');
  49. let user = res.data;
  50. this.$store.commit("updateLogin", true);
  51. localStorage.userInfo = JSON.stringify(user);
  52. this.$router.push("/index").catch(err => { console.log(err, '错误1')});
  53. //强制刷新,避免index页面中的mounted不执行
  54. window.location.reload();
  55. } else {
  56. this.$toast.clear();
  57. // this.$router.push('/expire')
  58. // console.log('skip');
  59. // return
  60. this.$toast.fail(res.msg);
  61. }
  62. }).catch(err=> {this.$toast.clear();});
  63. },
  64. jumpTo() {
  65. this.$router.push("/register").catch(err => { console.log(err, '错误2')});
  66. },
  67. loginByCode(code, corpId) {
  68. this.$axios.post("/dingding/getUserByCode", {code:code, corpid:corpId})
  69. .then(res => {
  70. if(res.code == "ok") {
  71. let user = res.data;
  72. this.$store.commit("updateLogin", true);
  73. localStorage.userInfo = JSON.stringify(user);
  74. this.$router.push("/index").catch(err => { console.log(err, '错误3')});
  75. //强制刷新,避免index页面中的mounted不执行
  76. // window.location.reload();
  77. } else {
  78. this.$toast.fail(res.msg);
  79. }
  80. }).catch(err=> {this.$toast.clear();});
  81. },
  82. bindIfNessary() {
  83. let href = window.location.href;
  84. var requestUrl = "";
  85. if (this.isCorpWX) {//优先检查企业微信环境
  86. requestUrl = "/wxcorp/bindCorpWeiXin";
  87. } else if (this.isWX) {
  88. requestUrl = "/wechat/bindWeiXin";
  89. }
  90. if (requestUrl.length > 0) {
  91. // localStorage.openId = 'o1L3L5lOrOl3_UEJjONaoT2Rne1I';
  92. //会自动跳转到首页
  93. // let href = 'http://hq.tangusoft.com/?code=011Ptjgc2rx1eI09Irgc2Rvsgc2PtjgF&state=1#/index';
  94. if (href.includes("com/?code")) { //url包括 com/?code 证明为从微信跳转回来的
  95. var url = href; //vue自动在末尾加了 #/ 符号,截取去掉
  96. var jingPosit = url.indexOf("com/") + 4; //获取域名结束的位置
  97. // var urlLeft = url.substring(0, jingPosit);//url左侧部分
  98. var urlRight = url.substring(jingPosit, url.length); //url右侧部分
  99. console.log('urlRight=' + urlRight);
  100. urlRight = urlRight.substring(0, urlRight.indexOf('#/'));
  101. // window.location = urlLeft + "#/home" + urlRight;//拼接跳转
  102. //获取code
  103. var code = urlRight.substring('?code='.length,urlRight.indexOf('&state='));
  104. var passUserId = urlRight.substring(urlRight.indexOf('&state=')+'&state='.length);
  105. if (passUserId == '0') {
  106. //自动登录的回调
  107. this.$axios.get('/wxcorp/corpWeiXinLogin', {params:{code:code}})
  108. .then(res => {
  109. if (res == null) {
  110. } else if(res.errcode != null) {
  111. //报错了
  112. console.log(res.errmsg);
  113. } else {
  114. //获取openId
  115. if (res.data != null && ((this.isWX && res.data.wxOpenid != undefined)
  116. || (this.isCorpWX && res.data.corpwxUserid != undefined))) {
  117. localStorage.userInfo = JSON.stringify(res.data);
  118. console.log('登录成功');
  119. this.user = res.data;
  120. window.location.href = '/#/index';
  121. }
  122. }
  123. }).catch(err=> {
  124. alert('err=' + err);
  125. });
  126. } else {
  127. //绑定微信账号的回调
  128. //调用后台接口,注册用户
  129. this.$axios.get(requestUrl, {params:{code:code, userId: passUserId}})
  130. .then(res => {
  131. console.log(res);
  132. if (res == null) {
  133. this.$toast.fail('绑定失败');
  134. } else if(res.errcode != null) {
  135. //报错了
  136. console.log(res.errmsg);
  137. } else {
  138. //获取openId
  139. if (res.data != null && ((this.isWX && res.data.wxOpenid != undefined)
  140. || (this.isCorpWX && res.data.corpwxUserid != undefined))) {
  141. // localStorage.userInfo = JSON.stringify(res.data);
  142. localStorage.setItem('userInfo', JSON.stringify(res.data))
  143. console.log('绑定成功');
  144. this.user = res.data;
  145. window.location.href = '/#/my/center';
  146. }
  147. }
  148. }).catch(err=> {
  149. alert('err=' + err);
  150. });
  151. }
  152. }
  153. }
  154. },
  155. tryAutoLogin() {
  156. const rowConfig = obtainCorrespondingConfigurationInformation()
  157. var appId = "wx749c84daac654e1e";//工时管家公众号
  158. var url = "http://mobworktime.ttkuaiban.com/api/wechat/loginByWXCode";//工时管家公众号授权回调页面
  159. // if (this.isCorpWX) {
  160. // appId = "ww4e237fd6abb635af"; //企业微信第三方的SUIT ID
  161. // url = "http://worktime.ttkuaiban.com/api/corpWXAuth";//授权回调页面
  162. // }
  163. // var appId = "ww4e237fd6abb635af";//企业微信第三方的SUIT ID
  164. // var url = "http://worktime.ttkuaiban.com/api/corpWXAuth";//授权回调页面
  165. var weixinUrl = this.isCorpWX ? rowConfig.authorizationCallback : "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+"&redirect_uri="+encodeURI(url)+"&response_type=code&scope=snsapi_base&state=0#wechat_redirect";
  166. window.location.href = weixinUrl;
  167. },
  168. tryDingDingUrlRedirect() {
  169. let currentAddress = window.location.href+'?ddLoginUnique=true';
  170. let ddFixedPrefix = 'http://auth.dingtalk.com/login?redirectUri='
  171. let ddFixedUrl = 'https://login.dingtalk.com/oauth2/auth?response_type=code&client_id=dingwa4tibze6jwz7mgv&scope=openid&state=dddd&redirect_uri=' + encodeURIComponent(`${ddFixedPrefix}${currentAddress}`)
  172. window.location.href = ddFixedUrl;
  173. // window.location.href = window.location.href+'?ddLoginUnique=true';
  174. },
  175. loginByUserId(userId) {
  176. this.$axios.get("/user/loginByUserId", {params:{userId:userId}})
  177. .then(res => {
  178. console.log(res);
  179. if (res == null) {
  180. } else if(res.errcode != null) {
  181. } else {
  182. //获取openId
  183. if (res.data != null) {
  184. // alert('赋值成功' + JSON.stringify(res.data))
  185. localStorage.setItem('userInfo', JSON.stringify(res.data))
  186. this.$store.commit("updateLogin", true);
  187. this.user = res.data;
  188. // alert('本地存储的值' + localStorage.getItem('userInfo'))
  189. // window.location.href = '/#/index';
  190. this.$router.push("/index");
  191. }
  192. }
  193. }).catch(err=> {
  194. alert('err=' + err);
  195. });
  196. },
  197. automaticLogin(jobNumber, token) {
  198. this.$axios.post("/user/loginAdminByThirdParty", {jobNumber:jobNumber, token:token})
  199. .then(res => {
  200. if(res.code == "ok") {
  201. if(res.data.moduleList.length == 0){
  202. this.$toast.fail(`请联系管理员 ${res.data.roleName} 分配权限`);
  203. return
  204. }
  205. var user = res.data;
  206. localStorage.userInfo = JSON.stringify(res.data);
  207. this.$router.push("/index")
  208. } else {
  209. this.$toast.fail(res.msg);
  210. }
  211. }).catch(err=> {this.$toast.clear();});
  212. },
  213. // 飞书登录
  214. flyingBook(appid, code) {
  215. this.$axios.post('/feishu-info/loginByFeishu', {appId: appid,code: code})
  216. .then(res => {
  217. if(res.code == "ok") {
  218. let user = res.data;
  219. this.$store.commit("updateLogin", true);
  220. localStorage.userInfo = JSON.stringify(user);
  221. this.$router.push("/index").catch(err => { console.log(err, '错误3')});
  222. //强制刷新,避免index页面中的mounted不执行
  223. // window.location.reload();
  224. } else {
  225. this.$toast.fail(res.msg);
  226. }
  227. }).catch(err=> {this.$toast.clear();});
  228. }
  229. },
  230. created() {
  231. if (localStorage.userInfo == 'undefined') localStorage.removeItem('userInfo');
  232. if (localStorage.userInfo != null) {
  233. this.$router.push("/index").catch(err => { console.log(err, '错误4')});
  234. }
  235. // localStorage.clear()
  236. // 米莱的用工号登录
  237. let windowHerf = window.location.href
  238. if(windowHerf.indexOf('?jobNumber') != '-1') {
  239. let jobNumber = windowHerf.split('jobNumber=')[1].split('&token=')[0]
  240. let token = windowHerf.split('&token=')[1]
  241. this.automaticLogin(jobNumber, token)
  242. }
  243. },
  244. mounted() {
  245. var ua = navigator.userAgent.toLowerCase();
  246. if (ua.indexOf("wxwork") > 0) {
  247. this.isCorpWX = true;
  248. } else if (ua.indexOf("micromessenger") > 0) {
  249. this.isWX = true;
  250. }
  251. let href = window.location.href;
  252. //优先处理企业微信工作台点击进入,后台已经处理过,有userId传过来了
  253. if (href.indexOf("userId") > 0) {
  254. //判断企业微信,是否存在授权
  255. var loginUserId = href.substring(href.indexOf("userId=")+"userId=".length);
  256. if (loginUserId.includes('#/')) {
  257. loginUserId = loginUserId.substring(0, loginUserId.indexOf('#/'));
  258. }
  259. this.loginByUserId(loginUserId);
  260. } else {
  261. if (localStorage.userInfo != null && !this.isCorpWX) {
  262. this.$router.push("/index").catch(err => { console.log(err, '错误5')});
  263. } else {
  264. console.log('判断企业微信是否授权',this.isCorpWX || this.isWX)
  265. if (this.isCorpWX || this.isWX) {
  266. //判断企业微信,是否存在授权
  267. if (href.includes("com/?code")) {
  268. this.bindIfNessary();
  269. } else {
  270. if (href.indexOf('hasTriedAutoLogin') == -1) {
  271. this.tryAutoLogin();
  272. } else if (href.indexOf("userId") > 0) {
  273. //后台经过验证后,重定向过来带上了userId
  274. var loginUserId = href.substring(href.indexOf("userId=")+"userId=".length);
  275. if (loginUserId.includes('#/')) {
  276. loginUserId = loginUserId.substring(0, loginUserId.indexOf('#/'));
  277. }
  278. this.loginByUserId(loginUserId);
  279. }
  280. }
  281. } else {
  282. console.log(window.location.href)
  283. console.log(href.indexOf("corpid") > 0, '<==== href.indexOf("corpid") > 0')
  284. console.log(!(window.location.href.indexOf("ddLoginUnique") > 0), '!(window.location.href.indexOf("ddLoginUnique") > 0)')
  285. //检查环境,如果是钉钉有$CORPID$
  286. if(href.indexOf("corpid") > 0) {
  287. if(!(window.location.href.indexOf("ddLoginUnique") > 0)) {
  288. this.tryDingDingUrlRedirect()
  289. // return
  290. }
  291. var key = '?corpid=';
  292. var jumpkey = '&jumpto=';
  293. var url = location.href;
  294. var that = this;
  295. if (url.indexOf(key) > 0) {
  296. var corpId = ''
  297. if(url.indexOf(jumpkey) > 0){
  298. corpId = url.substring(url.indexOf(key)+key.length,url.indexOf(jumpkey));
  299. }else{
  300. corpId = url.substring(url.indexOf(key)+key.length,url.indexOf('#'));
  301. }
  302. this.isDingding = true
  303. dd.ready(function() {
  304. dd.runtime.permission.requestAuthCode({
  305. corpId: corpId, // 企业id
  306. onSuccess: function (info) {
  307. var code = info.code // 通过该免登授权码可以获取用户身份
  308. that.loginByCode(code, corpId);
  309. }});
  310. });
  311. }
  312. } else {
  313. // 飞书登陆
  314. if(href.indexOf("appId") > 0) {
  315. console.log('执行到这里---飞书')
  316. let arr = href.split('appId=')[1]
  317. let str = arr.split('#')[0]
  318. console.log(str, window)
  319. window.h5sdk.ready(() => { // ready方法不需要每次都调用
  320. tt.requestAuthCode({
  321. appId: str,
  322. success: (info) => {
  323. console.info(info.code, '飞书 code')
  324. this.flyingBook(str, info.code)
  325. },
  326. fail: (error) => {
  327. console.error(error)
  328. }
  329. });
  330. });
  331. }
  332. }
  333. }
  334. }
  335. }
  336. },
  337. };
  338. </script>
  339. <style lang="less" scoped>
  340. .logo {
  341. font-size: 100px !important;
  342. margin-bottom: 150px;
  343. }
  344. /* 本页公共样式 */
  345. .login {
  346. height: 100vh;
  347. background-color: #fff;
  348. }
  349. header {
  350. text-align: center;
  351. }
  352. // 手机号码
  353. .login_logo {
  354. margin: 55px 0 30px;
  355. }
  356. .login_title {
  357. font-size: 24px;
  358. color: #20a0ff;
  359. margin: 0 0 10px 0;
  360. }
  361. .login_subtitle {
  362. font-size: 14px;
  363. color: #afafaf;
  364. margin: 0 0 40px 0;
  365. }
  366. .login_form {
  367. .form_input {
  368. margin: 0 0 30px 0;
  369. }
  370. .form_btn {
  371. width: 80%;
  372. margin: 0 auto !important;
  373. button {
  374. background-color: #20a0ff;
  375. }
  376. }
  377. .form_jump {
  378. margin: 20px 0 0 0;
  379. color: #20a0ff;
  380. font-size: 14px;
  381. }
  382. }
  383. </style>