info.vue 12 KB

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