info.vue 13 KB

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