upBasic.vue 4.6 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="请输入手机号" />
  14. </uni-forms-item>
  15. <uni-forms-item label="性别" name="gender">
  16. <picker @change="genderChange" name="gender" :value="form.gender" :range="genderList"
  17. range-key="label">
  18. <view class="uni-input">{{form.zhGender||'请选择性别'}}</view>
  19. </picker>
  20. </uni-forms-item>
  21. <uni-forms-item label="生日" name="birth">
  22. <picker mode="date" :value="form.birth" name="birth" :start="startDate" :end="endDate"
  23. @change="bindDateChange">
  24. <view class="uni-input">{{form.birth||'请选择日期'}}</view>
  25. </picker>
  26. </uni-forms-item>
  27. </uni-forms>
  28. <view class="btn">
  29. <button type="primary" @click="onSubmit('form')" size="small">提交</button>
  30. </view>
  31. </view>
  32. </view>
  33. </mobile-frame>
  34. </template>
  35. <script>
  36. import upload from '@/components/upload/index.vue';
  37. export default {
  38. components: {
  39. upload
  40. },
  41. data() {
  42. return {
  43. frameStyle: {
  44. useBar: false
  45. },
  46. form: {},
  47. rules: {
  48. name: {
  49. rules: [{
  50. required: true,
  51. errorMessage: '请输入用户名',
  52. }]
  53. },
  54. phone: {
  55. rules: [{
  56. required: true,
  57. errorMessage: '请输入手机号',
  58. }]
  59. },
  60. },
  61. genderList: [{
  62. label: '保密',
  63. value: '0'
  64. },
  65. {
  66. label: '男',
  67. value: '1'
  68. },
  69. {
  70. label: '女',
  71. value: '2'
  72. },
  73. ],
  74. icon: []
  75. };
  76. },
  77. onLoad: function(e) {
  78. const that = this;
  79. that.watchLogin();
  80. },
  81. onShow: function() {},
  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>