开始

日期选择
image.png
上节课,我们这里写的全局变量的好处。定义全局变量。
image.png

image.png

image.png

生日的修改

image.png

  1. <view>{{birthday}}</view>

image.png

  1. birthday:'1987-02-07'

用到时间的选择器

image.png

image.png
image.png

image.png

  1. <picker mode="date" :value="birthday" :sstart="startDate" :end="endDate" @change="bindDateChange">
  2. <view class="u-f-ac">
  3. <view>{{birthday}}</view>
  4. <view class="icon iconfont icon-bianji1"></view>
  5. </view>
  6. </picker>

image.png
我们直接绑定生日的字段
image.png
计算属性复制过来
image.png

  1. computed: {
  2. startDate() {
  3. return this.getDate('start');
  4. },
  5. endDate() {
  6. return this.getDate('end');
  7. }
  8. },

getDate是在methods复制过来的方法

image.png

  1. getDate(type) {
  2. const date = new Date();
  3. let year = date.getFullYear();
  4. let month = date.getMonth() + 1;
  5. let day = date.getDate();
  6. if (type === 'start') {
  7. year = year - 60;
  8. } else if (type === 'end') {
  9. year = year + 2;
  10. }
  11. month = month > 9 ? month : '0' + month;;
  12. day = day > 9 ? day : '0' + day;
  13. return `${year}-${month}-${day}`;
  14. }

这个方法复制过来
image.png

image.png

image.png

image.png

  1. // 修改生日
  2. bindDateChange(e){
  3. this.birthday=e.target.value;
  4. },

image.png

image.png

本节代码

user-set-userinfo.vue页面

  1. <template>
  2. <view class="body">
  3. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  4. <view>头像</view>
  5. <view class="u-f-ac" @tap="changeimg">
  6. <image :src="userpic" mode="aspectFill" lazy-load="true"></image>
  7. <view class="icon iconfont icon-bianji1"></view>
  8. </view>
  9. </view>
  10. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  11. <view>昵称</view>
  12. <view class="u-f-ac">
  13. <!-- <view>昵称</view> -->
  14. <input type="text" v-model="username" />
  15. <view class="icon iconfont icon-bianji1"></view>
  16. </view>
  17. </view>
  18. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  19. <view>性别</view>
  20. <view class="u-f-ac" @tap="changeOne('sex')">
  21. <view>{{sex}}</view>
  22. <view class="icon iconfont icon-bianji1"></view>
  23. </view>
  24. </view>
  25. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  26. <view>生日</view>
  27. <picker mode="date" :value="birthday" :sstart="startDate" :end="endDate" @change="bindDateChange">
  28. <view class="u-f-ac">
  29. <view>{{birthday}}</view>
  30. <view class="icon iconfont icon-bianji1"></view>
  31. </view>
  32. </picker>
  33. </view>
  34. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  35. <view>情感</view>
  36. <view class="u-f-ac" @tap="changeOne('qg')">
  37. <view>{{qg}}</view>
  38. <view class="icon iconfont icon-bianji1"></view>
  39. </view>
  40. </view>
  41. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  42. <view>职业</view>
  43. <view class="u-f-ac" @tap="changeOne('job')">
  44. <view>{{job}}</view>
  45. <view class="icon iconfont icon-bianji1"></view>
  46. </view>
  47. </view>
  48. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  49. <view>家乡</view>
  50. <view class="u-f-ac">
  51. <view>中国广州</view>
  52. <view class="icon iconfont icon-bianji1"></view>
  53. </view>
  54. </view>
  55. <button class="user-set-btn" :class="{'user-set-btn-disable':disabled}" :loading="loading" @tap="submit"
  56. type="primary" :disabled="disabled">完成</button>
  57. </view>
  58. </template>
  59. <script>
  60. import {
  61. pathToBase64
  62. } from '../../js_sdk/mmmm-image-tools/index.js'
  63. let sex = ['不限', '男', '女'];
  64. let qg = ['秘密', '未婚', '已婚'];
  65. let job = ['秘密', 'IT', '老师'];
  66. export default {
  67. data() {
  68. return {
  69. userpic: '../../static/demo/userpic/11.jpg',
  70. username: '哈哈哈',
  71. sex: "不限",
  72. qg: "未婚",
  73. job: "IT",
  74. birthday: '1987-02-07'
  75. }
  76. },
  77. computed: {
  78. startDate() {
  79. return this.getDate('start');
  80. },
  81. endDate() {
  82. return this.getDate('end');
  83. }
  84. },
  85. methods: {
  86. // 修改生日
  87. bindDateChange(e){
  88. this.birthday=e.target.value;
  89. },
  90. changeOne(val) {
  91. let arr = [];
  92. switch (val) {
  93. case 'sex':
  94. arr = sex;
  95. break;
  96. case 'qg':
  97. arr = qg;
  98. break;
  99. case 'job':
  100. arr = job;
  101. break;
  102. }
  103. uni.showActionSheet({
  104. itemList: arr,
  105. success: res => {
  106. switch (val) {
  107. case 'sex':
  108. this.sex = arr[res.tapIndex];
  109. break;
  110. case 'qg':
  111. this.qg = arr[res.tapIndex];
  112. break;
  113. case 'job':
  114. this.job = arr[res.tapIndex];
  115. break;
  116. }
  117. console.log(res);
  118. }
  119. })
  120. },
  121. // 修改头像
  122. changeimg() {
  123. uni.chooseImage({
  124. count: 1, //默认9
  125. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  126. // sourceType: ['album'], //从相册选择
  127. success: res => {
  128. console.log(JSON.stringify(res));
  129. // this.userpic=res.tempFilePaths;
  130. //解决跨越问题,让通过微信上传的图片你能够展示在浏览器前端
  131. uni.getImageInfo({
  132. src: res.tempFilePaths[0],
  133. success: (path) => {
  134. pathToBase64(path.path).then(base64 => {
  135. // console.log(base64); // 这就是转为base64格式的图片
  136. console.log('base64赋值');
  137. this.userpic = base64;
  138. // console.log(this.userpic);
  139. })
  140. .catch(error => {
  141. console.error(error)
  142. })
  143. }
  144. });
  145. }
  146. });;
  147. },
  148. submit() {
  149. },
  150. getDate(type) {
  151. const date = new Date();
  152. let year = date.getFullYear();
  153. let month = date.getMonth() + 1;
  154. let day = date.getDate();
  155. if (type === 'start') {
  156. year = year - 60;
  157. } else if (type === 'end') {
  158. year = year + 2;
  159. }
  160. month = month > 9 ? month : '0' + month;;
  161. day = day > 9 ? day : '0' + day;
  162. return `${year}-${month}-${day}`;
  163. }
  164. }
  165. }
  166. </script>
  167. <style>
  168. @import url('@/common/form.css');
  169. .user-set-userinfo-list {
  170. padding: 20upx;
  171. border-bottom: 1upx solid #F4F4F4;
  172. }
  173. /* 左侧的文字 */
  174. .user-set-userinfo-list>view:first-child {
  175. font-size: 32upx;
  176. font-weight: bold;
  177. color: #9B9B9B;
  178. }
  179. /* 右侧头像 */
  180. .user-set-userinfo-list>view:last-child>image {
  181. width: 80upx;
  182. height: 80upx;
  183. border-radius: 100%;
  184. }
  185. /* 右侧文本框 */
  186. .user-set-userinfo-list>view:last-child>input {
  187. text-align: right;
  188. }
  189. /* 最右侧的修改的铅笔图标 */
  190. .user-set-userinfo-list>view:last-child>view:last-of-type {
  191. margin-left: 20upx;
  192. color: #9B9B9B;
  193. }
  194. </style>

结束