info.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <!-- 用户信息 -->
  2. <template>
  3. <s-layout :title="$t('user.user_info')" 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. {{$t('user.change')}}
  16. </button>
  17. <!-- #endif -->
  18. <!-- #ifndef MP -->
  19. <button class="ss-reset-button avatar-action-btn" @tap="onChangeAvatar">{{$t('user.change')}}</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="$t('user.username')" :error-message="state.usernameErrorMsg">
  26. <uni-easyinput v-model="state.model.username" :placeholder="$t('rules.username')" :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="$t('user.nickname')">
  39. <uni-easyinput v-model="state.model.nickname" type="nickname" :placeholder="t('rules.nickname')"
  40. :inputBorder="false" :placeholderStyle="placeholderStyle" :clearable="false" />
  41. </uni-forms-item>
  42. <uni-forms-item name="sex" :label="$t('user.sex')">
  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="$t('user.phone')" @tap="onChangeMobile">
  56. <uni-easyinput v-model="userInfo.mobile" :placeholder="t('user.bind_phone')" :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="password" :label="$t('user.password')" @tap="onSetPassword">
  69. <uni-easyinput v-model="userInfo.password" :placeholder="$t('rules.click_change_password')" :inputBorder="false"
  70. :styles="{ disableColor: '#fff' }" disabled
  71. placeholderStyle="color:#BBBBBB;font-size:28rpx;line-height:normal" :clearable="false">
  72. <template v-slot:right>
  73. <view class="ss-flex ss-col-center">
  74. <su-radio class="ss-flex" v-if="userInfo.verification?.password" :modelValue="true" />
  75. <button v-else class="ss-reset-button ss-flex ss-col-center ss-row-center">
  76. <text class="_icon-forward" style="color: #bbbbbb; font-size: 26rpx" />
  77. </button>
  78. </view>
  79. </template>
  80. </uni-easyinput>
  81. </uni-forms-item>
  82. </view>
  83. </uni-forms>
  84. <su-fixed bottom placeholder bg="none">
  85. <view class="footer-box ss-p-20">
  86. <button class="ss-rest-button logout-btn" @tap="onSubmit">{{$t('common.save')}}</button>
  87. </view>
  88. </su-fixed>
  89. </s-layout>
  90. </template>
  91. <script setup>
  92. import {
  93. computed,
  94. reactive,
  95. onBeforeMount,
  96. ref
  97. } from 'vue';
  98. import sheep from '@/sheep';
  99. import {
  100. clone
  101. } from 'lodash';
  102. import {
  103. onLoad,
  104. onShow
  105. } from '@dcloudio/uni-app';
  106. import {
  107. showAuthModal
  108. } from '@/sheep/hooks/useModal';
  109. import FileApi from '@/sheep/api/infra/file';
  110. import UserApi from '@/sheep/api/member/user';
  111. import AuthUtil from '@/sheep/api/member/auth';
  112. import {
  113. closeAuthModal,
  114. } from '@/sheep/hooks/useModal';
  115. import { useI18n } from 'vue-i18n';
  116. const { t } = useI18n();
  117. const state = reactive({
  118. verifyUsername: true,
  119. usernameErrorMsg: '',
  120. model: {
  121. }, // 个人信息
  122. rules: {},
  123. thirdInfo: {}, // 社交用户的信息
  124. });
  125. const userInfo = computed(() => sheep.$store('user').userInfo);
  126. const placeholderStyle = 'color:#BBBBBB;font-size:28rpx;line-height:normal';
  127. const sexRadioMap = [{
  128. name: t('user.male'),
  129. value: '1',
  130. },
  131. {
  132. name: t('user.female'),
  133. value: '2',
  134. }
  135. ];
  136. // 选择性别
  137. function onChangeGender(e) {
  138. state.model.sex = e.detail.value;
  139. }
  140. // 修改手机号
  141. const onChangeMobile = () => {
  142. showAuthModal('changeMobile');
  143. };
  144. // 选择微信的头像,进行上传
  145. function onChooseAvatar(e) {
  146. const tempUrl = e.detail.avatarUrl || '';
  147. uploadAvatar(tempUrl);
  148. }
  149. // 手动选择头像,进行上传
  150. function onChangeAvatar() {
  151. uni.chooseImage({
  152. success: async (chooseImageRes) => {
  153. const tempUrl = chooseImageRes.tempFilePaths[0];
  154. await uploadAvatar(tempUrl);
  155. },
  156. });
  157. }
  158. // 上传头像文件
  159. async function uploadAvatar(tempUrl) {
  160. if (!tempUrl) {
  161. return;
  162. }
  163. // 压缩图片
  164. const compressedImage = await compressImage(tempUrl);
  165. let {
  166. data
  167. } = await FileApi.uploadFile(compressedImage);
  168. state.model.avatar = data;
  169. }
  170. function compressImage(imagePath, scale = 0.5) { // 这里的 scale 是压缩比例,默认为 0.5(即 50%)
  171. return new Promise((resolve, reject) => {
  172. const canvas = document.createElement('canvas');
  173. const ctx = canvas.getContext('2d');
  174. const img = new Image();
  175. img.src = imagePath;
  176. img.onload = () => {
  177. const originalWidth = img.width;
  178. const originalHeight = img.height;
  179. // 根据原始尺寸进行比例压缩
  180. const newWidth = originalWidth * scale;
  181. const newHeight = originalHeight * scale;
  182. // 设置 canvas 尺寸为压缩后的尺寸
  183. canvas.width = newWidth;
  184. canvas.height = newHeight;
  185. // 在 canvas 上绘制压缩后的图片
  186. ctx.drawImage(img, 0, 0, newWidth, newHeight);
  187. // 将 canvas 转换为 base64 格式图片,压缩质量0.7
  188. const compressedImage = canvas.toDataURL('image/jpeg', 0.7);
  189. resolve(compressedImage);
  190. };
  191. img.onerror = (err) => {
  192. reject(err);
  193. };
  194. });
  195. }
  196. // 修改密码
  197. function onSetPassword() {
  198. showAuthModal('changePassword');
  199. }
  200. // 绑定第三方账号
  201. async function bindThirdOauth() {
  202. let result = await sheep.$platform.useProvider('wechat').bind();
  203. if (result) {
  204. await getUserInfo();
  205. }
  206. }
  207. // 解绑第三方账号
  208. function unBindThirdOauth() {
  209. uni.showModal({
  210. title: t('rules.unbind_reminder'),
  211. content:t('rules.unbind_warning'),
  212. cancelText: t('common.cancel'),
  213. confirmText: t('common.confirm'),
  214. success: async function(res) {
  215. if (!res.confirm) {
  216. return;
  217. }
  218. const result = await sheep.$platform.useProvider('wechat').unbind(state.thirdInfo.openid);
  219. if (result) {
  220. await getUserInfo();
  221. }
  222. },
  223. });
  224. }
  225. let lastUsername = ref('')
  226. async function verifyUsername(e) {
  227. // console.log(e.detail.value,state.username)
  228. const username = e.detail.value;
  229. if (username == '' || username == lastUsername.value) {
  230. // 为空或者没改东西,不调校验
  231. return false;
  232. } else if (username === state.username) {
  233. // 如果输入的用户名跟原来的本身的用户名一致,不做查询,默认正确(即点进来又点出去,没改变的)
  234. state.usernameErrorMsg = '';
  235. state.verifyUsername = true
  236. return false;
  237. }
  238. lastUsername.value = username
  239. // 提交数据
  240. const {
  241. data
  242. } = await AuthUtil.verifyUsername(username);
  243. // false就是已经有这个用户名,可以登录,true是没有,可以使用
  244. if (!data) {
  245. state.usernameErrorMsg = t('rules.username_taken')
  246. state.verifyUsername = data
  247. } else {
  248. state.usernameErrorMsg = ''
  249. state.verifyUsername = data
  250. }
  251. }
  252. // 保存信息
  253. async function onSubmit() {
  254. const {
  255. code
  256. } = await UserApi.updateUser({
  257. avatar: state.model.avatar,
  258. nickname: state.model.nickname,
  259. username: state.model.username,
  260. sex: state.model.sex,
  261. });
  262. if (code === 0 && state.model.username !== state.username) {
  263. // 修改成功并且是修改了用户名退出登录
  264. uni.showToast({
  265. icon: 'success',
  266. title: t('rules.modify_success'),
  267. });
  268. // 成功后,只需要1秒后推出登录关闭弹窗
  269. setTimeout(function() {
  270. sheep.$store('user').logout();
  271. closeAuthModal();
  272. sheep.$router.go('/pages/index/user')
  273. }, 1000)
  274. }
  275. if (code === 0) {
  276. await getUserInfo();
  277. }
  278. }
  279. // 获得用户信息
  280. const getUserInfo = async () => {
  281. // 个人信息
  282. const userInfo = await sheep.$store('user').getInfo();
  283. state.model = clone(userInfo);
  284. state.username = userInfo.username
  285. // 获得社交用户的信息
  286. if (sheep.$platform.name !== 'H5') {
  287. let result = await sheep.$platform.useProvider('wechat').getInfo();
  288. state.thirdInfo = result || {};
  289. }
  290. };
  291. onShow(async () => {
  292. await getUserInfo();
  293. });
  294. onLoad(async (options) => {
  295. getUserInfo();
  296. // uni.$on('submitComplete', getUserInfo);
  297. // setTimeout(getUserInfo, 2000)
  298. });
  299. </script>
  300. <style lang="scss" scoped>
  301. .icon {
  302. display: flex;
  303. align-items: center;
  304. margin-right: 7rpx
  305. }
  306. .icon image {
  307. width: 35rpx;
  308. height: 35rpx;
  309. }
  310. :deep() {
  311. .uni-file-picker {
  312. border-radius: 50%;
  313. }
  314. .uni-file-picker__container {
  315. margin: -14rpx -12rpx;
  316. }
  317. .file-picker__progress {
  318. height: 0 !important;
  319. }
  320. .uni-list-item__content-title {
  321. font-size: 28rpx !important;
  322. color: #333333 !important;
  323. line-height: normal !important;
  324. }
  325. .uni-icons {
  326. font-size: 40rpx !important;
  327. }
  328. .is-disabled {
  329. color: #333333;
  330. }
  331. }
  332. :deep(.disabled) {
  333. opacity: 1;
  334. }
  335. .gender-name {
  336. font-size: 28rpx;
  337. font-weight: 500;
  338. line-height: normal;
  339. color: #333333;
  340. }
  341. .title-box {
  342. font-size: 28rpx;
  343. font-weight: 500;
  344. color: #666666;
  345. line-height: 100rpx;
  346. }
  347. .logout-btn {
  348. width: 710rpx;
  349. height: 80rpx;
  350. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  351. border-radius: 40rpx;
  352. font-size: 30rpx;
  353. font-weight: 500;
  354. color: $white;
  355. }
  356. .radio-dark {
  357. filter: grayscale(100%);
  358. filter: gray;
  359. opacity: 0.4;
  360. }
  361. .content-img {
  362. border-radius: 50%;
  363. }
  364. .header-box-content {
  365. position: relative;
  366. width: 160rpx;
  367. height: 160rpx;
  368. overflow: hidden;
  369. border-radius: 50%;
  370. }
  371. .avatar-action {
  372. position: absolute;
  373. left: 50%;
  374. transform: translateX(-50%);
  375. bottom: 0;
  376. z-index: 1;
  377. width: 160rpx;
  378. height: 46rpx;
  379. background: rgba(#000000, 0.3);
  380. .avatar-action-btn {
  381. width: 160rpx;
  382. height: 46rpx;
  383. font-weight: 500;
  384. font-size: 24rpx;
  385. color: #ffffff;
  386. }
  387. }
  388. // 绑定项
  389. .account-list {
  390. background-color: $white;
  391. height: 100rpx;
  392. padding: 0 20rpx;
  393. .list-img {
  394. width: 40rpx;
  395. height: 40rpx;
  396. margin-right: 10rpx;
  397. }
  398. .list-name {
  399. font-size: 28rpx;
  400. color: #333333;
  401. }
  402. .info {
  403. .avatar {
  404. width: 38rpx;
  405. height: 38rpx;
  406. border-radius: 50%;
  407. overflow: hidden;
  408. }
  409. .name {
  410. font-size: 28rpx;
  411. font-weight: 400;
  412. color: $dark-9;
  413. }
  414. }
  415. .bind-box {
  416. width: 100rpx;
  417. height: 50rpx;
  418. line-height: normal;
  419. display: flex;
  420. justify-content: center;
  421. align-items: center;
  422. font-size: 24rpx;
  423. .bind-btn {
  424. width: 100%;
  425. height: 100%;
  426. border-radius: 25rpx;
  427. background: #f4f4f4;
  428. color: #999999;
  429. }
  430. .relieve-btn {
  431. width: 100%;
  432. height: 100%;
  433. border-radius: 25rpx;
  434. background: var(--ui-BG-Main-opacity-1);
  435. color: var(--ui-BG-Main);
  436. }
  437. }
  438. }
  439. .list-border {
  440. font-size: 28rpx;
  441. font-weight: 400;
  442. color: #333333;
  443. border-bottom: 2rpx solid #eeeeee;
  444. }
  445. image {
  446. width: 100%;
  447. height: 100%;
  448. }
  449. </style>