info.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <!-- 用户信息 -->
  2. <template>
  3. <s-layout title="用户信息" class="set-userinfo-wrap">
  4. <uni-forms :model="state.model" :rules="state.rules" labelPosition="left" border class="form-box"
  5. labelWidth='160'>
  6. <!-- 头像 -->
  7. <view class="ss-flex ss-row-center ss-col-center ss-p-t-60 ss-p-b-0 bg-white">
  8. <view class="header-box-content">
  9. <su-image class="content-img" isPreview :current="0" :src="state.model?.avatar" :height="160"
  10. :width="160" :radius="80" mode="scaleToFill" />
  11. <view class="avatar-action">
  12. <!-- #ifdef MP -->
  13. <button class="ss-reset-button avatar-action-btn" open-type="chooseAvatar"
  14. @chooseavatar="onChooseAvatar">
  15. 修改
  16. </button>
  17. <!-- #endif -->
  18. <!-- #ifndef MP -->
  19. <button class="ss-reset-button avatar-action-btn" @tap="onChangeAvatar">修改</button>
  20. <!-- #endif -->
  21. </view>
  22. </view>
  23. </view>
  24. <view class="bg-white ss-p-x-30">
  25. <uni-forms-item name="username" label="用户名" :error-message="state.usernameErrorMsg">
  26. <uni-easyinput v-model="state.model.username" placeholder="设置用户名" :inputBorder="false"
  27. :placeholderStyle="placeholderStyle" @blur="verifyUsername" :clearable="false">
  28. <template v-slot:right>
  29. <view v-if="!state.verifyUsername" class="icon">
  30. <image style :src="sheep.$url.static('/static/images/shibai.png')" />
  31. </view>
  32. <view v-else class="icon">
  33. <image :src="sheep.$url.static('/static/images/chenggong.png')" />
  34. </view>
  35. </template>
  36. </uni-easyinput>
  37. </uni-forms-item>
  38. <uni-forms-item name="nickname" label="昵称">
  39. <uni-easyinput v-model="state.model.nickname" type="nickname" placeholder="设置昵称"
  40. :inputBorder="false" :placeholderStyle="placeholderStyle" :clearable="false" />
  41. </uni-forms-item>
  42. <uni-forms-item name="sex" label="性别">
  43. <view class="ss-flex ss-col-center ss-h-100">
  44. <radio-group @change="onChangeGender" class="ss-flex ss-col-center">
  45. <label class="radio" v-for="item in sexRadioMap" :key="item.value">
  46. <view class="ss-flex ss-col-center ss-m-r-32">
  47. <radio :value="item.value" color="var(--ui-BG-Main)" style="transform: scale(0.8)"
  48. :checked="parseInt(item.value) === state.model?.sex" />
  49. <view class="gender-name">{{ item.name }}</view>
  50. </view>
  51. </label>
  52. </radio-group>
  53. </view>
  54. </uni-forms-item>
  55. <uni-forms-item name="mobile" label="手机号" @tap="onChangeMobile">
  56. <uni-easyinput v-model="userInfo.mobile" placeholder="请绑定手机号" :inputBorder="false" disabled
  57. :styles="{ disableColor: '#fff' }" :placeholderStyle="placeholderStyle" :clearable="false">
  58. <template v-slot:right>
  59. <view class="ss-flex ss-col-center">
  60. <su-radio v-if="userInfo.verification?.mobile" :modelValue="true" />
  61. <button v-else class="ss-reset-button ss-flex ss-col-center ss-row-center">
  62. <text class="_icon-forward" style="color: #bbbbbb; font-size: 26rpx"></text>
  63. </button>
  64. </view>
  65. </template>
  66. </uni-easyinput>
  67. </uni-forms-item>
  68. <!-- <uni-forms-item name="mobile" label="支付宝账号" @tap="onChangeAlipayAccount">
  69. <uni-easyinput v-model="alipayAccount" placeholder="请绑定支付宝账号" :inputBorder="false" disabled
  70. :styles="{ disableColor: '#fff' }" :placeholderStyle="placeholderStyle" :clearable="false">
  71. <template v-slot:right>
  72. <view class="ss-flex ss-col-center">
  73. <su-radio v-if="userInfo.verification?.mobile" :modelValue="true" />
  74. <button v-else class="ss-reset-button ss-flex ss-col-center ss-row-center">
  75. <text class="_icon-forward" style="color: #bbbbbb; font-size: 26rpx"></text>
  76. </button>
  77. </view>
  78. </template>
  79. </uni-easyinput>
  80. </uni-forms-item>
  81. <uni-forms-item name="mobile" label="银行卡" @tap="onChangeBankAccount">
  82. <uni-easyinput v-model="bankAccount" placeholder="请绑定银行卡" :inputBorder="false" disabled
  83. :styles="{ disableColor: '#fff' }" :placeholderStyle="placeholderStyle" :clearable="false">
  84. <template v-slot:right>
  85. <view class="ss-flex ss-col-center">
  86. <su-radio v-if="userInfo.verification?.mobile" :modelValue="true" />
  87. <button v-else class="ss-reset-button ss-flex ss-col-center ss-row-center">
  88. <text class="_icon-forward" style="color: #bbbbbb; font-size: 26rpx"></text>
  89. </button>
  90. </view>
  91. </template>
  92. </uni-easyinput>
  93. </uni-forms-item> -->
  94. <uni-forms-item name="password" label="登录密码" @tap="onSetPassword">
  95. <uni-easyinput v-model="userInfo.password" placeholder="点击修改登录密码" :inputBorder="false"
  96. :styles="{ disableColor: '#fff' }" disabled
  97. placeholderStyle="color:#BBBBBB;font-size:28rpx;line-height:normal" :clearable="false">
  98. <template v-slot:right>
  99. <view class="ss-flex ss-col-center">
  100. <su-radio class="ss-flex" v-if="userInfo.verification?.password" :modelValue="true" />
  101. <button v-else class="ss-reset-button ss-flex ss-col-center ss-row-center">
  102. <text class="_icon-forward" style="color: #bbbbbb; font-size: 26rpx" />
  103. </button>
  104. </view>
  105. </template>
  106. </uni-easyinput>
  107. </uni-forms-item>
  108. </view>
  109. <!-- <view class="bg-white ss-m-t-14">
  110. <uni-list>
  111. <uni-list-item
  112. clickable
  113. @tap="sheep.$router.go('/pages/user/address/list')"
  114. title="地址管理"
  115. showArrow
  116. :border="false"
  117. class="list-border"
  118. />
  119. </uni-list>
  120. </view> -->
  121. </uni-forms>
  122. <!-- 当前社交平台的绑定关系,只处理 wechat 微信场景 -->
  123. <view v-if="sheep.$platform.name !== 'H5'">
  124. <view class="title-box ss-p-l-30">第三方账号绑定</view>
  125. <view class="account-list ss-flex ss-row-between">
  126. <view v-if="'WechatOfficialAccount' === sheep.$platform.name" class="ss-flex ss-col-center">
  127. <image class="list-img" :src="sheep.$url.static('/static/images/WechatOfficialAccount.png')" />
  128. <text class="list-name">微信公众号</text>
  129. </view>
  130. <view v-if="'WechatMiniProgram' === sheep.$platform.name" class="ss-flex ss-col-center">
  131. <image class="list-img" :src="sheep.$url.static('/static/images/WechatMiniProgram.png')" />
  132. <text class="list-name">微信小程序</text>
  133. </view>
  134. <view v-if="'App' === sheep.$platform.name" class="ss-flex ss-col-center">
  135. <image class="list-img" :src="sheep.$url.static('/static/images/wechat.png')" />
  136. <text class="list-name">微信开放平台</text>
  137. </view>
  138. <view class="ss-flex ss-col-center">
  139. <view class="info ss-flex ss-col-center" v-if="state.thirdInfo">
  140. <image class="avatar ss-m-r-20" :src="sheep.$url.cdn(state.thirdInfo.avatar)" />
  141. <text class="name">{{ state.thirdInfo.nickname }}</text>
  142. </view>
  143. <view class="bind-box ss-m-l-20">
  144. <button v-if="state.thirdInfo.openid" class="ss-reset-button relieve-btn"
  145. @tap="unBindThirdOauth">
  146. 解绑
  147. </button>
  148. <button v-else class="ss-reset-button bind-btn" @tap="bindThirdOauth">绑定</button>
  149. </view>
  150. </view>
  151. </view>
  152. </view>
  153. <su-fixed bottom placeholder bg="none">
  154. <view class="footer-box ss-p-20">
  155. <button class="ss-rest-button logout-btn" @tap="onSubmit">保存</button>
  156. </view>
  157. </su-fixed>
  158. </s-layout>
  159. </template>
  160. <script setup>
  161. import {
  162. computed,
  163. reactive,
  164. onBeforeMount,
  165. ref
  166. } from 'vue';
  167. import sheep from '@/sheep';
  168. import {
  169. clone
  170. } from 'lodash';
  171. import {
  172. onLoad
  173. } from '@dcloudio/uni-app';
  174. import {
  175. showAuthModal
  176. } from '@/sheep/hooks/useModal';
  177. import FileApi from '@/sheep/api/infra/file';
  178. import UserApi from '@/sheep/api/member/user';
  179. import AuthUtil from '@/sheep/api/member/auth';
  180. import {
  181. closeAuthModal,
  182. } from '@/sheep/hooks/useModal';
  183. const state = reactive({
  184. verifyUsername: true,
  185. usernameErrorMsg: '',
  186. model: {
  187. }, // 个人信息
  188. rules: {},
  189. thirdInfo: {}, // 社交用户的信息
  190. });
  191. const userInfo = computed(() => sheep.$store('user').userInfo);
  192. const placeholderStyle = 'color:#BBBBBB;font-size:28rpx;line-height:normal';
  193. const sexRadioMap = [{
  194. name: '男',
  195. value: '1',
  196. },
  197. {
  198. name: '女',
  199. value: '2',
  200. }
  201. ];
  202. // 选择性别
  203. function onChangeGender(e) {
  204. state.model.sex = e.detail.value;
  205. }
  206. // 修改手机号
  207. const onChangeMobile = () => {
  208. showAuthModal('changeMobile');
  209. };
  210. // 选择微信的头像,进行上传
  211. function onChooseAvatar(e) {
  212. const tempUrl = e.detail.avatarUrl || '';
  213. uploadAvatar(tempUrl);
  214. }
  215. // 手动选择头像,进行上传
  216. function onChangeAvatar() {
  217. uni.chooseImage({
  218. success: async (chooseImageRes) => {
  219. const tempUrl = chooseImageRes.tempFilePaths[0];
  220. await uploadAvatar(tempUrl);
  221. },
  222. });
  223. }
  224. // 上传头像文件
  225. async function uploadAvatar(tempUrl) {
  226. if (!tempUrl) {
  227. return;
  228. }
  229. let {
  230. data
  231. } = await FileApi.uploadFile(tempUrl);
  232. state.model.avatar = data;
  233. }
  234. // 修改密码
  235. function onSetPassword() {
  236. showAuthModal('changePassword');
  237. }
  238. // 绑定第三方账号
  239. async function bindThirdOauth() {
  240. let result = await sheep.$platform.useProvider('wechat').bind();
  241. }
  242. // 解绑第三方账号
  243. function unBindThirdOauth() {
  244. uni.showModal({
  245. title: '解绑提醒',
  246. content: '解绑后您将无法通过微信登录此账号',
  247. cancelText: '再想想',
  248. confirmText: '确定',
  249. success: async function(res) {
  250. if (!res.confirm) {
  251. return;
  252. }
  253. const result = await sheep.$platform.useProvider('wechat').unbind(state.thirdInfo.openid);
  254. if (result) {
  255. await getUserInfo();
  256. }
  257. },
  258. });
  259. }
  260. let lastUsername = ref('')
  261. async function verifyUsername(e) {
  262. // console.log(e.detail.value,state.username)
  263. const username = e.detail.value;
  264. if (username == '' || username == lastUsername.value) {
  265. // 为空或者没改东西,不调校验
  266. return false;
  267. } else if (username === state.username) {
  268. // 如果输入的用户名跟原来的本身的用户名一致,不做查询,默认正确(即点进来又点出去,没改变的)
  269. state.usernameErrorMsg = '';
  270. state.verifyUsername = true
  271. return false;
  272. }
  273. lastUsername.value = username
  274. // 提交数据
  275. const {
  276. data
  277. } = await AuthUtil.verifyUsername(username);
  278. // false就是已经有这个用户名,可以登录,true是没有,可以使用
  279. if (!data) {
  280. state.usernameErrorMsg = '用户名已被使用!'
  281. state.verifyUsername = data
  282. } else {
  283. state.usernameErrorMsg = ''
  284. state.verifyUsername = data
  285. }
  286. }
  287. // 保存信息
  288. async function onSubmit() {
  289. const {
  290. code
  291. } = await UserApi.updateUser({
  292. avatar: state.model.avatar,
  293. nickname: state.model.nickname,
  294. username: state.model.username,
  295. sex: state.model.sex,
  296. });
  297. if (code === 0 && state.model.username !== state.username) {
  298. // 修改成功并且是修改了用户名退出登录
  299. uni.showToast({
  300. icon: 'success',
  301. title: "修改成功,请重新登录",
  302. });
  303. // 成功后,只需要1秒后推出登录关闭弹窗
  304. setTimeout(function() {
  305. sheep.$store('user').logout();
  306. closeAuthModal();
  307. sheep.$router.go('/pages/index/user')
  308. }, 1000)
  309. }
  310. if (code === 0) {
  311. await getUserInfo();
  312. }
  313. }
  314. // 获得用户信息
  315. const getUserInfo = async () => {
  316. // 个人信息
  317. const userInfo = await sheep.$store('user').getInfo();
  318. state.model = clone(userInfo);
  319. state.username = userInfo.username
  320. // 获得社交用户的信息
  321. if (sheep.$platform.name !== 'H5') {
  322. let result = await sheep.$platform.useProvider('wechat').getInfo();
  323. state.thirdInfo = result || {};
  324. }
  325. };
  326. onLoad(async (options) => {
  327. getUserInfo();
  328. // uni.$on('submitComplete', getUserInfo);
  329. // setTimeout(getUserInfo, 2000)
  330. });
  331. </script>
  332. <style lang="scss" scoped>
  333. .icon {
  334. display: flex;
  335. align-items: center;
  336. margin-right: 7rpx
  337. }
  338. .icon image {
  339. width: 35rpx;
  340. height: 35rpx;
  341. }
  342. :deep() {
  343. .uni-file-picker {
  344. border-radius: 50%;
  345. }
  346. .uni-file-picker__container {
  347. margin: -14rpx -12rpx;
  348. }
  349. .file-picker__progress {
  350. height: 0 !important;
  351. }
  352. .uni-list-item__content-title {
  353. font-size: 28rpx !important;
  354. color: #333333 !important;
  355. line-height: normal !important;
  356. }
  357. .uni-icons {
  358. font-size: 40rpx !important;
  359. }
  360. .is-disabled {
  361. color: #333333;
  362. }
  363. }
  364. :deep(.disabled) {
  365. opacity: 1;
  366. }
  367. .gender-name {
  368. font-size: 28rpx;
  369. font-weight: 500;
  370. line-height: normal;
  371. color: #333333;
  372. }
  373. .title-box {
  374. font-size: 28rpx;
  375. font-weight: 500;
  376. color: #666666;
  377. line-height: 100rpx;
  378. }
  379. .logout-btn {
  380. width: 710rpx;
  381. height: 80rpx;
  382. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  383. border-radius: 40rpx;
  384. font-size: 30rpx;
  385. font-weight: 500;
  386. color: $white;
  387. }
  388. .radio-dark {
  389. filter: grayscale(100%);
  390. filter: gray;
  391. opacity: 0.4;
  392. }
  393. .content-img {
  394. border-radius: 50%;
  395. }
  396. .header-box-content {
  397. position: relative;
  398. width: 160rpx;
  399. height: 160rpx;
  400. overflow: hidden;
  401. border-radius: 50%;
  402. }
  403. .avatar-action {
  404. position: absolute;
  405. left: 50%;
  406. transform: translateX(-50%);
  407. bottom: 0;
  408. z-index: 1;
  409. width: 160rpx;
  410. height: 46rpx;
  411. background: rgba(#000000, 0.3);
  412. .avatar-action-btn {
  413. width: 160rpx;
  414. height: 46rpx;
  415. font-weight: 500;
  416. font-size: 24rpx;
  417. color: #ffffff;
  418. }
  419. }
  420. // 绑定项
  421. .account-list {
  422. background-color: $white;
  423. height: 100rpx;
  424. padding: 0 20rpx;
  425. .list-img {
  426. width: 40rpx;
  427. height: 40rpx;
  428. margin-right: 10rpx;
  429. }
  430. .list-name {
  431. font-size: 28rpx;
  432. color: #333333;
  433. }
  434. .info {
  435. .avatar {
  436. width: 38rpx;
  437. height: 38rpx;
  438. border-radius: 50%;
  439. overflow: hidden;
  440. }
  441. .name {
  442. font-size: 28rpx;
  443. font-weight: 400;
  444. color: $dark-9;
  445. }
  446. }
  447. .bind-box {
  448. width: 100rpx;
  449. height: 50rpx;
  450. line-height: normal;
  451. display: flex;
  452. justify-content: center;
  453. align-items: center;
  454. font-size: 24rpx;
  455. .bind-btn {
  456. width: 100%;
  457. height: 100%;
  458. border-radius: 25rpx;
  459. background: #f4f4f4;
  460. color: #999999;
  461. }
  462. .relieve-btn {
  463. width: 100%;
  464. height: 100%;
  465. border-radius: 25rpx;
  466. background: var(--ui-BG-Main-opacity-1);
  467. color: var(--ui-BG-Main);
  468. }
  469. }
  470. }
  471. .list-border {
  472. font-size: 28rpx;
  473. font-weight: 400;
  474. color: #333333;
  475. border-bottom: 2rpx solid #eeeeee;
  476. }
  477. image {
  478. width: 100%;
  479. height: 100%;
  480. }
  481. </style>