info.vue 12 KB

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