my-login.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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, mapState } from 'vuex'
  14. export default {
  15. computed: {
  16. ...mapState('m_user', ['recoUID']),
  17. },
  18. name:"my-login",
  19. data() {
  20. return {
  21. queryObj: {
  22. latitude: 0,
  23. longitude: 0
  24. }
  25. };
  26. },
  27. methods: {
  28. // 调用 mapMutations 辅助方法,把 m_user 模块中的 updateUserInfo 映射到当前组件中使用
  29. ...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateLocation']),
  30. // 引导用户开启位置权限
  31. openSetting() {
  32. var that = this
  33. wx.showModal({
  34. title: '位置信息',
  35. content: '开启后将为你推送最近的资源',
  36. showCancel: true,
  37. cancelText: '取消',
  38. cancelColor: '#000000',
  39. confirmText: '开启',
  40. confirmColor: '#3CC51F',
  41. success: res => {
  42. if (res.confirm) {
  43. wx.openSetting({
  44. success(res) {
  45. if (res.authSetting['scope.userFuzzyLocation']) {
  46. // 获取经纬度
  47. wx.getFuzzyLocation({
  48. type: 'wgs84',
  49. success (res) {
  50. that.queryObj.latitude = res.latitude
  51. that.queryObj.longitude = res.longitude
  52. that.updateLocation(that.queryObj)
  53. that.getLogin()
  54. }
  55. })
  56. }
  57. },
  58. fail(res) {
  59. uni.$showMsg(res)
  60. }
  61. })
  62. } else if(res.cancel) {
  63. uni.$showMsg("需要位置权限")
  64. }
  65. }
  66. })
  67. },
  68. // 调用登录接口,换取永久的 token
  69. async getToken() {
  70. var that = this
  71. // 获取位置信息
  72. wx.getSetting({
  73. success(res) {
  74. if (!res.authSetting['scope.userFuzzyLocation']) {
  75. wx.authorize({
  76. scope: 'scope.userFuzzyLocation',
  77. success() {
  78. // 获取经纬度
  79. wx.getFuzzyLocation({
  80. type: 'wgs84',
  81. success (res) {
  82. that.queryObj.latitude = res.latitude
  83. that.queryObj.longitude = res.longitude
  84. that.updateLocation(that.queryObj)
  85. that.getLogin()
  86. }
  87. })
  88. },
  89. fail() {
  90. // 拒绝后引导用户开启
  91. if (wx.openSetting) {
  92. that.openSetting()
  93. }
  94. return
  95. }
  96. })
  97. } else {
  98. // 获取经纬度
  99. wx.getFuzzyLocation({
  100. type: 'wgs84',
  101. success (res) {
  102. that.queryObj.latitude = res.latitude
  103. that.queryObj.longitude = res.longitude
  104. that.updateLocation(that.queryObj)
  105. that.getLogin()
  106. }
  107. })
  108. that.getLogin()
  109. }
  110. }
  111. })
  112. },
  113. // 真正的登录逻辑
  114. async getLogin() {
  115. // 调用微信登录接口
  116. const [err, res] = await uni.login().catch(err => err)
  117. // 判断是否 wx.login() 调用失败
  118. if (err || res.errMsg !== 'login:ok') return uni.$showError('登录失败!')
  119. // 准备参数对象
  120. const query = {
  121. code: res.code,
  122. recoUID: this.recoUID
  123. }
  124. // 换取 token
  125. const { data: loginResult } = await uni.$http.get('/ucenter/mini-program-openid-uid/wxlogin', query)
  126. if (loginResult.code === 20000) {
  127. uni.$showMsg('登录成功!')
  128. }
  129. // 更新vuex中的token
  130. this.updateToken(loginResult.data.token)
  131. // // 将用户的基本信息存储到 vuex 中
  132. if (loginResult.data.avatar === '') {
  133. loginResult.data.avatar = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/'
  134. }
  135. const userInfo = {
  136. alias: loginResult.data.alias,
  137. avatar: loginResult.data.avatar,
  138. city: loginResult.data.city,
  139. uid: loginResult.data.uid,
  140. count: loginResult.data.count,
  141. isAdmin: loginResult.data.isAdmin
  142. }
  143. this.updateUserInfo(userInfo)
  144. let SocketTask = wx.connectSocket({
  145. url: `${uni.$wsLocation}/education/chat?uid=`+userInfo.uid,
  146. success(res) {
  147. console.log("开始连接",res)
  148. if (userInfo.count == 0) {
  149. uni.removeTabBarBadge({
  150. index: 2
  151. })
  152. } else {
  153. // 调用 uni.setTabBarBadge() 方法,为购物车设置右上角的徽标
  154. uni.setTabBarBadge({
  155. index: 2,
  156. text: userInfo.count + '', // 注意:text 的值必须是字符串,不能是数字
  157. })
  158. }
  159. }
  160. })
  161. SocketTask.onMessage( res => {
  162. console.log('监听',res.data)
  163. userInfo.count++;
  164. this.updateUserInfo(userInfo)
  165. })
  166. }
  167. }
  168. }
  169. </script>
  170. <style lang="scss" scoped>
  171. .login-container {
  172. // 登录盒子的样式
  173. height: 750rpx;
  174. display: flex;
  175. flex-direction: column;
  176. align-items: center;
  177. justify-content: center;
  178. background-color: #E2F0D9;
  179. position: relative;
  180. overflow: hidden;
  181. // 绘制登录盒子底部的半椭圆造型
  182. &::after {
  183. content: ' ';
  184. display: block;
  185. position: absolute;
  186. width: 100%;
  187. height: 40px;
  188. left: 0;
  189. bottom: 0;
  190. background-color: white;
  191. border-radius: 100%;
  192. transform: translateY(50%);
  193. }
  194. // 登录按钮的样式
  195. .btn-login {
  196. width: 90%;
  197. border-radius: 100px;
  198. margin: 15px 0;
  199. background-color: #008000;
  200. }
  201. // 按钮下方提示消息的样式
  202. .tips-text {
  203. font-size: 12px;
  204. color: gray;
  205. }
  206. }
  207. </style>