tnui-wx-user-info.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <template>
  2. <view
  3. v-if="openModal"
  4. class="wx-authorization-modal"
  5. >
  6. <view
  7. class="wam__mask"
  8. @touchmove.prevent=""
  9. @tap.stop="closeModal"
  10. ></view>
  11. <!-- 内容区域 -->
  12. <view class="wam__wrapper">
  13. <!-- 关闭按钮 -->
  14. <view class="wam__close-btn" @tap.stop="closeModal">
  15. <text class="tn-icon-close"></text>
  16. </view>
  17. <!-- 标题 -->
  18. <!-- #ifdef MP-WEIXIN -->
  19. <view class="wam__title">获取您的昵称、头像</view>
  20. <!-- tips -->
  21. <view class="wam__sub-title">
  22. 获取用户头像、昵称,主要用于向用户提供具有辨识度的用户中心界面
  23. </view>
  24. <!-- #endif -->
  25. <!-- #ifdef H5 -->
  26. <view class="wam__title">获取您的手机号</view>
  27. <!-- tips -->
  28. <view class="wam__sub-title">
  29. 获取用户手机号,主要用于向用户提供具有辨识度的用户中心界面
  30. </view>
  31. <!-- #endif -->
  32. <!-- 头像选择 -->
  33. <!-- #ifdef MP-WEIXIN -->
  34. <view class="wam__avatar">
  35. <view class="button-shadow">
  36. <button
  37. class="button"
  38. open-type="chooseAvatar"
  39. @chooseavatar="chooseAvatarEvent"
  40. >
  41. <view v-if="userInfo.avatar" class="avatar__image">
  42. <image class="image" :src="userInfo.avatar" mode="aspectFill"></image>
  43. </view>
  44. <view v-else class="avatar__empty">
  45. <image class="image" src="" mode="aspectFill"></image>
  46. </view>
  47. <view class="avatar--icon">
  48. <view class="tn-icon-camera-fill"></view>
  49. </view>
  50. </button>
  51. </view>
  52. </view>
  53. <!-- #endif -->
  54. <!-- 昵称输入 -->
  55. <view class="wam__nickname">
  56. <!-- #ifdef MP-WEIXIN -->
  57. <view class="nickname__data">
  58. <input class="input" type="nickname" maxlength="13" v-model="userInfo.nickname" placeholder="请输入昵称" placeholder-style="color: #AAAAAA;">
  59. </view>
  60. <!-- #endif -->
  61. <!-- #ifdef H5 -->
  62. <view class="nickname__data">
  63. <input class="input" type="number" v-model="userInfo.phone" placeholder="请输入手机号" placeholder-style="color: #AAAAAA;">
  64. </view>
  65. <view class="nickname__data tn-flex align-center justify-between">
  66. <input class="input" style="width: 145px;" type="number" v-model="userInfo.code" placeholder="请输入验证码" placeholder-style="color: #AAAAAA;">
  67. <view @tap="getCode" class="getcode">{{tips}}</view>
  68. </view>
  69. <!-- #endif -->
  70. </view>
  71. <u-code :seconds="seconds" @end="end" @start="start" ref="uCode"
  72. @change="codeChange"></u-code>
  73. <!-- #ifdef MP-WEIXIN -->
  74. <view class="wam__button">
  75. <!-- 保存按钮 -->
  76. <view
  77. class="wam__submit-btn"
  78. hover-class="tn-btn-hover-class"
  79. :hover-stay-time="150"
  80. @tap.stop="submitUserInfo"
  81. >
  82. <!-- :class="[{
  83. 'disabled': !userInfo.avatar || !userInfo.nickname
  84. }]" -->
  85. 授 权 登 录
  86. </view>
  87. <!-- <button v-if="platform == 'wxMiniProgram'&&userInfo.avatar&&userInfo.nickname" class="getbuttons" open-type="getPhoneNumber" @getphonenumber="getphone"></button> -->
  88. </view>
  89. <!-- #endif -->
  90. <!-- #ifdef H5 -->
  91. <view class="wam__button">
  92. <!-- 保存按钮 -->
  93. <view
  94. class="wam__submit-btn"
  95. :class="[{
  96. 'disabled': !userInfo.phone || !userInfo.code
  97. }]"
  98. hover-class="tn-btn-hover-class"
  99. :hover-stay-time="150"
  100. @tap.stop="binding"
  101. >
  102. 授 权 登 录
  103. </view>
  104. </view>
  105. <!-- #endif -->
  106. </view>
  107. </view>
  108. </template>
  109. <script>
  110. import { mapMutations, mapActions, mapState, mapGetters } from 'vuex';
  111. import wechat from '@/common/wechat/wechat';
  112. import { apiurl } from '@/common/request/request';
  113. import platform from '@/common/platform/index';
  114. export default {
  115. options: {
  116. // 在微信小程序中将组件节点渲染为虚拟节点,更加接近Vue组件的表现(不会出现shadow节点下再去创建元素)
  117. virtualHost: true
  118. },
  119. props: {
  120. value: {
  121. type: Boolean,
  122. default: false
  123. }
  124. },
  125. data() {
  126. return {
  127. tips: '获取验证码',
  128. seconds: 60,
  129. openModal: false,
  130. userInfo: {
  131. avatar: '',
  132. nickname: ''
  133. },
  134. platform:platform.get()
  135. }
  136. },
  137. watch: {
  138. value: {
  139. handler(val) {
  140. this.openModal = val
  141. },
  142. immediate: true
  143. }
  144. },
  145. computed: {
  146. ...mapGetters(['appInfo','isLogin'])
  147. },
  148. methods: {
  149. ...mapActions(['getUserInfo']),
  150. // 头像选择
  151. chooseAvatarEvent(e) {
  152. var that = this;
  153. uni.uploadFile({
  154. url: apiurl('common/upload'),
  155. filePath: e.detail.avatarUrl,
  156. name: 'file',
  157. formData: {
  158. accept: 'image'
  159. },
  160. success: res2 => {
  161. let a = JSON.parse(res2.data);
  162. if (a.code == 0) {
  163. that.userInfo.avatar = a.data.path
  164. }else{
  165. uni.showToast({
  166. title:'上传头像失败',
  167. icon:'none'
  168. })
  169. }
  170. }
  171. });
  172. },
  173. // 绑定手机号
  174. async getphone(e) {
  175. let that = this;
  176. let res = await wechat.getPhoneNumber(e.detail);
  177. if(res.code === 0){
  178. // uni.showToast({
  179. // title:that.cur?'绑定成功':'登录成功',
  180. // duration:1500
  181. // })
  182. // that.whether = true;
  183. // that.$store.commit('userInfo',res.data)
  184. // uni.navigateTo({
  185. // url:'/pages/public/webview?url=https://y.iduomi.cc/api/wechat_login.html'
  186. // })
  187. uni.showToast({
  188. icon:'loading',
  189. title:'授权登录中...'
  190. })
  191. that.userprofile()
  192. }
  193. },
  194. getCode(){
  195. var that = this;
  196. if(!that.userInfo.phone){
  197. // uni.$u.toast('请输入手机号');
  198. // return;
  199. return uni.showToast({
  200. icon: 'none',
  201. title: '请输入手机号'
  202. })
  203. }else if(!uni.$u.test.mobile(that.userInfo.phone)){
  204. // uni.$u.toast('请输入正确的手机号');
  205. // return;
  206. return uni.showToast({
  207. icon: 'none',
  208. title: '请输入正确的手机号'
  209. })
  210. }
  211. if(that.$refs.uCode.canGetCode) {
  212. // 模拟向后端请求验证码
  213. uni.showLoading({
  214. title: '正在获取验证码'
  215. })
  216. that.$http('common.sendMobileCode',{mobile:that.userInfo.phone,event:'register'}).then(res=>{
  217. if(res.code == 0){
  218. setTimeout(() => {
  219. uni.hideLoading();
  220. // 这里此提示会被that.start()方法中的提示覆盖
  221. // uni.$u.toast('验证码已发送');
  222. uni.showToast({
  223. icon: 'none',
  224. title: '验证码已发送'
  225. })
  226. // 通知验证码组件内部开始倒计时
  227. that.$refs.uCode.start();
  228. }, 2000);
  229. }
  230. })
  231. } else {
  232. // uni.$u.toast('倒计时结束后再发送');
  233. uni.showToast({
  234. icon: 'none',
  235. title: '倒计时结束后再发送'
  236. })
  237. }
  238. },
  239. end() {
  240. // uni.$u.toast('倒计时结束');
  241. uni.showToast({
  242. icon: 'none',
  243. title: '倒计时结束'
  244. })
  245. },
  246. start() {
  247. // uni.$u.toast('倒计时开始');
  248. uni.showToast({
  249. icon: 'none',
  250. title: '倒计时开始'
  251. })
  252. },
  253. codeChange(text){
  254. // console.log(text);
  255. this.tips = text;
  256. },
  257. // 更新用户信息
  258. submitUserInfo() {
  259. // console.log(platform.get());
  260. // 判断是否已经选择了用户头像和输入了用户昵称
  261. if (!this.userInfo.avatar) {
  262. return uni.showToast({
  263. icon: 'none',
  264. title: '请上传头像'
  265. })
  266. }else if(!this.userInfo.nickname){
  267. return uni.showToast({
  268. icon: 'none',
  269. title: '请输入昵称'
  270. })
  271. }else if(!uni.$u.test.rangeLength('abc', [1, 13])){
  272. return uni.showToast({
  273. icon: 'none',
  274. title: '昵称过长'
  275. })
  276. }else{
  277. uni.showToast({
  278. icon:'loading',
  279. title:'授权登录中...'
  280. })
  281. this.userprofile()
  282. }
  283. // 更新完成事件
  284. // this.$emit('updated', this.userInfo)
  285. },
  286. userprofile(){
  287. this.$http('user.profile',{nickname: this.userInfo.nickname,avatar: this.userInfo.avatar}).then( res => {
  288. if(res.code == 0){
  289. this.$store.dispatch('getUserInfo')
  290. setTimeout(function() {
  291. uni.showToast({
  292. title:'登录成功',
  293. })
  294. setTimeout(function() {
  295. uni.navigateBack()
  296. }, 1500);
  297. }, 1500);
  298. }else{
  299. that.$u.toast('登录失败,请重试');
  300. }
  301. })
  302. },
  303. // 关闭弹框
  304. closeModal() {
  305. this.$emit('input', false)
  306. },
  307. // 绑定手机号
  308. binding(){
  309. let that = this;
  310. if(!that.userInfo.phone){
  311. // uni.$u.toast('请输入手机号');
  312. // return;
  313. return uni.showToast({
  314. icon: 'none',
  315. title: '请输入手机号'
  316. })
  317. }else if(!uni.$u.test.mobile(that.userInfo.phone)){
  318. // uni.$u.toast('请输入正确的手机号');
  319. // return;
  320. return uni.showToast({
  321. icon: 'none',
  322. title: '请输入正确的手机号'
  323. })
  324. }else if(!that.userInfo.code){
  325. return uni.showToast({
  326. icon: 'none',
  327. title: '请输入验证码'
  328. })
  329. }
  330. uni.showToast({
  331. icon:'loading',
  332. title:'授权登录中...'
  333. })
  334. that.$http(
  335. 'wechat.authBindindPhoneCode',
  336. {
  337. phone: that.userInfo.phone,
  338. code: that.userInfo.code
  339. },
  340. // '中...'
  341. ).then(res => {
  342. if (res.code === 0) {
  343. uni.showToast({
  344. title:'登录成功',
  345. duration:1500
  346. })
  347. setTimeout(function(){
  348. // uni.navigateBack({
  349. // })
  350. // this.$emit('updated', that.userInfo)
  351. uni.switchTab({
  352. url:'/pages/user/user'
  353. })
  354. },1500);
  355. that.$store.commit('userInfo',res.data)
  356. } else {
  357. that.$u.toast(res.msg);
  358. }
  359. });
  360. },
  361. }
  362. }
  363. </script>
  364. <style lang="scss" scoped>
  365. @import '../../static/iconfont/tuniao.css';
  366. .wx-authorization-modal {
  367. position: fixed;
  368. left: 0;
  369. top: 0;
  370. width: 100vw;
  371. height: 100vh;
  372. // z-index: 99998;
  373. view {
  374. box-sizing: border-box;
  375. }
  376. .image {
  377. width: 100%;
  378. height: 100%;
  379. border-radius: inherit;
  380. }
  381. .wam {
  382. /* mask */
  383. &__mask {
  384. position: absolute;
  385. left: 0;
  386. top: 0;
  387. width: 100%;
  388. height: 100%;
  389. background-color: rgba(0, 0, 0, 0.5);
  390. opacity: 0;
  391. animation: showMask 0.25s ease 0.1s forwards;
  392. }
  393. /* close-btn */
  394. &__close-btn {
  395. position: absolute;
  396. top: 30rpx;
  397. right: 30rpx;
  398. // z-index: 99999;
  399. }
  400. /* wrapper */
  401. &__wrapper {
  402. position: absolute;
  403. left: 0;
  404. bottom: 0;
  405. width: 100%;
  406. background-color: #FFFFFF;
  407. border-radius: 20rpx 20rpx 0rpx 0rpx;
  408. padding: 40rpx;
  409. padding-top: 60rpx;
  410. padding-bottom: 40rpx;
  411. padding-bottom: calc(constant(safe-area-inset-bottom) + 40rpx);
  412. padding-bottom: calc(env(safe-area-inset-bottom) + 40rpx);
  413. transform-origin: center bottom;
  414. transform: scaleY(0);
  415. animation: showWrapper 0.25s ease 0.1s forwards;
  416. // z-index: 99999;
  417. }
  418. /* title */
  419. &__title {
  420. font-size: 34rpx;
  421. }
  422. /* sub-title */
  423. &__sub-title {
  424. font-size: 26rpx;
  425. color: #AAAAAA;
  426. margin-top: 16rpx;
  427. padding-bottom: 30rpx;
  428. }
  429. /* 头像选择 */
  430. &__avatar {
  431. width: 100%;
  432. margin-top: 30rpx;
  433. display: flex;
  434. align-items: center;
  435. justify-content: center;
  436. .button-shadow {
  437. border: 8rpx solid rgba(255,255,255,0.05);
  438. box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
  439. border-radius: 50%;
  440. }
  441. .button {
  442. position: relative;
  443. width: 160rpx;
  444. height: 160rpx;
  445. border-radius: 50%;
  446. overflow: visible;
  447. background-image: repeating-linear-gradient(45deg, #E4E9EC, #F8F7F8);
  448. color: #FFFFFF;
  449. background-color: transparent;
  450. padding: 0;
  451. margin: 0;
  452. font-size: inherit;
  453. line-height: inherit;
  454. border: none;
  455. &::after {
  456. border: none;
  457. }
  458. }
  459. .avatar {
  460. &__empty, &__image {
  461. width: 100%;
  462. height: 100%;
  463. border-radius: inherit;
  464. }
  465. &--icon {
  466. position: absolute;
  467. right: -10rpx;
  468. bottom: -6rpx;
  469. width: 60rpx;
  470. height: 60rpx;
  471. // transform: translate(50%, 50%);
  472. background-color: #1D2541;
  473. color: #FFFFFF;
  474. border-radius: 50%;
  475. border: 6rpx solid #FFFFFF;
  476. line-height: 1;
  477. font-size: 36rpx;
  478. display: flex;
  479. align-items: center;
  480. justify-content: center;
  481. }
  482. }
  483. }
  484. /* 昵称 */
  485. &__nickname {
  486. margin-top: 40rpx;
  487. .nickname {
  488. &__data {
  489. margin-top: 26rpx;
  490. width: 100%;
  491. // padding: 26rpx 46rpx;
  492. padding: 26rpx 26rpx 26rpx 46rpx;
  493. border-radius: 16rpx;
  494. background-color: #F8F7F8;
  495. .input {
  496. color: #080808;
  497. }
  498. }
  499. }
  500. }
  501. /* 保存按钮 */
  502. &__submit-btn {
  503. width: 100%;
  504. background-color: #05C160;
  505. color: #FFFFFF;
  506. margin-top: 60rpx;
  507. border-radius: 16rpx;
  508. padding: 25rpx;
  509. font-size: 32rpx;
  510. display: flex;
  511. align-items: center;
  512. justify-content: center;
  513. &.disabled {
  514. background-color: #E6E6E6;
  515. }
  516. }
  517. }
  518. }
  519. .tn-btn-hover-class {
  520. box-shadow: inset 10rpx 2rpx 40rpx 0rpx rgba(0, 0, 0, 0.05);
  521. }
  522. @keyframes showMask {
  523. 0% {
  524. opacity: 0;
  525. }
  526. 100% {
  527. opacity: 1;
  528. }
  529. }
  530. @keyframes showWrapper {
  531. 0% {
  532. transform: scaleY(0);
  533. }
  534. 100% {
  535. transform: scaleY(1);
  536. }
  537. }
  538. .wam__button{
  539. position: relative;
  540. font-weight: bold;
  541. .getbuttons{
  542. position: absolute;
  543. width: 100%;
  544. height: 88rpx;
  545. opacity: 0;
  546. top: 0;
  547. }
  548. }
  549. .getcode{
  550. color: #fff;
  551. background: #05C160;
  552. padding: 8rpx 26rpx;
  553. border-radius: 12rpx;
  554. font-size: 24rpx;
  555. }
  556. </style>