my-login.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view class="login-container">
  3. <!-- 提示登录的图标 -->
  4. <uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
  5. <!-- 登录按钮 -->
  6. <button type="primary" class="btn-login" @click="getToken">一键登录</button>
  7. <!-- 登录提示 -->
  8. <view class="tips-text">登录后尽享更多权益</view>
  9. </view>
  10. </template>
  11. <script>
  12. // 按需导入 mapMutations 辅助函数
  13. import { mapMutations } from 'vuex'
  14. export default {
  15. name:"my-login",
  16. data() {
  17. return {
  18. queryObj: {
  19. latitude: 0,
  20. longitude: 0
  21. }
  22. };
  23. },
  24. methods: {
  25. // 调用 mapMutations 辅助方法,把 m_user 模块中的 updateUserInfo 映射到当前组件中使用
  26. ...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateLocation']),
  27. // 引导用户开启位置权限
  28. openSetting() {
  29. var that = this
  30. wx.showModal({
  31. title: '位置信息',
  32. content: '开启后将为你推送最近的资源',
  33. showCancel: true,
  34. cancelText: '取消',
  35. cancelColor: '#000000',
  36. confirmText: '开启',
  37. confirmColor: '#3CC51F',
  38. success: res => {
  39. if (res.confirm) {
  40. wx.openSetting({
  41. success(res) {
  42. if (res.authSetting['scope.userFuzzyLocation']) {
  43. // 获取经纬度
  44. wx.getFuzzyLocation({
  45. type: 'wgs84',
  46. success (res) {
  47. that.queryObj.latitude = res.latitude
  48. that.queryObj.longitude = res.longitude
  49. that.updateLocation(that.queryObj)
  50. that.getLogin()
  51. }
  52. })
  53. }
  54. },
  55. fail(res) {
  56. uni.$showMsg(res)
  57. }
  58. })
  59. } else if(res.cancel) {
  60. uni.$showMsg("需要位置权限")
  61. }
  62. }
  63. })
  64. },
  65. // 调用登录接口,换取永久的 token
  66. async getToken() {
  67. var that = this
  68. // 获取位置信息
  69. wx.getSetting({
  70. success(res) {
  71. if (!res.authSetting['scope.userFuzzyLocation']) {
  72. wx.authorize({
  73. scope: 'scope.userFuzzyLocation',
  74. success() {
  75. // 获取经纬度
  76. wx.getFuzzyLocation({
  77. type: 'wgs84',
  78. success (res) {
  79. that.queryObj.latitude = res.latitude
  80. that.queryObj.longitude = res.longitude
  81. that.updateLocation(that.queryObj)
  82. that.getLogin()
  83. }
  84. })
  85. },
  86. fail() {
  87. // 拒绝后引导用户开启
  88. if (wx.openSetting) {
  89. that.openSetting()
  90. }
  91. return
  92. }
  93. })
  94. } else {
  95. that.getLogin()
  96. }
  97. }
  98. })
  99. },
  100. // 真正的登录逻辑
  101. async getLogin() {
  102. // 调用微信登录接口
  103. const [err, res] = await uni.login().catch(err => err)
  104. // 判断是否 wx.login() 调用失败
  105. if (err || res.errMsg !== 'login:ok') return uni.$showError('登录失败!')
  106. // 准备参数对象
  107. const query = {
  108. code: res.code,
  109. }
  110. // 换取 token
  111. const { data: loginResult } = await uni.$http.get('/ucenter/mini-program-openid-uid/wxlogin', query)
  112. if (loginResult.code === 20000) {
  113. uni.$showMsg('登录成功!')
  114. }
  115. // console.log(loginResult)
  116. // 更新vuex中的token
  117. this.updateToken(loginResult.data.token)
  118. // // 将用户的基本信息存储到 vuex 中
  119. const userInfo = {
  120. alias: loginResult.data.alias,
  121. avatar: loginResult.data.avatar,
  122. city: loginResult.data.city,
  123. uid: loginResult.data.uid
  124. }
  125. this.updateUserInfo(userInfo)
  126. }
  127. }
  128. }
  129. </script>
  130. <style lang="scss" scoped>
  131. .login-container {
  132. // 登录盒子的样式
  133. height: 750rpx;
  134. display: flex;
  135. flex-direction: column;
  136. align-items: center;
  137. justify-content: center;
  138. background-color: #f8f8f8;
  139. position: relative;
  140. overflow: hidden;
  141. // 绘制登录盒子底部的半椭圆造型
  142. &::after {
  143. content: ' ';
  144. display: block;
  145. position: absolute;
  146. width: 100%;
  147. height: 40px;
  148. left: 0;
  149. bottom: 0;
  150. background-color: white;
  151. border-radius: 100%;
  152. transform: translateY(50%);
  153. }
  154. // 登录按钮的样式
  155. .btn-login {
  156. width: 90%;
  157. border-radius: 100px;
  158. margin: 15px 0;
  159. background-color: #c00000;
  160. }
  161. // 按钮下方提示消息的样式
  162. .tips-text {
  163. font-size: 12px;
  164. color: gray;
  165. }
  166. }
  167. </style>