info.vue 13 KB

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