wxAuth.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <view >
  3. <view class="content">
  4. <view class="info-item">
  5. <view class="item">
  6. <image class="head" :src="user.avatar==null?'https://fs-1319721001.cos.ap-chongqing.myqcloud.com/fs/20240229/22cb9518a55040dea74d8f730551a7a2.jpg':user.avatar" mode=""></image>
  7. <button class="wx-head" type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  8. </button>
  9. </view>
  10. </view>
  11. <view class="info-item">
  12. <view class="item">
  13. <input type="nickname" @blur="bindblur" @input="bindinput" placeholder="请点击授权昵称" v-model="nickName" class="input" />
  14. </view>
  15. </view>
  16. <view class="btn-box">
  17. <view class="btn" @click="submit()">保存</view>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import {getUserInfo,editUser} from '@/api/user.js'
  24. export default {
  25. data() {
  26. return {
  27. user:{
  28. nickName:"",
  29. avatar:null,
  30. user:null,
  31. }
  32. }
  33. },
  34. onLoad() {
  35. },
  36. methods: {
  37. bindblur(e) {
  38. this.nickName = e.detail.value; // 获取微信昵称
  39. },
  40. bindinput(e){
  41. this.nickName = e.detail.value; //这里要注意如果只用blur方法的话用户在输入玩昵称后直接点击保存按钮,会出现修改不成功的情况。
  42. },
  43. onChooseAvatar(e){
  44. let {
  45. avatarUrl
  46. } = e.detail;
  47. uni.uploadFile({
  48. url: uni.getStorageSync('requestPath')+'/app/common/uploadOSS', //仅为示例,非真实的接口地址
  49. filePath: avatarUrl,
  50. name: 'file',
  51. formData: {
  52. 'user': 'test' // 上传附带参数
  53. },
  54. success: (uploadFileRes) => {
  55. this.user.avatar =JSON.parse(uploadFileRes.data).url
  56. }
  57. });
  58. },
  59. // chooseImage() {
  60. // var that = this;
  61. // uni.chooseImage({
  62. // count: 1, // 默认9
  63. // sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  64. // sourceType: ['album', 'camera'], //从相册选择
  65. // success: (res) => {
  66. // uni.uploadFile({
  67. // url: uni.getStorageSync('requestPath')+'/api/common/uploadOSS', //仅为示例,非真实的接口地址
  68. // filePath: res.tempFilePaths[0],
  69. // name: 'file',
  70. // formData: {
  71. // 'user': 'test' // 上传附带参数
  72. // },
  73. // success: (uploadFileRes) => {
  74. // console.log(uploadFileRes)
  75. // this.user.avatar =JSON.parse(uploadFileRes.data).data.url
  76. // }
  77. // });
  78. // }
  79. // });
  80. // },
  81. submit(){
  82. if(this.nickName==null||this.nickName==""){
  83. uni.showToast({
  84. icon:'none',
  85. title: "请输入昵称",
  86. });
  87. return;
  88. }
  89. this.user.nickName=this.nickName;
  90. this.user.isWeixinAuth=1;
  91. editUser(this.user).then(
  92. res => {
  93. if(res.code==200){
  94. uni.setStorageSync('avatar',this.avatar);
  95. uni.setStorageSync('nickName',this.nickName);
  96. this.$emit('updateUser');
  97. }else{
  98. uni.showToast({
  99. icon:'none',
  100. title: "请求失败",
  101. });
  102. }
  103. },
  104. rej => {}
  105. );
  106. },
  107. getUserInfo(){
  108. getUserInfo().then(
  109. res => {
  110. if(res.code==200){
  111. if(res.user!=null){
  112. this.user=res.user;
  113. console.log(this.user)
  114. }
  115. else{
  116. this.utils.loginOut();
  117. }
  118. }else{
  119. uni.showToast({
  120. icon:'none',
  121. title: "请求失败",
  122. });
  123. }
  124. },
  125. rej => {}
  126. );
  127. },
  128. }
  129. }
  130. </script>
  131. <style lang="scss" scoped>
  132. .content{
  133. width: 100%;
  134. padding: 0 60upx;
  135. }
  136. .info-item{
  137. width: 100%;
  138. height: 104upx;
  139. background: #FFFFFF;
  140. display: flex;
  141. align-items: center;
  142. justify-content: center;
  143. margin: 15rpx 0rpx;
  144. .item{
  145. display: flex;
  146. align-items: center;
  147. justify-content: center;
  148. width: 100%;
  149. .input{
  150. width: 100%;
  151. height: 50rpx;
  152. padding: 15rpx 30rpx;
  153. border-radius: 30rpx;
  154. background-color: #F5F6FA;
  155. text-align: left;
  156. font-size: 30upx;
  157. font-family: PingFang SC;
  158. font-weight: 400;
  159. color: #0F1826;
  160. }
  161. .wx-head{
  162. position: absolute;
  163. width: 120upx;
  164. height: 120upx;
  165. opacity: 0;
  166. }
  167. }
  168. .head{
  169. border-radius: 50%;
  170. width: 120upx;
  171. height: 120upx;
  172. }
  173. .input{
  174. text-align: right;
  175. font-size: 30upx;
  176. font-family: PingFang SC;
  177. font-weight: 400;
  178. color: #0F1826;
  179. }
  180. }
  181. .btn-box{
  182. height: 140upx;
  183. width: 100%;
  184. display: flex;
  185. align-items: center;
  186. justify-content: center;
  187. .btn{
  188. width: 100%;
  189. height: 80upx;
  190. line-height: 80upx;
  191. text-align: center;
  192. font-size: 34upx;
  193. font-family: PingFang SC;
  194. font-weight: 400;
  195. color: #FFFFFF;
  196. background: #C39A58;
  197. border-radius: 60upx;
  198. }
  199. }
  200. </style>