user-info.vue 6.2 KB

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