login.vue 7.3 KB

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