user.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. // 福利类型:1=咨询服务(数量),2=课程服务,3=Al使用[咨询+文书](限次),4=Al使用[咨询+文书](限时长),5=合同下载(次数),
  28. // 6=合同审查(次数),7=诉前辅导(次数),8=文书代写[律师](次数),9=法律顾问(次数),10=应急法援(次数),11=风险查询(次数)
  29. userEquity: uni.getStorageSync("userEquity") || {}, // 权益信息
  30. imgUrl: '', //服务器图片baseUrl
  31. videoHistory: uni.getStorageSync("videoHistory") || [], //查找视频历史记录
  32. helpHistory: uni.getStorageSync("helpHistory") || [], //查找帮助历史记录
  33. pageParams: {}
  34. }
  35. // getters 可以获取 computer 进行动态刷新
  36. const getters = {
  37. dynamic_switch: state => state.dynamic_switch,
  38. alumni_switch: state => state.alumni_switch,
  39. product_switch: state => state.product_switch,
  40. token: state => state.token,
  41. isStart: state => state.isStart,
  42. isLogin: state => state.isLogin,
  43. isOneLogin: state => state.isOneLogin,
  44. userInfo: state => state.userInfo,
  45. userEquity: state => state.userEquity,
  46. imgUrl: imgURL => state.imgUrl,
  47. videoHistory: videoHistory => state.videoHistory,
  48. helpHistory: helpHistory => state.helpHistory,
  49. pageParams: pageParams => state.pageParams
  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. // dispatch('chat/start', '', {
  63. // root: true
  64. // })
  65. const user_id = uni.getStorageSync('user_id')
  66. // 登录重新配置分享信息
  67. // appShareConfig().then((res) => {
  68. // if (res.code === 1) {
  69. // const path = userInfo.user_id ?
  70. // `/pages/index/index?user_id=${userInfo.id}` :
  71. // `/pages/index/index`
  72. // const href = userInfo.user_id ?
  73. // `${this.H5_URL}/pages/h5/download?user_id=${userInfo.id}` :
  74. // `${this.H5_URL}/pages/h5/download`
  75. // res.data.forEach((item) => {
  76. // if (item.type == 2) {
  77. // // #ifdef MP-WEIXIN
  78. // uni.$u.mpShare = {
  79. // title: item.title,
  80. // path: path,
  81. // imageUrl: item.image
  82. // }
  83. // // #endif
  84. // } else if (item.type == 1) {
  85. // // #ifdef APP
  86. // uni.$u.mpShare = {
  87. // title: item.title,
  88. // path: href,
  89. // imageUrl: item.image,
  90. // summary: item.subtitle
  91. // }
  92. // // #endif
  93. // }
  94. // })
  95. // }
  96. // })
  97. // 分享绑定关系
  98. if (user_id) {
  99. bindUser({
  100. share_id: user_id
  101. }).then(res => {}).catch(err => {
  102. uni.$u.toast(err.msg);
  103. console.log(err)
  104. // 错误提示框
  105. });
  106. uni.removeStorageSync('user_id')
  107. }
  108. // #ifdef MP-WEIXIN
  109. // 获取openid
  110. if (!uni.getStorageSync('openid')) {
  111. uni.login({
  112. success(res) {
  113. Openid({
  114. code: res.code
  115. })
  116. .then((res) => {
  117. if (res.code == 1) {
  118. uni.setStorageSync('openid', res.data.openid)
  119. }
  120. })
  121. .catch((err) => {
  122. uni.$u.toast(err.msg)
  123. })
  124. }
  125. })
  126. }
  127. // #endif
  128. },
  129. // 退出登录
  130. userLogout({
  131. commit,
  132. dispatch
  133. },isPort) {
  134. if(isPort) {
  135. logout({}).then(res => {
  136. if (res.code == 1) {
  137. commit('setToken', "");
  138. commit('setLogin', false);
  139. commit('setisOneLogin', 0);
  140. commit('setUserInfo', {});
  141. uni.$u.toast(res.msg);
  142. } else {
  143. uni.$u.toast(res.msg);
  144. }
  145. }).catch(err => {
  146. uni.$u.toast(err.msg);
  147. })
  148. }else{
  149. commit('setToken', "");
  150. commit('setLogin', false);
  151. commit('setisOneLogin', 0);
  152. commit('setUserInfo', {});
  153. }
  154. // dispatch('chat/initChat', '', {
  155. // root: true
  156. // })
  157. // dispatch('chat/close', '', {
  158. // root: true
  159. // })
  160. // 重置全局分享信息
  161. // share.setShareInfo();
  162. },
  163. // 获取权益信息
  164. getUserEquity({
  165. commit,
  166. dispatch
  167. }) {
  168. getRemainNumber().then(res => {
  169. commit('setUserEquity', res.data);
  170. }).catch(err => {
  171. uni.$u.toast(err.msg);
  172. // 错误提示框
  173. });
  174. },
  175. // 更新用户信息
  176. updateUserInfo({
  177. commit,
  178. dispatch
  179. }) {
  180. return new Promise((resolve, reject) => {
  181. //调用登陆接口
  182. getUserInfo().then(res => {
  183. if (res.code == 1) {
  184. res.data.tagList = res.data.tag ? res.data.tag.split(",") : []
  185. commit('setUserInfo', res.data)
  186. resolve(res)
  187. } else {
  188. uni.$u.toast(res.msg);
  189. reject(res)
  190. }
  191. }).catch(err => {
  192. uni.$u.toast(err.msg);
  193. reject(err)
  194. })
  195. })
  196. },
  197. getPagePath({
  198. commit,
  199. dispatch
  200. }) {
  201. let curPage = getCurrentPages();
  202. let route = curPage[curPage.length - 1].route; //获取当前页面的路由
  203. let params = curPage[curPage.length - 1].options; //获取当前页面参数,如果有则返回参数的对象,没有参数返回空对象{}
  204. // 需要返回格式 /pages/XX?id=XX 打开下面的
  205. let query = '';
  206. let keys = Object.keys(params); //获取对象的key 返回对象key的数组
  207. if (keys.length > 0) {
  208. query = keys.reduce((pre, cur) => {
  209. return pre + cur + '=' + params[cur] + '&';
  210. }, '?').slice(0, -1);
  211. }
  212. return new Promise((resolve, reject) => {
  213. //调用登陆接口
  214. getShareImage({
  215. url: '/' + route
  216. }).then(res => {
  217. if (res.code == 1) {
  218. let obj = {
  219. pathObj: {
  220. url: route,
  221. params
  222. },
  223. shareImg: res.data,
  224. path:route + query
  225. }
  226. //存储信息在state中
  227. commit('setPageParams', obj)
  228. resolve(obj)
  229. }
  230. }).catch(err => {
  231. reject(err)
  232. })
  233. })
  234. }
  235. }
  236. //静态数据 方法 可更改数据
  237. const mutations = {
  238. set_dynamic_switch(state, payload) {
  239. state.dynamic_switch = +payload;
  240. uni.setStorageSync("dynamic_switch", +payload);
  241. },
  242. set_alumni_switch(state, payload) {
  243. state.alumni_switch = +payload;
  244. uni.setStorageSync("alumni_switch", +payload);
  245. },
  246. set_product_switch(state, payload) {
  247. state.product_switch = +payload;
  248. uni.setStorageSync("product_switch", +payload);
  249. },
  250. setToken(state, payload) {
  251. state.token = payload;
  252. uni.setStorageSync("token", payload);
  253. },
  254. // 开屏动画
  255. setStart(state, data) {
  256. state.isStart = data;
  257. },
  258. // 登录态
  259. setLogin(state, data) {
  260. state.isLogin = data;
  261. uni.setStorageSync('isLogin', data);
  262. },
  263. // 是否第一次登录
  264. setisOneLogin(state, data) {
  265. state.isOneLogin = data;
  266. uni.setStorageSync('isOneLogin', data);
  267. },
  268. // 用户信息
  269. setUserInfo(state, data) {
  270. state.userInfo = data;
  271. uni.setStorageSync("userInfo", data);
  272. },
  273. // 权益信息
  274. setUserEquity(state, data) {
  275. state.userEquity = data;
  276. uni.setStorageSync("userEquity", data);
  277. },
  278. //搜索视频历史记录
  279. setVideoHistory(state, data) {
  280. if (!data) {
  281. console.log(data)
  282. // let arr=[]
  283. state.videoHistory = []
  284. uni.setStorageSync("videoHistory", state.videoHistory);
  285. } else {
  286. state.videoHistory.push(data);
  287. uni.setStorageSync("videoHistory", state.videoHistory);
  288. }
  289. },
  290. //搜索帮助历史记录
  291. setHelpHistory(state, data) {
  292. if (!data) {
  293. console.log(data)
  294. // let arr=[]
  295. state.helpHistory = []
  296. uni.setStorageSync("helpHistory", state.helpHistory);
  297. } else {
  298. state.helpHistory.push(data);
  299. uni.setStorageSync("helpHistory", state.helpHistory);
  300. }
  301. },
  302. // 缓存当前路径和分享参数
  303. setPageParams(state, payload) {
  304. state.pageParams = payload;
  305. },
  306. }
  307. export default {
  308. state,
  309. mutations,
  310. actions,
  311. getters
  312. }