userinfo.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <template>
  2. <view>
  3. <view class="usermain">
  4. <view class="usermain-item ">
  5. <view>头像</view>
  6. <!-- #ifdef MP-WEIXIN -->
  7. <view>
  8. <button open-type="chooseAvatar" @chooseavatar="onChooseAvatar" style="background: #FFFFFF;">
  9. <image src="../../static/logo.png" v-if="avatar==null" mode=""
  10. style="width: 111rpx;height: 111rpx;border-radius: 50%;"></image>
  11. <image v-else :src="avatar" mode="" style="width: 111rpx;height: 111rpx;border-radius: 50%;">
  12. </image>
  13. </button>
  14. </view>
  15. <!-- #endif -->
  16. <!-- #ifndef MP-WEIXIN -->
  17. <view @click="uploadImg()">
  18. <image src="../../static/logo.png" v-if="avatar==null" mode=""
  19. style="width: 111rpx;height: 111rpx;border-radius: 50%;"></image>
  20. <image v-else :src="avatar" mode="" style="width: 111rpx;height: 111rpx;border-radius: 50%;">
  21. </image>
  22. </view>
  23. <!-- #endif -->
  24. </view>
  25. <view class="usermain-item item-padding ">
  26. <view>用户名</view>
  27. <view>
  28. <view class="cu-form-group">
  29. <input type="nickname" v-model="userName" placeholder="请输入用户名" />
  30. </view>
  31. </view>
  32. </view>
  33. <view class="usermain-item item-padding ">
  34. <view>年龄</view>
  35. <view>
  36. <view class="cu-form-group">
  37. <input v-model="age" />
  38. </view>
  39. </view>
  40. </view>
  41. <!-- <view class="usermain-item item-padding">
  42. <view >姓名</view>
  43. <view class="cu-form-group">
  44. <input v-model="realName" placeholder="请填写您的真实姓名" />
  45. </view>
  46. </view> -->
  47. <view class="usermain-item item-padding ">
  48. <view>手机</view>
  49. <view>
  50. <view class="cu-form-group" v-if="phone">
  51. <input v-model="phone" disabled placeholder="请输入联系电话" />
  52. </view>
  53. <!-- #ifdef MP-WEIXIN -->
  54. <view class="cu-form-group" v-if="!phone">
  55. <button style="border: none;font-size: 28rpx;background: #FFFFFF;" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  56. 授权手机号</button>
  57. </view>
  58. <!-- #endif -->
  59. </view>
  60. </view>
  61. <view class="usermain-item item-padding ">
  62. <view>性别</view>
  63. <view>
  64. <view class="cu-form-group">
  65. <u-radio-group v-model="sex">
  66. <u-radio shape="circle" active-color="#2FB57A" :name="1">男</u-radio>
  67. <u-radio shape="circle" active-color="#2FB57A" :name="2">女</u-radio>
  68. </u-radio-group>
  69. </view>
  70. </view>
  71. </view>
  72. </view>
  73. <view class="footer-btn">
  74. <view class="usermain-btn" @click="messagebtn()">保存</view>
  75. </view>
  76. </view>
  77. </template>
  78. <script>
  79. import configdata from '../../common/config.js';
  80. export default {
  81. data() {
  82. return {
  83. phone: '',
  84. avatar: '../../static/logo.png',
  85. userName: '',
  86. nickName: '',
  87. userId: '',
  88. realName: '',
  89. weChatId: "",
  90. password: '',
  91. platform: '',
  92. createTime: '',
  93. money: '',
  94. jiFen: '',
  95. status: '',
  96. zhiFuBao: '',
  97. zhiFuBaoName: '',
  98. sex: 1,
  99. age: 0
  100. };
  101. },
  102. onLoad(e) {
  103. this.getUserInfo()
  104. // this.avatar = uni.getStorageSync('avatar')
  105. },
  106. methods: {
  107. //小程序微信登录后获取手机号
  108. getPhoneNumber: function(e) {
  109. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  110. console.log('用户拒绝提供手机号');
  111. } else {
  112. console.log('用户同意提供手机号');
  113. console.log(e)
  114. this.setPhoneByInsert(e.detail.encryptedData, e.detail.iv);
  115. }
  116. },
  117. //小程序微信登录后获取手机号
  118. setPhoneByInsert(decryptData, iv) {
  119. let that = this;
  120. uni.login({
  121. provider: 'weixin',
  122. success: function(loginRes) {
  123. console.log(loginRes, '************')
  124. let data = {
  125. code: loginRes.code,
  126. type: 2
  127. }
  128. that.$Request.get('/app/Login/wxLogin', data).then(res => {
  129. if (res.code == 0) {
  130. uni.setStorageSync('openId', res.data.open_id)
  131. uni.setStorageSync('unionId', res.data.unionId)
  132. // that.sessionkey = res.data.session_key;
  133. let datat = {
  134. decryptData: decryptData,
  135. key: res.data.session_key,
  136. iv: iv
  137. };
  138. that.$Request.postJson('/app/Login/selectPhone', datat).then(res => {
  139. if (res.code == 0) {
  140. that.phone = res.data.phoneNumber;
  141. } else {
  142. uni.showToast({
  143. title: res.msg,
  144. icon: 'none',
  145. duration: 2000
  146. });
  147. }
  148. })
  149. }
  150. })
  151. }
  152. });
  153. },
  154. //微信填写能力获取头像
  155. onChooseAvatar(e) {
  156. console.log(e.detail.avatarUrl)
  157. let that = this;
  158. let token = uni.getStorageSync('token');
  159. uni.showLoading({
  160. title: '上传中...'
  161. });
  162. let userId = uni.getStorageSync('userId');
  163. uni.uploadFile({
  164. url: that.config("APIHOST1") + '/alioss/upload', //真实的接口地址
  165. filePath: e.detail.avatarUrl,
  166. header: {
  167. token: token
  168. },
  169. name: 'file',
  170. success: uploadFileRes => {
  171. let url = JSON.parse(uploadFileRes.data);
  172. that.avatar = url.data
  173. uni.hideLoading();
  174. }
  175. });
  176. },
  177. goMyAddress() {
  178. uni.navigateTo({
  179. url: '../jifen/myaddress'
  180. });
  181. },
  182. uploadImg() {
  183. let token = uni.getStorageSync('token')
  184. if (!token) {
  185. this.goLoginInfo();
  186. return;
  187. }
  188. let that = this;
  189. var url = null;
  190. uni.showActionSheet({
  191. // itemList按钮的文字接受的是数组
  192. itemList: ["查看头像", "从相册选择图片"],
  193. success(e) {
  194. var index = e.tapIndex
  195. if (index === 0) {
  196. // 用户点击了预览当前图片
  197. // 可以自己实现当前头像链接的读取
  198. let url = that.avatar;
  199. let arr = []
  200. arr.push(url)
  201. uni.previewImage({
  202. // 预览功能图片也必须是数组的
  203. urls: arr
  204. })
  205. } else if (index === 1) {
  206. uni.chooseImage({
  207. count: 1, //默认9
  208. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  209. sourceType: ['album'], //从相册选择
  210. success: function(res) {
  211. uni.showLoading({
  212. title: '上传中...'
  213. });
  214. let token = uni.getStorageSync('token');
  215. let userId = uni.getStorageSync('userId');
  216. uni.uploadFile({
  217. url: that.config("APIHOST1") + '/alioss/upload', //真实的接口地址
  218. filePath: res.tempFilePaths[0],
  219. header: {
  220. token: token
  221. },
  222. name: 'file',
  223. success: uploadFileRes => {
  224. url = JSON.parse(uploadFileRes.data);
  225. that.avatar = url.data
  226. uni.hideLoading();
  227. }
  228. });
  229. }
  230. });
  231. }
  232. }
  233. })
  234. },
  235. config: function(name) {
  236. var info = null;
  237. if (name) {
  238. var name2 = name.split("."); //字符分割
  239. if (name2.length > 1) {
  240. info = configdata[name2[0]][name2[1]] || null;
  241. } else {
  242. info = configdata[name] || null;
  243. }
  244. if (info == null) {
  245. let web_config = cache.get("web_config");
  246. if (web_config) {
  247. if (name2.length > 1) {
  248. info = web_config[name2[0]][name2[1]] || null;
  249. } else {
  250. info = web_config[name] || null;
  251. }
  252. }
  253. }
  254. }
  255. return info;
  256. },
  257. getUserInfo() {
  258. let userId = uni.getStorageSync('userId')
  259. this.$Request.get("/app/user/selectUserById").then(res => {
  260. if (res.code == 0) {
  261. this.$queue.setData('avatar', res.data.avatar);
  262. this.$queue.setData('userId', res.data.userId);
  263. this.$queue.setData('userName', res.data.userName);
  264. this.$queue.setData('phone', res.data.phone);
  265. this.$queue.setData('age', res.data.age);
  266. this.sex = res.data.sex
  267. this.age = res.data.age
  268. this.phone = res.data.phone;
  269. this.avatar = res.data.avatar;
  270. this.userName = res.data.userName;
  271. if (this.userName == null) {
  272. this.userName = res.data.nickName;
  273. } else {
  274. this.userName = res.data.userName;
  275. }
  276. }
  277. uni.hideLoading();
  278. });
  279. },
  280. // 保存
  281. messagebtn() {
  282. if (!this.userName) {
  283. // this.$queue.showToast('用户名不能为空');
  284. uni.showToast({
  285. title: "用户名不能为空",
  286. icon: "none"
  287. })
  288. } else if (!this.phone) {
  289. // this.$queue.showToast('用户名不能为空');
  290. uni.showToast({
  291. title: "联系电话不能为空",
  292. icon: "none"
  293. })
  294. } else {
  295. uni.showModal({
  296. title: '温馨提示',
  297. content: '确定保存信息',
  298. success: e => {
  299. if (e.confirm) {
  300. this.$Request.postJson("/app/user/updateUser", {
  301. userName: this.userName,
  302. avatar: this.avatar,
  303. phone: this.phone,
  304. sex: this.sex,
  305. age: this.age
  306. }).then(res => {
  307. if (res.code === 0) {
  308. uni.showToast({
  309. title: '保存成功',
  310. icon: "none"
  311. })
  312. setTimeout(function() {
  313. uni.navigateBack()
  314. }, 1000)
  315. } else {
  316. uni.showToast({
  317. title: res.msg,
  318. icon: "none"
  319. })
  320. }
  321. })
  322. }
  323. }
  324. });
  325. }
  326. }
  327. },
  328. // userphone(){
  329. // uni.navigateTo({
  330. // url:'/pages/my/userphone'
  331. // })
  332. // }
  333. };
  334. </script>
  335. <style>
  336. page {
  337. /* background: #1c1b20; */
  338. }
  339. button::after {
  340. border: none;
  341. }
  342. .usermain {
  343. background: #ffffff;
  344. /* color: #fff; */
  345. }
  346. .usermain-item {
  347. display: flex;
  348. align-items: center;
  349. margin: 0 40rpx;
  350. padding: 10rpx 0;
  351. justify-content: space-between;
  352. border-bottom: 1rpx solid #e5e5e5;
  353. /* border-bottom: 2rpx solid #f2f2f2; */
  354. }
  355. .usermain-item.item-padding {
  356. /* padding: 0; */
  357. }
  358. .cu-form-group {
  359. padding: 0;
  360. background: #ffffff;
  361. text-align: right;
  362. }
  363. .cu-form-group input {
  364. background: #ffffff;
  365. font-size: 28rpx;
  366. /* color: #fff; */
  367. }
  368. .footer-btn {
  369. margin-top: 50rpx;
  370. }
  371. .footer-btn .usermain-btn {
  372. width: 686rpx;
  373. height: 100rpx;
  374. line-height: 100rpx;
  375. text-align: center;
  376. background: #1EDA94;
  377. border-radius: 60rpx 60rpx 60rpx 60rpx;
  378. font-weight: bold;
  379. font-size: 32rpx;
  380. color: #FFFFFF;
  381. margin-left: 32rpx;
  382. }
  383. </style>