index.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <template>
  2. <view>
  3. <view class="product-window" :class="{'on':isShow}">
  4. <view class="iconfont icon-guanbi" @click="closeAttr"></view>
  5. <view class="trip-msg">
  6. <view class="title">
  7. 获取您的昵称、头像
  8. </view>
  9. <view class="trip">
  10. 提供具有辨识度的用户中心界面
  11. </view>
  12. </view>
  13. <form @submit="formSubmit">
  14. <view class="edit">
  15. <view class="avatar edit-box">
  16. <view class="left">
  17. <view class="head">头像</view>
  18. <!-- <image :src="userInfo.avatar || defaultAvatar" mode=""></image> -->
  19. <button class="avatar-box" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  20. <image :src="userInfo.avatar || defHead"></image>
  21. </button>
  22. </view>
  23. <!-- <view class="iconfont icon-xiangyou"></view> -->
  24. </view>
  25. <view class="nickname edit-box">
  26. <view class="left">
  27. <view class="head">昵称</view>
  28. <view class='input'><input type='nickname' :always-embed="true" :adjust-position="true"
  29. cursor-spacing="30" placeholder-class="pl-sty" placeholder="请输入昵称" name='nickname'
  30. :maxlength="16" :value='userInfo.nickname'></input>
  31. </view>
  32. </view>
  33. <!-- <view class="iconfont icon-xiangyou"></view> -->
  34. </view>
  35. </view>
  36. <view class="bottom">
  37. <button class="save" formType="submit" :class="{'open': userInfo.avatar}">
  38. 保存
  39. </button>
  40. </view>
  41. </form>
  42. </view>
  43. <canvas canvas-id="canvas" v-if="canvasStatus"
  44. :style="{width: canvasWidth + 'px', height: canvasHeight + 'px',position: 'absolute',left:'-100000px',top:'-100000px'}"></canvas>
  45. <view class="mask" @touchmove.prevent v-if="isShow" @click="closeAttr"></view>
  46. </view>
  47. </uni-popup>
  48. </template>
  49. <script>
  50. import store from '@/store/index.js'
  51. import {
  52. upload
  53. } from '@/api/order.js';
  54. import {
  55. userEdit,
  56. } from '@/api/set.js';
  57. export default {
  58. props: {
  59. isShow: {
  60. type: Boolean,
  61. value: false
  62. }
  63. },
  64. data() {
  65. return {
  66. defHead: require('@/static/error/missing-face.png'),
  67. userInfo: {
  68. avatar: '',
  69. nickname: '',
  70. },
  71. canvasStatus: false,
  72. };
  73. },
  74. methods: {
  75. /**
  76. * 上传文件
  77. *
  78. */
  79. uploadpic: function() {
  80. let that = this;
  81. this.canvasStatus = true
  82. upload().then((res) => {
  83. let userInfo = that.userInfo;
  84. if (userInfo !== undefined) {
  85. that.userInfo.avatar = res.data.url;
  86. }
  87. this.canvasStatus = false
  88. }, (res) => {
  89. this.canvasStatus = false
  90. }, (res) => {
  91. this.canvasWidth = res.w
  92. this.canvasHeight = res.h
  93. });
  94. },
  95. // 微信头像获取
  96. onChooseAvatar(e) {
  97. const {
  98. avatarUrl
  99. } = e.detail;
  100. let that = this;
  101. console.log("upimg",avatarUrl);
  102. uni.uploadFile({
  103. url: store.state.baseURL + '/api/' + 'upload/image',
  104. filePath: avatarUrl,
  105. fileType: 'image',
  106. name: 'pics',
  107. formData: {
  108. 'filename': 'pics'
  109. },
  110. header: {
  111. // #ifdef MP
  112. "Content-Type": "multipart/form-data",
  113. // #endif
  114. "Authori-zation": 'Bearer ' + uni.getStorageSync('token')
  115. },
  116. success: (res) => {
  117. uni.hideLoading();
  118. console.log(res,'陈宫');
  119. if (res.statusCode == 403) {
  120. uni.showToast({
  121. title: res.data,
  122. icon: "error"
  123. })
  124. } else if (res.statusCode == 413) {
  125. uni.showToast({
  126. title: '上传图片失败,请重新上传小尺寸图片',
  127. icon: "error"
  128. })
  129. } else {
  130. console.log(res,'res');
  131. let data = res.data ? JSON.parse(res.data) : {};
  132. console.log(data.status == 200,data,'data');
  133. if (data.status == 200) {
  134. that.userInfo.avatar = data.data.url
  135. } else {
  136. uni.showToast({
  137. title: data.msg,
  138. icon: "error"
  139. })
  140. }
  141. }
  142. },
  143. fail: (err) => {
  144. console.log(err,'失败');
  145. uni.hideLoading();
  146. uni.showToast({
  147. title: '上传图片失败',
  148. icon: "error"
  149. })
  150. }
  151. })
  152. },
  153. closeAttr: function() {
  154. this.$emit('closeEdit');
  155. },
  156. /**
  157. * 提交修改
  158. */
  159. formSubmit(e) {
  160. let that = this
  161. if (!this.userInfo.avatar) {
  162. uni.showToast({
  163. title: '请上传头像',
  164. icon: "error"
  165. })
  166. return
  167. }
  168. if (!e.detail.value.nickname) {
  169. uni.showToast({
  170. title: '请输入昵称',
  171. icon: "error"
  172. })
  173. return
  174. }
  175. this.userInfo.nickname = e.detail.value.nickname
  176. userEdit(this.userInfo).then(res => {
  177. this.$emit('editSuccess')
  178. }).catch(msg => {
  179. uni.showToast({
  180. title: msg || '保存失败',
  181. icon: "error"
  182. })
  183. return
  184. });
  185. }
  186. }
  187. }
  188. </script>
  189. <style>
  190. .pl-sty {
  191. color: #999999;
  192. font-size: 30rpx;
  193. }
  194. </style>
  195. <style scoped lang="scss">
  196. uni-popup{
  197. height: 100vh;
  198. }
  199. .product-window.on {
  200. transform: translate3d(0, 0, 0);
  201. }
  202. .avatar-box{
  203. width: 48rpx;
  204. background-color: #fff;
  205. }
  206. .avatar-box:after{
  207. border: none;
  208. }
  209. .mask {
  210. z-index: 100;
  211. }
  212. .product-window {
  213. position: fixed;
  214. bottom: 0;
  215. width: 100%;
  216. left: 0;
  217. background-color: #fff;
  218. z-index: 1000;
  219. border-radius: 20rpx 20rpx 0 0;
  220. transform: translate3d(0, 100%, 0);
  221. transition: all .3s cubic-bezier(.25, .5, .5, .9);
  222. padding: 38rpx 40rpx;
  223. padding-bottom: 80rpx;
  224. padding-bottom: calc(80rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  225. padding-bottom: calc(80rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
  226. .icon-guanbi {
  227. position: absolute;
  228. top: 30rpx;
  229. right: 30rpx;
  230. font-size: 24rpx;
  231. font-weight: bold;
  232. color: #999;
  233. padding: 10rpx;
  234. }
  235. .mp-data {
  236. display: flex;
  237. align-items: center;
  238. margin-bottom: 30rpx;
  239. .mp-name {
  240. font-size: 28rpx;
  241. font-weight: bold;
  242. color: #000000;
  243. }
  244. image {
  245. width: 48rpx;
  246. height: 48rpx;
  247. border-radius: 50%;
  248. margin-right: 16rpx;
  249. }
  250. }
  251. .trip-msg {
  252. padding-bottom: 32rpx;
  253. border-bottom: 1px solid #F5F5F5;
  254. .title {
  255. font-size: 30rpx;
  256. font-weight: bold;
  257. color: #000;
  258. margin-bottom: 6rpx;
  259. }
  260. .trip {
  261. font-size: 26rpx;
  262. color: #777777;
  263. }
  264. }
  265. .edit {
  266. border-bottom: 1px solid #F5F5F5;
  267. .avatar {
  268. border-bottom: 1px solid #F5F5F5;
  269. }
  270. .nickname {
  271. .input {
  272. width: 100%;
  273. }
  274. input {
  275. height: 80rpx;
  276. }
  277. }
  278. .edit-box {
  279. display: flex;
  280. justify-content: space-between;
  281. align-items: center;
  282. font-size: 30rpx;
  283. padding: 22rpx 0;
  284. .left {
  285. display: flex;
  286. align-items: center;
  287. flex: 1;
  288. .head {
  289. color: rgba(0, 0, 0, 0.9);
  290. white-space: nowrap;
  291. margin-right: 60rpx;
  292. }
  293. button {
  294. flex: 1;
  295. display: flex;
  296. align-items: center;
  297. }
  298. }
  299. image {
  300. width: 80rpx;
  301. height: 80rpx;
  302. border-radius: 6rpx;
  303. }
  304. }
  305. .icon-xiangyou {
  306. color: #cfcfcf;
  307. }
  308. }
  309. .bottom {
  310. display: flex;
  311. align-items: center;
  312. justify-content: center;
  313. .save {
  314. border: 1px solid #F5F5F5;
  315. display: flex;
  316. align-items: center;
  317. justify-content: center;
  318. width: 368rpx;
  319. height: 80rpx;
  320. border-radius: 12rpx;
  321. margin-top: 52rpx;
  322. background-color: #F5F5F5;
  323. color: #ccc;
  324. font-size: 30rpx;
  325. font-weight: bold;
  326. }
  327. .save.open {
  328. border: 1px solid #fff;
  329. background-color:#007c92;
  330. color: #fff;
  331. }
  332. }
  333. }
  334. </style>