info.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 || sheep.$url.static('/static/img/shop/default_avatar.png')"
  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 UserApi from '@/sheep/api/member/user';
  182. import {
  183. chooseAndUploadFile,
  184. uploadFilesFromPath,
  185. } from '@/sheep/components/s-uploader/choose-and-upload-file';
  186. const state = reactive({
  187. model: {}, // 个人信息
  188. rules: {},
  189. thirdInfo: {}, // 社交用户的信息
  190. });
  191. const placeholderStyle = 'color:#BBBBBB;font-size:28rpx;line-height:normal';
  192. const sexRadioMap = [
  193. {
  194. name: '男',
  195. value: '1',
  196. },
  197. {
  198. name: '女',
  199. value: '2',
  200. },
  201. ];
  202. const userInfo = computed(() => sheep.$store('user').userInfo);
  203. // 选择性别
  204. function onChangeGender(e) {
  205. state.model.sex = e.detail.value;
  206. }
  207. // 修改手机号
  208. const onChangeMobile = () => {
  209. showAuthModal('changeMobile');
  210. };
  211. // 选择微信的头像,进行上传
  212. async function onChooseAvatar(e) {
  213. debugger;
  214. const tempUrl = e.detail.avatarUrl || '';
  215. if (!tempUrl) return;
  216. const files = await uploadFilesFromPath(tempUrl);
  217. if (files.length > 0) {
  218. state.model.avatar = files[0].url;
  219. }
  220. }
  221. // 手动选择头像,进行上传
  222. async function onChangeAvatar() {
  223. const files = await chooseAndUploadFile({ type: 'image' });
  224. if (files.length > 0) {
  225. state.model.avatar = files[0].url;
  226. }
  227. }
  228. // 修改密码
  229. function onSetPassword() {
  230. showAuthModal('changePassword');
  231. }
  232. // 绑定第三方账号
  233. async function bindThirdOauth() {
  234. let result = await sheep.$platform.useProvider('wechat').bind();
  235. if (result) {
  236. await getUserInfo();
  237. }
  238. }
  239. // 解绑第三方账号
  240. function unBindThirdOauth() {
  241. uni.showModal({
  242. title: '解绑提醒',
  243. content: '解绑后您将无法通过微信登录此账号',
  244. cancelText: '再想想',
  245. confirmText: '确定',
  246. success: async function (res) {
  247. if (!res.confirm) {
  248. return;
  249. }
  250. const result = await sheep.$platform.useProvider('wechat').unbind(state.thirdInfo.openid);
  251. if (result) {
  252. await getUserInfo();
  253. }
  254. },
  255. });
  256. }
  257. // 保存信息
  258. async function onSubmit() {
  259. const { code } = await UserApi.updateUser({
  260. avatar: state.model.avatar,
  261. nickname: state.model.nickname,
  262. sex: state.model.sex,
  263. });
  264. if (code === 0) {
  265. await getUserInfo();
  266. }
  267. }
  268. // 获得用户信息
  269. const getUserInfo = async () => {
  270. // 个人信息
  271. const userInfo = await sheep.$store('user').getInfo();
  272. state.model = clone(userInfo);
  273. // 获得社交用户的信息
  274. if (sheep.$platform.name !== 'H5') {
  275. const result = await sheep.$platform.useProvider('wechat').getInfo();
  276. state.thirdInfo = result || {};
  277. }
  278. };
  279. onBeforeMount(() => {
  280. getUserInfo();
  281. });
  282. </script>
  283. <style lang="scss" scoped>
  284. :deep() {
  285. .uni-file-picker {
  286. border-radius: 50%;
  287. }
  288. .uni-file-picker__container {
  289. margin: -14rpx -12rpx;
  290. }
  291. .file-picker__progress {
  292. height: 0 !important;
  293. }
  294. .uni-list-item__content-title {
  295. font-size: 28rpx !important;
  296. color: #333333 !important;
  297. line-height: normal !important;
  298. }
  299. .uni-icons {
  300. font-size: 40rpx !important;
  301. }
  302. .is-disabled {
  303. color: #333333;
  304. }
  305. }
  306. :deep(.disabled) {
  307. opacity: 1;
  308. }
  309. .gender-name {
  310. font-size: 28rpx;
  311. font-weight: 500;
  312. line-height: normal;
  313. color: #333333;
  314. }
  315. .title-box {
  316. font-size: 28rpx;
  317. font-weight: 500;
  318. color: #666666;
  319. line-height: 100rpx;
  320. }
  321. .logout-btn {
  322. width: 710rpx;
  323. height: 80rpx;
  324. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  325. border-radius: 40rpx;
  326. font-size: 30rpx;
  327. font-weight: 500;
  328. color: $white;
  329. }
  330. .radio-dark {
  331. filter: grayscale(100%);
  332. filter: gray;
  333. opacity: 0.4;
  334. }
  335. .content-img {
  336. border-radius: 50%;
  337. }
  338. .header-box-content {
  339. position: relative;
  340. width: 160rpx;
  341. height: 160rpx;
  342. overflow: hidden;
  343. border-radius: 50%;
  344. }
  345. .avatar-action {
  346. position: absolute;
  347. left: 50%;
  348. transform: translateX(-50%);
  349. bottom: 0;
  350. z-index: 1;
  351. width: 160rpx;
  352. height: 46rpx;
  353. background: rgba(#000000, 0.3);
  354. .avatar-action-btn {
  355. width: 160rpx;
  356. height: 46rpx;
  357. font-weight: 500;
  358. font-size: 24rpx;
  359. color: #ffffff;
  360. }
  361. }
  362. // 绑定项
  363. .account-list {
  364. background-color: $white;
  365. height: 100rpx;
  366. padding: 0 20rpx;
  367. .list-img {
  368. width: 40rpx;
  369. height: 40rpx;
  370. margin-right: 10rpx;
  371. }
  372. .list-name {
  373. font-size: 28rpx;
  374. color: #333333;
  375. }
  376. .info {
  377. .avatar {
  378. width: 38rpx;
  379. height: 38rpx;
  380. border-radius: 50%;
  381. overflow: hidden;
  382. }
  383. .name {
  384. font-size: 28rpx;
  385. font-weight: 400;
  386. color: $dark-9;
  387. }
  388. }
  389. .bind-box {
  390. width: 100rpx;
  391. height: 50rpx;
  392. line-height: normal;
  393. display: flex;
  394. justify-content: center;
  395. align-items: center;
  396. font-size: 24rpx;
  397. .bind-btn {
  398. width: 100%;
  399. height: 100%;
  400. border-radius: 25rpx;
  401. background: #f4f4f4;
  402. color: #999999;
  403. }
  404. .relieve-btn {
  405. width: 100%;
  406. height: 100%;
  407. border-radius: 25rpx;
  408. background: var(--ui-BG-Main-opacity-1);
  409. color: var(--ui-BG-Main);
  410. }
  411. }
  412. }
  413. .list-border {
  414. font-size: 28rpx;
  415. font-weight: 400;
  416. color: #333333;
  417. border-bottom: 2rpx solid #eeeeee;
  418. }
  419. image {
  420. width: 100%;
  421. height: 100%;
  422. }
  423. </style>