profile.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <template>
  2. <view class="page">
  3. <ul class="profile" style="height: 60px;line-height: 60px;" @tap='uploadImage'>
  4. <li>头像:</li>
  5. <li>
  6. <image class="avatar" :src="user.avatar"></image>
  7. </li>
  8. <uni-icons class="next" type="arrowright"></uni-icons>
  9. </ul>
  10. <ul class="profile" style="height: 40px;line-height: 40px;">
  11. <li>账号:</li>
  12. <li>{{user.name}}
  13. </li>
  14. </ul>
  15. <ul class="profile">
  16. <li>昵称:</li>
  17. <li>
  18. <input type="text" class="input1" id="nickname" v-model="nickname" />
  19. </li>
  20. </ul>
  21. <ul class="profile">
  22. <li>所在地:</li>
  23. <li>
  24. <picker mode="multiSelector" @cancel="oncancel()" @columnchange="bindMultiPickerColumnChange"
  25. :value="multiIndex" :range="area">
  26. <view class="uni-input" @tap="tapaddress();">{{user.province}},{{user.city}}</view>
  27. </picker>
  28. </li>
  29. <uni-icons class="next" type="arrowright"></uni-icons>
  30. </ul>
  31. <ul class="profile">
  32. <li>性别:</li>
  33. <li>
  34. <picker @change="bindPickerChange" :value="user.sex" :range="sex" style="width: 100$;">
  35. <view class="uni-input" v-if="user.sex">{{sex[user.sex]}}</view>
  36. <view class="uni-input" v-else>保密</view>
  37. </picker>
  38. </li>
  39. <uni-icons class="next" type="arrowright"></uni-icons>
  40. </ul>
  41. <ul class="profile" style="padding: 10px 0px;">
  42. <li>个性签名:</li>
  43. <li>
  44. <textarea id="sign" placeholder="请填写您的个性签名" v-model="user.sign"></textarea>
  45. </li>
  46. </ul>
  47. <view style="margin:15px auto;display:block;width: 80%;">
  48. <button class="button1" @tap="submit">确认修改</button>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. import {
  54. mapState
  55. } from 'vuex';
  56. import api from "../../library/index.js"
  57. import cache from "../../library/cache.js"
  58. import helper from "../../library/helper.js"
  59. import config from "../../config.js"
  60. import http from "../../library/http.js"
  61. import address from '../../library/address.js'
  62. import uniIcons from '../../components/uni-icons/uni-icons.vue'
  63. export default {
  64. components: {
  65. uniIcons,
  66. },
  67. data() {
  68. return {
  69. user: uni.getStorageSync('userInfo'),
  70. sex: ['保密', '男', '女'],
  71. avatar: '',
  72. province: [],
  73. nickname: '',
  74. city: [],
  75. multiIndex: [0, 0],
  76. area: [
  77. ['省份'],
  78. ['城市']
  79. ],
  80. showarea: 0,
  81. provice1: '',
  82. city1: '',
  83. provice2: '',
  84. city2: '',
  85. c1: 0,
  86. c2: 0,
  87. }
  88. },
  89. methods: {
  90. uploadImage() {
  91. this.navCroper(400, 400, 'avatar')
  92. // uni.chooseImage({
  93. // success: (chooseImageRes) => {
  94. // const tempFilePaths = chooseImageRes.tempFilePaths;
  95. // console.log(tempFilePaths,'tempFilePaths');
  96. // helper.uploadFiles(tempFilePaths, 'image', {
  97. // type: 'avatar'
  98. // }).then(res => {
  99. // this.user.avatar = res;
  100. // this.avatar = res.replace(config.imgUri, '');
  101. // var postdata = {
  102. // id: this.user.id,
  103. // avatar: this.avatar,
  104. // };
  105. // http.setWait(false).post('user.php?act=profile', postdata).then(res => {
  106. // this.user = res.data;
  107. // uni.setStorageSync('userInfo', this.user)
  108. // })
  109. // })
  110. // }
  111. // });
  112. },
  113. submit() {
  114. if (this.nickname == '') {
  115. uni.showToast({
  116. icon: 'none',
  117. title: '请输入昵称',
  118. duration: 1000
  119. })
  120. return false;
  121. }
  122. // if (this.nickname.length > 7) {
  123. // uni.showToast({
  124. // icon: 'none',
  125. // title: '昵称最多7个字符',
  126. // duration: 1000
  127. // })
  128. // return false;
  129. // }
  130. // var nickname=this.nickname.replace(/-/g,"");
  131. // nickname=nickname.replace(/_/g,"");
  132. // nickname=nickname.replace(/\(/g,"");
  133. // nickname=nickname.replace(/\)/g,"");
  134. // nickname=nickname.replace(/\[/g,"");
  135. // nickname=nickname.replace(/\]/g,"");
  136. // nickname=nickname.replace(/ /g,"");
  137. // var reglx = /^[\u0391-\uFFE5A-Za-z]+$/;
  138. // if(!reglx.test(nickname)){
  139. // uni.showToast({
  140. // icon:'none',
  141. // title:'昵称只能包含中文或者英文',
  142. // duration:1000
  143. // })
  144. // return false;
  145. // }
  146. let data = {
  147. id: this.user.id,
  148. nickname: this.nickname,
  149. sex: this.user.sex,
  150. sign: this.user.sign,
  151. };
  152. http.setWait(true).post('user.php?act=profile', data).then(res => {
  153. this.user = res.data;
  154. this.userdata();
  155. uni.showToast({
  156. icon: 'none',
  157. title: '资料已更新',
  158. duration: 1000
  159. })
  160. setTimeout(function() {
  161. var profileback = uni.getStorageSync('profileback');
  162. if (profileback != undefined && profileback != null && profileback.length > 3) {
  163. uni.setStorageSync('profileback', '')
  164. uni.redirectTo({
  165. url: profileback
  166. })
  167. }
  168. }, 800)
  169. })
  170. },
  171. bindPickerChange(e) {
  172. this.user.sex = e.target.value
  173. },
  174. oncancel() {
  175. let data = {
  176. id: this.user.id,
  177. province: this.provice2,
  178. city: this.city2,
  179. };
  180. api.changeProfile(data).then(res => {
  181. uni.setStorageSync('userInfo', res.data);
  182. this.user = res.data;
  183. this.userdata();
  184. //this.$store.state.userinfo=res.data;;
  185. })
  186. },
  187. tapaddress() {
  188. // console.log( this.sex);
  189. // console.log( this.area[0]);
  190. // console.log( this.area[1]);
  191. this.provice2 = this.user.province;
  192. this.city2 = this.user.city;
  193. },
  194. bindMultiPickerColumnChange(e) {
  195. var detail = e.detail;
  196. detail.value = parseInt(detail.value);
  197. if (detail.column == 0) {
  198. // console.log(this.city[detail.value].city);
  199. this.c1 = detail.value;
  200. this.area[1] = this.city[detail.value].city;
  201. this.multiIndex[0] = detail.value;
  202. this.provice1 = this.city[detail.value].province;
  203. this.city1 = this.area[1][this.c2];
  204. } else {
  205. this.c2 = detail.value;
  206. this.multiIndex[1] = detail.value;
  207. this.city1 = this.area[1][detail.value];
  208. }
  209. var postdata = {
  210. id: this.user.id,
  211. province: this.provice1,
  212. city: this.city1,
  213. };
  214. http.setWait(false).post('user.php?act=profile', postdata).then(res => {
  215. this.user = res.data;
  216. this.userdata();
  217. })
  218. },
  219. getAddress() {
  220. if (!this.user.province || this.user.province == null) this.user.province = '省份';
  221. if (!this.user.city || this.user.city == null) this.user.city = '城市';
  222. var c1 = 0;
  223. address.forEach(item => {
  224. this.province.push(item.provinceName);
  225. // console.log(this.user.province,item.provinceName);
  226. if (this.user.province == item.provinceName) this.multiIndex[0] = parseInt(c1);
  227. c1++;
  228. var arr = {};
  229. arr.province = item.provinceName;
  230. var city1 = [];
  231. var c2 = 0;
  232. item.city.forEach(c => {
  233. if (c.cityName != '')
  234. city1.push(c.cityName)
  235. if (this.user.province == item.provinceName && this.user.city == c.cityName)
  236. this.multiIndex[1] = parseInt(c2);
  237. c2++;
  238. })
  239. arr.city = city1;
  240. this.city.push(arr)
  241. })
  242. // console.log(this.province);
  243. // this.area=[];
  244. this.area[0] = this.province;
  245. this.area[1] = this.city[this.multiIndex[0]].city;
  246. this.showarea = 1;
  247. },
  248. userdata() {
  249. if (this.user.issetname == 1) this.nickname = this.user.nickname;
  250. else this.nickname = "";
  251. if (this.user.signature == null) {
  252. this.user.signature = ''
  253. }
  254. uni.setStorageSync('userInfo', this.user)
  255. },
  256. getuserinfo() {
  257. var postdata = {
  258. id: uni.getStorageSync('access_token')
  259. };
  260. http.setWait(false).post('user.php?act=userinfo', postdata).then(res => {
  261. this.user = res.data;
  262. this.userdata();
  263. this.getAddress();
  264. })
  265. },
  266. // 图片上传
  267. upLoad(path) {
  268. let that = this
  269. uni.showLoading({
  270. title: '图片上传中',
  271. mask: true
  272. });
  273. console.log('图片上传中')
  274. return new Promise((resolve, error) => {
  275. let arr = [];
  276. arr.push(path)
  277. const tempFilePaths = arr;
  278. helper.uploadFiles(tempFilePaths, 'image', {
  279. type: 'avatar'
  280. }).then(res => {
  281. that.user.avatar = res;
  282. that.avatar = res.replace(config.imgUri, '');
  283. var postdata = {
  284. id: that.user.id,
  285. avatar: that.avatar,
  286. };
  287. http.setWait(false).post('user.php?act=profile', postdata).then(res => {
  288. that.user = res.data;
  289. uni.setStorageSync('userInfo', that.user)
  290. })
  291. })
  292. })
  293. },
  294. chooseImage: function(index) {
  295. const _this = this
  296. return new Promise((ok, error) => {
  297. // 从相册/相机选择
  298. // 如需直接开相机或直接选相册,请只使用一个选项
  299. const sourceType = index === 0 ? ['camera'] : ['album']
  300. uni.chooseImage({
  301. count: 1, //默认9
  302. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  303. sourceType: sourceType,
  304. success: function(res) {
  305. ok(res.tempFilePaths[0])
  306. },
  307. fail(e) {
  308. uni.showModal({
  309. title: '文件打开错误',
  310. content: '请设置授权文件存储权限',
  311. showCancel: false,
  312. });
  313. error(e)
  314. }
  315. });
  316. })
  317. },
  318. onImg(type) {
  319. const _this = this
  320. return new Promise((ok, erro) => {
  321. // 判断是否需要选择
  322. if (type == 1) {
  323. uni.showActionSheet({
  324. itemList: ['拍照', '选择一张照片'],
  325. success: function(res) {
  326. console.log(res, '上传')
  327. _this.chooseImage(res.tapIndex).then((url) => {
  328. console.log(url, 'url');
  329. ok(url)
  330. }).catch((res) => {
  331. erro(res)
  332. })
  333. },
  334. fail: function(res) {
  335. erro(res)
  336. console.log(res.errMsg);
  337. }
  338. });
  339. }
  340. // 判断是否只需要拍照
  341. if (type == 2) {
  342. _this.chooseImage(0).then((url) => {
  343. ok(url)
  344. }).catch((res) => {
  345. erro(res)
  346. })
  347. }
  348. })
  349. },
  350. //图像裁剪
  351. navCroper(w, h, type) {
  352. let that = this;
  353. let tt = (type == 'upimg' ? 2 : 1)
  354. this.onImg(tt).then((url) => {
  355. uni.navigateTo({
  356. url: `/pages/index/cropper?width=${w}&height=${h}`,
  357. events: {
  358. uploadSuccess(res) {
  359. console.log(res, '这里')
  360. that.upLoad(res).then((urldata) => {
  361. console.log(urldata, '这里')
  362. that[type] = urldata.url;
  363. })
  364. }
  365. },
  366. success: function(res) {
  367. console.log(res, 'success');
  368. // 通过eventChannel向被打开页面传送数据
  369. res.eventChannel.emit('urlNext', {
  370. url
  371. })
  372. }
  373. })
  374. })
  375. },
  376. },
  377. onLoad() {
  378. this.user = cache.storage('userInfo');
  379. this.userdata();
  380. this.getAddress();
  381. this.getuserinfo();
  382. }
  383. }
  384. </script>
  385. <style lang="scss" scoped>
  386. @import "@/static/css/user.css";
  387. .page {
  388. background-color: #fafafa;
  389. }
  390. .profile {
  391. background-color: #fff;
  392. margin: 10px auto;
  393. width: 100%;
  394. position: relative;
  395. }
  396. .profile li {
  397. vertical-align: middle;
  398. }
  399. .profile li:nth-child(2) {
  400. width: calc(100% - 100px);
  401. }
  402. .profile .avatar {
  403. height: 50px;
  404. width: 50px;
  405. border-radius: 5px;
  406. vertical-align: middle;
  407. }
  408. .profile .next {
  409. position: absolute;
  410. right: 10px;
  411. top: 0px;
  412. color: #666 !important;
  413. font-size: 16px !important;
  414. }
  415. </style>