myInfo.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <view class="my-info-page">
  3. <!-- 导航区域 -->
  4. <view class="icon-title-navBar-box">
  5. <view @click="handleBack" class="nav-bar-icon"></view>
  6. <text class="nav-bar-title">个人信息</text>
  7. </view>
  8. <!-- 头像 -->
  9. <view class="user-img-box">
  10. <uni-file-picker limit="1" :del-icon="false" disable-preview :auto-upload="false"
  11. @select="handleSelect" file-mediatype="image" class="phone-file-picker user-file-picker">
  12. <text class="user-title">头像</text>
  13. <img class="head-img" :src="data.icon" v-if="data.icon">
  14. <icon class="phone-default-userImg" v-else></icon>
  15. <icon class="user-jt-icon"></icon>
  16. </uni-file-picker>
  17. <!-- <view>图片地址:{{data.icon}}</view> -->
  18. </view>
  19. <!-- 姓名 -->
  20. <view class="form-label-input">
  21. <view class="phone-form-label"><text class="form-label-require">*</text>姓名</view>
  22. <input v-model="data.realName" placeholder="请输入姓名" />
  23. <icon></icon>
  24. </view>
  25. <!-- 手机号 -->
  26. <view class="form-label-input">
  27. <view class="phone-form-label"><text class="form-label-require">*</text>手机号</view>
  28. <input v-model="data.userName" placeholder="请输入手机号" />
  29. <icon></icon>
  30. </view>
  31. <button type="default" class="phone-green-btn info-btn" @click="handleUpdate">修改资料</button>
  32. </view>
  33. </template>
  34. <script setup>
  35. import * as myApi from "@/api/my.js";
  36. import {getAliyunPolicy} from "@/api/jiazheng.js"
  37. import {ref,reactive} from "vue"
  38. import {onLoad} from "@dcloudio/uni-app"
  39. const imageStyles = ref({
  40. width: 120,
  41. height: 120,
  42. });
  43. const data = reactive({
  44. from: 'shouye', // my | kaoshi | shouye
  45. gender: 0,
  46. icon: '',
  47. realName: '',
  48. userName: '',
  49. userId: null
  50. })
  51. onLoad((options) => {
  52. data.from = options.from || 'shouye';
  53. initPage();
  54. })
  55. function handleSelect(e) {
  56. console.log('eee',e)
  57. doUploadImage(e)
  58. }
  59. function genderSelect(genderValue){
  60. data.gender = genderValue;
  61. }
  62. function doUploadImage(params) {
  63. const filePath = params.tempFiles[0];
  64. const fileUrl = params.tempFilePaths;
  65. try {
  66. const suffix = filePath.file.name.split('.').pop();
  67. let req = {
  68. prefix: 'resource/',
  69. suffix: suffix,
  70. }
  71. uni.showLoading({
  72. title: '上传中...',
  73. mask: true
  74. });
  75. getAliyunPolicy(req).then(res => {
  76. if (res.code == 0) {
  77. const policyData = res.data;
  78. const formData = {
  79. key: policyData.key,
  80. policy: policyData.policy,
  81. signature: policyData.signature,
  82. OSSAccessKeyId: policyData.accessid,
  83. success_action_status: '200',
  84. }
  85. uni.uploadFile({
  86. url: policyData.uploadUrl,
  87. filePath: fileUrl[0],
  88. name: 'file',
  89. formData: formData,
  90. success(uploadRes) {
  91. if (uploadRes.statusCode === 200) {
  92. console.log('uploadRes',uploadRes)
  93. data.icon = `${policyData.downloadUrl}/${policyData.key}`;
  94. console.log( `${policyData.downloadUrl}/${policyData.key}`)
  95. uni.showToast({
  96. title: '上传成功',
  97. icon: 'success'
  98. });
  99. } else {
  100. uni.showToast({
  101. icon: 'none',
  102. title: '上传失败',
  103. });
  104. uni.hideLoading();
  105. }
  106. },
  107. fail(err) {
  108. uni.showToast({
  109. icon: 'none',
  110. title: '上传失败',
  111. });
  112. uni.hideLoading();
  113. }
  114. });
  115. } else {
  116. uni.showToast({
  117. title: '获取凭证失败',
  118. });
  119. uni.hideLoading();
  120. }
  121. })
  122. } catch (error) {
  123. uni.showToast({
  124. title: '上传失败',
  125. icon: 'none'
  126. });
  127. uni.hideLoading();
  128. }
  129. }
  130. function handleUpdate() {
  131. let arr = [];
  132. if (!data.realName) {
  133. arr.push('姓名');
  134. }
  135. if (!data.userName) {
  136. arr.push('电话');
  137. }
  138. if (!data.icon) {
  139. arr.push('头像');
  140. }
  141. if (!data.realName || !data.userName||!data.icon) {
  142. uni.showToast({
  143. icon: 'none',
  144. title: `请完善${arr.join('、')}信息!`
  145. })
  146. return;
  147. }
  148. const opt = {
  149. gender: data.gender,
  150. icon: data.icon,
  151. realName: data.realName,
  152. userId: data.userId,
  153. userName: data.userName,
  154. }
  155. myApi.getMineUpdate(opt).then(res => {
  156. if (res.data) {
  157. uni.showToast({
  158. title: '更新成功'
  159. })
  160. handleBack();
  161. }
  162. })
  163. }
  164. function handleBack() {
  165. const pages = getCurrentPages();
  166. if (pages.length>1) {
  167. uni.navigateBack()
  168. } else {
  169. /* if (data.from == 'my') {
  170. uni.redirectTo({
  171. url: '/pages/client/my/index'
  172. })
  173. } else if (data.from == 'kaoshi') {
  174. uni.redirectTo({
  175. url: '/pages/client/ShouYe/shouye'
  176. })
  177. } else {
  178. uni.redirectTo({
  179. url: '/pages/client/my/index'
  180. })
  181. } */
  182. history.back();
  183. }
  184. }
  185. function initPage() {
  186. myApi.getMineUser().then(res => {
  187. const {gender,icon,realName,userName,userId} = res.data;
  188. data.gender = res.data.gender;
  189. data.icon = res.data.icon;
  190. data.realName = res.data.realName;
  191. data.userName = res.data.userName;
  192. data.userId = res.data.userId;
  193. })
  194. }
  195. </script>
  196. <style>
  197. </style>