login.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <view class="page-body">
  3. <view class="page-section">
  4. <view>请输入手机号</view>
  5. <view>
  6. <view>
  7. <input class="weui-input" @input="bindPhoneInput" type="number" placeholder="手机号"></input>
  8. </view>
  9. </view>
  10. </view>
  11. <view class="page-section">
  12. <view>请输入验证码</view>
  13. <view class="auth-code-container">
  14. <input @input="bindCodeInput" type="number" placeholder="验证码"></input>
  15. <button :disabled="phone.length !== 11" @tap="bindAuthCodeTap">获取验证码</button>
  16. </view>
  17. </view>
  18. <button :disabled="!(phone && code)" class="confirm-button" @tap="bindLoginTap">登录</button>
  19. </view>
  20. </template>
  21. <script>
  22. import wfc from "../../wfc/client/wfc";
  23. import wfcUIKit from "../../wfc/uikit/wfcUIKit";
  24. import Config from '../../config';
  25. import {getItem, setItem} from "../util/storageHelper";
  26. import ConnectionStatus from "../../wfc/client/connectionStatus";
  27. import appServerApi from "../../api/appServerApi";
  28. export default {
  29. data() {
  30. return {
  31. focus: false,
  32. phone: '',
  33. code: ''
  34. };
  35. },
  36. components: {},
  37. props: {},
  38. onShow() {
  39. console.log('login onShow');
  40. if((Config.APP_SERVER.indexOf('wildfirechat') >= 0 && Config.IM_SERVER_HOST.indexOf('wildfirechat') === -1)
  41. || (Config.APP_SERVER.indexOf('wildfirechat') === -1 && Config.IM_SERVER_HOST.indexOf('wildfirechat') >= 0)){
  42. console.error('!!!! 严重错误!!!! Config.APP_SERVER 和 Config.IM_SERVER_HOST要一起修改,不能一个用官方服务,一个用自己部署的服务');
  43. } else if(Config.IM_SERVER_HOST.indexOf(':') >= 0){
  44. console.error('!!!! 严重错误!!!! Config.IM_SERVER_HOST 不能包含端口,只需要 HOST 即可');
  45. } else if(Config.IM_SERVER_HOST.indexOf('http') >= 0){
  46. console.error('!!!! 严重错误!!!! Config.IM_SERVER_HOST 不能包含http,只需要 HOST 即可');
  47. }
  48. },
  49. methods: {
  50. bindPhoneInput: function (e) {
  51. this.phone = e.detail.value;
  52. },
  53. bindCodeInput: function (e) {
  54. this.code = e.detail.value;
  55. },
  56. bindLoginTap: function (e) {
  57. console.log(this.phone); // console.log(this.data.code)
  58. this.login(this.phone, this.code);
  59. },
  60. login(phone, code) {
  61. console.log('login', wfc.getClientId(), Config.getWFCPlatform());
  62. appServerApi.loginWithAuthCode(phone, code)
  63. .then(result => {
  64. console.log('login result', result);
  65. let userId = result.userId;
  66. let token = result.token;
  67. wfc.connect(userId, token);
  68. setItem('userId', userId);
  69. setItem('token', token)
  70. this.go2ConversationList();
  71. })
  72. .catch(r => {
  73. console.log('login failed', r)
  74. uni.showToast({
  75. title: r,
  76. icon: 'none',
  77. });
  78. });
  79. },
  80. bindAuthCodeTap: function (e) {
  81. console.log(this.phone);
  82. this.authCode(this.phone);
  83. },
  84. authCode(phone) {
  85. appServerApi.requestAuthCode(phone)
  86. .then(result => {
  87. uni.showToast({
  88. title: '发送验证码成功',
  89. icon: 'none',
  90. });
  91. })
  92. .catch(reason => {
  93. uni.showToast({
  94. title: reason,
  95. icon: 'none',
  96. });
  97. })
  98. },
  99. go2ConversationList() {
  100. uni.switchTab({
  101. url: '/pages/conversationList/ConversationListView',
  102. success: () => {
  103. console.log('to conversation list success');
  104. },
  105. fail: e => {
  106. console.log('to conversation list error', e);
  107. },
  108. complete: () => {
  109. console.log('switch tab complete')
  110. }
  111. });
  112. }
  113. }
  114. };
  115. </script>
  116. <style>
  117. .page-body {
  118. padding: 20rpx;
  119. }
  120. .page-section {
  121. margin-top: 20rpx;
  122. }
  123. .auth-code-container {
  124. display: flex;
  125. flex-direction: row;
  126. align-items: center;
  127. justify-content: space-between;
  128. width: 100%;
  129. height: 50px;
  130. /*background-color: green;*/
  131. }
  132. .auth-code-container input {
  133. flex: 1;
  134. padding-right: 5px;
  135. }
  136. .auth-code-container button {
  137. /*background-color: red;*/
  138. font-size: 14px;
  139. }
  140. .confirm-button {
  141. margin-top: 20px;
  142. }
  143. /*.other-button-hover {*/
  144. /* background-color: #7497f1;*/
  145. /*}*/
  146. /*.button-hover {*/
  147. /* background-color: #4168e0;*/
  148. /*}*/
  149. </style>