login.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template >
  2. <view @touchmove.stop.prevent="clear" v-show="dialogShow">
  3. <view class="popup_mask" @touchmove.stop.prevent="clear"></view>
  4. <view class="">
  5. <view class="dt-login-wrap">
  6. <view class="title">请先登录</view>
  7. <view class="content">
  8. <view class="userinfo-avatar">
  9. <open-data type="userAvatarUrl"></open-data>
  10. </view>
  11. <open-data type="userNickName"></open-data>
  12. <text class="subtitle">申请获取您的公开信息(昵称,头像)</text>
  13. </view>
  14. <view class="btn-content setBtnCss">
  15. <button @click="hide" >暂不登录</button>
  16. <block>
  17. <button v-if="canIUseGetUserProfile" @click="getUserProfile">立即登录</button>
  18. <button v-else open-type="getUserInfo" @getuserinfo="getUserInfo">立即登录</button>
  19. </block>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. dialogShow: false,
  30. jscode:'',
  31. //邀请码
  32. inviteCode:'',
  33. canIUseGetUserProfile: false,
  34. };
  35. },
  36. beforeDestroy() {
  37. if(this.timer){
  38. clearTimeout(this.timer)
  39. this.timer = null
  40. }
  41. },
  42. created() {
  43. if (uni.getUserProfile) {
  44. this.canIUseGetUserProfile=false
  45. }
  46. console.log(this.canIUseGetUserProfile);
  47. },
  48. methods:{
  49. show(){
  50. //获取邀请码
  51. this.inviteCode=this.$cache.get('inviteCode')
  52. this.dialogShow=true
  53. this.$mpi.wxLogin().then(res => {
  54. this.jscode = res.code
  55. })
  56. this.setTimer()
  57. },
  58. hide () {
  59. if(this.timer){
  60. clearTimeout(this.timer)
  61. this.timer = null
  62. }
  63. this.dialogShow=false;
  64. },
  65. async setTimer(){
  66. let respWx = await this.$mpi.wxLogin()
  67. this.jscode=respWx.code
  68. // 每隔4分钟重新请求 jscode
  69. this.timer = setTimeout(()=>{
  70. this.setTimer()
  71. },240000)
  72. },
  73. getUserProfile(e) {
  74. uni.getUserProfile({
  75. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  76. success: (res) => {
  77. console.log(res);
  78. this.userInfo = res.userInfo
  79. this.hasUserInfo = true
  80. }
  81. })
  82. },
  83. async getUserInfo(e) {
  84. if(e.detail.errMsg != 'getUserInfo:ok') {
  85. return
  86. }
  87. this.dialogShow=false
  88. try{
  89. // this.respLogin = await this.$api.loginByCode({
  90. // code:this.jscode
  91. // })
  92. //通过openid查询数据库中该用户是否存在,存在就直接将openid放进缓存中
  93. //不存在就将用户基本信息存进数据库中,头像,昵称,openid
  94. this.$emit('signIn', resp)
  95. console.log(e.detail.userInfo.avatarUrl);
  96. console.log(e.detail.userInfo.nickName);
  97. return
  98. //将openid,sessionKey,和邀请码存进数据库中
  99. let resp = await this.$api.loginByWxapp({
  100. openId: this.respLogin.openId,
  101. inviteCode: this.inviteCode
  102. })
  103. if (resp) {
  104. //登录成功
  105. //将openid和sessionkey存进缓存中,有效时间为七天
  106. that.$cache.put("openId",this.respLogin.openId,1*24*60*60*7)
  107. //移除邀请码
  108. this.$cache.remove('inviteCode')
  109. this.dialogShow=false
  110. this.$emit('signIn', resp)
  111. }else{
  112. uni.showToast({
  113. icon:"none",
  114. title:"登录失败"
  115. })
  116. }
  117. }catch(e){
  118. console.log(e);
  119. uni.showToast({
  120. icon:"none",
  121. title:"登录失败"
  122. })
  123. }finally{
  124. if(this.timer){
  125. clearTimeout(this.timer)
  126. this.setTimer()
  127. }
  128. }
  129. },
  130. clear(){
  131. return
  132. }
  133. }
  134. };
  135. </script>
  136. <style lang="scss">
  137. .popup_mask {
  138. position: fixed;
  139. bottom: 0;
  140. top: 0;
  141. left: 0;
  142. right: 0;
  143. background-color: rgba(0, 0, 0, 0.4);
  144. opacity: 1;
  145. z-index: 98;
  146. }
  147. .dt-login-wrap {
  148. position: fixed;
  149. border-radius: 16rpx;
  150. background: #ffffff;
  151. width: 560rpx;
  152. z-index: 99;
  153. bottom: 30%;
  154. left: 50%;
  155. margin-left: -280rpx;
  156. right: 0;
  157. text-align: center;
  158. .title {
  159. padding-top:40upx;
  160. font-size: 34upx;
  161. font-weight: bold;
  162. }
  163. .content{
  164. display: flex;
  165. justify-content: center;
  166. align-items: center;
  167. flex-direction: column;
  168. padding-top: 30rpx;
  169. padding-bottom: 100rpx;
  170. .userinfo-avatar {
  171. overflow:hidden;
  172. display: block;
  173. width: 140rpx;
  174. height: 140rpx;
  175. margin: 20rpx 20rpx 40rpx;
  176. border-radius: 50%;
  177. border: 2px solid #fff;
  178. }
  179. }
  180. .subtitle {
  181. color: #7e7e7e;
  182. font-size: 26upx;
  183. margin-top: 30rpx;
  184. }
  185. .btn-content{
  186. display: flex;
  187. width: 100%;
  188. padding: 10rpx 0;
  189. border-radius: 0;
  190. border-top:1upx solid #ebebeb;
  191. button{
  192. border:none;
  193. width: 50%;
  194. font-size: 30rpx;
  195. background-color: #FFFFFF;
  196. color:#66656a;
  197. }
  198. button:first-child{
  199. border-right: 1rpx solid #ebebeb;
  200. border-radius: 0 0 0 18rpx;
  201. }
  202. button:last-child{
  203. color: #2f7ff5;
  204. border-radius: 0 0 18rpx 0;
  205. }
  206. }
  207. .btn-content button::after{
  208. border: none;
  209. }
  210. }
  211. </style>