展品维保小程序前端代码接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

472 lines
11 KiB

2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
  1. <template>
  2. <view class="user-info-container" :style="{ minHeight: containerHeight + 'rpx' }">
  3. <!-- 内容区域 -->
  4. <view class="content">
  5. <!-- 头像区域 -->
  6. <view class="avatar-section">
  7. <view class="app-info">
  8. <image class="app-logo" :src="configParamImage('app_logo')" mode="aspectFit"></image>
  9. <text class="app-name">{{ configParamText('app_name') }}</text>
  10. <text class="app-subname">申请获取您的头像昵称</text>
  11. </view>
  12. </view>
  13. <!-- 表单区域 -->
  14. <view class="form-section">
  15. <!-- 头像 -->
  16. <view class="form-item">
  17. <text class="form-label">头像</text>
  18. <button class="avatar-button" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  19. <image
  20. class="avatar-image"
  21. :src="userInfo.headImage || '/static/默认图片.png'"
  22. mode="aspectFill"
  23. ></image>
  24. </button>
  25. </view>
  26. <!-- 昵称 -->
  27. <view class="form-item">
  28. <text class="form-label">昵称</text>
  29. <input
  30. class="form-input"
  31. type="nickname"
  32. v-model="userInfo.nickName"
  33. placeholder="请输入昵称"
  34. @blur="onNicknameBlur"
  35. />
  36. </view>
  37. <!-- 手机号 -->
  38. <view class="form-item">
  39. <text class="form-label">手机号</text>
  40. <view class="phone-container" v-if="!userInfo.phone">
  41. <button
  42. class="get-phone-btn"
  43. open-type="getPhoneNumber"
  44. @getphonenumber="getPhoneNumber"
  45. >
  46. 获取手机号
  47. </button>
  48. </view>
  49. <text class="form-label" v-else>{{ userInfo.phone }}</text>
  50. </view>
  51. <!-- 所在部门 -->
  52. <view class="form-item">
  53. <text class="form-label">所在部门</text>
  54. <view class="department-container" @click="showDepartmentPicker">
  55. <text class="department-text" :class="{ 'active': userInfo.department.title }">{{ userInfo.department.title || '请选择' }}</text>
  56. <uv-icon name="arrow-down" size="20" color="#000"></uv-icon>
  57. </view>
  58. </view>
  59. <!-- 备注 -->
  60. <view class="form-item ">
  61. <text class="form-label">备注<text class="form-remark">(非必填)</text></text>
  62. <input
  63. class="form-input"
  64. v-model="userInfo.remark"
  65. />
  66. </view>
  67. </view>
  68. <!-- 确定按钮 -->
  69. <view class="submit-section">
  70. <view class="submit-btn" @click="submitUserInfo">
  71. <text class="submit-text">确定</text>
  72. </view>
  73. </view>
  74. </view>
  75. <!-- 部门选择器 -->
  76. <uv-picker
  77. confirm-color="#C70019"
  78. ref="departmentPicker"
  79. :columns="departmentColumns"
  80. keyName="title"
  81. @confirm="confirmDepartment"
  82. @cancel="cancelDepartment"
  83. ></uv-picker>
  84. </view>
  85. </template>
  86. <script>
  87. export default {
  88. name: 'UserInfo',
  89. data() {
  90. return {
  91. userInfo: {
  92. headImage: '',
  93. nickName: '',
  94. phone: '',
  95. department: {
  96. title: '',
  97. id: ''
  98. },
  99. remark: ''
  100. },
  101. showDepartmentSheet: false,
  102. // 部门选择器数据
  103. }
  104. },
  105. async onLoad() {
  106. this.calculateContainerHeight();
  107. await this.getUserInfo();
  108. },
  109. computed: {
  110. // 部门选择器数据格式转换
  111. departmentColumns() {
  112. if (!this.$store.state.departmentList || this.$store.state.departmentList.length === 0) {
  113. return [[]]
  114. }
  115. const departmentNames = this.$store.state.departmentList.map(item => {
  116. return {
  117. title: item.title,
  118. id: item.id
  119. }
  120. })
  121. return [departmentNames]
  122. }
  123. },
  124. methods: {
  125. // 获取微信用户信息
  126. async getWechatUserInfo() {
  127. const { result } = await this.$api.user.queryUser()
  128. this.userInfo.nickName = result.nickName
  129. this.userInfo.headImage = result.headImage
  130. this.userInfo.phone = result.phone
  131. },
  132. // 提交表单
  133. async getUserInfo(){
  134. const { result:info } = await this.$api.user.queryUser()
  135. this.userInfo.nickName = info.nickName
  136. this.userInfo.headImage = info.headImage
  137. this.userInfo.phone = info.phone
  138. },
  139. // 选择头像并上传到OSS
  140. async onChooseAvatar(e) {
  141. console.log('选择头像回调', e);
  142. if (e.detail.avatarUrl) {
  143. try {
  144. // 显示上传中提示
  145. uni.showLoading({ title: '上传头像中...' });
  146. // 构造文件对象
  147. const file = {
  148. path: e.detail.avatarUrl,
  149. tempFilePath: e.detail.avatarUrl
  150. };
  151. // 上传到OSS
  152. const uploadResult = await this.$utils.uploadImage(file);
  153. uni.hideLoading();
  154. if (uploadResult.success) {
  155. // 上传成功,更新头像URL
  156. this.userInfo.headImage = uploadResult.url;
  157. console.log('头像上传成功', uploadResult.url);
  158. uni.showToast({
  159. title: '头像上传成功',
  160. icon: 'success'
  161. });
  162. } else {
  163. }
  164. } catch (error) {
  165. uni.hideLoading();
  166. console.error('头像上传异常:', error);
  167. // 异常情况下使用本地头像
  168. this.userInfo.headImage = e.detail.avatarUrl;
  169. uni.showToast({
  170. title: '头像处理异常,使用本地头像',
  171. icon: 'none'
  172. });
  173. }
  174. } else {
  175. uni.showToast({
  176. title: '头像选择失败',
  177. icon: 'none'
  178. });
  179. }
  180. },
  181. // 昵称输入失焦
  182. onNicknameBlur() {
  183. if (!this.userInfo.nickName.trim()) {
  184. uni.showToast({
  185. title: '请输入昵称',
  186. icon: 'none'
  187. });
  188. }
  189. },
  190. // 显示部门选择器
  191. showDepartmentPicker() {
  192. this.$refs.departmentPicker.open();
  193. },
  194. // 确认部门选择
  195. confirmDepartment(e) {
  196. this.userInfo.department = e.value[0];
  197. },
  198. // 取消部门选择
  199. cancelDepartment() {
  200. // 取消操作
  201. },
  202. // 选择部门(保留原方法以防其他地方使用)
  203. selectDepartment(item) {
  204. this.userInfo.department = item.value;
  205. this.showDepartmentSheet = false;
  206. },
  207. // 获取手机号
  208. async getPhoneNumber(e) {
  209. console.log('获取手机号回调', e);
  210. if (e.detail.errMsg === 'getPhoneNumber:ok') {
  211. // 获取成功,可以通过e.detail.code发送到后端换取手机号
  212. console.log('获取手机号成功', e.detail);
  213. const res = await this.$api.login.bindPhone({
  214. phoneCode: e.detail.code
  215. })
  216. const str = JSON.parse(res.result);
  217. this.userInfo.phone = str.phone_info.phoneNumber
  218. uni.showToast({
  219. title: '手机号获取成功',
  220. icon: 'success'
  221. });
  222. } else {
  223. // 如果失败了就申请开启权限
  224. uni.showToast({
  225. title: '手机号获取失败',
  226. icon: 'error'
  227. })
  228. }
  229. },
  230. // 提交用户信息
  231. async submitUserInfo() {
  232. if (!this.userInfo.nickName.trim()) {
  233. uni.showToast({
  234. title: '请输入昵称',
  235. icon: 'none'
  236. });
  237. return;
  238. }
  239. if (!this.userInfo.phone.trim()) {
  240. uni.showToast({
  241. title: '请获取手机号',
  242. icon: 'none'
  243. });
  244. return;
  245. }
  246. if (!this.userInfo.department.title.trim()) {
  247. uni.showToast({
  248. title: '请选择所在部门',
  249. icon: 'none'
  250. });
  251. return;
  252. }
  253. console.log('提交用户信息', this.userInfo);
  254. try {
  255. // 提交用户信息
  256. await this.$api.user.updateUser({
  257. nickName: this.userInfo.nickName,
  258. phone: this.userInfo.phone,
  259. headImage: this.userInfo.headImage,
  260. department: this.userInfo.department.id,
  261. remark: this.userInfo.remark
  262. });
  263. uni.showToast({
  264. title: '信息保存成功',
  265. icon: 'success'
  266. });
  267. // 跳转到首页
  268. setTimeout(() => {
  269. uni.switchTab({
  270. url: '/pages/index/home'
  271. });
  272. }, 1000);
  273. } catch (error) {
  274. console.error('保存用户信息失败:', error);
  275. uni.showToast({
  276. title: '保存失败,请重试',
  277. icon: 'none'
  278. });
  279. }
  280. }
  281. }
  282. }
  283. </script>
  284. <style lang="scss" scoped>
  285. .user-info-container {
  286. background-color: #fff;
  287. box-sizing: border-box;
  288. }
  289. .content {
  290. padding: 0 66rpx;
  291. }
  292. .avatar-section {
  293. display: flex;
  294. justify-content: center;
  295. margin-bottom: 90rpx;
  296. margin-top: 192rpx;
  297. .app-info {
  298. display: flex;
  299. flex-direction: column;
  300. align-items: center;
  301. gap: 22rpx;
  302. .app-logo {
  303. width: 92rpx;
  304. height: 92rpx;
  305. // border-radius: 20rpx;
  306. // margin-bottom: 23rpx;
  307. }
  308. .app-name {
  309. font-size: 36rpx;
  310. font-weight: bold;
  311. color: $primary-text-color;
  312. }
  313. .app-subname {
  314. font-size: 30rpx;
  315. color: $primary-text-color;
  316. }
  317. }
  318. }
  319. .form-item {
  320. // background: green;
  321. display: flex;
  322. align-items: center;
  323. // margin-bottom: 30rpx;
  324. height: 114rpx;
  325. justify-content: space-between;
  326. &:last-child {
  327. margin-bottom: 45rpx;
  328. }
  329. .form-label {
  330. // width: 120rpx;
  331. // flex: 1;
  332. font-size: 32rpx;
  333. color: $primary-text-color;
  334. // font-weight: 500;
  335. }
  336. .form-input {
  337. // flex: 1;
  338. // height: 80rpx;
  339. // padding: 0 20rpx;
  340. // border-radius: 10rpx;
  341. font-size: 30rpx;
  342. color: $primary-text-color;
  343. border: none;
  344. background-color: transparent;
  345. text-align: right;
  346. }
  347. .form-remark {
  348. margin-left: 16rpx;
  349. color: $secondary-text-color;
  350. }
  351. .department-container{
  352. display: flex;
  353. align-items: center;
  354. gap: 22rpx;
  355. color: $secondary-text-color;
  356. .department-text.active {
  357. color: $primary-text-color;
  358. }
  359. }
  360. }
  361. // 头像按钮
  362. .avatar-button {
  363. margin: 0;
  364. padding: 0;
  365. background: transparent;
  366. border: none;
  367. width: 100rpx;
  368. height: 100rpx;
  369. border: 2rpx solid #cccccc;
  370. &::after {
  371. border: none;
  372. }
  373. .avatar-image {
  374. width: 100rpx;
  375. height: 100rpx;
  376. // border-radius: 10rpx;
  377. }
  378. }
  379. // background: white;
  380. .get-phone-btn {
  381. // margin-left: auto;
  382. width: 176rpx;
  383. height: 76rpx;
  384. background-color: $primary-color;
  385. color: white;
  386. border-radius: 38rpx;
  387. font-size: 22rpx;
  388. line-height: 76rpx;
  389. border: none;
  390. &::after {
  391. border: none;
  392. }
  393. &:active {
  394. opacity: 0.8;
  395. }
  396. }
  397. // }
  398. .submit-section {
  399. width: 594rpx;
  400. height: 94rpx;
  401. margin: 0 auto;
  402. .submit-btn {
  403. width: 100%;
  404. height: 94rpx;
  405. background-color: $primary-color;
  406. border-radius: 41rpx;
  407. display: flex;
  408. align-items: center;
  409. justify-content: center;
  410. .submit-text {
  411. font-size: 32rpx;
  412. // font-weight: 500;
  413. color: #ffffff;
  414. }
  415. }
  416. }
  417. </style>