index.js 2.4 KB

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