index.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <view class="app-page">
  3. <!-- <view class="card-box" style="margin-top: 20rpx;" @click="show = true">
  4. <text class="left-title">用户身份</text>
  5. <view class="h-view">
  6. <text>{{selectItem.label || '请选择'}}</text>
  7. <u-icon name="arrow-right" color="#AEAEAE" size="15rpx" customStyle="padding: 12rpx;margin-right:8rpx">
  8. </u-icon>
  9. </view>
  10. </view> -->
  11. <button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar"
  12. style="border: none;background: transparent;margin: 0;padding: 0;">
  13. <view class="card-box">
  14. <text class="left-title">头像</text>
  15. <view class="h-view">
  16. <u--image v-if="headimg" width='50rpx' height='50rpx' radius="100%" shape='circle'
  17. customStyle="margin: 0" :src="headimg" :fade="true" duration="450" mode='heightFix'></u--image>
  18. <view class="v1" v-else>
  19. <u--image width='50rpx' height='50rpx' radius="100%" shape='circle'
  20. :src="`${$getImgBaseUrl()}nologin.png`" :fade="true" duration="450" mode='heightFix'>
  21. </u--image>
  22. </view>
  23. <u-icon name="arrow-right" color="#AEAEAE" size="15rpx"
  24. customStyle="padding: 12rpx;margin-right:8rpx">
  25. </u-icon>
  26. </view>
  27. </view>
  28. </button>
  29. <view class="card-box">
  30. <text class="left-title">姓名</text>
  31. <view class="h-view">
  32. <u-input v-model="username" placeholder="请输入" type="nickname" border="none" :auto-height="true"
  33. clearable customStyle="margin:0 20rpx;text-align: right;" />
  34. </view>
  35. </view>
  36. <view class="card-box">
  37. <text class="left-title">手机号</text>
  38. <view class="h-view">
  39. <u-input v-model="phone" placeholder="请输入" type="number" border="none" :auto-height="true" clearable
  40. :maxlength="11" customStyle="margin:0 20rpx;text-align: right;" />
  41. </view>
  42. </view>
  43. <!-- <view class="card-box">
  44. <text class="left-title">微信号</text>
  45. <view class="h-view">
  46. <u-input v-model="wechat" placeholder="请输入" type="text" border="none" :auto-height="true" clearable
  47. customStyle="margin:0 20rpx;text-align: right;" />
  48. </view>
  49. </view> -->
  50. <!-- <view class="card-box" v-if="selectItem.value && (selectItem.value == '2' || selectItem.value == '3')">
  51. <text class="left-title">单位名称</text>
  52. <view class="h-view">
  53. <u-input v-model="corp_name" placeholder="请输入" type="text" border="none" :auto-height="true" clearable
  54. customStyle="margin:0 20rpx;text-align: right;" />
  55. </view>
  56. </view> -->
  57. <view class="bottom-view">
  58. </view>
  59. <view class="bottom-menu-box">
  60. <view class="common-btn" @click="commit">提交</view>
  61. </view>
  62. </view>
  63. </template>
  64. <script>
  65. import {
  66. uploadBase64File,
  67. } from "@/common/api/common.js";
  68. import {
  69. updateUserInfo,
  70. userRegister,
  71. getUserInfo,
  72. userBindPhone,
  73. userBindNickname
  74. } from "@/common/api/user.js";
  75. export default {
  76. data() {
  77. return {
  78. show: false,
  79. isNeedUpLoad: false,
  80. userInfo: {},
  81. selectItem: {},
  82. username: undefined,
  83. headimg: undefined,
  84. avatarUrl: undefined,
  85. phone: undefined,
  86. wechat: undefined,
  87. corp_name: undefined,
  88. list: [{
  89. value: '1',
  90. label: '网红达人'
  91. },
  92. {
  93. value: '2',
  94. label: '品牌商家'
  95. },
  96. {
  97. value: '3',
  98. label: '经纪机构'
  99. }
  100. ],
  101. }
  102. },
  103. computed: {
  104. formData() {
  105. return {
  106. username: this.username,
  107. headimg: this.avatarUrl,
  108. phone: this.phone,
  109. // wechat: this.wechat,
  110. // corp_name: this.corp_name,
  111. }
  112. }
  113. },
  114. onLoad(options) {},
  115. onShow() {
  116. this.reUserInfo();
  117. this.getUserInfo();
  118. },
  119. methods: {
  120. commit() {
  121. if (this.isNeedUpLoad) {
  122. this.getImageInfo()
  123. return
  124. }
  125. userBindPhone({phone: this.phone}).then(res => {
  126. uni.setStorageSync('token', res.data.token.token)
  127. uni.setStorageSync('userInfo', JSON.stringify(res.data))
  128. uni.$u.toast('提交成功')
  129. setTimeout(() => {
  130. uni.navigateBack()
  131. }, 800);
  132. })
  133. },
  134. reUserInfo() {
  135. const userInfo = uni.getStorageSync('userInfo');
  136. if (userInfo && JSON.parse(userInfo).nickname != '' && JSON.parse(userInfo).headimg != '') {
  137. this.userInfo = JSON.parse(userInfo);
  138. this.avatarUrl = this.userInfo.headimg
  139. this.headimg = this.userInfo.headimg
  140. this.username = this.userInfo.username
  141. this.phone = this.userInfo.phone
  142. this.wechat = this.userInfo.wechat
  143. this.corp_name = this.userInfo.corp_name
  144. if (this.userInfo.vip_code && this.userInfo.vip_code != '0') {
  145. this.selectItem = this.list[this.userInfo.vip_code - 1]
  146. }
  147. }
  148. console.log("reUserInfo==>", this.userInfo);
  149. },
  150. async getUserInfo() {
  151. const data = {}
  152. await getUserInfo(data)
  153. .then(res => {
  154. if (res.data) {
  155. uni.setStorageSync('token', res.data.token.token)
  156. uni.setStorageSync('userInfo', JSON.stringify(res.data))
  157. this.userInfo = res.data;
  158. console.log("getUserInfo==>", this.userInfo)
  159. }
  160. })
  161. .catch(() => {
  162. uni.hideLoading();
  163. });
  164. await this.reUserInfo();
  165. },
  166. onChooseAvatar(e) {
  167. const {
  168. avatarUrl
  169. } = e.detail
  170. console.log('成功授权', avatarUrl)
  171. this.headimg = avatarUrl
  172. this.isNeedUpLoad = true;
  173. },
  174. getImageInfo() {
  175. // 获取当前图片信息
  176. uni.getImageInfo({
  177. src: this.headimg,
  178. success: (image) => {
  179. // 做png/jpeg的类型判断————对不同类型的图像添加不同的转换头信息
  180. if (image.type == 'png' || image.type == 'jpeg') {
  181. // 对符合类型的图片转换为base64类型
  182. uni.getFileSystemManager().readFile({ // 【重点来啦】人家自提供的转码API
  183. filePath: image.path, // 所需转码图像路径
  184. encoding: "base64", // 转码类型
  185. success: (res) => {
  186. // 生成base64
  187. let imageBase64 = 'data:image/' + image.type + ';base64,' + res
  188. .data;
  189. console.log('转base64后:', imageBase64);
  190. this.uploadBase64File(imageBase64)
  191. }
  192. })
  193. } else { // 友好一点,不是以上类型做出提醒
  194. uni.showToast({
  195. title: '当前只支持png/jpeg格式',
  196. duration: 2500,
  197. icon: 'none'
  198. })
  199. }
  200. }
  201. });
  202. },
  203. uploadBase64File(imageBase64) {
  204. uploadBase64File({
  205. base64: imageBase64
  206. }, {
  207. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  208. })
  209. .then((res) => {
  210. console.log("uploadFile==>", res.data.url)
  211. this.avatarUrl = res.data.url
  212. this.headimg = res.data.url
  213. this.bindNickname();
  214. })
  215. .catch(() => {
  216. uni.hideLoading();
  217. });
  218. },
  219. bindNickname(url) {
  220. this.isNeedUpLoad = false;
  221. const data = {
  222. nickname: this.userName,
  223. username: this.userName,
  224. headimg: url,
  225. };
  226. userBindNickname(data).then((res) => {
  227. console.log("绑定昵称等==>", res.data);
  228. if (res.data) {
  229. if(res.data.token) {
  230. uni.setStorageSync('token', res.data.token.token)
  231. }
  232. uni.setStorageSync('userInfo', JSON.stringify(res.data))
  233. // uni.$u.toast('注册成功')
  234. // setTimeout(() => {
  235. // uni.navigateBack()
  236. // }, 800);
  237. }
  238. });
  239. }
  240. }
  241. }
  242. </script>
  243. <style lang="scss" scoped>
  244. .app-page {
  245. width: 100%;
  246. display: flex;
  247. align-items: center;
  248. flex-direction: column;
  249. padding: 40rpx 0 0;
  250. }
  251. .card-box {
  252. width: 710rpx;
  253. background: #FFFFFF;
  254. border-radius: 15rpx 15rpx 15rpx 15rpx;
  255. margin: 0 auto 20rpx auto;
  256. display: flex;
  257. padding: 20rpx 0;
  258. flex-direction: row;
  259. align-items: center;
  260. justify-content: space-between;
  261. }
  262. .h-view {
  263. flex: 1;
  264. display: flex;
  265. flex-direction: row;
  266. justify-content: flex-end;
  267. align-items: center;
  268. }
  269. .left-title {
  270. font-size: 28rpx;
  271. }
  272. button::after {
  273. border: none;
  274. }
  275. .avatar-wrapper button::before {
  276. padding: 0;
  277. margin: 0;
  278. border-radius: 20rpx;
  279. background: transparent;
  280. border: none;
  281. line-height: 1;
  282. }
  283. .avatar-wrapper button::after {
  284. padding: 0;
  285. margin: 0;
  286. border-radius: 20rpx;
  287. background: transparent;
  288. border: none;
  289. line-height: 1;
  290. }
  291. .bottom-view {
  292. margin-bottom: var(--window-bottom);
  293. padding-bottom: calc(150rpx);
  294. }
  295. .bottom-menu-box {
  296. height: calc(150rpx);
  297. display: flex;
  298. flex-direction: row;
  299. justify-content: center;
  300. align-items: flex-start;
  301. position: fixed;
  302. z-index: 9;
  303. background: white;
  304. bottom: var(--window-bottom);
  305. left: 0;
  306. right: 0;
  307. width: 100vw;
  308. padding: 0 20rpx;
  309. box-sizing: border-box;
  310. .common-btn {
  311. width: 100%;
  312. }
  313. button::after {
  314. height: 78rpx;
  315. line-height: 78rpx;
  316. }
  317. .primary-btn:active {
  318. opacity: 0.7;
  319. }
  320. }
  321. </style>