edit.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <template>
  2. <view class="edit">
  3. <uv-navbar title="修改资料" placeholder autoBack></uv-navbar>
  4. <form @submit="formSubmit">
  5. <view class="middle">
  6. <view class="item">
  7. <view class="left"> 头像 </view>
  8. <view class="right">
  9. <image
  10. v-if="unserInfo.headPhoto"
  11. :src="unserInfo.headPhoto"
  12. @click="uploadImg"
  13. mode="aspectFill"
  14. >
  15. </image>
  16. <image
  17. v-else
  18. :src="$defaultAvatar"
  19. mode="aspectFill"
  20. @click="uploadImg"
  21. ></image>
  22. <text class="iconfont">&#xe6c7;</text>
  23. </view>
  24. </view>
  25. <view class="name-item">
  26. <view class="name-left"> 姓名 </view>
  27. <view class="name-right">
  28. <input
  29. type="text"
  30. :value="unserInfo.nickname"
  31. maxlength="32"
  32. @input="onName"
  33. class="my-name"
  34. />
  35. <text class="iconfont">&#xe6c7;</text>
  36. </view>
  37. </view>
  38. <view class="item">
  39. <view class="left"> 手机号 </view>
  40. <view class="right">
  41. <view class="phone">
  42. {{ unserInfo.mobile }}
  43. </view>
  44. </view>
  45. </view>
  46. <view class="item">
  47. <view class="left"> 性别 </view>
  48. <view class="right" v-if="platform.length > 0">
  49. <picker
  50. class="choice"
  51. @change="bindPickerChange"
  52. :value="unserInfo.gender"
  53. :range="platform"
  54. range-key="name"
  55. >
  56. <view class="uni-input"
  57. >{{ platform[index].name
  58. }}<text class="iconfont">&#xe6c7;</text>
  59. </view>
  60. </picker>
  61. </view>
  62. </view>
  63. <view class="item">
  64. <view class="left"> 生日 </view>
  65. <view class="right" style="flex: 1">
  66. <picker
  67. style="flex: 1; height: 38rpx; text-align: right"
  68. mode="date"
  69. :value="date"
  70. :start="startDate"
  71. :end="endDate"
  72. @change="bindDateChange"
  73. >
  74. <view class="uni-input">{{ date ? date : "未知" }}</view>
  75. </picker>
  76. <text class="iconfont">&#xe6c7;</text>
  77. </view>
  78. </view>
  79. </view>
  80. <view class="submit-top">
  81. <button class="submit" form-type="submit">修改资料</button>
  82. </view>
  83. </form>
  84. </view>
  85. </template>
  86. <script setup>
  87. import { ref } from "vue";
  88. import { onShow, onLoad } from "@dcloudio/uni-app";
  89. import { userInfo, personalDataUpdate_Api } from "@/api/login.js";
  90. import { commonUpload_api } from "@/api/index";
  91. const platform = ref([
  92. {
  93. name: "保密",
  94. },
  95. {
  96. name: "男",
  97. },
  98. {
  99. name: "女",
  100. },
  101. ]);
  102. // 获取日期
  103. const getDate = (type) => {
  104. const date = new Date();
  105. let year = date.getFullYear();
  106. let month = date.getMonth() + 1;
  107. let day = date.getDate();
  108. if (type === "start") {
  109. year = year - 150;
  110. } else if (type === "end") {
  111. year = year + 150;
  112. }
  113. month = month > 9 ? month : "0" + month;
  114. day = day > 9 ? day : "0" + day;
  115. return `${year}-${month}-${day}`;
  116. };
  117. const imgSize = ref(4);
  118. const index = ref(0);
  119. const date = ref(getDate({ format: true }));
  120. const startDate = ref(getDate("start"));
  121. const endDate = ref(getDate("end"));
  122. const unserInfo = ref({});
  123. const imgType = ref(["jpeg", "png", "jpg"]);
  124. // 获取个人信息
  125. const getUserInfo = () => {
  126. userInfo().then((res) => {
  127. index.value = res.data.gender; //男女
  128. date.value = res.data.birthday;
  129. unserInfo.value = res.data;
  130. });
  131. };
  132. const formSubmit = (e) => {
  133. let gender = index.value.toString();
  134. let param = {
  135. realName: unserInfo.value.realName,
  136. headPhoto: unserInfo.value.headPhoto, //this.unserInfo.headPhoto, //图像
  137. gender: gender, //性别
  138. birthday: date.value, //生日
  139. nickname: unserInfo.value.realName,
  140. };
  141. console.log(param, "param");
  142. personalDataUpdate_Api(param).then((res) => {
  143. if (res.code == 200) {
  144. uni.$uv.toast("修改成功");
  145. } else {
  146. uni.$uv.toast("修改失败");
  147. }
  148. });
  149. };
  150. const updataImg = (filePath, nameStr) => {
  151. console.log(filePath, nameStr);
  152. let shopid = uni.getStorageSync("shop");
  153. let id = 0;
  154. if (!shopid) {
  155. id = 0;
  156. } else {
  157. id = shopid.id;
  158. }
  159. commonUpload_api(filePath)
  160. .then((res) => {
  161. uni.hideLoading();
  162. if (res.code != 200) {
  163. return;
  164. }
  165. // let resObj = JSON.parse(res.data);
  166. let imgurl = res.data.url;
  167. uni.showToast({
  168. title: "上传成功",
  169. icon: "success",
  170. duration: 1000,
  171. });
  172. unserInfo.value.headPhoto = imgurl;
  173. })
  174. .catch((err) => {
  175. uni.hideLoading();
  176. uni.showModal({
  177. content: err.errMsg,
  178. showCancel: false,
  179. });
  180. });
  181. // uni.uploadFile({
  182. // url: uni.$mConfig.baseUrl + "/sys/oss/upload",
  183. // filePath: filePath,
  184. // fileType: "image",
  185. // name: "file",
  186. // success: (res) => {},
  187. // fail: (err) => {},
  188. // });
  189. };
  190. // 随机字符串
  191. const random_string = (len) => {
  192. len = len || 32;
  193. var chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789";
  194. var maxPos = chars.length;
  195. var pwd = "";
  196. for (let i = 0; i < len; i++) {
  197. pwd += chars.charAt(Math.floor(Math.random() * maxPos));
  198. }
  199. return pwd;
  200. };
  201. //更换图片
  202. const uploadImg = () => {
  203. uni.chooseImage({
  204. count: 1, //默认9
  205. sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
  206. sourceType: ["album"], //从相册选择
  207. success: (res) => {
  208. if (res) {
  209. let filePath = res.tempFilePaths[0];
  210. let nameStr =
  211. random_string(8) + "." + res.tempFilePaths[0].split(".").pop();
  212. if (res.tempFiles) {
  213. for (let item of res.tempFiles) {
  214. if (item.size > imgSize.value * 1024 * 1024) {
  215. uni.showToast({
  216. title: `图片不能大于${imgSize.value}M`,
  217. icon: "none",
  218. });
  219. return false;
  220. }
  221. if (item.type) {
  222. let r = imgType.value.some((v) => {
  223. let type = item.type.split("/");
  224. if (type.length) return v === type[1];
  225. });
  226. if (!r) {
  227. uni.showToast({
  228. title: `只允许上传${imgType.value}的类型`,
  229. icon: "none",
  230. });
  231. return false;
  232. }
  233. }
  234. }
  235. }
  236. updataImg(filePath, nameStr);
  237. }
  238. },
  239. });
  240. };
  241. const onName = (e) => {
  242. console.log(e.detail.value, "e.detail.value");
  243. unserInfo.value.realName = e.detail.value;
  244. };
  245. const bindPickerChange = (e) => {
  246. console.log("picker发送选择改变,携带值为", e.detail.value);
  247. index.value = e.detail.value;
  248. };
  249. const bindDateChange = (e) => {
  250. console.log("picker发送选择改变,携带值为", e.detail.value);
  251. date.value = e.detail.value;
  252. };
  253. onLoad(() => {
  254. getUserInfo();
  255. });
  256. onShow(() => {});
  257. </script>
  258. <style scoped lang="scss">
  259. .name-item {
  260. box-sizing: border-box;
  261. display: flex;
  262. align-items: center;
  263. justify-content: space-between;
  264. border-bottom: 1rpx solid #e6e6e6;
  265. padding: 34rpx 0 30rpx 0;
  266. .name-right {
  267. flex: 1;
  268. padding-left: 30rpx;
  269. text-align: right;
  270. display: flex;
  271. align-items: center;
  272. .my-name {
  273. width: 100%;
  274. }
  275. }
  276. }
  277. .submit-top {
  278. box-sizing: border-box;
  279. width: 100%;
  280. padding: 0 30rpx;
  281. position: fixed;
  282. bottom: 60rpx;
  283. }
  284. .submit {
  285. box-sizing: border-box;
  286. text-align: center;
  287. border-radius: 43rpx;
  288. margin-top: 350rpx;
  289. font-size: 36rpx;
  290. color: #ffffff;
  291. font-weight: 400;
  292. background-color: #eb5153;
  293. }
  294. .distance {
  295. height: 10rpx;
  296. background-color: #f5f5f5;
  297. }
  298. .middle {
  299. padding: 0 30rpx;
  300. .item {
  301. display: flex;
  302. align-items: center;
  303. justify-content: space-between;
  304. border-bottom: 1rpx solid #e6e6e6;
  305. padding: 34rpx 0 30rpx 0;
  306. .left {
  307. font-size: 28rpx;
  308. color: #1a1a1a;
  309. font-weight: 400;
  310. }
  311. .right {
  312. display: flex;
  313. align-items: center;
  314. .my-phone {
  315. text-align: right;
  316. }
  317. .my-name {
  318. text-align: right;
  319. }
  320. .name {
  321. font-size: 28rpx;
  322. color: #1a1a1a;
  323. font-weight: 400;
  324. }
  325. image {
  326. width: 76rpx;
  327. height: 76rpx;
  328. border-radius: 50%;
  329. }
  330. .iconfont {
  331. margin-left: 15rpx;
  332. }
  333. }
  334. }
  335. }
  336. </style>