在很多Flex的登錄窗口應用中,利用SharedObject可以完成記住帳號等問題。今天將給大家展示合理利用SharedObject完成的另外一個鮮為人知的秘密功能。

在流行OpenSource之前,很多Flex Component都是收費的,那收費肯定也會牽扯到Flex Component的試用。其實試用版和注冊版里面的功能是一樣的,就是多了幾個多用戶是否已經注冊的驗證判斷而已。

那我們怎么判斷用戶是否已經過期呢?請看下面的代碼(簡單是簡單了點,但其實思路上一樣的哦):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
private static var trialErrorMessage:String = "The trial period has been passed.";
private static var undefinedMessage:String = "Unknow Error!";
 
import com.richmediaplus.utils.DateUtils;
 
import flash.net.SharedObject;
import flash.net.SharedObjectFlushStatus;
 
/**
 * 驗證是否已過試用期期。
 * @return True: 超出試用期; False: 試用期內
 * @author Alvin/AedisJu
 * @blog http://blog.richmediaplus.com
 */
private function checkExpDate(className:String):Boolean {
	trialErrorMessage = className + ":: " + trialErrorMessage;
	var so:SharedObject = SharedObject.getLocal(className, "/");
	var strExpDate:Date = so.data.validateData;
 
	//調試的時候記住清空啊!
	//so.clear()
 
	//如果第一次使用,計算試用期過期時間。
	if (strExpDate == null) {
		var trailBeginDate:Date = new Date();
		//效率,讓你10秒就過期啦!
		trailBeginDate = DateUtils.dateAdd(DateUtils.SECONDS, 10, trailBeginDate);
		so.data.validateData = trailBeginDate;
 
		var flushStatus:String = null;
		try {
			flushStatus = so.flush();
		}
		catch (error:Error) {
			throw new Error(undefinedMessage);
			return true;
		}
		if (flushStatus != null) {
			switch (flushStatus) {
				case SharedObjectFlushStatus.PENDING:
					throw new Error(undefinedMessage);
					return true;
					//trace("Requesting permission to save object...")
					break;
				case SharedObjectFlushStatus.FLUSHED:
					//trace("Value flushed to disk.")
					break;
			}
		}
		return false;
	}
 
	//試用期過期時間與今天比較,如果今天已經超過試用期,則返回true。
	var strNowDate:Date = new Date();
	if (strExpDate < strNowDate) {
		return true;
	}
 
	return false;
}

判斷寫好了,那么就加到Flex Component中去吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.richmediaplus.trail
{
	import flash.utils.getQualifiedClassName;
	import mx.containers.Panel;
 
	/**
	 * TrailPanel
	 * @author Alvin/AedisJu
	 * @blog http://blog.richmediaplus.com
	 */
	public class TrailPanel extends Panel
	{
		include "trail.as"
 
		public function TrailPanel()
		{
			super();
			if(checkExpDate("com.richmediaplus.trail.TrailPanel")) {
				throw new Error(trialErrorMessage);
			}
			this.title = "Free trail product!"
		}
	}
}

加到Flex Application里面:

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:trail="com.richmediaplus.trail.*">
	<trail:TrailPanel width="400" height="300"/>
</mx:Application>

沒錯,就是這么簡單啦!
看看過期后的反應如何!

Trail Error in ActionScript3 & Flex

Trail Error in ActionScript3 & Flex

工程代碼下載:TrailExample.rar

Related posts:

  1. htmlFormat for Reduce DataBase Storage in Client
  2. TextEvent.LINK 建立安全信任網址跳轉方案
  3. hitTestTarget(source:DisplayObject, target:DisplayObject):Boolean
  4. richmediaplus.utils.AlertUtils
  5. DesktopBackgroundLayout in ActionScript