login.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. onShow() {
  47. },
  48. mounted() {},
  49. onLoad(option) {
  50. // 为解决首次登录异常问题 进入页面先调微信登录获取code
  51. this.wxLogin()
  52. const {
  53. path,
  54. lanuch
  55. } = option;
  56. this.lanuch = (lanuch + '') === 'true';
  57. if (path) {
  58. this.path = decodeURIComponent(path);
  59. }
  60. },
  61. onUnload() {
  62. uni.$off('getUserInfo')
  63. },
  64. methods: {
  65. cansel(){
  66. this.$store.state.vuex_noLogin = true
  67. uni.reLaunch({
  68. url: '/pages/index/index'
  69. })
  70. },
  71. toYs(){
  72. uni.navigateTo({
  73. url: '/pages/agreement/agreement'
  74. })
  75. },
  76. getPhoneNumber(e) {
  77. let _this = this
  78. uni.showLoading({
  79. mask: true,
  80. title: '加载中'
  81. });
  82. if (!this.code) {
  83. this.wxLogin()
  84. }
  85. if (e.target.errMsg === 'getPhoneNumber:ok') {
  86. setTimeout(()=>{
  87. login({
  88. code: _this.code,
  89. encryptedData: e.target.encryptedData,
  90. iv: e.target.iv
  91. }).then(res => {
  92. uni.hideLoading();
  93. _this.code = ''
  94. console.log(res,_this.path, 'login data')
  95. if (res.status == '200') {
  96. getApp().globalData.token = res.data
  97. _this.$u.vuex('vuex_token',res.data)
  98. uni.$emit("getUserInfo")
  99. if (_this.path === '/pages/index/index' || _this.lanuch) {
  100. uni.reLaunch({
  101. url: _this.path
  102. });
  103. } else {
  104. uni.redirectTo({
  105. url: _this.path
  106. });
  107. }
  108. } else {
  109. uni.showToast({
  110. title: res.message,
  111. icon: 'none',
  112. duration: 2500
  113. });
  114. }
  115. });
  116. },300)
  117. } else {
  118. uni.showToast({
  119. title: '授权失败',
  120. icon: 'none',
  121. duration: 2500
  122. });
  123. }
  124. },
  125. // 调微信登录 获取code
  126. wxLogin () {
  127. let _this = this
  128. uni.login({
  129. provider: 'weixin',
  130. success(res) {
  131. _this.code = res.code
  132. }
  133. })
  134. },
  135. // 手机号登录
  136. phoneLogin () {
  137. wx.navigateTo({
  138. url: '/pages/login/phoneLogin'
  139. })
  140. }
  141. }
  142. }
  143. </script>
  144. <style lang="scss">
  145. .content {
  146. width: 100%;
  147. height: 100%;
  148. padding: 40upx;
  149. display: flex;
  150. flex-direction: column;
  151. > view{
  152. text-align: center;
  153. &:first-child{
  154. flex-grow: 1;
  155. height: 80vh;
  156. padding: 20% 10%;
  157. display: flex;
  158. flex-direction: column;
  159. justify-content: space-between;
  160. }
  161. &:last-child{
  162. font-size: 24upx;
  163. text{
  164. color: #07c160;
  165. }
  166. }
  167. .logo{
  168. text-align: center;
  169. display: flex;
  170. justify-content: center;
  171. }
  172. .login-btns{
  173. padding: 60upx 0;
  174. .login-btn{
  175. margin: 30upx 0;
  176. }
  177. }
  178. }
  179. }
  180. </style>