profile.vue 17 KB

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