set.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <view class="container">
  3. <view class="body">
  4. <!-- 个人头像区域 -->
  5. <view class="body_box">
  6. 个人头像
  7. <view class="box_right" @click="handleClickPhoto">
  8. <img mode="aspectFill" class="img" :src="imgUrl || '../../static/my/portrait.png'" />
  9. <img class="img2" src="../../static/my/right.png" />
  10. </view>
  11. </view>
  12. <!-- 账号名区域 -->
  13. <view class="body_box" @click="handleClickName">
  14. 账号名
  15. <view class="box_right">
  16. <view class="msg">{{ name }}</view>
  17. <img class="img2" src="../../static/my/right.png" />
  18. </view>
  19. </view>
  20. <!-- ID区域 -->
  21. <view class="body_box">
  22. ID
  23. <view class="box_right">
  24. <view class="msg">{{ id }}</view>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. data() {
  33. return {
  34. // 头像
  35. imgUrl: '',
  36. // 账号名
  37. name: '',
  38. //用户id
  39. id: ''
  40. }
  41. },
  42. onLoad() {
  43. this.getUserInfo()
  44. },
  45. methods: {
  46. getUserInfo() {
  47. let userInfo = uni.getStorageSync('userInfo')
  48. this.name = userInfo.user_name
  49. this.imgUrl = userInfo.headPhoto
  50. this.id = userInfo.id
  51. },
  52. // 点击头像图片回调
  53. handleClickPhoto() {
  54. uni.showActionSheet({
  55. itemList: ['更换头像'],
  56. success: (res) => {
  57. if (res.tapIndex === 0) {
  58. this.changeImg()
  59. }
  60. }
  61. })
  62. },
  63. // 更换头像回调
  64. changeImg() {
  65. uni.chooseImage({
  66. count: 1,
  67. sizeType: ['compressed'],
  68. sourceType: ['album'], //从相册选择
  69. success: (res) => {
  70. uni.showLoading({
  71. title: '上传中'
  72. })
  73. uni.uploadFile({
  74. url: `https://www.jinganrenjiams.com/hotelReservation/file/cos/upload`,
  75. filePath: res.tempFilePaths[0],
  76. name: 'files',
  77. success: async (uploadFileRes) => {
  78. const res = JSON.parse(uploadFileRes.data)
  79. if (res.code === 200 || res.code === 1) {
  80. uni.hideLoading()
  81. this.imgUrl = res.data
  82. const result = await this.$myRequest({
  83. url: '/mhotel/ampupdateUserInfo.action',
  84. data: {
  85. userId: this.id,
  86. userName: this.name,
  87. headPhoto: this.imgUrl
  88. }
  89. })
  90. if (result.code === 200) {
  91. uni.showToast({
  92. title: '头像修改成功',
  93. icon: 'success',
  94. mask: true
  95. })
  96. setTimeout(() => {
  97. this.getUser()
  98. }, 1500)
  99. }
  100. }
  101. },
  102. fail: () => {
  103. uni.showToast({
  104. title: '上传失败',
  105. icon: 'error'
  106. })
  107. }
  108. })
  109. }
  110. })
  111. },
  112. // 点击账号名回调
  113. handleClickName() {
  114. uni.showActionSheet({
  115. itemList: ['修改账号名'],
  116. success: (res) => {
  117. if (res.tapIndex === 0) {
  118. this.changeName()
  119. }
  120. }
  121. })
  122. },
  123. // 修改账号名回调
  124. changeName() {
  125. uni.showModal({
  126. title: '请输入账号名',
  127. editable: true,
  128. success: async (res) => {
  129. if (res.confirm) {
  130. if (!res.content) {
  131. uni.showToast({
  132. title: '账号名不能为空',
  133. icon: 'none'
  134. })
  135. setTimeout(() => {
  136. this.changeName()
  137. }, 1500)
  138. } else if (res.content.length > 8) {
  139. uni.showToast({
  140. title: '账号名最多8位',
  141. icon: 'none'
  142. })
  143. setTimeout(() => {
  144. this.changeName()
  145. }, 1500)
  146. } else {
  147. const result = await this.$myRequest({
  148. url: '/mhotel/ampupdateUserInfo.action',
  149. data: {
  150. userId: this.id,
  151. userName: res.content,
  152. headPhoto: this.imgUrl
  153. }
  154. })
  155. if (result.code === 200) {
  156. uni.showToast({
  157. title: '账号修改成功',
  158. icon: 'success',
  159. mask: true
  160. })
  161. setTimeout(() => {
  162. this.getUser()
  163. }, 1500)
  164. }
  165. }
  166. }
  167. }
  168. })
  169. },
  170. //获取个人信息
  171. async getUser() {
  172. const res = await this.$myRequest({
  173. url: '/mhotel/ampqueryUsersById.action',
  174. data: {
  175. userId: uni.getStorageSync('userInfo').id
  176. }
  177. })
  178. if (res.code === 200) {
  179. this.name = res.data.user_name
  180. this.imgUrl = res.data.headPhoto
  181. uni.setStorageSync('userInfo', res.data)
  182. }
  183. }
  184. }
  185. }
  186. </script>
  187. <style lang="scss" scoped>
  188. .container {
  189. display: flex;
  190. flex-direction: column;
  191. min-height: 100vh;
  192. background-color: #ebeced;
  193. .body {
  194. box-sizing: border-box;
  195. margin-top: 20rpx;
  196. padding: 0 30rpx;
  197. height: calc(100vh - 20rpx);
  198. background-color: #fff;
  199. .body_box {
  200. display: flex;
  201. justify-content: space-between;
  202. align-items: center;
  203. height: 100rpx;
  204. font-size: 28rpx;
  205. border-bottom: 1rpx solid #e6e6e6;
  206. .box_right {
  207. display: flex;
  208. justify-content: flex-end;
  209. align-items: center;
  210. height: 100rpx;
  211. .img {
  212. width: 64rpx;
  213. height: 64rpx;
  214. border-radius: 50%;
  215. }
  216. .img2 {
  217. margin-left: 7rpx;
  218. width: 47rpx;
  219. height: 47rpx;
  220. }
  221. .msg {
  222. color: #808080;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. </style>