index.vue 14 KB

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