user.js 7.0 KB

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