upBasic.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <template>
  2. <mobile-frame>
  3. <view class="info">
  4. <view class="one">
  5. <uni-forms ref="form" :modelValue="form" :rules="rules" label-width="auto">
  6. <uni-forms-item label="头像" name="icon">
  7. <view class="icon">
  8. <view class="icon_1" v-if="form.icon&&form.icon.length>0">
  9. <image class="image" v-for="i in form.icon" :key="i" :src="i.url" mode=""></image>
  10. <uni-icons class="del" type="close" size="30" color="#ff0000" @click="iconDel()"></uni-icons>
  11. </view>
  12. <view class="icon_2" v-else>
  13. <button open-type="chooseAvatar" @chooseavatar="chooseavatar">上传头像</button>
  14. </view>
  15. </view>
  16. </uni-forms-item>
  17. <uni-forms-item label="用户名" name="name">
  18. <input class="input" type="nickname" :value="form.name" placeholder="请输入用户名" @input="nameInput">
  19. <!-- <uni-easyinput type="nick" v-model="form.name" placeholder="请输入用户名" /> -->
  20. </uni-forms-item>
  21. <uni-forms-item label="性别" name="gender">
  22. <picker @change="genderChange" name="gender" :value="form.gender" :range="genderList" range-key="label">
  23. <view class="uni-input">{{form.zhGender||'请选择性别'}}</view>
  24. </picker>
  25. </uni-forms-item>
  26. <uni-forms-item label="生日" name="birth">
  27. <picker mode="date" :value="form.birth" name="birth" @change="bindDateChange">
  28. <view class="uni-input">{{form.birth||'请选择日期'}}</view>
  29. </picker>
  30. </uni-forms-item>
  31. </uni-forms>
  32. <view class="btn">
  33. <button type="primary" @click="onSubmit('form')" size="small">提交</button>
  34. </view>
  35. </view>
  36. </view>
  37. <view class="two">
  38. {{pro_env}}
  39. </view>
  40. </mobile-frame>
  41. </template>
  42. <script>
  43. export default {
  44. components: {},
  45. data() {
  46. return {
  47. frameStyle: {
  48. useBar: false
  49. },
  50. form: {
  51. icon: []
  52. },
  53. rules: {
  54. name: {
  55. rules: [{
  56. required: true,
  57. errorMessage: '请输入用户名',
  58. }]
  59. },
  60. phone: {
  61. rules: [{
  62. required: true,
  63. errorMessage: '请输入手机号',
  64. }]
  65. },
  66. },
  67. genderList: [],
  68. pro_env: this.$config.pro_env
  69. };
  70. },
  71. onLoad: async function(e) {
  72. const that = this;
  73. await that.searchOther();
  74. that.watchLogin();
  75. },
  76. methods: {
  77. async searchOther() {
  78. const that = this;
  79. let res;
  80. res = await that.$api(`/dictData`, 'GET', {
  81. code: "gender"
  82. });
  83. if (res.errcode == '0') {
  84. that.$set(that, `genderList`, res.data)
  85. }
  86. },
  87. watchLogin() {
  88. const that = this;
  89. uni.getStorage({
  90. key: 'token',
  91. success: async (res) => {
  92. let user = that.$jwt(res.data);
  93. if (user) {
  94. const arr = await that.$api(`/user/${user.id}`, 'GET');
  95. if (arr.errcode == '0') {
  96. let gender = that.genderList.find(i => i.value == arr.data
  97. .gender)
  98. if (gender) arr.data.zhGender = gender.label;
  99. that.$set(that, `form`, arr.data)
  100. } else {
  101. uni.showToast({
  102. title: arr.errmsg,
  103. icon: 'error',
  104. duration: 2000
  105. });
  106. }
  107. }
  108. },
  109. fail: (err) => {}
  110. })
  111. },
  112. toPath(e) {
  113. if (e && e.route) uni.redirectTo({
  114. url: `/${e.route}`
  115. })
  116. },
  117. // 删除头像
  118. iconDel() {
  119. const that = this;
  120. that.$set(that.form, `icon`, [])
  121. },
  122. // 头像上传
  123. async chooseavatar(e) {
  124. const that = this;
  125. let avatarUrl = e.detail.avatarUrl;
  126. let serverUrl = that.$config.serverUrl;
  127. const res = await that.$apifile(`/point/upload`, 'file', avatarUrl, 'file');
  128. if (res.errcode == '0') {
  129. let obj = {
  130. name: res.name,
  131. uri: res.uri,
  132. url: serverUrl + res.uri
  133. }
  134. that.$set(that.form, `icon`, [obj])
  135. } else {
  136. uni.showToast({
  137. title: res.errmsg,
  138. icon: 'none'
  139. })
  140. }
  141. },
  142. // 选择,输入昵称
  143. nameInput(e) {
  144. const that = this;
  145. that.$set(that.form, `name`, e.detail.value);
  146. },
  147. // 选择性别
  148. genderChange(e) {
  149. const that = this;
  150. let data = that.genderList[e.detail.value];
  151. if (data) {
  152. that.$set(that.form, `gender`, data.value);
  153. that.$set(that.form, `zhGender`, data.label);
  154. }
  155. },
  156. // 选择日期
  157. bindDateChange(e) {
  158. const that = this;
  159. that.$set(that.form, `birth`, e.detail.value);
  160. },
  161. // 返回
  162. back() {
  163. uni.navigateBack({
  164. delta: 1
  165. })
  166. },
  167. // 提交保存
  168. onSubmit(ref) {
  169. const that = this;
  170. that.$refs[ref].validate().then(async params => {
  171. const arr = await that.$api(`/user/${that.form.id}`, 'POST', params);
  172. if (arr.errcode == '0') {
  173. uni.showToast({
  174. title: `维护信息成功`,
  175. icon: 'success',
  176. });
  177. that.back();
  178. } else {
  179. uni.showToast({
  180. title: arr.errmsg,
  181. })
  182. }
  183. })
  184. },
  185. }
  186. }
  187. </script>
  188. <style lang="scss">
  189. .info {
  190. display: flex;
  191. flex-direction: column;
  192. width: 100vw;
  193. height: 100vh;
  194. .one {
  195. padding: 2vw;
  196. .uni-input {
  197. border: #f1f1ff 1px solid;
  198. padding: 2vw 2vw;
  199. border-radius: 1vw;
  200. }
  201. .input {
  202. border: #f1f1ff 1px solid;
  203. padding: 1.5vw 1vw;
  204. border-radius: 5px;
  205. }
  206. .btn {
  207. text-align: center;
  208. button {
  209. margin: 0 2vw 2vw 2vw;
  210. background-color: var(--ff0Color);
  211. color: var(--fffColor);
  212. }
  213. .name {
  214. color: var(--f85Color);
  215. font-size: var(--font14Size);
  216. }
  217. }
  218. .icon {
  219. display: flex;
  220. .icon_1 {
  221. position: relative;
  222. .image {
  223. width: 30vw;
  224. height: 30vw;
  225. }
  226. .del {
  227. position: absolute;
  228. top: 0;
  229. left: 22vw;
  230. }
  231. }
  232. .icon_2 {
  233. text-align: left;
  234. button {
  235. width: 30vw;
  236. padding: 0vw 1vw;
  237. font-size: 14px;
  238. }
  239. }
  240. }
  241. }
  242. }
  243. .uni-forms-item {
  244. margin-bottom: 6vw !important;
  245. display: flex;
  246. flex-direction: row;
  247. }
  248. .two {
  249. position: absolute;
  250. bottom: 0;
  251. text-align: center;
  252. width: 100vw;
  253. font-size: 12px;
  254. color: #f1f1ff;
  255. }
  256. </style>