login.vue 7.2 KB

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