index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <template>
  2. <view class="content">
  3. <view class="head">
  4. <text>登录</text>
  5. </view>
  6. <view class="login-wrap">
  7. <view class="username item">
  8. <text>账号:</text>
  9. <!-- <text></text> -->
  10. <uni-easyinput :inputBorder="false" class="login_input" type="text" v-model="username" placeholder="请输入账号" />
  11. </view>
  12. <view class="password item">
  13. <text>密码:</text>
  14. <uni-easyinput :inputBorder="false" class="login_input" type="password" v-model="password" placeholder="请输入密码">
  15. </uni-easyinput>
  16. </view>
  17. <!-- <view class="tip">
  18. <text>没有账号去注册</text>
  19. </view> -->
  20. <button type="primary" @click="loginSubmit" class="submit-BT">登录</button>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. import store from '@/store/index.js'
  26. import { login ,wxWorkLogin} from '@/api/user';
  27. import { setToken } from '@/libs/auth';
  28. export default {
  29. data() {
  30. return {
  31. username: "",
  32. password: "",
  33. }
  34. },
  35. onLoad({username,password}) {
  36. if(username){
  37. this.username=username;
  38. this.password=password;
  39. }
  40. },
  41. methods: {
  42. loginSubmit() {
  43. let username = (this.username).trim();
  44. let password = (this.password).trim();
  45. if (username.length < 1) {
  46. uni.showToast({
  47. title: '请填写账号',
  48. icon: "error"
  49. })
  50. return;
  51. }
  52. if (password.length < 1) {
  53. uni.showToast({
  54. title: '请填写密码',
  55. icon: "error"
  56. })
  57. return;
  58. }
  59. login({
  60. username,
  61. password
  62. }).then((res) => {
  63. let token = res.data.accessToken;
  64. let accountInfo = JSON.stringify(res.data.user);
  65. const userId=res.data.user.userId;
  66. setToken(token);
  67. uni.setStorageSync('accountInfo', accountInfo);
  68. store.commit('setToken', token);
  69. let info=uni.getSystemInfoSync();
  70. if(info.environment==="wxwork"){
  71. if(!userId)
  72. {
  73. uni.showToast({
  74. icon:"none",
  75. title:"未获取到userId!"
  76. })
  77. return;
  78. }
  79. wx.qy.login({
  80. success:(res)=>{
  81. console.log(res)
  82. let code=res.code;
  83. wxWorkLogin({
  84. userId,code
  85. }).then((res)=>{
  86. if(res.code===0){
  87. uni.switchTab({
  88. url: '/pages/index/index'
  89. })
  90. }else{
  91. uni.showToast({
  92. icon:"none",
  93. title:"wxWork登录失败"
  94. })
  95. }
  96. })
  97. }
  98. })
  99. }else{
  100. //#ifdef APP-PLUS
  101. this.jpushInit(userId);
  102. return;
  103. //#endif
  104. uni.switchTab({
  105. url: '/pages/index/index'
  106. })
  107. }
  108. }).catch((res)=>{
  109. console.log(res)
  110. })
  111. },
  112. //极光推送初始化
  113. jpushInit(userId){
  114. //#ifdef APP-PLUS
  115. let jpushModule=store.state.jpush;
  116. jpushModule.setAlias({
  117. 'alias':`push${userId}`
  118. })
  119. setTimeout(()=>{
  120. uni.switchTab({
  121. url: '/pages/index/index'
  122. })
  123. },1000)
  124. //#endif
  125. }
  126. }
  127. }
  128. </script>
  129. <style lang="scss" scoped>
  130. .content {
  131. .head {
  132. font-size: 50upx;
  133. text-align: center;
  134. padding: 40upx 0;
  135. letter-spacing: 2upx;
  136. }
  137. .login-wrap {
  138. width: 90%;
  139. margin: 50upx auto 0;
  140. box-sizing: border-box;
  141. padding: 20upx;
  142. border-radius: 8upx;
  143. .item {
  144. display: flex;
  145. justify-content: flex-start;
  146. align-items: center;
  147. height: 100upx;
  148. font-size: 32upx;
  149. line-height: 100upx;
  150. width: 150%;
  151. input {
  152. font-size: 32upx;
  153. height: 100upx;
  154. border: 0;
  155. }
  156. .login_input {
  157. width: 580upx;
  158. border: none;
  159. }
  160. }
  161. .tip {
  162. text-align: right;
  163. padding: 18upx 0;
  164. font-size: 28upx;
  165. }
  166. .submit-BT {
  167. border-radius: 16upx;
  168. margin-top: 50upx;
  169. background-color: var(--sysblue);
  170. }
  171. }
  172. }
  173. </style>