user_profile.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <template>
  2. <view class="user-profile p-t-10">
  3. <view class="content">
  4. <view class="bg-white">
  5. <view class="user-avatar-box flex-col col-center">
  6. <uploader @after-read="afterRead" use-slot preview-size="120rpx">
  7. <image class="user-avatar" :src="userInfo.avatar || '/static/images/portrait_empty.png'" />
  8. </uploader>
  9. <!-- #ifdef MP-WEIXIN -->
  10. <view class="xs muted flex row-center m-t-20" @tap="getUserProfileFun">更新微信头像</view>
  11. <!-- #endif -->
  12. </view>
  13. <view class="row-info flex bdb-line" @tap="changeName">
  14. <view class="label md">昵称</view>
  15. <view class="md flex-1">{{ userInfo.nickname }}</view>
  16. <u-icon name="arrow-right" />
  17. </view>
  18. <view class="row-info flex bdb-line" @tap="changeSex">
  19. <view class="label md">性别</view>
  20. <view class="md flex-1" :class="userInfo.sex == 0 ? 'muted' : ''">{{ userInfo.sex }}</view>
  21. <u-icon name="arrow-right" />
  22. </view>
  23. <view class="row-info flex bdb-line">
  24. <view class="label md">手机号</view>
  25. <view class="md flex-1" :class="{ muted: !userInfo.mobile }">{{ userInfo.mobile ? userInfo.mobile : '未绑定' }}</view>
  26. <!-- #ifdef H5 || APP-PLUS -->
  27. <view class="bd-btn br60 flex row-center" @tap="showModifyMobile">{{ userInfo.mobile ? '更换手机号' : '绑定手机号' }}</view>
  28. <!-- #endif -->
  29. <!-- #ifdef MP-WEIXIN -->
  30. <button class="bd-btn br60 flex row-center" size="sm" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  31. {{ userInfo.mobile ? '更换手机号' : '绑定手机号' }}
  32. </button>
  33. <!-- #endif -->
  34. </view>
  35. <!-- #ifndef MP-WEIXIN-->
  36. <view class="row-info flex row-between" @tap="showPwdPop">
  37. <view class="label md">登录密码</view>
  38. <view class="flex">
  39. <view class="muted">点击设置</view>
  40. <u-icon name="arrow-right" />
  41. </view>
  42. </view>
  43. <!-- #endif -->
  44. <view class="row-info flex row-between bdb-line mt10" @tap="goToExplain(0)">
  45. <view class="label md">服务协议</view>
  46. <u-icon name="arrow-right" />
  47. </view>
  48. <view class="row-info flex row-between bdb-line" @tap="goToExplain(1)">
  49. <view class="label md">隐私政策</view>
  50. <u-icon name="arrow-right" />
  51. </view>
  52. <view class="row-info flex row-between bdb-line" @tap="goLicense()">
  53. <view class="label md">资质信息</view>
  54. <u-icon name="arrow-right" />
  55. </view>
  56. <view class="row-info flex row-between">
  57. <view class="label md">关于我们</view>
  58. <view>v{{ version }}</view>
  59. </view>
  60. </view>
  61. </view>
  62. <view class="bg-primary white save-btn flex row-center lg" @tap="logout">退出登录</view>
  63. <!-- #ifndef MP-WEIXIN -->
  64. <view class="bg-primary white save-btn flex row-center lg" @click="cancel">注销账户</view>
  65. <u-popup mode="center" type="center" v-model="showGs">
  66. <view class="psw-wrapper">
  67. <view class="psw-title">请输入自己的账户</view>
  68. <input type="text" v-model="password" class="psw-ipt" />
  69. <view class="psw-btn">
  70. <text @click.stop="qx">取消</text>
  71. <text class="psw-qd" @click.stop="pswQd">确定</text>
  72. </view>
  73. </view>
  74. </u-popup>
  75. <!-- #endif -->
  76. <!-- 版权信息 -->
  77. <view class="license xs text-center" v-if="appConfig.copyright_info">
  78. <view>{{ appConfig.copyright_info }}</view>
  79. <view>{{ appConfig.icp_number }}</view>
  80. </view>
  81. <!-- #ifndef MP-WEIXIN -->
  82. <u-popup type="center" closeable v-model="showMobile" mode="center" border-radius="14">
  83. <view class="modify-container bg-white" v-show="showMobile">
  84. <view class="title xl text-center">{{ userInfo.mobile ? '更换手机号' : '绑定手机号' }}</view>
  85. <u-field label="+86" v-if="userInfo.mobile" label-width="100" disabled v-model="userInfo.mobile"></u-field>
  86. <u-field label="+86" v-else label-width="140" v-model="new_mobile" placeholder="请输入手机号"></u-field>
  87. <u-field v-model="mobileCode" label="验证码" label-width="140" placeholder="请输入验证码">
  88. <view slot="right" class="primary send-code-btn br60 flex row-center" @tap="sendSmsFun">
  89. <u-verification-code :keep-running="true" unique-key="mobile" ref="uCode" @change="codeChange"></u-verification-code>
  90. <view class="sm">{{ codeTips }}</view>
  91. </view>
  92. </u-field>
  93. <u-field label="新手机号" v-if="userInfo.mobile" label-width="140" placeholder="请输入新的手机号码" v-model="new_mobile"></u-field>
  94. <view class="primary m-t-10 xs">{{ userInfo.mobile ? '更改' : '绑定' }}手机号码成功后,您的账号将会变更为该设置号码</view>
  95. <view class="btn bg-primary white flex row-center" @tap="changeUserMobileFun">确定</view>
  96. </view>
  97. </u-popup>
  98. <!-- #endif -->
  99. <u-popup v-model="showNickName" :closeable="true" mode="center" border-radius="14">
  100. <view class="modify-container bg-white" v-show="showNickName">
  101. <view class="title xl text-center">修改用户名</view>
  102. <u-field v-model="newNickname" label="新昵称" placeholder="请输入新的昵称"></u-field>
  103. <view class="btn bg-primary white flex row-center" @tap="changeNameConfirm">确定</view>
  104. </view>
  105. </u-popup>
  106. <!-- #ifndef MP-WEIXIN -->
  107. <u-popup v-model="showPwd" closeable mode="center" border-radius="14">
  108. <view class="modify-container bg-white">
  109. <view class="title xl text-center">设置密码</view>
  110. <u-field label="+86" disabled label-width="100" v-model="userInfo.mobile"></u-field>
  111. <u-field v-model="pwdCode" label="验证码" label-width="140" placeholder="请输入验证码">
  112. <view slot="right" class="primary send-code-btn br60 flex row-center" @tap="sendSmsFun">
  113. <u-verification-code unique-key="password" :keep-running="true" ref="uCode" @change="codeChange"></u-verification-code>
  114. <view class="sm">{{ codeTips }}</view>
  115. </view>
  116. </u-field>
  117. <u-field label="设置密码" label-width="140" type="password" placeholder="请输入新密码" v-model="pwd"></u-field>
  118. <u-field label="确认密码" label-width="140" type="password" placeholder="再次输入新密码确认" v-model="comfirmPwd"></u-field>
  119. <view class="btn bg-primary white flex row-center" @tap="forgetPwdFun">确定</view>
  120. </view>
  121. </u-popup>
  122. <!-- #endif -->
  123. <u-picker mode="selector" v-model="showPicker" :default-selector="[0]" :range="sexList" @confirm="onConfirm" />
  124. </view>
  125. </template>
  126. <script>
  127. import { userLogout, getUserInfo, getWxMnpMobile, setUserInfo, setWechatInfo, changeUserMobile } from '@/api/user';
  128. import { baseURL, version } from '@/config/app';
  129. import { sendSms, forgetPwd } from '@/api/app';
  130. import { SMSType } from '@/utils/type';
  131. import { mapGetters } from 'vuex';
  132. import { uploadFile, isWeixinClient, trottle } from '@/utils/tools';
  133. import { getWxCode, getUserProfile } from '@/utils/login';
  134. const FieldType = {
  135. NONE: '',
  136. SEX: 'sex',
  137. NICKNAME: 'nickname',
  138. AVATAR: 'avatar',
  139. MOBILE: 'mobile'
  140. };
  141. export default {
  142. name: 'userProfile',
  143. data() {
  144. return {
  145. action: baseURL + '/api/file/formimage',
  146. password: '',
  147. fileList: [],
  148. userInfo: {},
  149. new_mobile: '',
  150. pwdCode: '',
  151. mobileCode: '',
  152. newNickname: '',
  153. sexList: ['男', '女'],
  154. fieldType: FieldType.NONE,
  155. showGs: false,
  156. showPicker: false,
  157. showMobile: false,
  158. showPwd: false,
  159. showNickName: false,
  160. codeTips: '',
  161. canSendSms: true,
  162. pwd: '',
  163. comfirmPwd: '',
  164. smsType: '',
  165. code: '',
  166. version
  167. };
  168. },
  169. methods: {
  170. codeChange(text) {
  171. this.codeTips = text;
  172. },
  173. onSuccess(e) {
  174. console.log(e);
  175. },
  176. // 文件上传读取
  177. afterRead(e) {
  178. const file = e;
  179. console.log(file);
  180. uni.showLoading({
  181. title: '正在上传中...',
  182. mask: true
  183. });
  184. file.forEach(item => {
  185. uploadFile(item.path).then(res => {
  186. uni.hideLoading();
  187. this.fieldType = FieldType.AVATAR;
  188. this.setUserInfoFun(res.base_uri);
  189. });
  190. });
  191. },
  192. // 更新小程序头像信息
  193. async getUserProfileFun() {
  194. const { userInfo } = await getUserProfile();
  195. const { avatarUrl, nickName, gender } = userInfo;
  196. const { msg, code } = await setWechatInfo({
  197. nickname: nickName,
  198. avatar: avatarUrl,
  199. sex: gender
  200. });
  201. if (code == 1) {
  202. this.$toast({
  203. title: msg
  204. });
  205. this.getUserInfoFun();
  206. }
  207. },
  208. cancel() {
  209. this.showGs = true;
  210. },
  211. qx() {
  212. this.password = '';
  213. this.showGs = false;
  214. },
  215. pswQd() {
  216. if (this.password != this.userInfo.mobile) {
  217. this.showGs = false;
  218. this.password = '';
  219. uni.showToast({
  220. title: '请输入自己的账户',
  221. icon: 'none'
  222. });
  223. return;
  224. }
  225. this.showGs = false;
  226. this.password = '';
  227. uni.showToast({
  228. title: '申请注销成功,请耐心等待审核',
  229. icon: 'none'
  230. });
  231. },
  232. // end
  233. logout() {
  234. //  退出登录
  235. userLogout().then(res => {
  236. if (res.code == 1) {
  237. this.$store.commit('logout');
  238. this.$toast({
  239. title: '退出成功'
  240. });
  241. setTimeout(() => {
  242. this.$Router.replaceAll('/pages/index/index');
  243. }, 500);
  244. }
  245. });
  246. },
  247. goToExplain(value) {
  248. this.$Router.push({
  249. path: '/bundle/pages/server_explan/server_explan',
  250. query: {
  251. type: value
  252. }
  253. });
  254. },
  255. goLicense() {
  256. this.$Router.push({
  257. path: '/bundle/pages/license/license',
  258. query: {
  259. id: ''
  260. }
  261. });
  262. },
  263. // 发送短信
  264. sendSmsFun(type) {
  265. console.log(this.smsType);
  266. if (!this.$refs.uCode.canGetCode) return;
  267. sendSms({
  268. mobile: this.userInfo.mobile || this.new_mobile,
  269. key: this.smsType
  270. }).then(res => {
  271. if (res.code == 1) {
  272. this.$toast({
  273. title: res.msg
  274. });
  275. this.$refs.uCode.start();
  276. }
  277. });
  278. },
  279. getUserInfoFun() {
  280. getUserInfo().then(res => {
  281. if (res.code == 1) {
  282. this.userInfo = res.data;
  283. }
  284. });
  285. },
  286. // 更换手机号
  287. showModifyMobile() {
  288. this.new_mobile = '';
  289. this.showMobile = true;
  290. this.smsType = this.userInfo.mobile ? SMSType.CHANGE_MOBILE : SMSType.BIND;
  291. },
  292. changeUserMobileFun() {
  293. if (!this.mobileCode) {
  294. this.$toast({
  295. title: '请输入验证码'
  296. });
  297. return;
  298. }
  299. if (!this.new_mobile) {
  300. this.$toast({
  301. title: '请输入新的手机号码'
  302. });
  303. return;
  304. }
  305. changeUserMobile({
  306. mobile: this.userInfo.mobile,
  307. new_mobile: this.new_mobile,
  308. code: this.mobileCode,
  309. action: this.userInfo.mobile ? 'change' : 'binding'
  310. }).then(res => {
  311. if (res.code == 1) {
  312. this.showMobile = false;
  313. this.$toast({
  314. title: res.msg
  315. });
  316. this.getUserInfoFun();
  317. }
  318. });
  319. },
  320. // 修改用户信息
  321. setUserInfoFun(value) {
  322. setUserInfo({
  323. field: this.fieldType,
  324. value: value
  325. }).then(res => {
  326. if (res.code == 1) {
  327. this.$toast({
  328. title: res.msg
  329. });
  330. this.getUserInfoFun();
  331. }
  332. });
  333. },
  334. onConfirm(value) {
  335. this.setUserInfoFun(value[0] + 1);
  336. this.showPicker = false;
  337. },
  338. changeSex(e) {
  339. this.showPicker = true;
  340. this.fieldType = FieldType.SEX;
  341. },
  342. // 修改密码
  343. showPwdPop() {
  344. if (!this.userInfo.mobile) {
  345. this.$toast({
  346. title: '请绑定手机后再设置密码'
  347. });
  348. return;
  349. }
  350. this.smsType = SMSType.FINDPWD;
  351. this.showPwd = true;
  352. },
  353. forgetPwdFun() {
  354. let { pwdCode, pwd, comfirmPwd } = this;
  355. if (!pwdCode) {
  356. this.$toast({
  357. title: '请输入短信验证码'
  358. });
  359. return;
  360. }
  361. if (!pwd) {
  362. this.$toast({
  363. title: '请输入新密码'
  364. });
  365. return;
  366. }
  367. if (!comfirmPwd) {
  368. this.$toast({
  369. title: '再次输入新密码确认'
  370. });
  371. return;
  372. }
  373. if (pwd != comfirmPwd) {
  374. this.$toast({
  375. title: '两次密码输入不一致'
  376. });
  377. return;
  378. }
  379. let data = {
  380. mobile: this.userInfo.mobile,
  381. code: pwdCode,
  382. password: pwd,
  383. repassword: comfirmPwd
  384. };
  385. forgetPwd(data).then(res => {
  386. if (res.code == 1) {
  387. this.showPwd = false;
  388. this.$toast({
  389. title: '设置密码成功'
  390. });
  391. this.getUserInfoFun();
  392. }
  393. });
  394. },
  395. // 修改昵称
  396. changeName() {
  397. this.fieldType = FieldType.NICKNAME;
  398. this.newNickname = '';
  399. this.showNickName = true;
  400. },
  401. changeNameConfirm() {
  402. this.setUserInfoFun(this.newNickname);
  403. this.showNickName = false;
  404. },
  405. // 微信获取手机号
  406. // #ifdef MP-WEIXIN
  407. async getPhoneNumber(e) {
  408. console.log(e, this.code);
  409. const { encryptedData, iv } = e.detail;
  410. const params = {
  411. code: this.code,
  412. encrypted_data: encryptedData,
  413. iv
  414. };
  415. this.fieldType = FieldType.MOBILE;
  416. if (encryptedData) {
  417. const { data, code, msg } = await getWxMnpMobile(params);
  418. if (code == 1) {
  419. this.$toast({
  420. title: msg
  421. });
  422. // #ifdef MP-WEIXIN
  423. getWxCode().then(res => {
  424. this.code = res;
  425. });
  426. // #endif
  427. this.getUserInfoFun();
  428. }
  429. }
  430. }
  431. // #endif
  432. },
  433. onLoad() {
  434. this.getUserInfoFun();
  435. // #ifdef MP-WEIXIN
  436. getWxCode().then(res => {
  437. this.code = res;
  438. });
  439. // #endif
  440. this.getUserProfileFun = trottle(this.getUserProfileFun, 500, this);
  441. },
  442. computed: {
  443. ...mapGetters(['token', 'appConfig'])
  444. }
  445. };
  446. </script>
  447. <style lang="scss">
  448. .user-profile {
  449. min-height: calc(100vh - env(safe-area-inset-bottom));
  450. display: flex;
  451. flex-direction: column;
  452. .content {
  453. // flex: 1;
  454. border-top-left-radius: 28rpx;
  455. border-top-right-radius: 28rpx;
  456. .user-avatar-box {
  457. padding: 30rpx;
  458. .user-avatar {
  459. width: 120rpx;
  460. height: 120rpx;
  461. border-radius: 50%;
  462. }
  463. }
  464. .row-info {
  465. padding: 30rpx 20rpx;
  466. .label {
  467. width: 180rpx;
  468. }
  469. .bd-btn {
  470. padding: 8rpx 24rpx;
  471. border: 1px solid $-color-primary;
  472. color: $-color-primary;
  473. }
  474. }
  475. .bdb-line {
  476. border-bottom: 1rpx solid #e5e5e5;
  477. }
  478. }
  479. .license {
  480. margin-top: 80rpx;
  481. color: #a7a7a7;
  482. }
  483. .save-btn {
  484. margin: 40rpx 54rpx 0;
  485. height: 88rpx;
  486. border-radius: 10rpx;
  487. }
  488. .modify-container {
  489. padding: 30rpx;
  490. width: 620rpx;
  491. border-radius: 30rpx;
  492. .title {
  493. padding: 26rpx 0rpx;
  494. }
  495. .btn {
  496. height: 80rpx;
  497. border-radius: 20rpx;
  498. margin: 60rpx 50rpx 0;
  499. }
  500. }
  501. }
  502. .psw-wrapper {
  503. width: 548rpx;
  504. height: 344rpx;
  505. background-color: #ffffff;
  506. .psw-title {
  507. width: 100%;
  508. font-size: 35rpx;
  509. padding: 43rpx 0 49rpx;
  510. text-align: center;
  511. font-weight: 800;
  512. }
  513. .psw-ipt {
  514. display: block;
  515. background-color: #dce3ed;
  516. height: 90rpx;
  517. width: 464rpx;
  518. padding-left: 30rpx;
  519. margin: 0 auto;
  520. font-size: 80rpx;
  521. }
  522. .psw-btn text {
  523. display: inline-block;
  524. text-align: center;
  525. width: 50%;
  526. padding-top: 29rpx;
  527. font-size: 35rpx;
  528. }
  529. .psw-qd {
  530. color: #32c6ff;
  531. }
  532. }
  533. </style>