店播抖音版小程序

index.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import { req,rootDocment } from '../config/util.js'
  4. import { api } from '../config/api.js'
  5. Vue.use(Vuex)
  6. Vue.prototype.$req = req
  7. Vue.prototype.$api = api
  8. var md5 = require('../config/md5.js');
  9. const store = new Vuex.Store({
  10. state: {
  11. hasLogin: false,//是否登录
  12. rootDocment: rootDocment,//域名
  13. loginProvider: "",//登录的平台 - 目前只是微信小程序
  14. userInfo: null,//用户基础信息详情
  15. systemInfo:{},//系统信息
  16. brandCategoryList:[],//品牌分类数据
  17. colorIndex: 0,
  18. colorList: ['#FF0000', '#00FF00', '#0000FF'],
  19. noMatchLeftWindow: true,
  20. active: 'componentPage',
  21. leftWinActive: '/pages/component/view/view',
  22. activeOpen: '',
  23. menu: [],
  24. univerifyErrorMsg: ''
  25. },
  26. mutations: {
  27. login(state, provider) {
  28. state.hasLogin = true;
  29. state.loginProvider = provider;
  30. },
  31. setSystemInfo(state,data){//设置系统信息
  32. state.systemInfo = data
  33. },
  34. logout(state) {
  35. state.hasLogin = false
  36. state.openid = null
  37. },
  38. setOpenid(state, openid) {
  39. state.openid = openid
  40. },
  41. setColorIndex(state, index) {
  42. state.colorIndex = index
  43. },
  44. setMatchLeftWindow(state, matchLeftWindow) {
  45. state.noMatchLeftWindow = !matchLeftWindow
  46. },
  47. setActive(state, tabPage) {
  48. state.active = tabPage
  49. },
  50. setLeftWinActive(state, leftWinActive) {
  51. state.leftWinActive = leftWinActive
  52. },
  53. setActiveOpen(state, activeOpen) {
  54. state.activeOpen = activeOpen
  55. },
  56. setMenu(state, menu) {
  57. state.menu = menu
  58. },
  59. setUniverifyLogin(state, payload) {
  60. typeof payload !== 'boolean' ? payload = !!payload : '';
  61. state.isUniverifyLogin = payload;
  62. },
  63. setUniverifyErrorMsg(state,payload = ''){
  64. state.univerifyErrorMsg = payload
  65. }
  66. },
  67. getters: {
  68. currentColor(state) {
  69. return state.colorList[state.colorIndex]
  70. }
  71. },
  72. actions: {
  73. // lazy loading openid
  74. //loginInfo {'encrypted_data':encrypted_data,'iv':iv,'provider':provider}
  75. getUserInfo_login: async function({commit,state},loginInfo) {
  76. return await new Promise((resolve, reject) => {
  77. console.log((loginInfo.iv),'loginInfo')
  78. uni.login({
  79. 'provider':loginInfo.provider,
  80. success: (data) => {
  81. if(data.code){
  82. req(api.LOGIN,'get',{
  83. 'code': data.code,
  84. 'iv': loginInfo.iv,
  85. 'encrypted_data': loginInfo.encryptedData,
  86. },function(res){
  87. if(res && res.errno == 0){
  88. commit('login')
  89. uni.setStorageSync('session_key',res.rst.session_key)
  90. uni.setStorageSync('isLogin',true)
  91. state.userInfo = res.rst.info
  92. uni.setStorageSync('admin_id',res.rst.info.id)
  93. uni.setStorageSync('ttl',res.rst.ttl)
  94. var sign=md5.hex_md5("random="+res.rst.random+"&ttl="+res.rst.ttl+"&user_id="+res.rst.info.id);
  95. uni.setStorageSync('sign',sign)
  96. resolve()
  97. }else{
  98. reject({err:'interface',info:res.err})
  99. }
  100. })
  101. }else{
  102. reject({err:'code获取失败'})
  103. }
  104. },
  105. fail: (err) => {
  106. console.log('uni.login 接口调用失败,将无法正常使用开放接口等服务', err)
  107. reject({err:'uni.login失败',info:err})
  108. }
  109. })
  110. })
  111. },
  112. login_verified: function({commit}) {//过审核接口
  113. return new Promise((resolve, reject) => {
  114. req(api.verified,'get',{
  115. 'version': 'v6.7',
  116. },function(res){
  117. if(res && res.errno == 0){
  118. commit('login')
  119. uni.setStorageSync('session_key',res.rst.session_key)
  120. uni.setStorageSync('isLogin',true)
  121. state.userInfo = res.rst.data
  122. uni.setStorageSync('admin_id',res.rst.data.id)
  123. uni.setStorageSync('ttl',res.rst.ttl)
  124. var sign=md5.hex_md5("random="+res.rst.random+"&ttl="+res.rst.ttl+"&user_id="+res.rst.data.id);
  125. uni.setStorageSync('sign',sign)
  126. resolve()
  127. }else if(res.errno == 9014){
  128. reject()
  129. }
  130. },(err)=>{
  131. reject()
  132. })
  133. })
  134. },
  135. login_first:function({commit,state}){//首次进入,登录
  136. return new Promise((resolve, reject) => {
  137. uni.login({
  138. success: (data) => {
  139. req(api.loginByUnionId,'get',{
  140. 'code': data.code,
  141. },function(res){
  142. if(res && res.errno == 0){
  143. commit('login')
  144. uni.setStorageSync('session_key',res.rst.session_key)
  145. uni.setStorageSync('isLogin',true)
  146. state.userInfo = res.rst.info
  147. uni.setStorageSync('admin_id',res.rst.info.id)
  148. uni.setStorageSync('ttl',res.rst.ttl)
  149. var sign=md5.hex_md5("random="+res.rst.random+"&ttl="+res.rst.ttl+"&user_id="+res.rst.info.id);
  150. uni.setStorageSync('sign',sign)
  151. resolve()
  152. }else{
  153. }
  154. },(err)=>{
  155. })
  156. }
  157. })
  158. })
  159. }
  160. }
  161. })
  162. export default store