login.vue 4.0 KB

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