index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <view class="page">
  3. <navBar title="个人资料" :back="true" color="black" background="white" />
  4. <view class="content">
  5. <view class="item" style="border-radius: 0rpx 0rpx 0rpx 24rpx;;">
  6. <view class="line" @click="chooseAvatar">
  7. <view class="left">
  8. 头像
  9. </view>
  10. <view class="right">
  11. <image :src="userInfo&&userInfo.head?userInfo.head:picUrl+'/static/other/defaultAvatar.png'"
  12. mode="" class="avatar"></image>
  13. <image src="/static/my/arr_r.png" mode="" class="arr"></image>
  14. </view>
  15. </view>
  16. <view class="line">
  17. <view class="left">
  18. 昵称
  19. </view>
  20. <view class="right"
  21. style="width: 80%;margin-left: 50rpx;display: flex;justify-content: space-between;">
  22. <input ref='nickInput' :focus="focus" type="text" v-model="userInfo.nickname"
  23. placeholder="请输入昵称" style="width: 90%;text-align: right;">
  24. <image @click="updateNickName" :src="picUrl+'/static/my/bj.png'" mode="" class=""
  25. style="width: 24rpx;height: 24rpx;margin-left: 8rpx;"></image>
  26. </view>
  27. </view>
  28. <view class="line" @click="showSex=true">
  29. <view class="left">
  30. 性别
  31. </view>
  32. <view class="right">
  33. <view class="" style="margin-right: 20rpx;">
  34. {{!userInfo.sex?'男':'女'}}
  35. </view>
  36. <image src="/static/my/arr_r.png" mode="" class="arr"></image>
  37. </view>
  38. </view>
  39. </view>
  40. <view class="item" style="">
  41. <view class="line">
  42. <view class="left">
  43. 真实姓名
  44. </view>
  45. <view class="right" style="width: 70%;">
  46. <input type="text" v-model="userInfo.name" placeholder="请输入真实姓名"
  47. style="width: 100%;text-align: right;">
  48. </view>
  49. </view>
  50. <view class="line" @click.stop="showDate=true">
  51. <view class="left">
  52. 生日
  53. </view>
  54. <view class="right">
  55. {{userInfo.birthday?userInfo.birthday:'请选择'}}
  56. </view>
  57. </view>
  58. <view class="line">
  59. <view class="left">
  60. 手机号
  61. </view>
  62. <view class="right" style="width: 70%;">
  63. <input type="text" v-model="userInfo.phone" placeholder="请输入手机号"
  64. style="width: 100%;text-align: right;">
  65. </view>
  66. </view>
  67. </view>
  68. <u-button text="修改" @click="update" color="#333"></u-button>
  69. <view class="" style="margin-top: 20rpx;">
  70. <u-button text="退出登录" @click="logoOut" color="#333"></u-button>
  71. </view>
  72. </view>
  73. <u-action-sheet :actions="list" :title="title" :show="showSex" cancelText="取消" @close="showSex=false"
  74. @select="handleSelSex"></u-action-sheet>
  75. <u-datetime-picker ref="datetimePicker" :formatter="formatter" @cancel="showDate = false" @confirm="confirmDate"
  76. :show="showDate" :minDate="new Date('1960-1-1').getTime()" :maxDate="Date.now()" v-model="value1"
  77. mode="date" confirmColor="#E77817"></u-datetime-picker>
  78. <!-- <u-loading-page :loading="loading" image="/static/index/logo.png" loading-text="页面加载中..."></u-loading-page> -->
  79. <privacy-popup @confirmP="confirmP" @cancleP="cancleP" :showPrivateBox="showPop"></privacy-popup>
  80. </view>
  81. </template>
  82. <script>
  83. import uploadUrl from '@/common/config.js'
  84. import moment from 'moment'
  85. import PrivacyPopup from "@/components/privacyPopup/index.vue";
  86. PrivacyPopup
  87. import {
  88. getUserInfoReq,
  89. updateUserInfoReq,
  90. } from '@/api/test/index.js'
  91. export default {
  92. components: {
  93. PrivacyPopup
  94. },
  95. data() {
  96. return {
  97. showPop: false,
  98. showDate: false,
  99. value1: Number(new Date()),
  100. picUrl: this.$picUrl,
  101. avatarUrl: '',
  102. showSex: false,
  103. nickName: '',
  104. sex: '男',
  105. list: [{
  106. name: '男'
  107. },
  108. {
  109. name: '女'
  110. }
  111. ],
  112. userInfo: null,
  113. loading: true,
  114. focus: false,
  115. }
  116. },
  117. async onLoad(o) {
  118. this.handlePrivate()
  119. let res = await getUserInfoReq()
  120. if (res.code == 0) {
  121. this.userInfo = res.data
  122. // this.userInfo = null
  123. this.loading = false
  124. } else {
  125. this.loading = false
  126. uni.showToast({
  127. title: res.message,
  128. icon: 'none'
  129. })
  130. }
  131. },
  132. methods: {
  133. cancleP() {
  134. this.showPop = false
  135. },
  136. handlePrivate() {
  137. let _this = this
  138. if (getApp().globalData.showPrivacy) {
  139. _this.showPop = true
  140. } else {
  141. _this.showPop = false
  142. }
  143. },
  144. // 用户同意隐私内容
  145. confirmP() {
  146. this.showPop = false
  147. let _this = this
  148. uni.chooseImage({
  149. count: 1,
  150. success(res) {
  151. console.log('res', res.tempFilePaths[0]);
  152. _this.upImg(res.tempFilePaths[0])
  153. }
  154. })
  155. },
  156. updateNickName() {
  157. console.log('昵称输入框的引用--------------', this.$refs);
  158. this.focus = true
  159. },
  160. logoOut() {
  161. uni.showModal({
  162. title: '提示',
  163. content: '确认退出?',
  164. confirmColor: '#F7790C',
  165. success: function(res) {
  166. if (res.confirm) {
  167. uni.removeStorageSync('token')
  168. uni.setStorageSync('switchs', [1, 0, 0])
  169. uni.reLaunch({
  170. url: '/pages/my'
  171. })
  172. } else if (res.cancel) {
  173. console.log('用户点击取消');
  174. }
  175. }
  176. });
  177. },
  178. async update() {
  179. let phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/
  180. if (this.userInfo.phone && !phoneReg.test(this.userInfo.phone)) {
  181. return this.$toast('请输入合法的手机号')
  182. }
  183. let p = {
  184. nickname: this.userInfo.nickname,
  185. head: this.userInfo.head,
  186. phone: this.userInfo.phone,
  187. sex: this.userInfo.sex,
  188. name: this.userInfo.name,
  189. birthday: this.userInfo.birthday
  190. }
  191. // p = {
  192. // nickname: null,
  193. // head: null,
  194. // phone: null,
  195. // sex: null,
  196. // name: null,
  197. // birthday: null
  198. // }
  199. let res = await updateUserInfoReq(p)
  200. if (res.code == 0) {
  201. uni.showToast({
  202. title: '修改成功',
  203. icon: 'none'
  204. })
  205. setTimeout(() => {
  206. uni.switchTab({
  207. url: '/pages/my'
  208. })
  209. }, 1500)
  210. } else {
  211. uni.showToast({
  212. title: res.message,
  213. icon: 'none'
  214. })
  215. }
  216. },
  217. confirmDate(e) {
  218. this.userInfo.birthday = moment(this.$refs.datetimePicker.innerValue).format("YYYY-MM-DD")
  219. this.showDate = false
  220. },
  221. handleSelSex(e) {
  222. if (e.name == '男') {
  223. this.userInfo.sex = 0
  224. } else {
  225. this.userInfo.sex = 1
  226. }
  227. this.showSex = false
  228. },
  229. chooseAvatar() {
  230. let _this = this
  231. if (getApp().globalData.showPrivacy) {
  232. _this.showPop = true;
  233. return;
  234. } else {
  235. uni.chooseImage({
  236. count: 1,
  237. success(res) {
  238. console.log('res', res.tempFilePaths[0]);
  239. _this.upImg(res.tempFilePaths[0])
  240. }
  241. })
  242. }
  243. },
  244. upImg(file) {
  245. console.log('upImg的file', file)
  246. let _this = this
  247. uni.uploadFile({
  248. url: uploadUrl.baseUrl + '/api/upload',
  249. filePath: file,
  250. name: 'file',
  251. header: {
  252. 'Content-Type': 'multipart/form-data'
  253. },
  254. formData: {
  255. 'fileType': 'images',
  256. 'dirName': 'cert',
  257. 'tag': 'lamp'
  258. },
  259. success: function(uploadRes) {
  260. let result = JSON.parse(uploadRes.data)
  261. if (result.code == 0) {
  262. _this.userInfo.head = result.data.file
  263. console.log('上传后的头像url地址', result.data.file)
  264. }
  265. },
  266. fail: function(err) {
  267. console.log('upload failed:', err)
  268. }
  269. })
  270. },
  271. }
  272. }
  273. </script>
  274. <style lang="scss" scoped>
  275. @import "./index.scss";
  276. </style>