index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. console.log('第三方回调')
  156. var appId = "ww4e237fd6abb635af";//企业微信第三方的SUIT ID
  157. var url = "http://worktime.ttkuaiban.com/api/corpWXAuth";//授权回调页面
  158. 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";
  159. window.location.href = weixinUrl;
  160. },
  161. loginByUserId(userId) {
  162. this.$axios.get("/user/loginByUserId", {params:{userId:userId}})
  163. .then(res => {
  164. console.log(res);
  165. if (res == null) {
  166. } else if(res.errcode != null) {
  167. } else {
  168. //获取openId
  169. if (res.data != null) {
  170. // alert('赋值成功' + JSON.stringify(res.data))
  171. localStorage.setItem('userInfo', JSON.stringify(res.data))
  172. this.$store.commit("updateLogin", true);
  173. this.user = res.data;
  174. // alert('本地存储的值' + localStorage.getItem('userInfo'))
  175. // window.location.href = '/#/index';
  176. this.$router.push("/index");
  177. }
  178. }
  179. }).catch(err=> {
  180. alert('err=' + err);
  181. });
  182. },
  183. automaticLogin(jobNumber, token) {
  184. this.$axios.post("/user/loginAdminByThirdParty", {jobNumber:jobNumber, token:token})
  185. .then(res => {
  186. if(res.code == "ok") {
  187. if(res.data.moduleList.length == 0){
  188. this.$toast.fail(`请联系管理员 ${res.data.roleName} 分配权限`);
  189. return
  190. }
  191. var user = res.data;
  192. localStorage.userInfo = JSON.stringify(res.data);
  193. this.$router.push("/index")
  194. } else {
  195. this.$toast.fail(res.msg);
  196. }
  197. }).catch(err=> {this.$toast.clear();});
  198. }
  199. },
  200. created() {
  201. // if (localStorage.userInfo != null) {
  202. // this.$router.push("/index").catch(err => { console.log(err, '错误4')});
  203. // }
  204. localStorage.clear()
  205. // 米莱的用工号登录
  206. let windowHerf = window.location.href
  207. if(windowHerf.indexOf('?jobNumber') != '-1') {
  208. let jobNumber = windowHerf.split('jobNumber=')[1].split('&token=')[0]
  209. let token = windowHerf.split('&token=')[1]
  210. this.automaticLogin(jobNumber, token)
  211. }
  212. },
  213. mounted() {
  214. var ua = navigator.userAgent.toLowerCase();
  215. if (ua.indexOf("wxwork") > 0) {
  216. this.isCorpWX = true;
  217. } else if (ua.indexOf("micromessenger") > 0) {
  218. this.isWX = true;
  219. }
  220. let href = window.location.href;
  221. //优先处理企业微信工作台点击进入,后台已经处理过,有userId传过来了
  222. if (this.isCorpWX && href.indexOf("userId") > 0) {
  223. //判断企业微信,是否存在授权
  224. var loginUserId = href.substring(href.indexOf("userId=")+"userId=".length);
  225. if (loginUserId.includes('#/')) {
  226. loginUserId = loginUserId.substring(0, loginUserId.indexOf('#/'));
  227. }
  228. this.loginByUserId(loginUserId);
  229. } else {
  230. if (localStorage.userInfo != null && !this.isCorpWX) {
  231. this.$router.push("/index").catch(err => { console.log(err, '错误5')});
  232. } else {
  233. console.log('判断企业微信是否授权',this.isCorpWX || this.isWX)
  234. if (this.isCorpWX || this.isWX) {
  235. //判断企业微信,是否存在授权
  236. if (href.includes("com/?code")) {
  237. this.bindIfNessary();
  238. } else {
  239. if (href.indexOf('hasTriedAutoLogin') == -1) {
  240. this.tryAutoLogin();
  241. } else if (href.indexOf("userId") > 0) {
  242. //后台经过验证后,重定向过来带上了userId
  243. var loginUserId = href.substring(href.indexOf("userId=")+"userId=".length);
  244. if (loginUserId.includes('#/')) {
  245. loginUserId = loginUserId.substring(0, loginUserId.indexOf('#/'));
  246. }
  247. this.loginByUserId(loginUserId);
  248. }
  249. }
  250. } else {
  251. //检查环境,如果是钉钉有$CORPID$
  252. var key = '?corpid=';
  253. var jumpkey = '&jumpto=';
  254. var url = location.href;
  255. var that = this;
  256. if (url.indexOf(key) > 0) {
  257. var corpId = ''
  258. if(url.indexOf(jumpkey) > 0){
  259. corpId = url.substring(url.indexOf(key)+key.length,url.indexOf(jumpkey));
  260. }else{
  261. corpId = url.substring(url.indexOf(key)+key.length,url.indexOf('#'));
  262. }
  263. this.isDingding = true
  264. dd.ready(function() {
  265. dd.runtime.permission.requestAuthCode({
  266. corpId: corpId, // 企业id
  267. onSuccess: function (info) {
  268. var code = info.code // 通过该免登授权码可以获取用户身份
  269. that.loginByCode(code, corpId);
  270. }});
  271. });
  272. }
  273. }
  274. }
  275. }
  276. }
  277. };
  278. </script>
  279. <style lang="less" scoped>
  280. .logo {
  281. font-size: 100px !important;
  282. margin-bottom: 150px;
  283. }
  284. /* 本页公共样式 */
  285. .login {
  286. height: 100vh;
  287. background-color: #fff;
  288. }
  289. header {
  290. text-align: center;
  291. }
  292. // 手机号码
  293. .login_logo {
  294. margin: 55px 0 30px;
  295. }
  296. .login_title {
  297. font-size: 24px;
  298. color: #20a0ff;
  299. margin: 0 0 10px 0;
  300. }
  301. .login_subtitle {
  302. font-size: 14px;
  303. color: #afafaf;
  304. margin: 0 0 40px 0;
  305. }
  306. .login_form {
  307. .form_input {
  308. margin: 0 0 30px 0;
  309. }
  310. .form_btn {
  311. width: 80%;
  312. margin: 0 auto !important;
  313. button {
  314. background-color: #20a0ff;
  315. }
  316. }
  317. .form_jump {
  318. margin: 20px 0 0 0;
  319. color: #20a0ff;
  320. font-size: 14px;
  321. }
  322. }
  323. </style>