setting.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <view class="page">
  3. <!-- 表单 -->
  4. <view class="form">
  5. <u-form label-position="left" :model="form" ref="form" label-width="180rpx">
  6. <u-form-item label="头像">
  7. <!-- @click="uploadAvatar" -->
  8. <view class="value avatarbox">
  9. <!-- <u-avatar style="height: 72rpx;" size="72" src="../../../static/user/1.png"></u-avatar> -->
  10. <button open-type="chooseAvatar" class="avatarBtn" @chooseavatar="onChooseavatar" >
  11. <u-avatar style="height: 72rpx;" size="72" :src="userInfo.avatar"></u-avatar>
  12. <!-- <u-icon :color="arrowColor" style="margin-left: 20rpx;" size="28" name="arrow-right"></u-icon> -->
  13. </button>
  14. </view>
  15. </u-form-item>
  16. <u-form-item label="昵称">
  17. <view class="value" @click="openUsernameModal">
  18. <text>{{ userInfo.nickname || '无' }}</text>
  19. <u-icon :color="arrowColor" style="margin-left: 20rpx;" size="28" name="arrow-right"></u-icon>
  20. </view>
  21. </u-form-item>
  22. <u-form-item label="收货地址">
  23. <view class="value" @click="$u.route({ url: '/pages-mine/pages/address/list' })">
  24. <text>{{ '设置收货地址' }}</text>
  25. <u-icon :color="arrowColor" style="margin-left: 20rpx;" size="28" name="arrow-right"></u-icon>
  26. </view>
  27. </u-form-item>
  28. </u-form>
  29. </view>
  30. <!-- 按钮 -->
  31. <view class="btn">
  32. <u-button type="primary" shape="circle" @click="logout"><text>退出登录</text></u-button>
  33. </view>
  34. <!-- modal -->
  35. <u-modal ref="showUsernameRef" v-model="showUsername" title="昵称修改" show-cancel-button :confirm-color="themeColor" :async-close="true" @confirm="changeNickname">
  36. <view class="slot-content">
  37. <u-field label-width="60" placeholder="请输入昵称" clearable :border-bottom="false" v-model="edit.nickname" label="昵称" required></u-field>
  38. </view>
  39. </u-modal>
  40. </view>
  41. </template>
  42. <script>
  43. import { hex_sha1 } from '@/utils/sha1.js'
  44. import { objKeySort,naviBackEmit } from '@/utils/tools.js'
  45. export default {
  46. data() {
  47. return {
  48. arrowColor: this.$appTheme.appThemeTextGrayColor,
  49. themeColor: this.$appTheme.appThemeColor,
  50. // 用户信息
  51. userInfo:{
  52. nickname: '',
  53. avatar: ''
  54. },
  55. edit:{
  56. nickname: '',
  57. avatar: ''
  58. },
  59. updateData:true,
  60. secret:'yrb1vdc2qc',
  61. // modal
  62. showUsername: false,
  63. // 默认回显
  64. username: '',
  65. signature: '',
  66. // 表单
  67. form: {},
  68. // 上传地址
  69. uploadUrl: ''
  70. };
  71. },
  72. onLoad(options) {
  73. this.getUserInfo();
  74. },
  75. methods: {
  76. getUserInfo(){
  77. this.$u.api.getUserBaseInfoAjax().then(({code,data}) => {
  78. console.log(data);
  79. if(code==1){
  80. this.userInfo = data;
  81. this.edit = {...data};
  82. }
  83. });
  84. },
  85. // 上传头像
  86. uploadAvatar() {
  87. uni.chooseImage({
  88. count: 1,
  89. sizeType: ['original', 'compressed'],
  90. sourceType: ['album', 'camera'],
  91. success(res) {
  92. console.log(JSON.stringify(res.tempFilePaths));
  93. }
  94. });
  95. },
  96. // 修改昵称
  97. openUsernameModal() {
  98. this.showUsername = true;
  99. },
  100. // 设置微信头像
  101. onChooseavatar(e) {
  102. console.log(e);
  103. let that = this;
  104. this.edit.avatar = e.detail.avatarUrl;
  105. let formData = {
  106. target: "oss",
  107. type: 'image',
  108. }
  109. let sign = hex_sha1(that.secret + objKeySort(formData));
  110. console.log(sign);
  111. uni.showLoading({
  112. title: '头像上传中'
  113. })
  114. uni.uploadFile({
  115. url: this.$env.apiUrl + "/api/client/index/upload",
  116. filePath: e.detail.avatarUrl,
  117. name: 'file',
  118. formData:{
  119. ...formData,
  120. file:e.detail.avatarUrl,
  121. },
  122. header: {
  123. 'Content-Type': 'multipart/form-data',
  124. sign,
  125. },
  126. success(res) {
  127. let response = JSON.parse(res.data);
  128. if (response.code == 1) {
  129. that.edit.avatar = response.data.full_url;
  130. // uni.$u.toast('上传成功!');
  131. that.submit();
  132. }else{
  133. uni.$u.toast('上传失败,请稍后重试')
  134. }
  135. },
  136. fail(error) {
  137. uni.$u.toast('上传失败,请稍后重试')
  138. console.log(' 上传文件失败', error)
  139. },
  140. })
  141. },
  142. // 退出登录
  143. logout() {
  144. uni.showModal({
  145. title: '提示',
  146. content: '是否退出当前账号?',
  147. confirmColor: this.themeColor,
  148. success(res) {
  149. if (res.confirm) {
  150. uni.removeStorageSync('token');
  151. uni.navigateBack({
  152. delta: 1
  153. });
  154. }
  155. }
  156. });
  157. },
  158. changeNickname(){
  159. if (!this.edit.nickname) {
  160. this.$refs.showUsernameRef.clearLoading();
  161. uni.$u.toast('请输入昵称');
  162. return false;
  163. }
  164. this.submit();
  165. },
  166. submit() {
  167. this.$nextTick(() => {
  168. // if (!this.edit.avatar && !this.edit.nickname) {
  169. // return uni.$u.toast('请设置头像和昵称')
  170. // }
  171. let param = {...this.edit}
  172. this.$u.api.setUserBaseInfoAjax(param).then(({code,data}) => {
  173. if (code == 1) {
  174. uni.$u.toast('设置成功');
  175. this.showUsername = false;
  176. this.userInfo = {...param};
  177. setTimeout(() => {
  178. // naviBackEmit('getUserInfo')
  179. uni.$emit("getUserInfo");
  180. }, 1000)
  181. }
  182. })
  183. })
  184. },
  185. }
  186. };
  187. </script>
  188. <style lang="scss" scoped>
  189. .page {
  190. padding: 24rpx 30rpx;
  191. background-color: $app-theme-bg-color;
  192. .form {
  193. .value {
  194. display: flex;
  195. align-items: center;
  196. justify-content: flex-end;
  197. font-size: 28rpx;
  198. font-family: PingFangSC-Regular, PingFang SC;
  199. font-weight: 400;
  200. color: $app-theme-card-gray-deep-color;
  201. }
  202. }
  203. }
  204. .btn {
  205. padding: 60rpx 0rpx;
  206. }
  207. .slot-content {
  208. padding: 30rpx;
  209. }
  210. .avatarbox{
  211. }
  212. .avatarBtn{
  213. border:none;
  214. background-color: transparent;
  215. display: inline-flex;
  216. align-items: center;
  217. padding: 0;
  218. margin: 0;
  219. width:75rpx;
  220. height:75rpx;
  221. border-radius: 50%;
  222. }
  223. </style>