index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex) // vue的插件机制
  4. // 模块
  5. import requestList from "./requestList.js";
  6. // 共用工具
  7. import request from "@/common/request/index.js";
  8. import {
  9. appId,
  10. login,
  11. getUserProfile
  12. } from "@/common/device/index";
  13. const store = new Vuex.Store({
  14. state: {},
  15. mutations: {
  16. increment(state, options) {
  17. Object.keys(options).forEach(item => state[item] = options[item])
  18. }
  19. },
  20. actions: {
  21. // 自动登录获取用户状态
  22. async codeSession({
  23. commit,
  24. state
  25. }, options) {
  26. var result = await login();
  27. let {
  28. code
  29. } = result;
  30. // 相关请求
  31. result = await request({
  32. url: "/customer/code2Session/",
  33. method: "post",
  34. data: {
  35. code,
  36. appid: appId
  37. }
  38. });
  39. if (result.code != 0) return;
  40. let {
  41. openid,
  42. bind,
  43. customer_id = '',
  44. face = '',
  45. name = '',
  46. tel = '',
  47. token = '',
  48. cart_count=''
  49. } = result.data; //bind
  50. if (token) uni.setStorageSync('TOKEN', token);
  51. commit('increment', {
  52. appId,
  53. openid,
  54. cart_count,
  55. customer_id,
  56. bind,
  57. face,
  58. name,
  59. tel
  60. });
  61. },
  62. // 快捷登录
  63. async wxbind({
  64. commit,
  65. state
  66. }, options) {
  67. let {
  68. encryptedData,
  69. iv
  70. } = options;
  71. let {
  72. appId,
  73. openid
  74. } = state;
  75. var result = await request({
  76. url: "/customer/wxbind/",
  77. method: "post",
  78. data: {
  79. encryptedData,
  80. iv,
  81. appid: appId,
  82. openid
  83. }
  84. });
  85. if (result.code != 0) return;
  86. let {
  87. customer_id,
  88. face,
  89. name,
  90. tel,
  91. token
  92. } = result.id;
  93. uni.setStorageSync('TOKEN', token);
  94. commit('increment', {
  95. customer_id,
  96. bind: 1,
  97. face,
  98. name,
  99. tel,
  100. appId,
  101. openid
  102. });
  103. },
  104. // 用户更新资料
  105. async getUserProfile({
  106. commit,
  107. state
  108. }, options) {
  109. var result = await getUserProfile("完善用户登录资料!");
  110. let {
  111. encryptedData,
  112. iv
  113. } = result;
  114. let {
  115. appId,
  116. openid
  117. } = state;
  118. result = await request({
  119. method: "post",
  120. url: "/customer/setUserInfo/",
  121. data: {
  122. encryptedData,
  123. iv,
  124. appid: appId,
  125. openid
  126. }
  127. })
  128. if (result.code != 0) return;
  129. let {
  130. face,
  131. name
  132. } = result.data;
  133. // 相关请求
  134. commit('increment', {
  135. face,
  136. name
  137. });
  138. }
  139. },
  140. modules: {
  141. requestList
  142. },
  143. })
  144. export default store