LoginPop.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <u-popup :show="value" @close="close" round="18rpx">
  3. <form @submit="saveUserData">
  4. <view class="m-content-view">
  5. <text class="top-view"> {{shopName}} </text>
  6. <text class="tips">请您授权头像昵称信息,您授权的信息将受到严格保护,请放心使用。</text>
  7. <button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar"
  8. style="border: 0">
  9. <u--image v-if="avatarUrl !== defaultAvatarUrl" width="149rpx" height="149rpx" radius="100%"
  10. shape="circle" :src="avatarUrl" :fade="true" duration="450" mode="heightFix"></u--image>
  11. <view class="v1" v-else>
  12. <u--image width="80rpx" height="80rpx" radius="100%" shape="circle" :src="avatarUrl"
  13. :fade="true" duration="450" mode="heightFix"></u--image>
  14. <text v-if="avatarUrl === defaultAvatarUrl">获取头像</text>
  15. </view>
  16. </button>
  17. <input name="nickname" v-model="userName" type="nickname" class="m-input" placeholder="请输入昵称" />
  18. <button class="red-btn" form-type="submit">确认授权</button>
  19. </view>
  20. </form>
  21. </u-popup>
  22. </template>
  23. <script>
  24. import {
  25. uploadBase64File,
  26. getNewRemindCnt,
  27. addAddress,
  28. } from "@/common/api/common.js";
  29. import {
  30. updateUserInfo,
  31. getUserInfo,
  32. userBindNickname
  33. } from "@/common/api/user.js";
  34. const defaultAvatarUrl =
  35. "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0";
  36. export default {
  37. name: "LoginPop",
  38. props: {
  39. value: {
  40. type: Boolean,
  41. default: false
  42. }
  43. },
  44. data() {
  45. return {
  46. shopName: "六感熟眠商城",
  47. userName: "",
  48. userInfo: undefined,
  49. defaultAvatarUrl,
  50. avatarUrl: defaultAvatarUrl,
  51. };
  52. },
  53. mounted() {
  54. const configs = uni.getStorageSync("configs");
  55. if (configs) {
  56. this.shopName = configs.shop_name
  57. }
  58. },
  59. methods: {
  60. close() {
  61. this.$emit('input', false)
  62. },
  63. onChooseAvatar(e) {
  64. const {
  65. avatarUrl
  66. } = e.detail;
  67. console.log("成功授权", avatarUrl);
  68. this.avatarUrl = avatarUrl;
  69. },
  70. saveUserData(e) {
  71. console.log(e.detail.value.nickname);
  72. if (
  73. this.avatarUrl === this.defaultAvatarUrl ||
  74. !e.detail.value.nickname
  75. ) {
  76. uni.showToast({
  77. icon: "none",
  78. title: "请先授权完善信息",
  79. });
  80. return;
  81. }
  82. this.userName = e.detail.value.nickname;
  83. this.close()
  84. this.getImageInfo();
  85. },
  86. getImageInfo() {
  87. // 获取当前图片信息
  88. uni.getImageInfo({
  89. src: this.avatarUrl,
  90. success: (image) => {
  91. // 做png/jpeg的类型判断————对不同类型的图像添加不同的转换头信息
  92. if (image.type == "png" || image.type == "jpeg") {
  93. // 对符合类型的图片转换为base64类型
  94. uni.getFileSystemManager().readFile({
  95. // 【重点来啦】人家自提供的转码API
  96. filePath: image.path, // 所需转码图像路径
  97. encoding: "base64", // 转码类型
  98. success: (res) => {
  99. // 生成base64
  100. let imageBase64 =
  101. "data:image/" + image.type + ";base64," + res.data;
  102. console.log("转base64后:", imageBase64);
  103. this.uploadBase64File(imageBase64);
  104. },
  105. });
  106. } else {
  107. // 友好一点,不是以上类型做出提醒
  108. uni.showToast({
  109. title: "当前只支持png/jpeg格式",
  110. duration: 2500,
  111. icon: "none",
  112. });
  113. }
  114. },
  115. });
  116. },
  117. uploadBase64File(imageBase64) {
  118. uploadBase64File({
  119. base64: imageBase64,
  120. }, {
  121. "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
  122. })
  123. .then((res) => {
  124. console.log("uploadFile==>", res.data.url);
  125. this.avatarUrl = res.data.url;
  126. this.bindNickname(res.data.url);
  127. })
  128. .catch(() => {
  129. uni.hideLoading();
  130. });
  131. },
  132. bindNickname(url) {
  133. // this.isUserRegister = true;
  134. // this.showUserRegister = false;
  135. const data = {
  136. nickname: this.userName,
  137. username: this.userName,
  138. headimg: url,
  139. };
  140. userBindNickname(data).then((res) => {
  141. console.log("绑定昵称等==>", res.data);
  142. if (res.data) {
  143. // uni.setStorageSync("token", res.data.token.token);
  144. if (!res.data.headimg) {
  145. res.data.headimg = url;
  146. }
  147. uni.setStorageSync("userInfo", JSON.stringify(res.data));
  148. this.getUserInfo()
  149. }
  150. });
  151. },
  152. async getUserInfo() {
  153. const data = {};
  154. await getUserInfo(data)
  155. .then((res) => {
  156. this.close()
  157. if (res.data) {
  158. this.$emit('updateUserInfo')
  159. // uni.clearStorageSync()
  160. uni.setStorageSync("token", res.data.token.token);
  161. uni.setStorageSync("userInfo", JSON.stringify(res.data));
  162. this.userInfo = res.data;
  163. console.log("getUserInfo==>", this.userInfo);
  164. }
  165. })
  166. .catch(() => {
  167. uni.hideLoading();
  168. });
  169. },
  170. }
  171. }
  172. </script>
  173. <style lang="scss" scoped>
  174. .m-content-view {
  175. width: 100%;
  176. display: flex;
  177. align-items: center;
  178. flex-direction: column;
  179. background-color: #f7f7f7;
  180. border-radius: 24rpx 24rpx 0 0;
  181. }
  182. .top-view {
  183. font-size: 32rpx;
  184. font-family: PingFangSC-Medium, PingFang SC;
  185. font-weight: 500;
  186. color: #1d161f;
  187. line-height: 34rpx;
  188. text-align: left;
  189. margin: 55rpx 49rpx 0;
  190. width: calc(100% - 98rpx);
  191. }
  192. .tips {
  193. font-size: 26rpx;
  194. font-weight: 400;
  195. color: #858585;
  196. line-height: 34rpx;
  197. margin: 21rpx 49rpx 0;
  198. }
  199. .red-btn {
  200. color: white;
  201. background: #f39800;
  202. width: calc(100% - 98rpx);
  203. margin-top: 52rpx;
  204. margin-bottom: 52rpx;
  205. font-size: 32rpx;
  206. line-height: 84rpx;
  207. border-radius: 40rpx;
  208. text-align: center;
  209. }
  210. .red-btn:active {
  211. opacity: 0.7;
  212. }
  213. .v1 {
  214. height: 149rpx;
  215. width: 149rpx;
  216. display: flex;
  217. flex-direction: column;
  218. background: rgba(244, 244, 246, 1);
  219. border-radius: 100%;
  220. align-items: center;
  221. justify-content: center;
  222. }
  223. .avatar-wrapper {
  224. margin-top: 26rpx;
  225. }
  226. .avatar-wrapper button::before {
  227. padding: 0;
  228. border-radius: 20rpx;
  229. border: 0;
  230. }
  231. .avatar {
  232. height: 100rpx;
  233. width: 100rpx;
  234. }
  235. .v1 text {
  236. font-size: 22rpx;
  237. margin-top: -16rpx;
  238. }
  239. // 去掉烦人的边框
  240. button::after {
  241. border: 0; // 或者 border: none;
  242. }
  243. .m-input {
  244. width: calc(100% - 138rpx);
  245. line-height: 117rpx;
  246. padding: 0 20rpx;
  247. margin-top: 44rpx;
  248. background: #ffffff;
  249. border-radius: 15rpx;
  250. height: 117rpx;
  251. }
  252. </style>