user-info.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <view class="user-info">
  3. <view class="info-item">
  4. <text class="tit">头像</text>
  5. <view class="avater" @click="gainimg">
  6. <image :src="userinfos.image" mode="aspectFill"></image>
  7. </view>
  8. </view>
  9. <view class="info-item">
  10. <text class="tit">用户名</text>
  11. <text class="val">{{userinfos.username}}</text>
  12. <!-- <input type="text" v-model="userinfos.username" :class="compileTF?'valinput valinput2':'valinput'" :disabled="!compileTF"
  13. maxlength='8' /> -->
  14. </view>
  15. <view class="info-item">
  16. <text class="tit">用户身份</text>
  17. <text class="val">管理员</text>
  18. </view>
  19. <view class="info-item">
  20. <text class="tit">用户电话</text>
  21. <input type="text" v-model="userinfos.mobile" :class="compileTF?'valinput valinput2':'valinput'" :disabled="!compileTF"
  22. @blur="verifyphone" />
  23. <p class="hint" v-if="phonehint">手机号格式不正确</p>
  24. </view>
  25. <view class="info-item">
  26. <text class="tit">E-mail</text>
  27. <input type="text" v-model="userinfos.email" :class="compileTF?'valinput valinput2':'valinput'" :disabled="!compileTF"
  28. @blur="verifyemail" />
  29. <p class="hint" v-if="emailhint">邮箱格式不正确</p>
  30. </view>
  31. <view class="info-item">
  32. <text class="tit">我的地址</text>
  33. <input type="text" v-model="location" :class="compileTF?'valinput valinput2':'valinput'"
  34. :disabled="!compileTF" @click="cutticy"/>
  35. </view>
  36. <view class="compile">
  37. <p @click="compile" v-if="!compileTF">编辑</p>
  38. <p @click="submit" v-else>提交</p>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. export default {
  44. data() {
  45. return {
  46. userinfos: {
  47. image:"",
  48. image2:""
  49. },
  50. location:'',
  51. imageList: '',
  52. compileTF: false,
  53. phonehint: false,
  54. emailhint: false
  55. }
  56. },
  57. methods: {
  58. gainimg() { //添加图片
  59. if (this.compileTF) {
  60. uni.chooseImage({
  61. count: 1, //默认9
  62. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  63. sourceType: ['album', 'camera'], //从相册选择
  64. success: (res) => {
  65. console.log(JSON.stringify(res))
  66. this.userinfos.image2 = JSON.stringify(res)
  67. uni.uploadFile({
  68. url: 'https://wx.hnyfwlw.com/api/api_gateway?method=base.bases.base_photo', //仅为示例,非真实的接口地址
  69. filePath: res.tempFilePaths[0],
  70. name: 'img_file',
  71. formData: {
  72. 'user': 'test'
  73. },
  74. success: (uploadFileRes) => {
  75. console.log(uploadFileRes)
  76. this.userinfos.image = JSON.parse(uploadFileRes.data).data.src
  77. this.$forceUpdate() //强制刷新视图
  78. },
  79. fail(res) {
  80. this.userinfos.image = JSON.stringify(res)
  81. console.log(55555555555)
  82. }
  83. });
  84. }
  85. })
  86. }
  87. }, //
  88. async postusers() {
  89. const res = await this.$myRequest({
  90. url: '/api/api_gateway?method=home.homes.personal_center',
  91. data: {
  92. ret: 'change',
  93. username: this.userinfos.username,
  94. mobile: this.userinfos.mobile,
  95. province: this.userinfos.province,
  96. city: this.userinfos.city,
  97. district: this.userinfos.district,
  98. image: this.userinfos.image,
  99. email: this.userinfos.email
  100. }
  101. })
  102. console.log(res)
  103. if(!res){
  104. this.compileTF = false
  105. uni.removeStorage({
  106. key: "location",
  107. })
  108. uni.navigateBack({
  109. delta:1
  110. })
  111. }
  112. },
  113. compile() { //编辑按钮
  114. this.compileTF = true
  115. },
  116. submit() {//提交按钮
  117. if(this.phonehint == false &&this.emailhint == false){
  118. this.userinfos.province = this.location.slice(0,this.location.indexOf("省")+1)
  119. this.userinfos.city = this.location.slice(this.location.indexOf("省")+1,this.location.indexOf("市")+1)
  120. this.userinfos.district = this.location.slice(this.location.indexOf("市")+1)
  121. this.postusers()
  122. }
  123. },
  124. verifyphone() { //手机号验证
  125. var reg = /^1[356789]\d{9}$/;
  126. if (!reg.test(this.userinfos.mobile)) {
  127. this.phonehint = true
  128. } else {
  129. this.phonehint = false
  130. }
  131. },
  132. verifyemail() { //邮箱验证
  133. var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  134. if (!reg.test(this.userinfos.email)) {
  135. this.emailhint = true
  136. } else {
  137. this.emailhint = false
  138. }
  139. },
  140. cutticy(){
  141. if(this.compileTF){
  142. uni.navigateTo({
  143. url: "../../fourBase/city"
  144. })
  145. }
  146. },
  147. selectaddress(lng, lat) { //获取分布位置
  148. uni.request({
  149. type: "GET",
  150. url: "https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=" + lng + "," + lat +
  151. "&key=78ce288400f4fc6d9458989875c833c2&radius=1000&extensions=all",
  152. dataType: "json",
  153. complete: ress => {
  154. // console.log(ress)
  155. if (ress.data.regeocode.formatted_address.length == 0) {
  156. this.location = "--"
  157. } else {
  158. this.location = ress.data.regeocode.formatted_address
  159. }
  160. }
  161. });
  162. },
  163. },
  164. onShow(){
  165. uni.getStorage({
  166. key: "location",
  167. success: (res) => {
  168. var lat = res.data[1]
  169. var lng = res.data[0]
  170. this.selectaddress(lng, lat)
  171. }
  172. })
  173. },
  174. onLoad(option) {
  175. this.userinfos = JSON.parse(option.data)
  176. this.location = this.userinfos.province+this.userinfos.city+this.userinfos.district
  177. console.log(this.userinfos)
  178. }
  179. }
  180. </script>
  181. <style lang="scss">
  182. page {
  183. background: $uni-bg-color-grey;
  184. .user-info {
  185. background: #fff;
  186. padding: 0 40rpx;
  187. .info-item {
  188. display: flex;
  189. justify-content: space-between;
  190. line-height: 100rpx;
  191. position: relative;
  192. .avater {
  193. width: 100rpx;
  194. height: 100rpx;
  195. border-radius: 50%;
  196. overflow: hidden;
  197. image {
  198. width: 100%;
  199. height: 100%;
  200. }
  201. }
  202. .tit {
  203. font-size: 14px
  204. }
  205. .val {
  206. font-size: 12px;
  207. color: #666;
  208. }
  209. .valinput {
  210. margin-top: 24rpx;
  211. text-align: right;
  212. font-size: 12px;
  213. color: #666;
  214. padding: 10rpx 0;
  215. width: 360rpx;
  216. }
  217. .valinput2 {
  218. background-color: #e1e5ee;
  219. }
  220. .hint {
  221. position: absolute;
  222. top: 40rpx;
  223. right: 220rpx;
  224. font-size: 24rpx;
  225. color: #ff0000;
  226. }
  227. }
  228. .compile {
  229. width: 100%;
  230. margin-top: 40rpx;
  231. height: 60rpx;
  232. line-height: 60rpx;
  233. text-align: center;
  234. background-color: #58C77A;
  235. border-radius: 30rpx;
  236. color: #FFFFFF;
  237. }
  238. }
  239. }
  240. </style>