Authored by QC-L

修复首页在清除缓存的情况下失效的 bug --- review by 黄敬囿

@@ -63,7 +63,7 @@ export const setOpenId = (id) => { @@ -63,7 +63,7 @@ export const setOpenId = (id) => {
63 63
64 export const setSessionkey = (sessionKey) => { 64 export const setSessionkey = (sessionKey) => {
65 sessionKey = !sessionKey ? user.getYHStorageSync('session_key', 'app') : sessionKey; 65 sessionKey = !sessionKey ? user.getYHStorageSync('session_key', 'app') : sessionKey;
66 - console.log(sessionKey); 66 +
67 return { 67 return {
68 sessionKey, 68 sessionKey,
69 type: globalData.SESSIONKEY 69 type: globalData.SESSIONKEY
@@ -8,6 +8,8 @@ import * as globalData from './actions/globalData'; @@ -8,6 +8,8 @@ import * as globalData from './actions/globalData';
8 import udid from './utils/udid'; 8 import udid from './utils/udid';
9 import './app.scss' 9 import './app.scss'
10 import event from './utils/event'; 10 import event from './utils/event';
  11 +import { loginAction } from './libs/login/login.js'
  12 +import getPrivateKey from './libs/request/getPrivateKey.js'
11 13
12 const store = createStore(); 14 const store = createStore();
13 15
@@ -82,7 +84,7 @@ class App extends Component { @@ -82,7 +84,7 @@ class App extends Component {
82 } 84 }
83 }; 85 };
84 86
85 - componentWillMount () { 87 + componentWillMount () {
86 let {setSystemInfo, wechatLogin, setSessionkey} = this.props; 88 let {setSystemInfo, wechatLogin, setSessionkey} = this.props;
87 89
88 // 获取系统信息 90 // 获取系统信息
@@ -104,7 +106,7 @@ class App extends Component { @@ -104,7 +106,7 @@ class App extends Component {
104 this.props.setWxUnionId(); 106 this.props.setWxUnionId();
105 // this.props.getUnionTypeWithUid(this.props.globalData.userInfo.uid); // 有赚 107 // this.props.getUnionTypeWithUid(this.props.globalData.userInfo.uid); // 有赚
106 }, 108 },
107 - fail: () => { 109 + async fail: () => {
108 console.log('登录已经过期') 110 console.log('登录已经过期')
109 //登录态过期 111 //登录态过期
110 Taro.setStorage({key: 'WXThird_session', data: ''}); 112 Taro.setStorage({key: 'WXThird_session', data: ''});
1 -import { Component } from '@tarojs/taro'; 1 +import Taro, { Component } from '@tarojs/taro';
2 import { View } from '@tarojs/components'; 2 import { View } from '@tarojs/components';
3 import { common as commonModel } from '../../models'; 3 import { common as commonModel } from '../../models';
4 import Focus from '../resource-focus'; 4 import Focus from '../resource-focus';
5 import HotSeries from '../resource-hot-series'; 5 import HotSeries from '../resource-hot-series';
6 import Category from '../resource-category'; 6 import Category from '../resource-category';
7 import './index.scss'; 7 import './index.scss';
  8 +import getPrivateKey from '../../libs/request/getPrivateKey.js'
8 9
9 export default class Resources extends Component { 10 export default class Resources extends Component {
10 constructor(props) { 11 constructor(props) {
@@ -14,7 +15,9 @@ export default class Resources extends Component { @@ -14,7 +15,9 @@ export default class Resources extends Component {
14 }; 15 };
15 } 16 }
16 17
17 - componentDidMount() { 18 + async componentDidMount() {
  19 + const pk = await getPrivateKey();
  20 + Taro.setStorage({ key: 'verifyKey', data: pk });
18 commonModel.resource(this.props.code).then(res => { 21 commonModel.resource(this.props.code).then(res => {
19 if (res && res.code === 200) { 22 if (res && res.code === 200) {
20 this.setState({ 23 this.setState({
@@ -11,9 +11,13 @@ export const loginAction = (callBack) => { @@ -11,9 +11,13 @@ export const loginAction = (callBack) => {
11 // 获取 srd_session 11 // 获取 srd_session
12 const loginData = data; 12 const loginData = data;
13 handleLoginData(loginData); 13 handleLoginData(loginData);
14 - callBack(null, data); 14 + if (callBack) {
  15 + callBack(null, data);
  16 + }
15 }).catch((error) => { 17 }).catch((error) => {
16 - callBack(error); 18 + if (callBack) {
  19 + callBack(error);
  20 + }
17 }); 21 });
18 } 22 }
19 }, 23 },
@@ -65,7 +69,7 @@ export const decodeUnionId = (srd_session, result) => { @@ -65,7 +69,7 @@ export const decodeUnionId = (srd_session, result) => {
65 } else { 69 } else {
66 union_id = globalData.union_id; 70 union_id = globalData.union_id;
67 } 71 }
68 - console.log('+++++++++++++++', union_id); 72 +
69 if (!union_id) { 73 if (!union_id) {
70 throw new Error('union_id is null'); 74 throw new Error('union_id is null');
71 } 75 }
@@ -158,7 +162,7 @@ export const getUserInfo = (e) => { @@ -158,7 +162,7 @@ export const getUserInfo = (e) => {
158 if (error) return; 162 if (error) return;
159 decodeUnionId(loginData.srd_session, e).then(data => { 163 decodeUnionId(loginData.srd_session, e).then(data => {
160 wechatUserIsBind(data.union_id, data.userInfo).then(message => { 164 wechatUserIsBind(data.union_id, data.userInfo).then(message => {
161 - console.log(message); 165 +
162 if (message.code === 10003) { 166 if (message.code === 10003) {
163 event.emit('user-login-success'); 167 event.emit('user-login-success');
164 setTimeout(() => { 168 setTimeout(() => {
@@ -252,18 +256,13 @@ export const getPhoneNumber = (e) => { @@ -252,18 +256,13 @@ export const getPhoneNumber = (e) => {
252 export const getLoginButtonType = () => { 256 export const getLoginButtonType = () => {
253 const globalData = getGlobalData(); 257 const globalData = getGlobalData();
254 258
255 - console.log(globalData);  
256 if (globalData && globalData.userInfo && globalData.userInfo.uid) { 259 if (globalData && globalData.userInfo && globalData.userInfo.uid) {
257 return ''; // 已经是登录状态,合法用户 260 return ''; // 已经是登录状态,合法用户
258 } 261 }
259 - console.log(globalData); 262 +
260 let isUnionID = globalData.userInfo && globalData.userInfo.union_id;// 如果有unionID情况 263 let isUnionID = globalData.userInfo && globalData.userInfo.union_id;// 如果有unionID情况
261 let isPhoneNumber = isUnionID ? !(globalData.userInfo && globalData.userInfo.uid) : false; // 如果有unionID且没有uid,可以获取手机号自动绑定登录 264 let isPhoneNumber = isUnionID ? !(globalData.userInfo && globalData.userInfo.uid) : false; // 如果有unionID且没有uid,可以获取手机号自动绑定登录
262 265
263 - console.log(isUnionID);  
264 - console.log(isPhoneNumber);  
265 - console.log(globalData.userInfo.uid);  
266 - console.log(globalData.userInfo.union_id);  
267 if (!isUnionID) { 266 if (!isUnionID) {
268 event.emit('change-login-status', { 267 event.emit('change-login-status', {
269 text: '微信登录', 268 text: '微信登录',
@@ -5,6 +5,7 @@ import {Resources} from '../../components'; @@ -5,6 +5,7 @@ import {Resources} from '../../components';
5 import {classify as classifyModel} from '../../models'; 5 import {classify as classifyModel} from '../../models';
6 import {getImgUrl} from '../../utils'; 6 import {getImgUrl} from '../../utils';
7 import './index.scss'; 7 import './index.scss';
  8 +import getPrivateKey from '../../libs/request/getPrivateKey.js'
8 9
9 export default class Classify extends Component { 10 export default class Classify extends Component {
10 constructor() { 11 constructor() {
@@ -20,7 +21,9 @@ export default class Classify extends Component { @@ -20,7 +21,9 @@ export default class Classify extends Component {
20 navigationBarTitleText: '分类' 21 navigationBarTitleText: '分类'
21 } 22 }
22 23
23 - componentDidMount () { 24 + async componentDidMount () {
  25 + const pk = await getPrivateKey();
  26 + Taro.setStorage({ key: 'verifyKey', data: pk });
24 classifyModel.getBrandList().then(ret => { 27 classifyModel.getBrandList().then(ret => {
25 if (ret && ret.code === 200) { 28 if (ret && ret.code === 200) {
26 this.setState({ 29 this.setState({
@@ -7,6 +7,7 @@ import {common as commonModel} from '../../models'; @@ -7,6 +7,7 @@ import {common as commonModel} from '../../models';
7 import './index.scss'; 7 import './index.scss';
8 import { connect } from '@tarojs/redux'; 8 import { connect } from '@tarojs/redux';
9 import { loginAction, decodeUnionId, wechatUserIsBind } from '../../libs/login/login.js' 9 import { loginAction, decodeUnionId, wechatUserIsBind } from '../../libs/login/login.js'
  10 +import getPrivateKey from '../../libs/request/getPrivateKey.js'
10 11
11 12
12 @connect(({ filterMenu }) => ({ 13 @connect(({ filterMenu }) => ({
@@ -42,74 +43,77 @@ export default class Index extends Component { @@ -42,74 +43,77 @@ export default class Index extends Component {
42 navigationBarTitleText: 'UFO' 43 navigationBarTitleText: 'UFO'
43 }; 44 };
44 45
45 - componentDidMount() {  
46 - let obj = {  
47 - page: {},  
48 - productList: {},  
49 - stopLoading: {}  
50 - };  
51 -  
52 - // 初始化数据  
53 - this.state.tabs.map(item => {  
54 - let type = item.type;  
55 -  
56 - obj.page[type] = 1;  
57 - obj.productList[type] = [];  
58 - obj.stopLoading[type] = false;  
59 - });  
60 -  
61 - this.setState(obj, () => {  
62 - this.getProductData();  
63 - });  
64 - }  
65 -  
66 - componentWillReceiveProps (nextProps) {  
67 - let curType = nextProps.filterMenu.curType;  
68 - let {productList} = this.state;  
69 -  
70 - if (productList[curType].length === 0) {  
71 - this.getProductData(nextProps.filterMenu);  
72 - } 46 + componentDidMount() {
  47 + let obj = {
  48 + page: {},
  49 + productList: {},
  50 + stopLoading: {}
  51 + };
  52 +
  53 + // 初始化数据
  54 + this.state.tabs.map(item => {
  55 + let type = item.type;
  56 +
  57 + obj.page[type] = 1;
  58 + obj.productList[type] = [];
  59 + obj.stopLoading[type] = false;
  60 + });
  61 +
  62 + this.setState(obj, () => {
  63 + this.getProductData();
  64 + });
  65 + }
  66 +
  67 + componentWillReceiveProps(nextProps) {
  68 + let curType = nextProps.filterMenu.curType;
  69 + let { productList } = this.state;
  70 +
  71 + if (productList[curType].length === 0) {
  72 + this.getProductData(nextProps.filterMenu);
73 } 73 }
74 -  
75 - getProductData(obj) {  
76 - let {filterMenu} = this.props;  
77 - let {page, productList, stopLoading} = this.state;  
78 - obj = obj || filterMenu;  
79 - let type = obj.curType;  
80 -  
81 - commonModel.search({  
82 - limit: 10,  
83 - type: type,  
84 - page: page[type]  
85 - }).then(ret => {  
86 - if (ret && ret.code === 200) {  
87 - let list = ret.data && ret.data.product_list || [];  
88 -  
89 - if (list.length === 0) {  
90 - this.setState({  
91 - stopLoading: Object.assign(stopLoading, {[type]: true})  
92 - });  
93 - } else {  
94 - this.setState({  
95 - productList: Object.assign(productList, {[type]: [...productList[type], ...list]})  
96 - });  
97 - }  
98 - }  
99 - })  
100 - }  
101 -  
102 - onScrollToLower() {  
103 - let {filterMenu} = this.props;  
104 - let {page} = this.state;  
105 - let type = filterMenu.curType;  
106 -  
107 - if (!this.state.stopLoading[type]) {  
108 - this.setState({page: Object.assign(page, {[type]: page[type] + 1})}, () => {  
109 - this.getProductData();  
110 - }); 74 + }
  75 +
  76 + async getProductData(obj) {
  77 + let { filterMenu } = this.props;
  78 + let { page, productList, stopLoading } = this.state;
  79 + obj = obj || filterMenu;
  80 + let type = obj.curType;
  81 +
  82 + const pk = await getPrivateKey();
  83 + Taro.setStorage({ key: 'verifyKey', data: pk });
  84 + commonModel.search({
  85 + limit: 10,
  86 + type: type,
  87 + page: page[type]
  88 + }).then(ret => {
  89 + console.log(ret);
  90 + if (ret && ret.code === 200) {
  91 + let list = ret.data && ret.data.product_list || [];
  92 +
  93 + if (list.length === 0) {
  94 + this.setState({
  95 + stopLoading: Object.assign(stopLoading, { [type]: true })
  96 + });
  97 + } else {
  98 + this.setState({
  99 + productList: Object.assign(productList, { [type]: [...productList[type], ...list] })
  100 + });
111 } 101 }
112 - } 102 + }
  103 + })
  104 + }
  105 +
  106 + onScrollToLower() {
  107 + let { filterMenu } = this.props;
  108 + let { page } = this.state;
  109 + let type = filterMenu.curType;
  110 +
  111 + if (!this.state.stopLoading[type]) {
  112 + this.setState({ page: Object.assign(page, { [type]: page[type] + 1 }) }, () => {
  113 + this.getProductData();
  114 + });
  115 + }
  116 + }
113 117
114 // gotoNative() { 118 // gotoNative() {
115 // Taro.navigateTo({ 119 // Taro.navigateTo({
@@ -16,7 +16,6 @@ Page({ @@ -16,7 +16,6 @@ Page({
16 * 生命周期函数--监听页面加载 16 * 生命周期函数--监听页面加载
17 */ 17 */
18 onLoad: function (options) { 18 onLoad: function (options) {
19 - console.log(options);  
20 let that = this; 19 let that = this;
21 event.on('user-login-success', () => { 20 event.on('user-login-success', () => {
22 // 返回上一页 21 // 返回上一页
@@ -66,7 +65,6 @@ Page({ @@ -66,7 +65,6 @@ Page({
66 */ 65 */
67 onShow: function () { 66 onShow: function () {
68 const buttonType = getLoginButtonType(); 67 const buttonType = getLoginButtonType();
69 - console.log('buttonType: ', buttonType);  
70 this.setData({ 68 this.setData({
71 loginButtonType: buttonType 69 loginButtonType: buttonType
72 }); 70 });
@@ -123,11 +123,9 @@ const api = { @@ -123,11 +123,9 @@ const api = {
123 123
124 return resultData; 124 return resultData;
125 } else { 125 } else {
126 - console.error('api statusCode:', result.statusCode);  
127 return result && result.data || {}; 126 return result && result.data || {};
128 } 127 }
129 }).catch(err => { 128 }).catch(err => {
130 - console.error(err);  
131 return {}; 129 return {};
132 }); 130 });
133 }, 131 },