login.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. if (this.$isEmpty(openId)) {
  113. this.$u.toast('登录失败')
  114. return
  115. }
  116. let unionid = this.respLogin.data.unionid || ''
  117. this.sessionKey = this.respLogin.data.session_key
  118. //通过openid查询数据库中该用户是否存在
  119. let params = {
  120. nickName: this.userInfo.nickName,
  121. avatar: this.userInfo.avatarUrl,
  122. gender: this.userInfo.gender,
  123. unionid,
  124. openid: openId,
  125. province: this.userInfo.province,
  126. city: this.userInfo.city
  127. }
  128. //将openid
  129. let resp = await this.$api.loginUser.login(params)
  130. if (resp.success) {
  131. //登录成功
  132. let obj = {
  133. ...resp.data,
  134. openId,
  135. sessionKey: this.sessionKey,
  136. userId: resp.data.id,
  137. }
  138. //将openid和sessionkey存进缓存中,有效时间为七天
  139. this.$cache.put("userInfo", obj)
  140. this.$u.vuex('vuex_userId', resp.data.id)
  141. if (resp.data.phone) {
  142. this.$u.vuex('vuex_phone', resp.data.phone)
  143. }
  144. //移除邀请码
  145. this.$cache.remove('inviteCode')
  146. this.dialogShow = false
  147. this.$emit('signIn', resp)
  148. } else {
  149. uni.showToast({
  150. icon: "none",
  151. title: "登录失败"
  152. })
  153. }
  154. } catch (e) {
  155. console.log(e);
  156. uni.showToast({
  157. icon: "none",
  158. title: "登录失败"
  159. })
  160. } finally {
  161. if (this.timer) {
  162. clearTimeout(this.timer)
  163. this.setTimer()
  164. }
  165. }
  166. },
  167. clear() {
  168. return
  169. },
  170. //获取手机号
  171. showPhoneModal() {
  172. this.phoneShow = true
  173. },
  174. async getphonenumber(e) {
  175. this.$emit('getphonenumber')
  176. if (this.$isEmpty(this.sessionKey)) {
  177. this.sessionKey =this.$cache.get('userInfo').sessionKey
  178. }
  179. let params = {
  180. sessionKey: this.sessionKey,
  181. encryptedData: e.detail.encryptedData,
  182. iv: e.detail.iv
  183. }
  184. this.$api.wxApp.getOpenData(params).then(res => {
  185. if (res.success) {
  186. let obj = JSON.parse(res.data)
  187. let phone = obj.phoneNumber
  188. this.cachePhone(phone)
  189. }
  190. })
  191. },
  192. cachePhone(phone) {
  193. let userInfo = this.$cache.get('userInfo')
  194. let params = {
  195. id: userInfo.userId,
  196. phone
  197. }
  198. this.$api.loginUser.submit(params).then(res => {
  199. if (res.success) {
  200. userInfo.phone = phone
  201. this.$cache.put('userInfo', userInfo)
  202. this.$u.vuex('vuex_phone', phone)
  203. }
  204. })
  205. }
  206. }
  207. };
  208. </script>
  209. <style lang="scss">
  210. .popup_mask {
  211. position: fixed;
  212. bottom: 0;
  213. top: 0;
  214. left: 0;
  215. right: 0;
  216. background-color: rgba(0, 0, 0, 0.4);
  217. opacity: 1;
  218. z-index: 98;
  219. }
  220. .dt-login-wrap {
  221. position: fixed;
  222. border-radius: 16rpx;
  223. background: #ffffff;
  224. width: 560rpx;
  225. z-index: 99;
  226. bottom: 30%;
  227. left: 50%;
  228. margin-left: -280rpx;
  229. right: 0;
  230. text-align: center;
  231. .title {
  232. padding-top: 40upx;
  233. font-size: 34upx;
  234. font-weight: bold;
  235. }
  236. .content {
  237. display: flex;
  238. justify-content: center;
  239. align-items: center;
  240. flex-direction: column;
  241. padding-top: 30rpx;
  242. padding-bottom: 150rpx;
  243. .userinfo-avatar {
  244. overflow: hidden;
  245. display: block;
  246. width: 140rpx;
  247. height: 140rpx;
  248. margin: 20rpx 20rpx 40rpx;
  249. border-radius: 50%;
  250. border: 2px solid #fff;
  251. }
  252. }
  253. .subtitle {
  254. color: #7e7e7e;
  255. font-size: 26upx;
  256. margin-top: 30rpx;
  257. }
  258. .btn-content {
  259. display: flex;
  260. width: 100%;
  261. border-radius: 0;
  262. border-top: 1upx solid #ebebeb;
  263. button {
  264. border: none;
  265. width: 50%;
  266. font-size: 30rpx;
  267. background-color: #FFFFFF;
  268. color: #66656a;
  269. }
  270. button:first-child {
  271. border-right: 1rpx solid #ebebeb;
  272. border-radius: 0 0 0 18rpx;
  273. }
  274. button:last-child {
  275. color: $base-color;
  276. border-radius: 0 0 18rpx 0;
  277. }
  278. }
  279. .btn-content button::after {
  280. border: none;
  281. }
  282. }
  283. </style>