login.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <view class="container">
  3. <view class="top">
  4. <view class="login">登录</view>
  5. </view>
  6. <view class="center">
  7. <view class="login_input">
  8. <image class="img" src="https://zhxc2030.com/img/img08.png"></image>
  9. <input class="input" v-model="username" focus placeholder="请输入手机号" />
  10. </view>
  11. <view class="login_input">
  12. <image class="img" src="https://zhxc2030.com/img/img67.png"></image>
  13. <input class="input" type="password" v-model="passward" placeholder="请输入密码" />
  14. </view>
  15. <!-- 登录按钮 -->
  16. <view class="login_buttom" @click="toLogin">
  17. 登录
  18. </view>
  19. <!-- 注册按钮 -->
  20. <view class="register_buttom" @click="register">
  21. 注册账号
  22. </view>
  23. <view class="login_else">
  24. <view class="login_else_border">
  25. </view>
  26. 其他方式登录
  27. <view class="login_else_border">
  28. </view>
  29. </view>
  30. <!-- 微信登录 -->
  31. <view class="login_wx" @click="appLogin">
  32. <image src="https://zhxc2030.com/img/wxlogin.png"></image>
  33. </view>
  34. <view class="login_wx_text" >
  35. 微信登录
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import { mapMutations } from 'vuex';
  42. import { login,getUserInfo } from '@/api/login.js';
  43. export default {
  44. data() {
  45. return {
  46. username: '',
  47. passward: '',
  48. // #ifdef APP-PLUS
  49. // 保存当前是否为微信浏览器
  50. weixinB:false,
  51. // #endif
  52. };
  53. },
  54. onLoad() {
  55. this.weixinB=uni.getStorageSync('weichatBrowser')
  56. },
  57. methods: {
  58. //微信登录
  59. appLogin: function() {
  60. uni.getProvider({
  61. service: 'oauth',
  62. success: function(res) {
  63. console.log(res.provider);
  64. //支持微信、qq和微博等
  65. if (~res.provider.indexOf('weixin')) {
  66. uni.login({
  67. provider: 'weixin',
  68. success: function(loginRes) {
  69. console.log('-------获取openid(unionid)-----');
  70. console.log(JSON.stringify(loginRes));
  71. // 获取用户信息
  72. uni.getUserInfo({
  73. provider: 'weixin',
  74. success: function(infoRes) {
  75. console.log('-------获取微信用户所有-----');
  76. console.log(JSON.stringify(infoRes.userInfo));
  77. }
  78. });
  79. }
  80. });
  81. }
  82. }
  83. });
  84. },
  85. ...mapMutations('user',['setUserInfo','login']),
  86. //登录
  87. async toLogin() {
  88. let obj = this;
  89. obj.logining = true;
  90. let reg = /^1[3456789]\d{9}$/;
  91. if (obj.username == '') {
  92. obj.$api.msg('请输入手机号');
  93. return;
  94. }
  95. if (!reg.test(obj.username)) {
  96. obj.$api.msg('请输入正确的手机号');
  97. return;
  98. }
  99. if (obj.passward == '') {
  100. obj.$api.msg('请输入密码');
  101. return;
  102. }
  103. login({
  104. account:obj.username,
  105. password:obj.passward
  106. })
  107. .then(function(e) {
  108. uni.setStorageSync('token',e.data.token);
  109. getUserInfo({}).then((e)=> {
  110. obj.login();
  111. // 保存返回用户数据
  112. obj.setUserInfo(e.data);
  113. })
  114. //调用注册接口,成功跳转首页
  115. uni.switchTab({
  116. url: '/pages/index/index'
  117. })
  118. })
  119. .catch(function(e) {
  120. console.log(e);
  121. });
  122. },
  123. //跳转注册页
  124. register() {
  125. uni.navigateTo({
  126. url: `/pages/login/register`
  127. });
  128. },
  129. //跳转忘记密码
  130. // forget() {
  131. // uni.navigateTo({
  132. // url: `/pages/public/forget`
  133. // });
  134. // },
  135. // 后退
  136. navBack() {
  137. uni.navigateBack();
  138. }
  139. }
  140. };
  141. </script>
  142. <style lang="scss">
  143. .container {
  144. line-height: 1;
  145. .top {
  146. width: 750rpx;
  147. height: 500rpx;
  148. position: relative;
  149. background-color: $base-color;
  150. image {
  151. width: 750rpx;
  152. height: 500rpx;
  153. }
  154. .login {
  155. font-size: 36rpx;
  156. font-weight: bold;
  157. position: absolute;
  158. top: 270rpx;
  159. left: 340rpx;
  160. color: #FFFFFF;
  161. }
  162. }
  163. .center {
  164. z-index: 9;
  165. width: 700rpx;
  166. height: 840rpx;
  167. background-color: #FFFFFF;
  168. border-radius: 20rpx;
  169. margin-top: -150rpx;
  170. margin-left: 24rpx;
  171. position: absolute;
  172. .login_input {
  173. width: 560rpx;
  174. display: flex;
  175. justify-content: center;
  176. margin: 70rpx auto;
  177. border-bottom: 2rpx solid #F0F0F0;
  178. padding-bottom: 9rpx;
  179. color: #999999;
  180. .img {
  181. width: 30rpx;
  182. height: auto;
  183. margin-right: 18rpx;
  184. }
  185. .input {
  186. width: 470rpx;
  187. font-size: $font-base;
  188. letter-spacing: 3rpx;
  189. }
  190. }
  191. .login_buttom {
  192. width: 560rpx;
  193. background: linear-gradient(0deg, #C9101B, #F24D33);
  194. border-radius: 40rpx;
  195. margin: 0 auto;
  196. margin-bottom: 20rpx;
  197. text-align: center;
  198. line-height: 80rpx;
  199. color: #FFFFFF;
  200. font-size: 30rpx;
  201. }
  202. .register_buttom {
  203. width: 560rpx;
  204. line-height: 80rpx;
  205. border: 1rpx solid $base-color;
  206. border-radius: 40rpx;
  207. text-align: center;
  208. margin: 0 auto;
  209. font-size: 30rpx;
  210. font-weight: 500;
  211. color: $base-color;
  212. }
  213. .login_else{
  214. width: 560rpx;
  215. margin: 0 auto;
  216. margin-top: 100rpx;
  217. margin-bottom: 45rpx;
  218. font-size: 24rpx;
  219. font-weight:500;
  220. color:rgba(153,153,153,1);
  221. display: flex;
  222. justify-content: center;
  223. align-items: center;
  224. .login_else_border{
  225. flex: 1;
  226. height:1rpx;
  227. background:rgba(238,238,238,1);
  228. }
  229. }
  230. .login_wx{
  231. width: 63rpx;
  232. height: 63rpx;
  233. margin: 0 auto;
  234. border: none;
  235. position: static;
  236. padding: 0;
  237. image{
  238. width: 63rpx;
  239. height: 63rpx;
  240. }
  241. }
  242. .login_wx_text{
  243. text-align: center;
  244. font-size:24rpx;
  245. font-weight:400;
  246. color:rgba(153,153,153,1);
  247. margin-top: 15rpx;
  248. }
  249. }
  250. }
  251. </style>