login.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <view class="content">
  3. <view>
  4. <view>
  5. <view class="logo">
  6. <u-image src="/static/logo.png" width="140" height="140" ></u-image>
  7. </view>
  8. <view class="login-btns">
  9. <u-button shape="circle" :custom-style="wxButton" class="login-btn" type="success" :hair-line="false" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  10. 微信登陆
  11. </u-button>
  12. <u-gap height="30"></u-gap>
  13. <u-button shape="circle" :custom-style="phoneButton" class="login-btn" @click="phoneLogin" type="primary">
  14. 手机号码登录
  15. </u-button>
  16. </view>
  17. </view>
  18. <view class="nologin">
  19. <u-button shape="circle" size="mini" plain lass="login-btn" @click="cansel" type="info">
  20. 暂不登录
  21. </u-button>
  22. </view>
  23. </view>
  24. <view @click="toYs">登录即代表同意<text>隐私政策</text>和<text>服务条款</text></view>
  25. </view>
  26. </template>
  27. <script>
  28. import {
  29. login
  30. } from "@/api/login"
  31. export default {
  32. components: {},
  33. data() {
  34. return {
  35. path: '/pages/index/index',
  36. lanuch: true,
  37. code: '',
  38. wxButton: {
  39. background: '#07c160'
  40. },
  41. phoneButton: {
  42. background: '#00aaff'
  43. }
  44. }
  45. },
  46. mounted() {},
  47. onLoad(option) {
  48. // 为解决首次登录异常问题 进入页面先调微信登录获取code
  49. this.wxLogin()
  50. const {
  51. path,
  52. lanuch
  53. } = option;
  54. this.lanuch = (lanuch + '') === 'true';
  55. if (path) {
  56. this.path = decodeURIComponent(path);
  57. }
  58. },
  59. methods: {
  60. cansel(){
  61. this.$store.state.vuex_noLogin = true
  62. uni.reLaunch({
  63. url: '/pages/index/index'
  64. })
  65. },
  66. toYs(){
  67. uni.navigateTo({
  68. url: '/pages/agreement/agreement'
  69. })
  70. },
  71. getPhoneNumber(e) {
  72. let _this = this
  73. uni.showLoading({
  74. mask: true,
  75. title: '加载中'
  76. });
  77. if (!this.code) {
  78. this.wxLogin()
  79. }
  80. console.log(this.code, e)
  81. if (e.target.errMsg === 'getPhoneNumber:ok') {
  82. setTimeout(()=>{
  83. login({
  84. code: _this.code,
  85. encryptedData: e.target.encryptedData,
  86. iv: e.target.iv
  87. }).then(res => {
  88. uni.hideLoading();
  89. _this.code = ''
  90. console.log(res,_this.path, 'login data')
  91. if (res.status == '200') {
  92. getApp().globalData.token = res.data
  93. _this.$u.vuex('vuex_token',res.data)
  94. uni.$emit("getUserInfo")
  95. if (_this.path === '/pages/index/index' || _this.lanuch) {
  96. uni.reLaunch({
  97. url: _this.path
  98. });
  99. } else {
  100. uni.redirectTo({
  101. url: _this.path
  102. });
  103. }
  104. } else {
  105. uni.showToast({
  106. title: res.message,
  107. icon: 'none',
  108. duration: 2500
  109. });
  110. }
  111. });
  112. },300)
  113. } else {
  114. uni.showToast({
  115. title: '授权失败',
  116. icon: 'none',
  117. duration: 2500
  118. });
  119. }
  120. },
  121. // 调微信登录 获取code
  122. wxLogin () {
  123. let _this = this
  124. uni.login({
  125. provider: 'weixin',
  126. success(res) {
  127. console.log(res.code)
  128. _this.code = res.code
  129. }
  130. })
  131. },
  132. // 手机号登录
  133. phoneLogin () {
  134. wx.navigateTo({
  135. url: '/pages/login/phoneLogin'
  136. })
  137. }
  138. }
  139. }
  140. </script>
  141. <style lang="scss">
  142. .content {
  143. width: 100%;
  144. height: 100%;
  145. padding: 40upx;
  146. display: flex;
  147. flex-direction: column;
  148. > view{
  149. text-align: center;
  150. &:first-child{
  151. flex-grow: 1;
  152. height: 80vh;
  153. padding: 20% 10%;
  154. display: flex;
  155. flex-direction: column;
  156. justify-content: space-between;
  157. }
  158. &:last-child{
  159. font-size: 24upx;
  160. text{
  161. color: #07c160;
  162. }
  163. }
  164. .logo{
  165. text-align: center;
  166. display: flex;
  167. justify-content: center;
  168. }
  169. .login-btns{
  170. padding: 60upx 0;
  171. .login-btn{
  172. margin: 30upx 0;
  173. }
  174. }
  175. }
  176. }
  177. </style>