login.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. console.log(uni.getStorageSync('present'),'地址')
  114. let ur = uni.getStorageSync('present') || '/pages/index/index';
  115. console.log(ur,'缓存的地址')
  116. // 用于处理缓存bug
  117. if (ur=='pages/shop/product') {
  118. ur = '/pages/index/index'
  119. }
  120. uni.navigateTo({
  121. url: ur,
  122. fail(e) {
  123. uni.navigateTo({
  124. url: ur,
  125. fail(e) {
  126. uni.navigateTo({
  127. url: '/pages/index/index',
  128. });
  129. }
  130. });
  131. }
  132. });
  133. })
  134. //调用注册接口,成功跳转首页
  135. // uni.switchTab({
  136. // url: '/pages/index/index'
  137. // })
  138. })
  139. .catch(function(e) {
  140. console.log(e);
  141. });
  142. },
  143. //跳转注册页
  144. register() {
  145. uni.navigateTo({
  146. url: `/pages/login/register`
  147. });
  148. },
  149. //跳转忘记密码
  150. // forget() {
  151. // uni.navigateTo({
  152. // url: `/pages/public/forget`
  153. // });
  154. // },
  155. // 后退
  156. navBack() {
  157. uni.navigateBack();
  158. }
  159. }
  160. };
  161. </script>
  162. <style lang="scss">
  163. .container {
  164. line-height: 1;
  165. .top {
  166. width: 750rpx;
  167. height: 500rpx;
  168. position: relative;
  169. background-color: $base-color;
  170. image {
  171. width: 750rpx;
  172. height: 500rpx;
  173. }
  174. .login {
  175. font-size: 36rpx;
  176. font-weight: bold;
  177. position: absolute;
  178. top: 270rpx;
  179. left: 340rpx;
  180. color: #FFFFFF;
  181. }
  182. }
  183. .center {
  184. z-index: 9;
  185. width: 700rpx;
  186. height: 840rpx;
  187. background-color: #FFFFFF;
  188. border-radius: 20rpx;
  189. margin-top: -150rpx;
  190. margin-left: 24rpx;
  191. position: absolute;
  192. .login_input {
  193. width: 560rpx;
  194. display: flex;
  195. justify-content: center;
  196. margin: 70rpx auto;
  197. border-bottom: 2rpx solid #F0F0F0;
  198. padding-bottom: 9rpx;
  199. color: #999999;
  200. .img {
  201. width: 30rpx;
  202. height: auto;
  203. margin-right: 18rpx;
  204. }
  205. .input {
  206. width: 470rpx;
  207. font-size: $font-base;
  208. letter-spacing: 3rpx;
  209. }
  210. }
  211. .login_buttom {
  212. width: 560rpx;
  213. background: linear-gradient(0deg, #C9101B, #F24D33);
  214. border-radius: 40rpx;
  215. margin: 0 auto;
  216. margin-bottom: 20rpx;
  217. text-align: center;
  218. line-height: 80rpx;
  219. color: #FFFFFF;
  220. font-size: 30rpx;
  221. }
  222. .register_buttom {
  223. width: 560rpx;
  224. line-height: 80rpx;
  225. border: 1rpx solid $base-color;
  226. border-radius: 40rpx;
  227. text-align: center;
  228. margin: 0 auto;
  229. font-size: 30rpx;
  230. font-weight: 500;
  231. color: $base-color;
  232. }
  233. .login_else{
  234. width: 560rpx;
  235. margin: 0 auto;
  236. margin-top: 100rpx;
  237. margin-bottom: 45rpx;
  238. font-size: 24rpx;
  239. font-weight:500;
  240. color:rgba(153,153,153,1);
  241. display: flex;
  242. justify-content: center;
  243. align-items: center;
  244. .login_else_border{
  245. flex: 1;
  246. height:1rpx;
  247. background:rgba(238,238,238,1);
  248. }
  249. }
  250. .login_wx{
  251. width: 63rpx;
  252. height: 63rpx;
  253. margin: 0 auto;
  254. border: none;
  255. position: static;
  256. padding: 0;
  257. image{
  258. width: 63rpx;
  259. height: 63rpx;
  260. }
  261. }
  262. .login_wx_text{
  263. text-align: center;
  264. font-size:24rpx;
  265. font-weight:400;
  266. color:rgba(153,153,153,1);
  267. margin-top: 15rpx;
  268. }
  269. }
  270. }
  271. </style>