123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import Vue from 'vue'
- import Vuex from 'vuex'
- Vue.use(Vuex) // vue的插件机制
- // 模块
- import requestList from "./requestList.js";
- // 共用工具
- import request from "@/common/request/index.js";
- import {
- appId,
- login,
- getUserProfile
- } from "@/common/device/index";
- const store = new Vuex.Store({
- state: {
- bind:0,
- face:"",
- name:""
- },
- mutations: {
- increment(state, options) {
- Object.keys(options).forEach(item => state[item] = options[item])
- }
- },
- actions: {
- // 自动登录获取用户状态
- async codeSession({
- commit,
- state
- }, options) {
- var result = await login();
- let {
- code
- } = result;
- // 相关请求
- result = await request({
- url: "/customer/code2Session/",
- method: "post",
- data: {
- code,
- appid: appId
- }
- });
- if (result.code != 0) return;
- let {
- openid,
- bind,
- customer_id = '',
- face = '',
- name = '',
- tel = '',
- token = '',
- cart_count=''
- } = result.data; //bind
- if (token) uni.setStorageSync('TOKEN', token);
- commit('increment', {
- appId,
- openid,
- cart_count,
- customer_id,
- bind,
- face,
- name,
- tel
- });
- },
- // 快捷登录
- async wxbind({
- commit,
- state
- }, options) {
- let {
- encryptedData,
- iv
- } = options;
- if( !encryptedData || !iv) return false;
- let {
- appId,
- openid
- } = state;
- var result = await request({
- url: "/customer/wxbind/",
- method: "post",
- data: {
- encryptedData,
- iv,
- appid: appId,
- openid
- }
- });
- if (result.code != 0) return;
- let {
- customer_id,
- face,
- name,
- tel,
- token
- } = result.id;
- uni.setStorageSync('TOKEN', token);
- commit('increment', {
- customer_id,
- bind: 1,
- face,
- name,
- tel,
- appId,
- openid
- });
- },
- // 用户更新资料
- async getUserProfile({
- commit,
- state
- }, options) {
- var result = await getUserProfile("完善用户登录资料!");
- let {
- encryptedData,
- iv
- } = result;
- let {
- appId,
- openid
- } = state;
- result = await request({
- method: "post",
- url: "/customer/setUserInfo/",
- data: {
- encryptedData,
- iv,
- appid: appId,
- openid
- }
- })
- if (result.code != 0) return;
- let {
- face,
- name
- } = result.data;
- // 相关请求
- commit('increment', {
- face,
- name
- });
- }
- },
- modules: {
- requestList
- },
- })
- export default store
|