index.vue 16 KB

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