info.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. onShow
  174. } from '@dcloudio/uni-app';
  175. import {
  176. showAuthModal
  177. } from '@/sheep/hooks/useModal';
  178. import FileApi from '@/sheep/api/infra/file';
  179. import UserApi from '@/sheep/api/member/user';
  180. import AuthUtil from '@/sheep/api/member/auth';
  181. import {
  182. closeAuthModal,
  183. } from '@/sheep/hooks/useModal';
  184. const state = reactive({
  185. verifyUsername: true,
  186. usernameErrorMsg: '',
  187. model: {
  188. }, // 个人信息
  189. rules: {},
  190. thirdInfo: {}, // 社交用户的信息
  191. });
  192. const userInfo = computed(() => sheep.$store('user').userInfo);
  193. const placeholderStyle = 'color:#BBBBBB;font-size:28rpx;line-height:normal';
  194. const sexRadioMap = [{
  195. name: '男',
  196. value: '1',
  197. },
  198. {
  199. name: '女',
  200. value: '2',
  201. }
  202. ];
  203. // 选择性别
  204. function onChangeGender(e) {
  205. state.model.sex = e.detail.value;
  206. }
  207. // 修改手机号
  208. const onChangeMobile = () => {
  209. showAuthModal('changeMobile');
  210. };
  211. // 选择微信的头像,进行上传
  212. function onChooseAvatar(e) {
  213. const tempUrl = e.detail.avatarUrl || '';
  214. uploadAvatar(tempUrl);
  215. }
  216. // 手动选择头像,进行上传
  217. function onChangeAvatar() {
  218. uni.chooseImage({
  219. success: async (chooseImageRes) => {
  220. const tempUrl = chooseImageRes.tempFilePaths[0];
  221. await uploadAvatar(tempUrl);
  222. },
  223. });
  224. }
  225. // 上传头像文件
  226. async function uploadAvatar(tempUrl) {
  227. if (!tempUrl) {
  228. return;
  229. }
  230. // 压缩图片
  231. const compressedImage = await compressImage(tempUrl);
  232. let {
  233. data
  234. } = await FileApi.uploadFile(compressedImage);
  235. state.model.avatar = data;
  236. }
  237. function compressImage(imagePath, scale = 0.5) { // 这里的 scale 是压缩比例,默认为 0.5(即 50%)
  238. return new Promise((resolve, reject) => {
  239. const canvas = document.createElement('canvas');
  240. const ctx = canvas.getContext('2d');
  241. const img = new Image();
  242. img.src = imagePath;
  243. img.onload = () => {
  244. const originalWidth = img.width;
  245. const originalHeight = img.height;
  246. // 根据原始尺寸进行比例压缩
  247. const newWidth = originalWidth * scale;
  248. const newHeight = originalHeight * scale;
  249. // 设置 canvas 尺寸为压缩后的尺寸
  250. canvas.width = newWidth;
  251. canvas.height = newHeight;
  252. // 在 canvas 上绘制压缩后的图片
  253. ctx.drawImage(img, 0, 0, newWidth, newHeight);
  254. // 将 canvas 转换为 base64 格式图片,压缩质量0.7
  255. const compressedImage = canvas.toDataURL('image/jpeg', 0.7);
  256. resolve(compressedImage);
  257. };
  258. img.onerror = (err) => {
  259. reject(err);
  260. };
  261. });
  262. }
  263. // 修改密码
  264. function onSetPassword() {
  265. showAuthModal('changePassword');
  266. }
  267. // 绑定第三方账号
  268. async function bindThirdOauth() {
  269. let result = await sheep.$platform.useProvider('wechat').bind();
  270. if (result) {
  271. await getUserInfo();
  272. }
  273. }
  274. // 解绑第三方账号
  275. function unBindThirdOauth() {
  276. uni.showModal({
  277. title: '解绑提醒',
  278. content: '解绑后您将无法通过微信登录此账号',
  279. cancelText: '再想想',
  280. confirmText: '确定',
  281. success: async function(res) {
  282. if (!res.confirm) {
  283. return;
  284. }
  285. const result = await sheep.$platform.useProvider('wechat').unbind(state.thirdInfo.openid);
  286. if (result) {
  287. await getUserInfo();
  288. }
  289. },
  290. });
  291. }
  292. let lastUsername = ref('')
  293. async function verifyUsername(e) {
  294. // console.log(e.detail.value,state.username)
  295. const username = e.detail.value;
  296. if (username == '' || username == lastUsername.value) {
  297. // 为空或者没改东西,不调校验
  298. return false;
  299. } else if (username === state.username) {
  300. // 如果输入的用户名跟原来的本身的用户名一致,不做查询,默认正确(即点进来又点出去,没改变的)
  301. state.usernameErrorMsg = '';
  302. state.verifyUsername = true
  303. return false;
  304. }
  305. lastUsername.value = username
  306. // 提交数据
  307. const {
  308. data
  309. } = await AuthUtil.verifyUsername(username);
  310. // false就是已经有这个用户名,可以登录,true是没有,可以使用
  311. if (!data) {
  312. state.usernameErrorMsg = '用户名已被使用!'
  313. state.verifyUsername = data
  314. } else {
  315. state.usernameErrorMsg = ''
  316. state.verifyUsername = data
  317. }
  318. }
  319. // 保存信息
  320. async function onSubmit() {
  321. const {
  322. code
  323. } = await UserApi.updateUser({
  324. avatar: state.model.avatar,
  325. nickname: state.model.nickname,
  326. username: state.model.username,
  327. sex: state.model.sex,
  328. });
  329. if (code === 0 && state.model.username !== state.username) {
  330. // 修改成功并且是修改了用户名退出登录
  331. uni.showToast({
  332. icon: 'success',
  333. title: "修改成功,请重新登录",
  334. });
  335. // 成功后,只需要1秒后推出登录关闭弹窗
  336. setTimeout(function() {
  337. sheep.$store('user').logout();
  338. closeAuthModal();
  339. sheep.$router.go('/pages/index/user')
  340. }, 1000)
  341. }
  342. if (code === 0) {
  343. await getUserInfo();
  344. }
  345. }
  346. // 获得用户信息
  347. const getUserInfo = async () => {
  348. // 个人信息
  349. const userInfo = await sheep.$store('user').getInfo();
  350. state.model = clone(userInfo);
  351. state.username = userInfo.username
  352. // 获得社交用户的信息
  353. if (sheep.$platform.name !== 'H5') {
  354. let result = await sheep.$platform.useProvider('wechat').getInfo();
  355. state.thirdInfo = result || {};
  356. }
  357. };
  358. onShow(async () => {
  359. await getUserInfo();
  360. });
  361. onLoad(async (options) => {
  362. getUserInfo();
  363. // uni.$on('submitComplete', getUserInfo);
  364. // setTimeout(getUserInfo, 2000)
  365. });
  366. </script>
  367. <style lang="scss" scoped>
  368. .icon {
  369. display: flex;
  370. align-items: center;
  371. margin-right: 7rpx
  372. }
  373. .icon image {
  374. width: 35rpx;
  375. height: 35rpx;
  376. }
  377. :deep() {
  378. .uni-file-picker {
  379. border-radius: 50%;
  380. }
  381. .uni-file-picker__container {
  382. margin: -14rpx -12rpx;
  383. }
  384. .file-picker__progress {
  385. height: 0 !important;
  386. }
  387. .uni-list-item__content-title {
  388. font-size: 28rpx !important;
  389. color: #333333 !important;
  390. line-height: normal !important;
  391. }
  392. .uni-icons {
  393. font-size: 40rpx !important;
  394. }
  395. .is-disabled {
  396. color: #333333;
  397. }
  398. }
  399. :deep(.disabled) {
  400. opacity: 1;
  401. }
  402. .gender-name {
  403. font-size: 28rpx;
  404. font-weight: 500;
  405. line-height: normal;
  406. color: #333333;
  407. }
  408. .title-box {
  409. font-size: 28rpx;
  410. font-weight: 500;
  411. color: #666666;
  412. line-height: 100rpx;
  413. }
  414. .logout-btn {
  415. width: 710rpx;
  416. height: 80rpx;
  417. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  418. border-radius: 40rpx;
  419. font-size: 30rpx;
  420. font-weight: 500;
  421. color: $white;
  422. }
  423. .radio-dark {
  424. filter: grayscale(100%);
  425. filter: gray;
  426. opacity: 0.4;
  427. }
  428. .content-img {
  429. border-radius: 50%;
  430. }
  431. .header-box-content {
  432. position: relative;
  433. width: 160rpx;
  434. height: 160rpx;
  435. overflow: hidden;
  436. border-radius: 50%;
  437. }
  438. .avatar-action {
  439. position: absolute;
  440. left: 50%;
  441. transform: translateX(-50%);
  442. bottom: 0;
  443. z-index: 1;
  444. width: 160rpx;
  445. height: 46rpx;
  446. background: rgba(#000000, 0.3);
  447. .avatar-action-btn {
  448. width: 160rpx;
  449. height: 46rpx;
  450. font-weight: 500;
  451. font-size: 24rpx;
  452. color: #ffffff;
  453. }
  454. }
  455. // 绑定项
  456. .account-list {
  457. background-color: $white;
  458. height: 100rpx;
  459. padding: 0 20rpx;
  460. .list-img {
  461. width: 40rpx;
  462. height: 40rpx;
  463. margin-right: 10rpx;
  464. }
  465. .list-name {
  466. font-size: 28rpx;
  467. color: #333333;
  468. }
  469. .info {
  470. .avatar {
  471. width: 38rpx;
  472. height: 38rpx;
  473. border-radius: 50%;
  474. overflow: hidden;
  475. }
  476. .name {
  477. font-size: 28rpx;
  478. font-weight: 400;
  479. color: $dark-9;
  480. }
  481. }
  482. .bind-box {
  483. width: 100rpx;
  484. height: 50rpx;
  485. line-height: normal;
  486. display: flex;
  487. justify-content: center;
  488. align-items: center;
  489. font-size: 24rpx;
  490. .bind-btn {
  491. width: 100%;
  492. height: 100%;
  493. border-radius: 25rpx;
  494. background: #f4f4f4;
  495. color: #999999;
  496. }
  497. .relieve-btn {
  498. width: 100%;
  499. height: 100%;
  500. border-radius: 25rpx;
  501. background: var(--ui-BG-Main-opacity-1);
  502. color: var(--ui-BG-Main);
  503. }
  504. }
  505. }
  506. .list-border {
  507. font-size: 28rpx;
  508. font-weight: 400;
  509. color: #333333;
  510. border-bottom: 2rpx solid #eeeeee;
  511. }
  512. image {
  513. width: 100%;
  514. height: 100%;
  515. }
  516. </style>