index.vue 19 KB

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