info.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <view :style="vuex_skin">
  3. <!-- #ifdef MP-WEIXIN -->
  4. <u-navbar title-color="#000000" :is-back="true" title="我的资料"></u-navbar>
  5. <!-- #endif -->
  6. <view class="bg-white margin-top-20">
  7. <view class="flex padding justify-between align-center" @click="upload">
  8. <view class="title">头像</view>
  9. <u-avatar :src="userData.avatar" size="80"></u-avatar>
  10. </view>
  11. <u-line color="#f1f1f1"></u-line>
  12. <view class="flex padding justify-between align-center">
  13. <view class="title">昵称<text class="text-red">*</text></view>
  14. <view class="center">
  15. <u-input :disabled="!isEdit" v-model="userData.nickName" :placeholder-style="placeholderStyle"
  16. :clearable="false" inputAlign="right" placeholder="请输入昵称">
  17. </u-input>
  18. <text class="cuIcon-right margin-left-10" style="color: #CCCCCC;"></text>
  19. </view>
  20. </view>
  21. <u-line color="#f1f1f1"></u-line>
  22. <view class="flex padding justify-between align-center">
  23. <view class="title">性别</view>
  24. <view class="center">
  25. <u-input disabled @click="showSex" :placeholder-style="placeholderStyle" v-model="userData.sex"
  26. :clearable="false" inputAlign="right" placeholder="性别"></u-input>
  27. <text class="cuIcon-right margin-left-10" style="color: #CCCCCC;"></text>
  28. <u-action-sheet :list="actionSheetList" v-model="sexShow" @click="actionSheetCallback">
  29. </u-action-sheet>
  30. </view>
  31. </view>
  32. <u-line color="#f1f1f1"></u-line>
  33. <!-- #ifdef MP-WEIXIN -->
  34. <view class="flex padding justify-between align-center">
  35. <view class="title">手机号</view>
  36. <view class="center">
  37. <u-input disabled v-model="userData.phone" :placeholder-style="placeholderStyle" :clearable="false"
  38. inputAlign="right" placeholder="请填写手机号码"></u-input>
  39. <button open-type="getPhoneNumber" @getphonenumber="getphonenumber"
  40. class="cu-btn radius u-reset-button round bg-base sm"
  41. style="padding: 8rpx 16rpx;font-size: 20rpx;margin-left: 30rpx;">
  42. 获取手机号
  43. </button>
  44. </view>
  45. </view>
  46. <!-- #endif -->
  47. <!-- #ifdef H5 -->
  48. <view class="flex padding justify-between align-center">
  49. <view class="title center">手机号</view>
  50. <view class="center flex">
  51. <view class="center">
  52. <u-input disabled v-model="userData.phone" :placeholder-style="placeholderStyle" :clearable="false" inputAlign="right" placeholder="请填写手机号码"></u-input>
  53. </view>
  54. <view class="center padding-left-20">
  55. <u-icon name="edit-pen-fill" color="#e72226" size="30" @click="changePhone"></u-icon>
  56. <!-- <text class="text-base" @click="changePhone">修改</text> -->
  57. </view>
  58. </view>
  59. </view>
  60. <!-- #endif -->
  61. </view>
  62. <view style="margin-top: 150rpx;" @click="confirm">
  63. <view class="bg-white center text-base" style="width: 100%;height: 90rpx;font-family: PingFang-SC-Medium;">
  64. <text v-if="isEdit">确认修改</text>
  65. <text v-else>退出登录</text>
  66. </view>
  67. </view>
  68. </view>
  69. </template>
  70. <script>
  71. export default {
  72. data() {
  73. return {
  74. placeholderStyle: 'color: #c0c4cc;font-size:26rpx',
  75. isEdit: false,
  76. userData: {
  77. avatar: "",
  78. nickName: "",
  79. sex: "",
  80. phone: "",
  81. code: "",
  82. smsId: ""
  83. },
  84. sexShow: false,
  85. border: true,
  86. show: false,
  87. actionSheetList: [{
  88. text: '男'
  89. },
  90. {
  91. text: '女'
  92. },
  93. {
  94. text: '保密'
  95. }
  96. ],
  97. }
  98. },
  99. onLoad(options) {
  100. // #ifdef MP-WEIXIN
  101. this.handelSessionKey()
  102. // #endif
  103. this.isEdit = options.edit || false
  104. if (this.isEdit) {
  105. uni.setNavigationBarTitle({
  106. title: "修改资料"
  107. })
  108. }
  109. this.getUserInfo();
  110. },
  111. methods: {
  112. changePhone() {
  113. uni.navigateTo({
  114. url: `./change-phone?oldPhone=${this.userData.phone}`,
  115. })
  116. },
  117. showSex() {
  118. if (this.isEdit) {
  119. this.sexShow = true
  120. return
  121. }
  122. },
  123. getUserInfo() {
  124. this.$api.loginUser.detail({
  125. id: this.vuex_userId
  126. }).then(res => {
  127. this.userData = res.data.data;
  128. })
  129. },
  130. // 点击actionSheet回调
  131. actionSheetCallback(index) {
  132. this.userData.sex = this.actionSheetList[index].text;
  133. },
  134. async upload() {
  135. if (!this.isEdit) {
  136. return false
  137. }
  138. let resp = await this.$mpi.chooseImage()
  139. this.$api.uploadFile(resp[0]).then(res => {
  140. this.userData.avatar = res.data.data.link
  141. })
  142. },
  143. confirm() {
  144. if (this.isEdit) {
  145. this.update()
  146. return
  147. }
  148. this.logout()
  149. },
  150. logout() {
  151. this.$cache.clear()
  152. this.$u.vuex('vuex_userId', null)
  153. this.$u.vuex('vuex_phone', null)
  154. uni.reLaunch({
  155. url: "../mine"
  156. })
  157. },
  158. update() {
  159. if (this.$u.test.isEmpty(this.userData.nickName)) {
  160. uni.showToast({
  161. icon: "none",
  162. title: "请填写必填信息",
  163. })
  164. return;
  165. }
  166. let data = {
  167. id: this.userData.id,
  168. nickName: this.userData.nickName,
  169. sex: this.userData.sex,
  170. phone: this.userData.phone,
  171. avatar: this.userData.avatar
  172. }
  173. this.$api.loginUser.updateInfo(data).then(res => {
  174. if (res.data.success) {
  175. this.cachePhone(this.userData.phone)
  176. this.$dialog.showModalAndBack('修改成功', this.vuex_theme.bgColor)
  177. }
  178. })
  179. },
  180. /**
  181. * 获取getSessionKey
  182. */
  183. async handelSessionKey() {
  184. let sessionKey = await this.getSessionKey()
  185. let userInfo = this.$cache.get('userInfo')
  186. userInfo.sessionKey = sessionKey
  187. this.$cache.put('userInfo', userInfo)
  188. this.sessionKey = sessionKey
  189. },
  190. async getSessionKey() {
  191. let respWx = await this.$mpi.wxLogin()
  192. let respLogin = (await this.$api.wxApp.getOpenId({
  193. code: respWx.code
  194. })).data
  195. return respLogin.data.session_key
  196. },
  197. async getphonenumber(e) {
  198. if (e.detail.errMsg != 'getPhoneNumber:ok') {
  199. this.$u.toast("获取手机号失败")
  200. return
  201. }
  202. if (this.$isEmpty(this.sessionKey)) {
  203. this.$u.toast("sessionKey获取失败")
  204. return
  205. }
  206. let params = {
  207. sessionKey: this.sessionKey,
  208. encryptedData: e.detail.encryptedData,
  209. iv: e.detail.iv
  210. }
  211. this.$api.wxApp.getOpenData(params).then(res => {
  212. if (res.data.success) {
  213. let obj = JSON.parse(res.data.data)
  214. let phone = obj.phoneNumber
  215. this.userData.phone = phone
  216. } else {
  217. this.$u.toast("获取手机号失败")
  218. }
  219. })
  220. },
  221. cachePhone(phone) {
  222. let userInfo = this.$cache.get('userInfo')
  223. this.$cache.put('userInfo', userInfo)
  224. this.$u.vuex('vuex_phone', phone)
  225. }
  226. }
  227. }
  228. </script>
  229. <style lang="scss" scoped>
  230. .padding {
  231. padding: 30rpx 40rpx;
  232. }
  233. .title {
  234. font-size: 28rpx;
  235. font-family: PingFang-SC-Medium;
  236. color: #060606;
  237. padding-left: 10upx;
  238. }
  239. </style>