login.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <template>
  2. <view class="">
  3. <!-- 获取手机号 -->
  4. <u-modal v-model="phoneShow" content="请授权获取手机号">
  5. <button class="cu-btn btn-bg-color round" style="width: 80%;height: 70rpx;margin-bottom: 20rpx;" slot="confirm-button"
  6. open-type="getPhoneNumber" @getphonenumber="getphonenumber">
  7. 去授权
  8. </button>
  9. </u-modal>
  10. <!-- 获取头像昵称 -->
  11. <view @touchmove.stop.prevent="clear" v-show="dialogShow">
  12. <view class="popup_mask" @touchmove.stop.prevent="clear"></view>
  13. <view class="">
  14. <view class="dt-login-wrap">
  15. <view class="title">请先登录</view>
  16. <view class="content">
  17. <view class="userinfo-avatar">
  18. <open-data type="userAvatarUrl"></open-data>
  19. </view>
  20. <open-data type="userNickName"></open-data>
  21. <text class="subtitle">申请获取您的公开信息(昵称,头像)</text>
  22. </view>
  23. <view class="btn-content setBtnCss" >
  24. <button @click="hide" style="padding: 10rpx 0;">暂不登录</button>
  25. <button @click="getUserProfile" style="padding: 10rpx 0;">立即登录</button>
  26. <!-- <button v-else open-type="getUserInfo" @getuserinfo="getuserinfo">立即登录</button> -->
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. data() {
  36. return {
  37. canIUseGetUserProfile: false,
  38. //保存时间七天
  39. cacheTime: 1 * 24 * 60 * 60 * 7,
  40. dialogShow: false,
  41. jscode: '',
  42. //邀请码
  43. inviteCode: '',
  44. userInfo: '',
  45. sessionKey: '',
  46. //获取手机号
  47. phoneShow: false
  48. };
  49. },
  50. beforeDestroy() {
  51. if (this.timer) {
  52. clearTimeout(this.timer)
  53. this.timer = null
  54. }
  55. },
  56. created() {
  57. if (uni.getUserProfile) {
  58. this.canIUseGetUserProfile = false
  59. }
  60. },
  61. methods: {
  62. show() {
  63. //获取邀请码
  64. this.inviteCode = this.$cache.get('inviteCode')
  65. this.dialogShow = true
  66. this.$mpi.wxLogin().then(res => {
  67. this.jscode = res.code
  68. })
  69. this.setTimer()
  70. },
  71. hide() {
  72. if (this.timer) {
  73. clearTimeout(this.timer)
  74. this.timer = null
  75. }
  76. this.dialogShow = false;
  77. this.$emit('hide')
  78. },
  79. async setTimer() {
  80. let respWx = await this.$mpi.wxLogin()
  81. this.jscode = respWx.code
  82. // 每隔4分钟重新请求 jscode
  83. this.timer = setTimeout(() => {
  84. this.setTimer()
  85. }, 240000)
  86. },
  87. getUserProfile(e) {
  88. uni.getUserProfile({
  89. desc: '用于获取头像和昵称',
  90. success: (res) => {
  91. this.userInfo = res.userInfo
  92. console.log("getUserProfile获取用户信息", this.userInfo);
  93. this.doLogin()
  94. }
  95. })
  96. },
  97. getuserinfo(e) {
  98. console.log("getuserinfo获取用户信息", e);
  99. if (e.detail.errMsg != 'getUserInfo:ok') {
  100. return
  101. }
  102. this.userInfo = e.detail.userInfo
  103. this.doLogin()
  104. },
  105. async doLogin() {
  106. this.dialogShow = false
  107. try {
  108. this.respLogin = await this.$api.wxApp.getOpenId({
  109. code: this.jscode
  110. })
  111. let openId = this.respLogin.data.openid
  112. let unionid = this.respLogin.data.unionid || ''
  113. this.sessionKey = this.respLogin.data.session_key
  114. //通过openid查询数据库中该用户是否存在
  115. let params = {
  116. nickName: this.userInfo.nickName,
  117. avatar: this.userInfo.avatarUrl,
  118. gender: this.userInfo.gender,
  119. unionid,
  120. openid: openId,
  121. province: this.userInfo.province,
  122. city: this.userInfo.city
  123. }
  124. //将openid
  125. let resp = await this.$api.loginUser.login(params)
  126. if (resp.success) {
  127. //登录成功
  128. let obj = {
  129. openId,
  130. sessionKey: this.sessionKey,
  131. userId: resp.data.id,
  132. phone: resp.data.phone
  133. }
  134. //将openid和sessionkey存进缓存中,有效时间为七天
  135. this.$cache.put("userInfo", obj)
  136. this.$u.vuex('vuex_userId', resp.data.id)
  137. if (resp.data.phone) {
  138. this.$u.vuex('vuex_phone', resp.data.phone)
  139. }
  140. //移除邀请码
  141. this.$cache.remove('inviteCode')
  142. this.dialogShow = false
  143. this.$emit('signIn', resp)
  144. } else {
  145. uni.showToast({
  146. icon: "none",
  147. title: "登录失败"
  148. })
  149. }
  150. } catch (e) {
  151. console.log(e);
  152. uni.showToast({
  153. icon: "none",
  154. title: "登录失败"
  155. })
  156. } finally {
  157. if (this.timer) {
  158. clearTimeout(this.timer)
  159. this.setTimer()
  160. }
  161. }
  162. },
  163. clear() {
  164. return
  165. },
  166. //获取手机号
  167. showPhoneModal() {
  168. this.phoneShow = true
  169. },
  170. async getphonenumber(e) {
  171. this.$emit('getphonenumber')
  172. if (this.$isEmpty(this.sessionKey)) {
  173. this.sessionKey =this.$cache.get('userInfo').sessionKey
  174. }
  175. let params = {
  176. sessionKey: this.sessionKey,
  177. encryptedData: e.detail.encryptedData,
  178. iv: e.detail.iv
  179. }
  180. this.$api.wxApp.getOpenData(params).then(res => {
  181. if (res.success) {
  182. let obj = JSON.parse(res.data)
  183. let phone = obj.phoneNumber
  184. this.cachePhone(phone)
  185. }
  186. })
  187. },
  188. cachePhone(phone) {
  189. let userInfo = this.$cache.get('userInfo')
  190. let params = {
  191. id: userInfo.userId,
  192. phone
  193. }
  194. this.$api.loginUser.submit(params).then(res => {
  195. if (res.success) {
  196. userInfo.phone = phone
  197. this.$cache.put('userInfo', userInfo)
  198. this.$u.vuex('vuex_phone', phone)
  199. }
  200. })
  201. }
  202. }
  203. };
  204. </script>
  205. <style lang="scss">
  206. .popup_mask {
  207. position: fixed;
  208. bottom: 0;
  209. top: 0;
  210. left: 0;
  211. right: 0;
  212. background-color: rgba(0, 0, 0, 0.4);
  213. opacity: 1;
  214. z-index: 98;
  215. }
  216. .dt-login-wrap {
  217. position: fixed;
  218. border-radius: 16rpx;
  219. background: #ffffff;
  220. width: 560rpx;
  221. z-index: 99;
  222. bottom: 30%;
  223. left: 50%;
  224. margin-left: -280rpx;
  225. right: 0;
  226. text-align: center;
  227. .title {
  228. padding-top: 40upx;
  229. font-size: 34upx;
  230. font-weight: bold;
  231. }
  232. .content {
  233. display: flex;
  234. justify-content: center;
  235. align-items: center;
  236. flex-direction: column;
  237. padding-top: 30rpx;
  238. padding-bottom: 150rpx;
  239. .userinfo-avatar {
  240. overflow: hidden;
  241. display: block;
  242. width: 140rpx;
  243. height: 140rpx;
  244. margin: 20rpx 20rpx 40rpx;
  245. border-radius: 50%;
  246. border: 2px solid #fff;
  247. }
  248. }
  249. .subtitle {
  250. color: #7e7e7e;
  251. font-size: 26upx;
  252. margin-top: 30rpx;
  253. }
  254. .btn-content {
  255. display: flex;
  256. width: 100%;
  257. border-radius: 0;
  258. border-top: 1upx solid #ebebeb;
  259. button {
  260. border: none;
  261. width: 50%;
  262. font-size: 30rpx;
  263. background-color: #FFFFFF;
  264. color: #66656a;
  265. }
  266. button:first-child {
  267. border-right: 1rpx solid #ebebeb;
  268. border-radius: 0 0 0 18rpx;
  269. }
  270. button:last-child {
  271. color: #2f7ff5;
  272. border-radius: 0 0 18rpx 0;
  273. }
  274. }
  275. .btn-content button::after {
  276. border: none;
  277. }
  278. }
  279. </style>