login.vue 7.1 KB

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