user.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. uni.$u.toast("登录成功!");
  61. setTimeout(()=>{
  62. uni.redirectTo({
  63. url:'/pages/index/index'
  64. })
  65. },1500)
  66. // dispatch('chat/start', '', {
  67. // root: true
  68. // })
  69. // const user_id = uni.getStorageSync('user_id')
  70. },
  71. // 退出登录
  72. userLogout({
  73. commit,
  74. dispatch
  75. }) {
  76. // if(isPort) {
  77. // logout({}).then(res => {
  78. // if (res.code == 1) {
  79. // commit('setToken', "");
  80. // commit('setLogin', false);
  81. // commit('setisOneLogin', 0);
  82. // commit('setUserInfo', {});
  83. // uni.$u.toast(res.msg);
  84. // } else {
  85. // uni.$u.toast(res.msg);
  86. // }
  87. // }).catch(err => {
  88. // uni.$u.toast(err.msg);
  89. // })
  90. // }else{
  91. commit('setToken', "");
  92. commit('setLogin', false);
  93. commit('setisOneLogin', 0);
  94. commit('setUserInfo', {});
  95. uni.$u.toast("退出成功!");
  96. setTimeout(()=>{
  97. uni.redirectTo({
  98. url:'/pages/login/login'
  99. })
  100. },1500)
  101. // }
  102. },
  103. // 获取权益信息
  104. getUserEquity({
  105. commit,
  106. dispatch
  107. }) {
  108. getRemainNumber().then(res => {
  109. commit('setUserEquity', res.data);
  110. }).catch(err => {
  111. uni.$u.toast(err.msg);
  112. // 错误提示框
  113. });
  114. },
  115. // 更新用户信息
  116. updateUserInfo({
  117. commit,
  118. dispatch
  119. }) {
  120. return new Promise((resolve, reject) => {
  121. //调用登陆接口
  122. getUserInfo().then(res => {
  123. if (res.code == 1) {
  124. res.data.tagList = res.data.tag ? res.data.tag.split(",") : []
  125. commit('setUserInfo', res.data)
  126. resolve(res)
  127. } else {
  128. uni.$u.toast(res.msg);
  129. reject(res)
  130. }
  131. }).catch(err => {
  132. uni.$u.toast(err.msg);
  133. reject(err)
  134. })
  135. })
  136. },
  137. getPagePath({
  138. commit,
  139. dispatch
  140. }) {
  141. let curPage = getCurrentPages();
  142. let route = curPage[curPage.length - 1].route; //获取当前页面的路由
  143. let params = curPage[curPage.length - 1].options; //获取当前页面参数,如果有则返回参数的对象,没有参数返回空对象{}
  144. // 需要返回格式 /pages/XX?id=XX 打开下面的
  145. let query = '';
  146. let keys = Object.keys(params); //获取对象的key 返回对象key的数组
  147. if (keys.length > 0) {
  148. query = keys.reduce((pre, cur) => {
  149. return pre + cur + '=' + params[cur] + '&';
  150. }, '?').slice(0, -1);
  151. }
  152. return new Promise((resolve, reject) => {
  153. //调用登陆接口
  154. getShareImage({
  155. url: '/' + route
  156. }).then(res => {
  157. if (res.code == 1) {
  158. let obj = {
  159. pathObj: {
  160. url: route,
  161. params
  162. },
  163. shareImg: res.data,
  164. path:route + query
  165. }
  166. //存储信息在state中
  167. commit('setPageParams', obj)
  168. resolve(obj)
  169. }
  170. }).catch(err => {
  171. reject(err)
  172. })
  173. })
  174. },
  175. }
  176. //静态数据 方法 可更改数据
  177. const mutations = {
  178. set_dynamic_switch(state, payload) {
  179. state.dynamic_switch = +payload;
  180. uni.setStorageSync("dynamic_switch", +payload);
  181. },
  182. set_alumni_switch(state, payload) {
  183. state.alumni_switch = +payload;
  184. uni.setStorageSync("alumni_switch", +payload);
  185. },
  186. set_product_switch(state, payload) {
  187. state.product_switch = +payload;
  188. uni.setStorageSync("product_switch", +payload);
  189. },
  190. setToken(state, payload) {
  191. state.token = payload;
  192. uni.setStorageSync("token", payload);
  193. },
  194. // 开屏动画
  195. setStart(state, data) {
  196. state.isStart = data;
  197. },
  198. // 登录态
  199. setLogin(state, data) {
  200. state.isLogin = data;
  201. uni.setStorageSync('isLogin', data);
  202. },
  203. // 是否第一次登录
  204. setisOneLogin(state, data) {
  205. state.isOneLogin = data;
  206. uni.setStorageSync('isOneLogin', data);
  207. },
  208. // 用户信息
  209. setUserInfo(state, data) {
  210. state.userInfo = data;
  211. uni.setStorageSync("userInfo", data);
  212. },
  213. // 权益信息
  214. setUserEquity(state, data) {
  215. state.userEquity = data;
  216. uni.setStorageSync("userEquity", data);
  217. },
  218. //搜索视频历史记录
  219. setVideoHistory(state, data) {
  220. if (!data) {
  221. console.log(data)
  222. // let arr=[]
  223. state.videoHistory = []
  224. uni.setStorageSync("videoHistory", state.videoHistory);
  225. } else {
  226. state.videoHistory.push(data);
  227. uni.setStorageSync("videoHistory", state.videoHistory);
  228. }
  229. },
  230. //搜索帮助历史记录
  231. setHelpHistory(state, data) {
  232. if (!data) {
  233. console.log(data)
  234. // let arr=[]
  235. state.helpHistory = []
  236. uni.setStorageSync("helpHistory", state.helpHistory);
  237. } else {
  238. state.helpHistory.push(data);
  239. uni.setStorageSync("helpHistory", state.helpHistory);
  240. }
  241. },
  242. // 缓存当前路径和分享参数
  243. setPageParams(state, payload) {
  244. state.pageParams = payload;
  245. },
  246. }
  247. export default {
  248. state,
  249. mutations,
  250. actions,
  251. getters
  252. }