1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
| <template> <up-navbar leftIconColor="#f5f5f5" :placeholder="true" :safeAreaInsetTop="true" title="获取用户头像昵称" :autoBack="true"> </up-navbar> <up-toast ref="uToastRef"></up-toast> <view class="containar"> <view class="index"> <view class="avatarUrl"> <button open-type="chooseAvatar" @chooseavatar="onChooseavatar"> <image :src="avatarUrl" class="refreshIcon"></image> </button> </view> <view class="userName"> <text>昵称:</text> <input :clearable="false" type="nickname" class="weui-input" :value="userName" @blur="bindblur" placeholder="请选择或输入昵称" @input="bindinput" /> </view> <view class="userPhone"> <text>手机号:</text> <up-button class="phone" type="success" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取手机号</up-button> </view> </view>
<view style="width: 100%;height: 1px; background: #EEE;">
</view> <view class="tips_1" style="width: 700rpx; height: 20px; font-size: 13px; margin: auto; margin-top: 40rpx;"> · 申请获取以下权限 </view> <view class="tips"> · 获得你的信息(昵称、头像、手机号等) </view>
<view> <br> <up-button @click="onSubmit" type="primary">保存</up-button> </view> </view> </template>
<script lang="ts" setup> import { ResponseCode } from "@/types"; import { getUserPhone } from "../alova/service" import { BASE_URL } from '@/config';
const avatarUrl = ref<any>() const userName = ref<string>('') const avatarPath = ref<string>('') const userPhone = ref<string>('') const uToastRef = ref(null) const list = ref([ { type: 'success', position: 'top', icon: false, duration: 3000, message: "小程序检测到没有您的用户信息,请依次点击下方头像和昵称以获取!" } ])
onReady(() => { showToast(list.value[0]) })
// 方法 const showToast = (params: any) => { uToastRef.value.show({ ...params, complete() { params.url && uni.navigateTo({ url: params.url }); } }); }
const getPhoneNumber = (e: any) => {
console.log("微信获取电话号码的 code: ", e.detail.code);
uni.login({ provider: 'weixin', success: (_: any) => { // 调用后端接口获取用户手机号码 getUserPhone({ code: e.detail.code }).then((res: any) => { if (res.data.code === ResponseCode.Success) { const data = JSON.parse(res.data.data) // console.log(data); userPhone.value = data.phone_info.phoneNumber; } }) }, fail: ((err: any) => { uni.showModal({ title: '错误', content: '获取用户手机号失败, err: ' + err, success(res) { if (res.confirm) { } else if (res.cancel) { } } }) }) });
// 手动赋值,dev 跳过获取手机号 // userPhone.value='18198086793'
}
const bindblur = (e: any) => { userName.value = e.detail.value; }
const bindinput = (e: any) => { userName.value = e.detail.value; }
const onChooseavatar = (e: any) => {
avatarUrl.value = e.detail.avatarUrl }
/** * 上传头像,返回头像的路径信息 */ const onSubmit = () => {
if (userName.value == '' || userPhone.value == '' || avatarUrl.value == '') { uni.showModal({ title: '个人信息未设置', content: '请点击相应选项以设置', }) return }
// 在微信小程序开发工具和真机调试中,不会出现问题。如果在体验版中,需要在小程序后台设置上传文件域名,不然报错! // 在提交中,将用户的昵称和头像信息提交到数据库存储 // 先上传图片获得 图片路径 uni.uploadFile({ url: BASE_URL + 'upload/avatar', filePath: avatarUrl.value, name: 'file', success: function (res) { let resp = JSON.parse(res.data) avatarPath.value = resp.data
// 保存用户信息 save() }, fail: function (err) { uni.showModal({ title: '上传头像文件失败', content: err.errMsg, }) console.log(err) } }) }
const save = () => {
const data = { nickName: userName.value, avatar: avatarPath.value, phone: userPhone.value, }
// 暂时存储到 storage 中,在 login 中使用 uni.removeStorageSync("userData") uni.setStorageSync("userData", data)
// 跳转至登录页 uni.redirectTo({ url: "login" }) } </script>
<style lang="scss" scoped> .containar { width: 100vw; height: 100vh; background: #fff; box-sizing: border-box; padding: 0 30rpx;
.index { display: flex; align-items: center; flex-direction: column; }
.avatarUrl { padding: 80rpx 0 40rpx; background: #fff;
button { background: #fff; line-height: 80rpx; height: auto; border: none !important; width: auto; // padding: 20rpx 30rpx; margin: 0; display: flex; border: none; justify-content: center; align-items: center;
&::after { border: none; }
.refreshIcon { width: 160rpx; height: 160rpx; border-radius: 50%; background-color: #ccc; }
.jt { width: 14rpx; height: 28rpx; } } }
.userName { background: #fff; padding: 20rpx 30rpx 80rpx; display: flex; align-items: center; justify-content: center;
// set font color color: black;
.weui-input { width: 110px; padding-left: 60rpx; } }
.userPhone { display: flex; width: 233px; align-items: center; margin-bottom: 10px;
// 解决如果用户手机设置了黑暗模式,字不显示的问题 color: black; } }
.tips { width: 700rpx; height: 20px; font-size: 13px; margin: 5px 0 15px 19px; color: #cbcbcb; }
.tips_1 { color: black; }
::v-deep .u-button { border-radius: 10px !important; width: 160px !important; } </style>
|