my-login.vue 4.4 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. 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. this.getLogin()
  100. },
  101. // 真正的登录逻辑
  102. async getLogin() {
  103. // 调用微信登录接口
  104. const [err, res] = await uni.login().catch(err => err)
  105. // 判断是否 wx.login() 调用失败
  106. if (err || res.errMsg !== 'login:ok') return uni.$showError('登录失败!')
  107. // 准备参数对象
  108. const query = {
  109. code: res.code,
  110. }
  111. // 换取 token
  112. const { data: loginResult } = await uni.$http.get('/ucenter/mini-program-openid-uid/wxlogin', query)
  113. if (loginResult.code === 20000) {
  114. uni.$showMsg('登录成功!')
  115. }
  116. // console.log(loginResult)
  117. // 更新vuex中的token
  118. this.updateToken(loginResult.data.token)
  119. // // 将用户的基本信息存储到 vuex 中
  120. const userInfo = {
  121. alias: loginResult.data.alias,
  122. avatar: loginResult.data.avatar,
  123. city: loginResult.data.city,
  124. uid: loginResult.data.uid
  125. }
  126. this.updateUserInfo(userInfo)
  127. }
  128. }
  129. }
  130. </script>
  131. <style lang="scss" scoped>
  132. .login-container {
  133. // 登录盒子的样式
  134. height: 750rpx;
  135. display: flex;
  136. flex-direction: column;
  137. align-items: center;
  138. justify-content: center;
  139. background-color: #f8f8f8;
  140. position: relative;
  141. overflow: hidden;
  142. // 绘制登录盒子底部的半椭圆造型
  143. &::after {
  144. content: ' ';
  145. display: block;
  146. position: absolute;
  147. width: 100%;
  148. height: 40px;
  149. left: 0;
  150. bottom: 0;
  151. background-color: white;
  152. border-radius: 100%;
  153. transform: translateY(50%);
  154. }
  155. // 登录按钮的样式
  156. .btn-login {
  157. width: 90%;
  158. border-radius: 100px;
  159. margin: 15px 0;
  160. background-color: #c00000;
  161. }
  162. // 按钮下方提示消息的样式
  163. .tips-text {
  164. font-size: 12px;
  165. color: gray;
  166. }
  167. }
  168. </style>