upBasic.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. <upload :list="icon" name="icon" :count="1" @uplSuc="uplSuc" @uplDel="uplDel"></upload>
  8. </uni-forms-item>
  9. <uni-forms-item label="用户名" name="name">
  10. <uni-easyinput type="text" v-model="form.name" placeholder="请输入用户名" />
  11. </uni-forms-item>
  12. <!-- <uni-forms-item label="手机号" name="phone">
  13. <uni-easyinput type="text" v-model="form.phone" placeholder="请输入手机号" disabled />
  14. </uni-forms-item>
  15. <uni-forms-item label="电子邮箱" name="email">
  16. <uni-easyinput type="text" v-model="form.email" placeholder="请输入电子邮箱" disabled />
  17. </uni-forms-item> -->
  18. <uni-forms-item label="性别" name="gender">
  19. <picker @change="genderChange" name="gender" :value="form.gender" :range="genderList" range-key="label">
  20. <view class="uni-input">{{form.zhGender||'请选择性别'}}</view>
  21. </picker>
  22. </uni-forms-item>
  23. <uni-forms-item label="生日" name="birth">
  24. <picker mode="date" :value="form.birth" name="birth" @change="bindDateChange">
  25. <view class="uni-input">{{form.birth||'请选择日期'}}</view>
  26. </picker>
  27. </uni-forms-item>
  28. </uni-forms>
  29. <view class="btn">
  30. <button type="primary" @click="onSubmit('form')" size="small">提交</button>
  31. </view>
  32. </view>
  33. </view>
  34. </mobile-frame>
  35. </template>
  36. <script>
  37. import upload from '@/components/upload/index.vue';
  38. export default {
  39. components: {
  40. upload
  41. },
  42. data() {
  43. return {
  44. frameStyle: {
  45. useBar: false
  46. },
  47. form: {},
  48. rules: {
  49. name: {
  50. rules: [{
  51. required: true,
  52. errorMessage: '请输入用户名',
  53. }]
  54. },
  55. phone: {
  56. rules: [{
  57. required: true,
  58. errorMessage: '请输入手机号',
  59. }]
  60. },
  61. },
  62. genderList: [{
  63. label: '保密',
  64. value: '0'
  65. },
  66. {
  67. label: '男',
  68. value: '1'
  69. },
  70. {
  71. label: '女',
  72. value: '2'
  73. },
  74. ],
  75. icon: []
  76. };
  77. },
  78. onLoad: function(e) {
  79. const that = this;
  80. that.watchLogin();
  81. },
  82. methods: {
  83. watchLogin() {
  84. const that = this;
  85. uni.getStorage({
  86. key: 'token',
  87. success: async (res) => {
  88. let user = that.$jwt(res.data);
  89. if (user) {
  90. const arr = await that.$api(`/user/${user.id}`, 'GET');
  91. if (arr.errcode == '0') {
  92. let gender = that.genderList.find(i => i.value == arr.data
  93. .gender)
  94. if (gender) arr.data.zhGender = gender.label;
  95. that.$set(that, `form`, arr.data)
  96. that.$set(that, `icon`, arr.data.icon)
  97. } else {
  98. uni.showToast({
  99. title: arr.errmsg,
  100. icon: 'error',
  101. duration: 2000
  102. });
  103. }
  104. }
  105. },
  106. fail: (err) => {}
  107. })
  108. },
  109. toPath(e) {
  110. if (e && e.route) uni.redirectTo({
  111. url: `/${e.route}`
  112. })
  113. },
  114. // 图片上传
  115. uplSuc(e) {
  116. const that = this;
  117. that.$set(that, `${e.name}`, [...that[e.name], e.data]);
  118. },
  119. // 图片删除
  120. uplDel(e) {
  121. const that = this;
  122. let data = that[e.name];
  123. let arr = data.filter((i, index) => index != e.data.index);
  124. that.$set(that, `${e.name}`, arr)
  125. },
  126. // 选择性别
  127. genderChange(e) {
  128. const that = this;
  129. let data = that.genderList[e.detail.value];
  130. if (data) {
  131. that.$set(that.form, `gender`, data.value);
  132. that.$set(that.form, `zhGender`, data.label);
  133. }
  134. },
  135. // 选择日期
  136. bindDateChange(e) {
  137. const that = this;
  138. that.$set(that.form, `birth`, e.detail.value);
  139. },
  140. // 返回
  141. back() {
  142. uni.navigateBack({
  143. delta: 1
  144. })
  145. },
  146. // 提交保存
  147. onSubmit(ref) {
  148. const that = this;
  149. that.$refs[ref].validate().then(async params => {
  150. params.icon = that.icon
  151. const arr = await that.$api(`/user/${that.form.id}`, 'POST', params);
  152. if (arr.errcode == '0') {
  153. uni.showToast({
  154. title: `维护信息成功`,
  155. icon: 'success',
  156. });
  157. that.back();
  158. } else {
  159. uni.showToast({
  160. title: arr.errmsg,
  161. })
  162. }
  163. })
  164. },
  165. }
  166. }
  167. </script>
  168. <style lang="scss">
  169. .info {
  170. display: flex;
  171. flex-direction: column;
  172. width: 100vw;
  173. height: 100vh;
  174. .one {
  175. padding: 2vw;
  176. .uni-input {
  177. border: #f1f1ff 1px solid;
  178. padding: 2vw 2vw;
  179. border-radius: 1vw;
  180. }
  181. .btn {
  182. text-align: center;
  183. button {
  184. margin: 0 2vw 2vw 2vw;
  185. background-color: var(--ff0Color);
  186. color: var(--fffColor);
  187. }
  188. .name {
  189. color: var(--f85Color);
  190. font-size: var(--font14Size);
  191. }
  192. }
  193. }
  194. }
  195. .uni-forms-item {
  196. margin-bottom: 6vw !important;
  197. display: flex;
  198. flex-direction: row;
  199. }
  200. </style>