user.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // 用户数据模块
  2. import http from '@/common/request/index'
  3. import store from '@/common/store'
  4. import tools from '@/common/utils/tools'
  5. // import {
  6. // bindUser,
  7. // getUserInfo
  8. // } from '@/common/request/apis/user.js';
  9. // import {
  10. // logout
  11. // } from '@/common/request/apis/account.js';
  12. // import {
  13. // Openid,
  14. // getShareImage,
  15. // appShareConfig
  16. // } from '@/common/request/apis/index';
  17. // state 静态
  18. const state = {
  19. dynamic_switch: uni.getStorageSync("dynamic_switch") || uni.getStorageSync("dynamic_switch") === 0 ? uni.getStorageSync("dynamic_switch") : 1,
  20. alumni_switch: uni.getStorageSync("alumni_switch") || uni.getStorageSync("alumni_switch") === 0 ? uni.getStorageSync("alumni_switch") : 1,
  21. product_switch: uni.getStorageSync("product_switch") || uni.getStorageSync("product_switch") === 0 ? uni.getStorageSync("product_switch") : 1,
  22. token: uni.getStorageSync("token") || "",
  23. isStart: false, // 是否开屏
  24. isLogin: uni.getStorageSync("isLogin") || false, // 是否登陆
  25. isOneLogin: 0, // 是否登陆
  26. userInfo: uni.getStorageSync("userInfo") || {}, // 用户信息
  27. userEquity: uni.getStorageSync("userEquity") || {}, // 权益信息
  28. imgUrl: '', //服务器图片baseUrl
  29. videoHistory: uni.getStorageSync("videoHistory") || [], //查找视频历史记录
  30. helpHistory: uni.getStorageSync("helpHistory") || [], //查找帮助历史记录
  31. pageParams: {}
  32. }
  33. // getters 可以获取 computer 进行动态刷新
  34. const getters = {
  35. dynamic_switch: state => state.dynamic_switch,
  36. alumni_switch: state => state.alumni_switch,
  37. product_switch: state => state.product_switch,
  38. token: state => state.token,
  39. isStart: state => state.isStart,
  40. isLogin: state => state.isLogin,
  41. isOneLogin: state => state.isOneLogin,
  42. userInfo: state => state.userInfo,
  43. userEquity: state => state.userEquity,
  44. imgUrl: imgURL => state.imgUrl,
  45. videoHistory: videoHistory => state.videoHistory,
  46. helpHistory: helpHistory => state.helpHistory,
  47. pageParams: pageParams => state.pageParams
  48. }
  49. // actions 异步数据 接口
  50. const actions = {
  51. // 登录
  52. userLogin({
  53. commit,
  54. dispatch
  55. }, userInfo) {
  56. commit('setToken', userInfo.token);
  57. commit('setLogin', true);
  58. commit('setisOneLogin', 1);
  59. commit('setUserInfo', userInfo);
  60. // dispatch('chat/start', '', {
  61. // root: true
  62. // })
  63. const user_id = uni.getStorageSync('user_id')
  64. },
  65. // 退出登录
  66. userLogout({
  67. commit,
  68. dispatch
  69. },isPort) {
  70. if(isPort) {
  71. logout({}).then(res => {
  72. if (res.code == 1) {
  73. commit('setToken', "");
  74. commit('setLogin', false);
  75. commit('setisOneLogin', 0);
  76. commit('setUserInfo', {});
  77. uni.$u.toast(res.msg);
  78. } else {
  79. uni.$u.toast(res.msg);
  80. }
  81. }).catch(err => {
  82. uni.$u.toast(err.msg);
  83. })
  84. }else{
  85. commit('setToken', "");
  86. commit('setLogin', false);
  87. commit('setisOneLogin', 0);
  88. commit('setUserInfo', {});
  89. }
  90. },
  91. // 获取权益信息
  92. getUserEquity({
  93. commit,
  94. dispatch
  95. }) {
  96. getRemainNumber().then(res => {
  97. commit('setUserEquity', res.data);
  98. }).catch(err => {
  99. uni.$u.toast(err.msg);
  100. // 错误提示框
  101. });
  102. },
  103. // 更新用户信息
  104. updateUserInfo({
  105. commit,
  106. dispatch
  107. }) {
  108. return new Promise((resolve, reject) => {
  109. //调用登陆接口
  110. getUserInfo().then(res => {
  111. if (res.code == 1) {
  112. res.data.tagList = res.data.tag ? res.data.tag.split(",") : []
  113. commit('setUserInfo', res.data)
  114. resolve(res)
  115. } else {
  116. uni.$u.toast(res.msg);
  117. reject(res)
  118. }
  119. }).catch(err => {
  120. uni.$u.toast(err.msg);
  121. reject(err)
  122. })
  123. })
  124. },
  125. getPagePath({
  126. commit,
  127. dispatch
  128. }) {
  129. let curPage = getCurrentPages();
  130. let route = curPage[curPage.length - 1].route; //获取当前页面的路由
  131. let params = curPage[curPage.length - 1].options; //获取当前页面参数,如果有则返回参数的对象,没有参数返回空对象{}
  132. // 需要返回格式 /pages/XX?id=XX 打开下面的
  133. let query = '';
  134. let keys = Object.keys(params); //获取对象的key 返回对象key的数组
  135. if (keys.length > 0) {
  136. query = keys.reduce((pre, cur) => {
  137. return pre + cur + '=' + params[cur] + '&';
  138. }, '?').slice(0, -1);
  139. }
  140. return new Promise((resolve, reject) => {
  141. //调用登陆接口
  142. getShareImage({
  143. url: '/' + route
  144. }).then(res => {
  145. if (res.code == 1) {
  146. let obj = {
  147. pathObj: {
  148. url: route,
  149. params
  150. },
  151. shareImg: res.data,
  152. path:route + query
  153. }
  154. //存储信息在state中
  155. commit('setPageParams', obj)
  156. resolve(obj)
  157. }
  158. }).catch(err => {
  159. reject(err)
  160. })
  161. })
  162. },
  163. }
  164. //静态数据 方法 可更改数据
  165. const mutations = {
  166. set_dynamic_switch(state, payload) {
  167. state.dynamic_switch = +payload;
  168. uni.setStorageSync("dynamic_switch", +payload);
  169. },
  170. set_alumni_switch(state, payload) {
  171. state.alumni_switch = +payload;
  172. uni.setStorageSync("alumni_switch", +payload);
  173. },
  174. set_product_switch(state, payload) {
  175. state.product_switch = +payload;
  176. uni.setStorageSync("product_switch", +payload);
  177. },
  178. setToken(state, payload) {
  179. state.token = payload;
  180. uni.setStorageSync("token", payload);
  181. },
  182. // 开屏动画
  183. setStart(state, data) {
  184. state.isStart = data;
  185. },
  186. // 登录态
  187. setLogin(state, data) {
  188. state.isLogin = data;
  189. uni.setStorageSync('isLogin', data);
  190. },
  191. // 是否第一次登录
  192. setisOneLogin(state, data) {
  193. state.isOneLogin = data;
  194. uni.setStorageSync('isOneLogin', data);
  195. },
  196. // 用户信息
  197. setUserInfo(state, data) {
  198. state.userInfo = data;
  199. uni.setStorageSync("userInfo", data);
  200. },
  201. // 权益信息
  202. setUserEquity(state, data) {
  203. state.userEquity = data;
  204. uni.setStorageSync("userEquity", data);
  205. },
  206. //搜索视频历史记录
  207. setVideoHistory(state, data) {
  208. if (!data) {
  209. console.log(data)
  210. // let arr=[]
  211. state.videoHistory = []
  212. uni.setStorageSync("videoHistory", state.videoHistory);
  213. } else {
  214. state.videoHistory.push(data);
  215. uni.setStorageSync("videoHistory", state.videoHistory);
  216. }
  217. },
  218. //搜索帮助历史记录
  219. setHelpHistory(state, data) {
  220. if (!data) {
  221. console.log(data)
  222. // let arr=[]
  223. state.helpHistory = []
  224. uni.setStorageSync("helpHistory", state.helpHistory);
  225. } else {
  226. state.helpHistory.push(data);
  227. uni.setStorageSync("helpHistory", state.helpHistory);
  228. }
  229. },
  230. // 缓存当前路径和分享参数
  231. setPageParams(state, payload) {
  232. state.pageParams = payload;
  233. },
  234. }
  235. export default {
  236. state,
  237. mutations,
  238. actions,
  239. getters
  240. }