myInfo.vue 6.3 KB

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