login.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <template>
  2. <view class="" :style="vuex_skin">
  3. <!-- #ifdef MP-WEIXIN -->
  4. <!-- 获取手机号 -->
  5. <u-popup v-model="phoneShow" mode="center" border-radius="14" width="70%">
  6. <view class="" style="padding: 30rpx 50rpx 0;">
  7. <view class="center">
  8. <text class="text-bold">授权手机号</text>
  9. </view>
  10. <view style="padding: 80rpx 0;">
  11. <view class=" margin-bottom-20 text-sm text-center" style="line-height: 42rpx;">
  12. <text style="color: #949494;">为了保证用户评选公平,大赛采用实名制投票,需要授权获取您的手机号</text>
  13. </view>
  14. </view>
  15. <view class="flex justify-around">
  16. <button @click="phoneShow=false" class="cu-btn radius bg-gray "
  17. style="height: 60rpx;margin-bottom: 20rpx;font-size: 24rpx;">
  18. 暂不授权
  19. </button>
  20. <button class="cu-btn bg-base radius" style="height: 60rpx;margin-bottom: 20rpx;font-size: 24rpx;"
  21. open-type="getPhoneNumber" @getphonenumber="getphonenumber">
  22. 确定授权
  23. </button>
  24. </view>
  25. </view>
  26. </u-popup>
  27. <!-- 获取头像昵称 -->
  28. <view @touchmove.stop.prevent="clear" v-show="dialogShow">
  29. <view class="popup_mask" @touchmove.stop.prevent="clear"></view>
  30. <view class="">
  31. <view class="dt-login-wrap" >
  32. <view class="title">请先登录</view>
  33. <view class="content">
  34. <view class="userinfo-avatar">
  35. <open-data type="userAvatarUrl"></open-data>
  36. </view>
  37. <open-data type="userNickName"></open-data>
  38. <text class="subtitle">申请获取您的公开信息(昵称,头像)</text>
  39. </view>
  40. <view class="btn-content setBtnCss">
  41. <button @click="hide" style="padding: 10rpx 0;">暂不登录</button>
  42. <button @click="getUserProfile" style="padding: 10rpx 0;">立即登录</button>
  43. <!-- <button v-else open-type="getUserInfo" @getuserinfo="getuserinfo">立即登录</button> -->
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. <!-- #endif -->
  49. <!-- #ifndef MP-WEIXIN -->
  50. <view @touchmove.stop.prevent="clear" v-show="dialogShow">
  51. <view class="popup_mask" @touchmove.stop.prevent="clear"></view>
  52. <view class="">
  53. <view class="dt-login-wrap" style="width: 580rpx;margin-left: -290rpx;">
  54. <view class="title">请先登录</view>
  55. <view class="content" style="padding: 120rpx 40rpx ;">
  56. <u-form :model="form" ref="uForm" label-width="140" :border-bottom="borderBottom">
  57. <u-form-item label="手机号">
  58. <u-input v-model="form.phone" :clearable="false" />
  59. </u-form-item>
  60. <u-form-item label="验证码">
  61. <u-input :clearable="false" placeholder="请输入验证码" v-model="form.code"
  62. type="text"></u-input>
  63. <button slot="right" class="u-reset-button cu-btn radius sm bg-base" style="padding: 0 15rpx;height: 42rpx;line-height: 42rpx;" @click="getCode">
  64. {{codeTips}}
  65. </button>
  66. </u-form-item>
  67. </u-form>
  68. <view class="text-sm text-gray margin-top-20 text-left">
  69. <text style="color: #EF9944;">* 为了保证投票的公平性,防止刷票行为,请先完成手机短信验证</text>
  70. </view>
  71. </view>
  72. <view class="btn-content setBtnCss "
  73. style="background-color: #FFFFFF;border-radius: 20rpx;border-top: 1rpx solid #f4f4f4;">
  74. <button @click="hide" style="padding: 5rpx 0;border-right: 1rpx solid #f4f4f4;">暂不验证</button>
  75. <button @click="h5Login" style="padding: 5rpx 0;">立即验证</button>
  76. </view>
  77. </view>
  78. </view>
  79. </view>
  80. <!-- #endif -->
  81. <u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
  82. <toast ref="toast"></toast>
  83. </view>
  84. </template>
  85. <script>
  86. import pointAuth from "./alert/pointAuth.vue"
  87. export default {
  88. name: "login",
  89. components: {
  90. pointAuth
  91. },
  92. data() {
  93. return {
  94. customStyle: {
  95. 'backgroundColor': '#EF9944',
  96. 'color': '#ffffff'
  97. },
  98. codeTips: '',
  99. borderBottom: false,
  100. notToast: false,
  101. canIUseGetUserProfile: false,
  102. //保存时间七天
  103. cacheTime: 1 * 24 * 60 * 60 * 7,
  104. dialogShow: false,
  105. jscode: '',
  106. //邀请码
  107. inviteCode: '',
  108. userInfo: '',
  109. sessionKey: '',
  110. //获取手机号
  111. phoneShow: false,
  112. //h5登录
  113. form: {
  114. phone: '',
  115. code: '',
  116. smsId:""
  117. }
  118. };
  119. },
  120. beforeDestroy() {
  121. if (this.timer) {
  122. clearTimeout(this.timer)
  123. this.timer = null
  124. }
  125. },
  126. created() {
  127. if (uni.getUserProfile) {
  128. this.canIUseGetUserProfile = false
  129. }
  130. },
  131. methods: {
  132. codeChange(text) {
  133. this.codeTips = text;
  134. },
  135. // 获取验证码
  136. getCode() {
  137. if (this.$isEmpty(this.form.phone)) {
  138. this.$refs.toast.info('请输入手机号')
  139. return
  140. }
  141. if (this.$refs.uCode.canGetCode) {
  142. // 模拟向后端请求验证码
  143. let params = {
  144. phone: this.form.phone
  145. }
  146. let p = this.$u.queryParams(params)
  147. this.$api.sms.send(p).then(res => {
  148. if (res.data.data.success) {
  149. this.form.smsId =res.data.data.id
  150. this.$refs.toast.info('验证码已发送')
  151. // 通知验证码组件内部开始倒计时
  152. } else {
  153. this.$u.toast(res.data);
  154. }
  155. this.$refs.uCode.start();
  156. })
  157. } else {
  158. this.$refs.toast.info('倒计时结束后再发送')
  159. }
  160. },
  161. async h5Login() {
  162. if (this.$isEmpty(this.form.phone)) {
  163. this.$u.toast("请输入手机号")
  164. return
  165. }
  166. if (this.$isEmpty(this.form.code)) {
  167. this.$u.toast("请输入验证码")
  168. return
  169. }
  170. if (this.$isEmpty(this.form.smsId)) {
  171. this.$u.toast("验证码已过期,请重新获取")
  172. return
  173. }
  174. let res = await this.$api.loginUser.h5Login(this.form)
  175. if (this.$isEmpty(res.data.data) && !res.success) {
  176. this.$u.toast(res.msg)
  177. return
  178. }
  179. let userInfo = res.data.data
  180. console.log(userInfo);
  181. this.$u.toast("登录成功")
  182. this.$cache.put("userInfo", userInfo)
  183. this.$u.vuex('vuex_userId', userInfo.id)
  184. this.$u.vuex('vuex_phone', userInfo.phone)
  185. console.log(this.vuex_phone, "this.vuex_phone");
  186. this.$emit('signIn', userInfo)
  187. this.hide()
  188. },
  189. //获取手机号
  190. showPhoneModal() {
  191. this.phoneShow = true
  192. },
  193. //隐藏授权弹框
  194. hidePhoneModal() {
  195. this.phoneShow = false
  196. },
  197. show(notToast) {
  198. this.notToast = notToast || false
  199. //获取邀请码
  200. this.inviteCode = this.$cache.get('inviteCode')
  201. this.dialogShow = true
  202. // #ifdef MP-WEIXIN
  203. this.$mpi.wxLogin().then(res => {
  204. this.jscode = res.code
  205. })
  206. this.setTimer()
  207. // #endif
  208. },
  209. hide() {
  210. if (this.timer) {
  211. clearTimeout(this.timer)
  212. this.timer = null
  213. }
  214. this.dialogShow = false;
  215. this.$emit('hide')
  216. },
  217. async setTimer() {
  218. let respWx = await this.$mpi.wxLogin()
  219. this.jscode = respWx.code
  220. // 每隔4分钟重新请求 jscode
  221. this.timer = setTimeout(() => {
  222. this.setTimer()
  223. }, 240000)
  224. },
  225. getUserProfile(e) {
  226. uni.getUserProfile({
  227. desc: '用于获取头像和昵称',
  228. success: (res) => {
  229. this.userInfo = res.userInfo
  230. console.log("getUserProfile获取用户信息", this.userInfo);
  231. this.doLogin()
  232. }
  233. })
  234. },
  235. getuserinfo(e) {
  236. console.log("getuserinfo获取用户信息", e);
  237. if (e.detail.errMsg != 'getUserInfo:ok') {
  238. return
  239. }
  240. this.userInfo = e.detail.userInfo
  241. this.doLogin()
  242. },
  243. getSex(gender) {
  244. if (gender == 1) {
  245. return "男"
  246. } else if (gender == 2) {
  247. return "女"
  248. } else {
  249. return "保密"
  250. }
  251. },
  252. async doLogin() {
  253. this.dialogShow = false
  254. try {
  255. this.respLogin = (await this.$api.wxApp.getOpenId({
  256. code: this.jscode
  257. })).data
  258. let openId = this.respLogin.data.openid
  259. if (this.$isEmpty(openId)) {
  260. this.$refs.toast.error('登录失败')
  261. return
  262. }
  263. let unionid = this.respLogin.data.unionid || ''
  264. this.sessionKey = this.respLogin.data.session_key
  265. //通过openid查询数据库中该用户是否存在
  266. let params = {
  267. nickName: this.userInfo.nickName,
  268. avatar: this.userInfo.avatarUrl,
  269. sex: this.getSex(this.userInfo.gender),
  270. unionid,
  271. openid: openId,
  272. province: this.userInfo.province,
  273. city: this.userInfo.city
  274. }
  275. //将openid
  276. let resp = await this.$api.loginUser.login(params)
  277. let userData = resp.data.data
  278. if (this.$isNotEmpty(userData.id)) {
  279. //登录成功
  280. let obj = {
  281. ...userData,
  282. sessionKey: this.sessionKey,
  283. }
  284. //将openid和sessionkey存进缓存中,有效时间为七天
  285. this.$cache.put("userInfo", obj)
  286. this.$u.vuex('vuex_userId', userData.id)
  287. if (userData.phone) {
  288. this.$u.vuex('vuex_phone', userData.phone)
  289. }
  290. //移除邀请码
  291. this.$cache.remove('inviteCode')
  292. this.dialogShow = false
  293. //获取手机号
  294. if (this.$isEmpty(this.vuex_phone) && this.$config.isAuthPhone) {
  295. this.showPhoneModal()
  296. } else {
  297. if (!this.notToast) {
  298. this.$u.toast('登录成功')
  299. }
  300. }
  301. //登录成功
  302. this.$emit('signIn', resp)
  303. } else {
  304. this.$refs.toast.error('登录失败')
  305. }
  306. } catch (e) {
  307. console.error(e);
  308. this.$refs.toast.error('登录失败')
  309. } finally {
  310. if (this.timer) {
  311. clearTimeout(this.timer)
  312. this.setTimer()
  313. }
  314. }
  315. },
  316. clear() {
  317. return
  318. },
  319. async getphonenumber(e) {
  320. this.hidePhoneModal()
  321. if (e.detail.errMsg != 'getPhoneNumber:ok') {
  322. console.log("获取手机号失败");
  323. return
  324. }
  325. if (this.$isEmpty(this.sessionKey)) {
  326. this.sessionKey = this.$cache.get('userInfo').sessionKey
  327. }
  328. let params = {
  329. sessionKey: this.sessionKey,
  330. encryptedData: e.detail.encryptedData,
  331. iv: e.detail.iv
  332. }
  333. this.$api.wxApp.getOpenData(params).then(res => {
  334. if (res.data.success) {
  335. let obj = JSON.parse(res.data.data)
  336. let phone = obj.phoneNumber
  337. this.cachePhone(phone)
  338. }
  339. })
  340. },
  341. cachePhone(phone) {
  342. let userInfo = this.$cache.get('userInfo')
  343. let params = {
  344. userId: userInfo.id,
  345. phone
  346. }
  347. this.$api.loginUser.savePhone(params).then(res => {
  348. console.log(res);
  349. if (res.data.success) {
  350. userInfo.phone = phone
  351. this.$cache.put('userInfo', userInfo)
  352. this.$u.vuex('vuex_phone', phone)
  353. this.$emit('phoneSuccess')
  354. } else {
  355. this.$refs.toast.error(res.msg)
  356. }
  357. })
  358. }
  359. }
  360. };
  361. </script>
  362. <style lang="scss">
  363. .popup_mask {
  364. position: fixed;
  365. bottom: 0;
  366. top: 0;
  367. left: 0;
  368. right: 0;
  369. background-color: rgba(0, 0, 0, 0.4);
  370. opacity: 1;
  371. z-index: 98;
  372. }
  373. .dt-login-wrap {
  374. position: fixed;
  375. border-radius: 16rpx;
  376. background: #ffffff;
  377. width: 560rpx;
  378. z-index: 99;
  379. bottom: 30%;
  380. left: 50%;
  381. margin-left: -280rpx;
  382. right: 0;
  383. text-align: center;
  384. .title {
  385. padding-top: 40upx;
  386. font-size: 34upx;
  387. font-weight: bold;
  388. }
  389. .content {
  390. display: flex;
  391. justify-content: center;
  392. align-items: center;
  393. flex-direction: column;
  394. padding-top: 30rpx;
  395. padding-bottom: 150rpx;
  396. .userinfo-avatar {
  397. overflow: hidden;
  398. display: block;
  399. width: 140rpx;
  400. height: 140rpx;
  401. margin: 20rpx 20rpx 40rpx;
  402. border-radius: 50%;
  403. border: 2px solid #fff;
  404. }
  405. }
  406. .subtitle {
  407. color: #7e7e7e;
  408. font-size: 26upx;
  409. margin-top: 30rpx;
  410. }
  411. .btn-content {
  412. display: flex;
  413. width: 100%;
  414. border-radius: 0;
  415. border-top: 1upx solid #ebebeb;
  416. button {
  417. border: none;
  418. width: 50%;
  419. font-size: 30rpx;
  420. background-color: #FFFFFF;
  421. color: #66656a;
  422. }
  423. button:first-child {
  424. border-right: 1rpx solid #ebebeb;
  425. border-radius: 0 0 0 18rpx;
  426. }
  427. button:last-child {
  428. color: var(--bgColor);
  429. border-radius: 0 0 18rpx 0;
  430. }
  431. }
  432. .btn-content button::after {
  433. border: none;
  434. }
  435. }
  436. </style>