info.vue 12 KB

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