PersonalData.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <template>
  2. <div class="personal-data">
  3. <NavBar title="用户中心"></NavBar>
  4. <div class="wrapper">
  5. <div class="title">管理我的账号</div>
  6. <div class="wrapList">
  7. <div
  8. class="item acea-row row-between-wrapper"
  9. :class="item.uid === userInfo.uid ? 'on' : ''"
  10. v-for="(item, index) in switchUserInfo"
  11. :key="index"
  12. >
  13. <div class="picTxt acea-row row-between-wrapper">
  14. <div class="pictrue">
  15. <VueCoreImageUpload
  16. class="btn btn-primary"
  17. :crop="false"
  18. compress="80"
  19. @imageuploaded="imageuploaded"
  20. :headers="headers"
  21. :max-file-size="5242880"
  22. :credentials="false"
  23. inputAccept="image/*"
  24. inputOfFile="file"
  25. :url="url"
  26. ref="upImg"
  27. v-if="item.uid === userInfo.uid"
  28. >
  29. <div class="pictrue">
  30. <img :src="item.avatar" />
  31. </div>
  32. </VueCoreImageUpload>
  33. <div class="pictrue" v-else>
  34. <img :src="item.avatar" />
  35. </div>
  36. <img
  37. src="@assets/images/alter.png"
  38. class="alter"
  39. v-if="item.uid === userInfo.uid"
  40. />
  41. </div>
  42. <div class="text">
  43. <div class="name line1">{{ item.nickname }}</div>
  44. <div class="phone">绑定手机号:{{ item.phone }}</div>
  45. </div>
  46. </div>
  47. <div
  48. class="currentBnt acea-row row-center-wrapper font-color-red"
  49. v-if="item.uid === userInfo.uid"
  50. >
  51. 当前账号
  52. </div>
  53. <div
  54. class="bnt font-color-red acea-row row-center-wrapper"
  55. v-else
  56. @click="switchAccounts(index)"
  57. >
  58. 使用账号
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. <div class="list">
  64. <div class="item acea-row row-between-wrapper">
  65. <div>昵称</div>
  66. <div class="input">
  67. <input type="text" v-model="userInfo.nickname" />
  68. </div>
  69. </div>
  70. <div class="item acea-row row-between-wrapper">
  71. <div>ID号</div>
  72. <div class="input acea-row row-between-wrapper">
  73. <input type="text" :value="userInfo.uid" disabled class="id" /><span
  74. class="iconfont icon-suozi"
  75. ></span>
  76. </div>
  77. </div>
  78. <div v-if="!userInfo.phone">
  79. <router-link
  80. :to="'/user/binding'"
  81. class="item acea-row row-between-wrapper"
  82. >
  83. <div>绑定手机号</div>
  84. <div class="input">
  85. 点击绑定手机号<span class="iconfont icon-xiangyou"></span>
  86. </div>
  87. </router-link>
  88. </div>
  89. <div class="item acea-row row-between-wrapper" v-else-if="userInfo.phone">
  90. <div>手机号码</div>
  91. <div class="input acea-row row-between-wrapper">
  92. <div class="input acea-row row-between-wrapper">
  93. <input
  94. type="text"
  95. :value="userInfo.phone"
  96. disabled
  97. class="id"
  98. /><span class="iconfont icon-suozi"></span>
  99. </div>
  100. </div>
  101. </div>
  102. <div v-if="userInfo.phone && userInfo.user_type === 'h5'">
  103. <router-link
  104. :to="'/change_password'"
  105. class="item acea-row row-between-wrapper"
  106. >
  107. <div>密码</div>
  108. <div class="input">
  109. 点击修改密码<span class="iconfont icon-xiangyou"></span>
  110. </div>
  111. </router-link>
  112. </div>
  113. </div>
  114. <div class="modifyBnt bg-color-red" @click="submit">保存修改</div>
  115. <div
  116. class="logOut cart-color acea-row row-center-wrapper"
  117. @click="logout"
  118. v-if="!isWeixin"
  119. >
  120. 退出登录
  121. </div>
  122. </div>
  123. </template>
  124. <script>
  125. import { mapGetters } from "vuex";
  126. import { trim, VUE_APP_API_URL, isWeixin } from "@utils";
  127. import VueCoreImageUpload from "vue-core-image-upload";
  128. import { postUserEdit, getLogout, switchH5Login, getUser } from "@api/user";
  129. import { clearAuthStatus } from "@libs/wechat";
  130. import cookie from "@utils/store/cookie";
  131. import store from "@/store";
  132. import NavBar from "@components/NavBar";
  133. export default {
  134. name: "PersonalData",
  135. components: {
  136. VueCoreImageUpload,
  137. NavBar
  138. },
  139. data: function() {
  140. return {
  141. url: `${VUE_APP_API_URL}/upload/image`,
  142. headers: {
  143. "Authori-zation": "Bearer " + this.$store.state.app.token
  144. },
  145. avatar: "",
  146. isWeixin: false,
  147. currentAccounts: 0,
  148. switchUserInfo: [],
  149. userIndex: 0
  150. };
  151. },
  152. watch: {
  153. $route(n) {
  154. if (n.name === "PersonalData") this.$store.dispatch("USERINFO", true);
  155. }
  156. },
  157. computed: mapGetters(["userInfo"]),
  158. mounted: function() {
  159. this.avatar = this.userInfo.avatar;
  160. this.isWeixin = isWeixin();
  161. this.getUserInfo();
  162. },
  163. methods: {
  164. switchAccounts: function(index) {
  165. let that = this;
  166. this.userIndex = index;
  167. let userInfo = this.switchUserInfo[this.userIndex];
  168. if (this.switchUserInfo.length <= 1) return true;
  169. if (userInfo === undefined)
  170. return this.$dialog.toast({ mes: "切换的账号不存在" });
  171. if (userInfo.user_type === "h5") {
  172. switchH5Login()
  173. .then(({ data }) => {
  174. that.$dialog.loading.close();
  175. // const expires_time = dayjs(data.expires_time);
  176. // store.commit("LOGIN", data.token, expires_time);
  177. let expires_time = data.expires_time.substring(0, 19);
  178. expires_time = expires_time.replace(/-/g, "/");
  179. expires_time = new Date(expires_time).getTime() - 28800000;
  180. const datas = {
  181. token: data.token,
  182. expires_time: expires_time
  183. };
  184. store.commit("LOGIN", datas);
  185. that.$emit("changeswitch", false);
  186. location.reload();
  187. })
  188. .catch(err => {
  189. that.$dialog.loading.close();
  190. return that.$dialog.toast({ mes: err });
  191. });
  192. } else {
  193. cookie.set("loginType", "wechat", 60);
  194. this.$dialog.loading.close();
  195. this.$store.commit("LOGOUT");
  196. clearAuthStatus();
  197. this.$emit("changeswitch", false);
  198. location.reload();
  199. }
  200. },
  201. getUserInfo: function() {
  202. let that = this;
  203. getUser().then(res => {
  204. let switchUserInfo = res.data.switchUserInfo;
  205. for (let i = 0; i < switchUserInfo.length; i++) {
  206. if (switchUserInfo[i].uid == that.userInfo.uid) that.userIndex = i;
  207. if (
  208. !that.isWeixin &&
  209. switchUserInfo[i].user_type != "h5" &&
  210. switchUserInfo[i].phone === ""
  211. )
  212. switchUserInfo.splice(i, 1);
  213. }
  214. that.$set(this, "switchUserInfo", switchUserInfo);
  215. });
  216. },
  217. imageuploaded(res) {
  218. if (res.status !== 200)
  219. return this.$dialog.error(res.msg || "上传图片失败");
  220. if (this.switchUserInfo[this.userIndex] === undefined) return;
  221. this.$set(this.switchUserInfo[this.userIndex], "avatar", res.data.url);
  222. },
  223. submit: function() {
  224. let userInfo = this.switchUserInfo[this.userIndex];
  225. postUserEdit({
  226. nickname: trim(this.userInfo.nickname),
  227. avatar: userInfo.avatar
  228. }).then(
  229. res => {
  230. this.$store.dispatch("USERINFO", true);
  231. this.$dialog.success(res.msg);
  232. this.$router.go(-1);
  233. },
  234. error => {
  235. this.$dialog.error(error.msg);
  236. }
  237. );
  238. },
  239. logout: function() {
  240. this.$dialog.confirm({
  241. mes: "确认退出登录?",
  242. opts: () => {
  243. getLogout()
  244. .then(res => {
  245. this.$store.commit("LOGOUT");
  246. clearAuthStatus();
  247. location.href = location.origin;
  248. console.log(res);
  249. })
  250. .catch(err => {
  251. console.log(err);
  252. });
  253. }
  254. });
  255. }
  256. }
  257. };
  258. </script>