index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. if(userss.companyId == '3449') {
  50. alert(JSON.stringify(res.data))
  51. }
  52. this.$store.commit("updateLogin", true);
  53. localStorage.userInfo = JSON.stringify(user);
  54. this.$router.push("/index").catch(err => { console.log(err, '错误1')});
  55. //强制刷新,避免index页面中的mounted不执行
  56. window.location.reload();
  57. } else {
  58. this.$toast.clear();
  59. // this.$router.push('/expire')
  60. // console.log('skip');
  61. // return
  62. this.$toast.fail(res.msg);
  63. }
  64. }).catch(err=> {this.$toast.clear();});
  65. },
  66. jumpTo() {
  67. this.$router.push("/register").catch(err => { console.log(err, '错误2')});
  68. },
  69. loginByCode(code, corpId) {
  70. this.$axios.post("/dingding/getUserByCode", {code:code, corpid:corpId})
  71. .then(res => {
  72. if(res.code == "ok") {
  73. let user = res.data;
  74. this.$store.commit("updateLogin", true);
  75. localStorage.userInfo = JSON.stringify(user);
  76. this.$router.push("/index").catch(err => { console.log(err, '错误3')});
  77. //强制刷新,避免index页面中的mounted不执行
  78. // window.location.reload();
  79. } else {
  80. this.$toast.fail(res.msg);
  81. }
  82. }).catch(err=> {this.$toast.clear();});
  83. },
  84. bindIfNessary() {
  85. let href = window.location.href;
  86. var requestUrl = "";
  87. if (this.isCorpWX) {//优先检查企业微信环境
  88. requestUrl = "/wxcorp/bindCorpWeiXin";
  89. } else if (this.isWX) {
  90. requestUrl = "/wechat/bindWeiXin";
  91. }
  92. if (requestUrl.length > 0) {
  93. // localStorage.openId = 'o1L3L5lOrOl3_UEJjONaoT2Rne1I';
  94. //会自动跳转到首页
  95. // let href = 'http://hq.tangusoft.com/?code=011Ptjgc2rx1eI09Irgc2Rvsgc2PtjgF&state=1#/index';
  96. if (href.includes("com/?code")) { //url包括 com/?code 证明为从微信跳转回来的
  97. var url = href; //vue自动在末尾加了 #/ 符号,截取去掉
  98. var jingPosit = url.indexOf("com/") + 4; //获取域名结束的位置
  99. // var urlLeft = url.substring(0, jingPosit);//url左侧部分
  100. var urlRight = url.substring(jingPosit, url.length); //url右侧部分
  101. console.log('urlRight=' + urlRight);
  102. urlRight = urlRight.substring(0, urlRight.indexOf('#/'));
  103. // window.location = urlLeft + "#/home" + urlRight;//拼接跳转
  104. //获取code
  105. var code = urlRight.substring('?code='.length,urlRight.indexOf('&state='));
  106. var passUserId = urlRight.substring(urlRight.indexOf('&state=')+'&state='.length);
  107. if (passUserId == '0') {
  108. //自动登录的回调
  109. this.$axios.get('/wxcorp/corpWeiXinLogin', {params:{code:code}})
  110. .then(res => {
  111. if (res == null) {
  112. } else if(res.errcode != null) {
  113. //报错了
  114. console.log(res.errmsg);
  115. } else {
  116. //获取openId
  117. if (res.data != null && ((this.isWX && res.data.wxOpenid != undefined)
  118. || (this.isCorpWX && res.data.corpwxUserid != undefined))) {
  119. localStorage.userInfo = JSON.stringify(res.data);
  120. console.log('登录成功');
  121. this.user = res.data;
  122. window.location.href = '/#/index';
  123. }
  124. }
  125. }).catch(err=> {
  126. alert('err=' + err);
  127. });
  128. } else {
  129. //绑定微信账号的回调
  130. //调用后台接口,注册用户
  131. this.$axios.get(requestUrl, {params:{code:code, userId: passUserId}})
  132. .then(res => {
  133. console.log(res);
  134. if (res == null) {
  135. this.$toast.fail('绑定失败');
  136. } else if(res.errcode != null) {
  137. //报错了
  138. console.log(res.errmsg);
  139. } else {
  140. //获取openId
  141. if (res.data != null && ((this.isWX && res.data.wxOpenid != undefined)
  142. || (this.isCorpWX && res.data.corpwxUserid != undefined))) {
  143. // localStorage.userInfo = JSON.stringify(res.data);
  144. localStorage.setItem('userInfo', JSON.stringify(res.data))
  145. console.log('绑定成功');
  146. this.user = res.data;
  147. window.location.href = '/#/my/center';
  148. }
  149. }
  150. }).catch(err=> {
  151. alert('err=' + err);
  152. });
  153. }
  154. }
  155. }
  156. },
  157. tryAutoLogin() {
  158. console.log('第三方回调')
  159. var appId = "ww4e237fd6abb635af";//企业微信第三方的SUIT ID
  160. var url = "http://worktime.ttkuaiban.com/api/corpWXAuth";//授权回调页面
  161. 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";
  162. window.location.href = weixinUrl;
  163. },
  164. loginByUserId(userId) {
  165. this.$axios.get("/user/loginByUserId", {params:{userId:userId}})
  166. .then(res => {
  167. console.log(res);
  168. if (res == null) {
  169. } else if(res.errcode != null) {
  170. } else {
  171. //获取openId
  172. if (res.data != null) {
  173. // alert('赋值成功' + JSON.stringify(res.data))
  174. localStorage.setItem('userInfo', JSON.stringify(res.data))
  175. this.$store.commit("updateLogin", true);
  176. this.user = res.data;
  177. // alert('本地存储的值' + localStorage.getItem('userInfo'))
  178. // window.location.href = '/#/index';
  179. this.$router.push("/index");
  180. }
  181. }
  182. }).catch(err=> {
  183. alert('err=' + err);
  184. });
  185. },
  186. automaticLogin(jobNumber, token) {
  187. this.$axios.post("/user/loginAdminByThirdParty", {jobNumber:jobNumber, token:token})
  188. .then(res => {
  189. if(res.code == "ok") {
  190. if(res.data.moduleList.length == 0){
  191. this.$toast.fail(`请联系管理员 ${res.data.roleName} 分配权限`);
  192. return
  193. }
  194. var user = res.data;
  195. localStorage.userInfo = JSON.stringify(res.data);
  196. this.$router.push("/index")
  197. } else {
  198. this.$toast.fail(res.msg);
  199. }
  200. }).catch(err=> {this.$toast.clear();});
  201. }
  202. },
  203. created() {
  204. if (localStorage.userInfo != null) {
  205. this.$router.push("/index").catch(err => { console.log(err, '错误4')});
  206. }
  207. // 米莱的用工号登录
  208. let windowHerf = window.location.href
  209. if(windowHerf.indexOf('?jobNumber') != '-1') {
  210. let jobNumber = windowHerf.split('jobNumber=')[1].split('&token=')[0]
  211. let token = windowHerf.split('&token=')[1]
  212. this.automaticLogin(jobNumber, token)
  213. }
  214. },
  215. mounted() {
  216. var ua = navigator.userAgent.toLowerCase();
  217. if (ua.indexOf("wxwork") > 0) {
  218. this.isCorpWX = true;
  219. } else if (ua.indexOf("micromessenger") > 0) {
  220. this.isWX = true;
  221. }
  222. let href = window.location.href;
  223. //优先处理企业微信工作台点击进入,后台已经处理过,有userId传过来了
  224. if (this.isCorpWX && href.indexOf("userId") > 0) {
  225. //判断企业微信,是否存在授权
  226. var loginUserId = href.substring(href.indexOf("userId=")+"userId=".length);
  227. if (loginUserId.includes('#/')) {
  228. loginUserId = loginUserId.substring(0, loginUserId.indexOf('#/'));
  229. }
  230. this.loginByUserId(loginUserId);
  231. } else {
  232. if (localStorage.userInfo != null && !this.isCorpWX) {
  233. this.$router.push("/index").catch(err => { console.log(err, '错误5')});
  234. } else {
  235. console.log('判断企业微信是否授权',this.isCorpWX || this.isWX)
  236. if (this.isCorpWX || this.isWX) {
  237. //判断企业微信,是否存在授权
  238. if (href.includes("com/?code")) {
  239. this.bindIfNessary();
  240. } else {
  241. if (href.indexOf('hasTriedAutoLogin') == -1) {
  242. this.tryAutoLogin();
  243. } else if (href.indexOf("userId") > 0) {
  244. //后台经过验证后,重定向过来带上了userId
  245. var loginUserId = href.substring(href.indexOf("userId=")+"userId=".length);
  246. if (loginUserId.includes('#/')) {
  247. loginUserId = loginUserId.substring(0, loginUserId.indexOf('#/'));
  248. }
  249. this.loginByUserId(loginUserId);
  250. }
  251. }
  252. } else {
  253. //检查环境,如果是钉钉有$CORPID$
  254. var key = '?corpid=';
  255. var jumpkey = '&jumpto=';
  256. var url = location.href;
  257. var that = this;
  258. if (url.indexOf(key) > 0) {
  259. var corpId = ''
  260. if(url.indexOf(jumpkey) > 0){
  261. corpId = url.substring(url.indexOf(key)+key.length,url.indexOf(jumpkey));
  262. }else{
  263. corpId = url.substring(url.indexOf(key)+key.length,url.indexOf('#'));
  264. }
  265. this.isDingding = true
  266. dd.ready(function() {
  267. dd.runtime.permission.requestAuthCode({
  268. corpId: corpId, // 企业id
  269. onSuccess: function (info) {
  270. var code = info.code // 通过该免登授权码可以获取用户身份
  271. that.loginByCode(code, corpId);
  272. }});
  273. });
  274. }
  275. }
  276. }
  277. }
  278. }
  279. };
  280. </script>
  281. <style lang="less" scoped>
  282. .logo {
  283. font-size: 100px !important;
  284. margin-bottom: 150px;
  285. }
  286. /* 本页公共样式 */
  287. .login {
  288. height: 100vh;
  289. background-color: #fff;
  290. }
  291. header {
  292. text-align: center;
  293. }
  294. // 手机号码
  295. .login_logo {
  296. margin: 55px 0 30px;
  297. }
  298. .login_title {
  299. font-size: 24px;
  300. color: #20a0ff;
  301. margin: 0 0 10px 0;
  302. }
  303. .login_subtitle {
  304. font-size: 14px;
  305. color: #afafaf;
  306. margin: 0 0 40px 0;
  307. }
  308. .login_form {
  309. .form_input {
  310. margin: 0 0 30px 0;
  311. }
  312. .form_btn {
  313. width: 80%;
  314. margin: 0 auto !important;
  315. button {
  316. background-color: #20a0ff;
  317. }
  318. }
  319. .form_jump {
  320. margin: 20px 0 0 0;
  321. color: #20a0ff;
  322. font-size: 14px;
  323. }
  324. }
  325. </style>