index.vue 13 KB

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