user.js 6.8 KB

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