set.vue 4.9 KB

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