德国人真的有点死板。。。只给JS的api,不给REST API
接入的是http://www.meinvz.net/
1,引入JS文件和初始化
1 <script type="text/javascript"
2 src="{{ media_url }}Res/jquery.js">
3 </script>
4 <script type="text/javascript"
5 src="http://imgs3.plinga.de/general/easyXDM.min.js?v=1">
6 </script>
7 <script type="text/javascript"
8 src="http://imgs3.plinga.de/plingaRpc/vz/plingaRpc.js?v=1">
9 </script>
10 <script type="text/javascript">
11 easyXDM.DomHelper.requiresJSON("http://imgs3.plinga.de.s3.amazonaws.com/general/json2.min.js?v=1");
12 </script>
13 <script type="text/javascript">
14 plingaRpc.init();
15 //plingaRpc.adjustHeight();
16 </script>
2,新鲜事的JS函数
function sendUserFeed(feedType, sns_id, style, val){
var feedSettings;
var images = [{"src":"{{ media_url }}images/newfeed/login.gif",
"href":"{{ sns_url }}"}];
var content;
var title_name = ["", ""];
var body_data = "";
.....
feeds_text = content[0] + "Sie" + content[1] + content[2] + content[3];
feeds_linktext = content[4];
feeds_link = images[0].href;
feeds_pic = images[0].src;
bundleData = {
"title":title_name[1],
"body":feeds_text,
"pic":feeds_pic,
"link":feeds_link,
"linktext":feeds_linktext
};
plingaRpc.post(bundleData);
};
3,获取好友的JS函数
额外请求一次,获得用户好友的游戏数据返回给前端
注意:要同步,而不是异步
function getFriendList(){
var friends = plingaRpc.getFriends()
var friend_data = new Array()
var return_data = {};
var request_data = {};
for(var i=0;i<friends.length;i++){
the_data = new Array(friends[i].uid, friends[i].firstname+" "+friends[i].lastname, friends[i].thumbnailurl)
friend_data[i] = the_data;
}
var request_data = {
info:friend_data,
};
//ajax get user_game_info
$.ajax({
url: "{{ sns_url }}ajax_firends_list/",
type: "POST",
dataType:"json",
async:false,
data: request_data,
success: function(res){return_data=res}
});
return return_data
}
对应服务器函数
def ajax_firends_list(request):
infos = request.POST.lists()
user_infos = {}
for info in infos:
user_infos[info[1][0]] = (info[1][1], info[1][2])
uids = user_infos.keys()
friend_sns_ids = []
friend_list = []
id_uid = {}
for uid in uids:
try:
plinga_sns = PlingaSnsid.objects.get(userid=uid)
except:
plinga_sns = None
if plinga_sns is not None:
friend_sns_ids.append(plinga_sns.id)
id_uid[plinga_sns.id] = plinga_sns.userid
friends = get_members_by_sns_ids(friend_sns_ids)
for friend in friends:
try:
level, remain_exp, next_level_exp = get_almanac_level_info(friend.id)
data = {'id':friend.id,
'sns_id':friend.sns_id,
'nickname':user_infos[id_uid[friend.id]][0],
'avatar':user_infos[id_uid[friend.id]][1],
'almanac_level':level}
friend_list.append(data)
except:
import sys
print sys.exc_info()
friend_list.sort(lambda x,y: cmp(y['almanac_level'], x['almanac_level']))
res_str = simplejson.dumps(friend_list)
return HttpResponse(res_str)
4,获得用户信息的JS函数
同样,要把用户的头像和昵称存起来
function getUserInfo() {
var user = plingaRpc.getOwner();
var user_info = {"nickname": user.firstname + " " + user.lastname,
"avatar": user.thumbnailurl,
"skey": "{{ skey }}"
};
$.ajax({
url: "{{ sns_url }}ajax_set_user/",
type: "POST",
data: user_info
});
return user_info
}
对应服务器函数
def ajax_set_user(request):
skey = request.POST.get('skey', '')
nickname = request.POST.get('nickname', '')
avatar = request.POST.get('avatar', '')
member = login_sns_user(request, skey)
if not member:
return HttpResponse('not exists user')
else:
if nickname != "" and avatar != "":
if member.nickname != nickname or member.avatar != avatar:
member.nickname = nickname
member.avatar = avatar
member.save()
return HttpResponse('ok')
5,充值
plingaRpc.initCoins('woodcoins');
调用上述JS函数,调出充值页面
回调函数
"""
post_dict = {u'price': [u'0.00'], u'coins': [u'100'], u'userid': [u'sandbox.developer.studivz.net:jjhTTcnJUWyMBj3TlGvKPA'], u'game': [u'BubbleFish'], u'ts': [u'1312797333'], u'currency': [u'EUR'], u'vcurrency': [u'coins'], u'signature': [u'b75c110666b555f4e0abefe2047777ad'], u'tid': [u'PlingaTestpay_101_131279733238370']}
"""
def _generate_sig(params):
hasher = hashlib.md5(''.join(['%s=%s&' % (str(x), str(params[x])) for x in sorted(params.keys()) if x != 'signature']))
hasher.update(settings.SNS_SECRET_KEY)
return hasher.hexdigest()
def callback(request):
post_dict = request.POST.copy()
plinga_sig = request.POST.get('signature', '')
my_sig = _generate_sig(post_dict)
if my_sig != plinga_sig:
return HttpResponse("sig_error")
userid = request.POST.get('userid', '')
pearls = request.POST.get('coins', '0')
pearls = int(pearls)
rmb = request.POST.get('price', '0')
rmb = Decimal(rmb)
user = PlingaSnsid.objects.get(userid=userid)
member = get_member_by_sns_id(user.id)
record = RechargeRecord(member_id=member.id,
pearls=pearls,
rmb=rmb,
status='UP')
result = finish_payment(record)
if result:
return HttpResponse("*OK*")
else:
return HttpResponse("pay_error")
6,用户SNS_ID的处理
plinga的用户ID是字符串的,不同于其他平台的纯数字,要处理一下
class PlingaSnsid(models.Model):
userid = models.CharField(max_length=128, unique=True)
class Meta:
db_table = 'sns_plingasnsid'
#稍微改动下这个函数
def get_sns_id(skey):
sessionid, userid, session_key = skey.split(PLINGA_SPLIT)
secret_key = settings.SNS_SECRET_KEY
my_sig = hashlib.md5(userid + session_key + secret_key).hexdigest()
if sessionid == my_sig:
user = PlingaSnsid.objects.filter(userid=userid)
if not user:
_user = PlingaSnsid(userid=userid)
_user.save()
return _user.id
else:
return user[0].id
else:
return 'SNS_API_ERROR'