本文目錄一覽:
- 1、C語言紅包代碼
- 2、求 ecshop 一次使用多個紅包的功能的代碼!!!!
- 3、C語言編程:求一段發紅包的代碼。(隨機數,能規定紅包總錢數總人數,每個人拿到的錢不為0)
- 4、C語言 微信紅包
- 5、JSON解析。
- 6、微信紅包PHP怎麼開發
C語言紅包代碼
#includestdio.h
#includestdlib.h
#includetime.h
main()
{
int i,n;
float a[100],all;
srand(time(NULL));
printf(“請輸入紅包金額:”);
scanf(“%f”,all);
printf(“請輸入紅包個數:”);
scanf(“%d”,n);
srand((unsigned)time(0));
for(i=1;in;i)
{
a[i]=(float)rand()/RAND_MAX*all;
if(a[i]0 )
{
all-=a[i];
printf(“%f\n”,a[i]);
i++;
}
}
printf(“%f\n”,all);
}
求 ecshop 一次使用多個紅包的功能的代碼!!!!
步驟
1) 添加一種新的紅包類型4 ,
文件 admin/templates/bonus_type_info.htm
找到 input type=”radio” name=”send_type” value=”0″ {if $bonus_arr.send_type eq 0} checked=”true” {/if} onClick=”showunit(0)” /{$lang.send_by[0]}
input type=”radio” name=”send_type” value=”1″ {if $bonus_arr.send_type eq 1} checked=”true” {/if} onClick=”showunit(1)” /{$lang.send_by[1]}
input type=”radio” name=”send_type” value=”2″ {if $bonus_arr.send_type eq 2} checked=”true” {/if} onClick=”showunit(2)” /{$lang.send_by[2]}
input type=”radio” name=”send_type” value=”3″ {if $bonus_arr.send_type eq 3} checked=”true” {/if} onClick=”showunit(3)” /{$lang.send_by[3]}
再其後面添加
input type=”radio” name=”send_type” value=”4″ {if $bonus_arr.send_type eq 4} checked=”true” {/if} onClick=”showunit(4)” /通用紅包 多次使用
2) 生成這類紅包字符串
增加文件 admin/templates/bonus_by_print_phpsir.htm
修改文件 admin/bonus.php 找到
elseif ($_REQUEST[‘send_by’] == SEND_BY_PRINT)
{
$smarty-assign(‘type_list’, get_bonus_type());
$smarty-display(‘bonus_by_print.htm’);
}
再其後添加
elseif ($_REQUEST[‘send_by’] == 4)
{
$smarty-assign(‘type_list’, get_bonus_type_phpsir());
$smarty-display(‘bonus_by_print_phpsir.htm’);
}
3) 增加 get_bonus_type_phpsir 函數
文件 admin/includes/lib_main.php
function get_bonus_type_phpsir()
{
$bonus = array();
$sql = ‘SELECT type_id, type_name, type_money FROM ‘ . $GLOBALS[‘ecs’]-table(‘bonus_type’) .
‘ WHERE send_type = 4’;
$res = $GLOBALS[‘db’]-query($sql);
while ($row = $GLOBALS[‘db’]-fetchRow($res))
{
$bonus[$row[‘type_id’]] = $row[‘type_name’].’ [‘ .sprintf($GLOBALS[‘_CFG’][‘currency_format’], $row[‘type_money’]).’]’;
}
return $bonus;
}
4) 在 bonus.php 裏面 找到
if ($_REQUEST[‘act’] == ‘send_by_print’)
{
………………………
}
再其後面添加,處理增加這類紅包時候生成方法
if ($_REQUEST[‘act’] == ‘send_by_print_phpsir’)
{
@set_time_limit(0);
/* 紅下紅包的類型ID和生成的數量的處理 */
$bonus_typeid = !empty($_POST[‘bonus_type_id’]) ? $_POST[‘bonus_type_id’] : 0;
$bonus_sum = !empty($_POST[‘bonus_sum’]) ? $_POST[‘bonus_sum’] : 1;
/* 生成紅包序列號 */
for ($i = 0, $j = 0; $i $bonus_sum; $i++)
{
$bonus_sn = $_POST[‘bonus_txt’];
$db-query(“INSERT INTO “.$ecs-table(‘user_bonus’).” (bonus_type_id, bonus_sn) VALUES(‘$bonus_typeid’, ‘$bonus_sn’)”);
$j++;
}
/* 記錄管理員操作 */
admin_log($bonus_sn, ‘add’, ‘userbonus’);
/* 清除緩存 */
clear_cache_files();
/* 提示信息 */
$link[0][‘text’] = $_LANG[‘back_bonus_list’];
$link[0][‘href’] = ‘bonus.php?act=bonus_listbonus_type=’ . $bonus_typeid;
sys_msg($_LANG[‘creat_bonus’] . $j . $_LANG[‘creat_bonus_num’], 0, $link);
}
5) 修改 bonus.php 讓後台顯示紅包內容
if ($_REQUEST[‘act’] == ‘bonus_list’)
{
………………………
}
和
if ($_REQUEST[‘act’] == ‘query_bonus’)
{
………………………
}
裏面增加
if ($bonus_type[‘send_type’] == 4)
{
$smarty-assign(‘show_bonus_sn’, 1);
}
至此 後台部分完成
前台部分 修改
includes/lib_order.php
function bonus_info($bonus_id, $bonus_sn = ”)
{
$sql = “SELECT t.*, b.* ” .
“FROM ” . $GLOBALS[‘ecs’]-table(‘bonus_type’) . ” AS t,” .
$GLOBALS[‘ecs’]-table(‘user_bonus’) . ” AS b ” .
“WHERE t.type_id = b.bonus_type_id “;
if ($bonus_id 0)
{
$sql .= ” AND b.bonus_id = ‘$bonus_id'”;
$row = $GLOBALS[‘db’]-getRow($sql);
return $row;
}
else
{
$sql .= ” AND b.bonus_sn = ‘$bonus_sn'”;
$row = $GLOBALS[‘db’]-getRow($sql);
}
if($row[‘send_type’] == 4) // phpsir 如果是第4類型紅包,那麼就找一個未使用的紅包,這種紅包可被多次使用,不限制每人使用次數
{
$sess_userid = $_SESSION[“user_id”];
$sql = “SELECT t.*, b.* ” .
“FROM ” . $GLOBALS[‘ecs’]-table(‘bonus_type’) . ” AS t,” .
$GLOBALS[‘ecs’]-table(‘user_bonus’) . ” AS b ” .
“WHERE t.type_id = b.bonus_type_id and b.used_time = 0 “;
if ($bonus_id 0)
{
$sql .= “AND b.bonus_id = ‘$bonus_id'”;
}
else
{
$sql .= “AND b.bonus_sn = ‘$bonus_sn'”;
}
//print $sql;
$row = $GLOBALS[‘db’]-getRow($sql);
//var_dump($row);
return $row;
}
// 如果想每人只使用N次,請用下面的部分
/*
if($row[‘send_type’] == 4) // phpsir 如果是第4類型紅包,那麼就找一個未使用的紅包,這種紅包可被多次使用,不限制每人使用次數
{
$sess_userid = $_SESSION[“user_id”];
$sql = “SELECT t.*, b.* ” .
“FROM ” . $GLOBALS[‘ecs’]-table(‘bonus_type’) . ” AS t,” .
$GLOBALS[‘ecs’]-table(‘user_bonus’) . ” AS b ” .
“WHERE t.type_id = b.bonus_type_id and b.user_id = ‘$sess_userid’ and b.bonus_sn = ‘$bonus_sn’ “;
$rows = $GLOBALS[‘db’]-getAll($sql);
$allow_used_bonus_num = 2; // 最大允許使用次數
if(count($rows) = $allow_used_bonus_num )
{
return false;
}else{
$sql = “SELECT t.*, b.* ” .
“FROM ” . $GLOBALS[‘ecs’]-table(‘bonus_type’) . ” AS t,” .
$GLOBALS[‘ecs’]-table(‘user_bonus’) . ” AS b ” .
“WHERE t.type_id = b.bonus_type_id and b.used_time = 0 and b.bonus_sn = ‘$bonus_sn’ “;
$row = $GLOBALS[‘db’]-getRow($sql);
return $row;
}
}
*/
return $row;
}
注意不要用記事本修改php文件,建議下載dreamweaver
C語言編程:求一段發紅包的代碼。(隨機數,能規定紅包總錢數總人數,每個人拿到的錢不為0)
#includestdio.h
#includetime.h
#includestdlib.h
int main(void)
{
float total;
printf(“輸入總錢數:\n”);
scanf(“%f”,total);
int num;
printf(“輸入紅包數量:\n”);
scanf(“%d”,num);
float min=0.01;
float safe_total;
float money;
int i;
srand((unsigned)time(NULL));
for(i=1;inum;i++){
safe_total=(total-(num-i)*min)/(num-1);
money=(float)(rand()%((int)(safe_total*100)))/100+min;
total=total-money;
printf(“紅包%2d: %.2f元,餘額:%.2f元\n”,i,money,total);
}
printf(“紅包%2d: %.2f元,餘額:0.00元\n”,num,total);
return 0;
}
C語言 微信紅包
#include stdio.h
#include string.h
#include time.h
#define MAX_TOTAL_MONEY 200 //紅包的最大金額
#define MIN_PER_PLAYER 1 //一個人搶到的的最小面額1元
#define MAX_PLAYER_CNT (MAX_TOTAL_MONEY/MIN_PER_PLAYER) //最大搶紅包的遊戲人數
typedef struct player
{
char *name;//標記玩家 可以不填
unsigned int money_get;//搶到的紅包
}PLAYER_T;
//每個人領取到的紅包金額不等 這個要求比較難搞 暫時不考慮
int main(int argc, char *argv[])
{
unsigned int total_money = 0; //不考慮角和分 浮點運算比較複雜
unsigned int player_cnt = 0;
int on_off = 0;
int i = 0;
int j = 0;
PLAYER_T player[MAX_PLAYER_CNT] = {0};
PLAYER_T tmp = {0};
printf(“輸入紅包金額:\n”);
scanf(“%u”, total_money);
printf(“輸入遊戲人數:\n”);
scanf(“%u”, player_cnt);
printf(“是否需要減小貧富差距(0為關閉其餘為開啟):\n”);
scanf(“%u”, on_off);
//不符合規則的輸入判斷
if (total_money MAX_TOTAL_MONEY || 0 == total_money || 0 == player_cnt || player_cnt*MIN_PER_PLAYER total_money)
{
printf(“紅包金額最小%u元 最大%u元 遊戲人數最小1人 最大%u人\n”, MIN_PER_PLAYER, MAX_TOTAL_MONEY, MAX_PLAYER_CNT);
return 0;
}
for (i = 0; i player_cnt; i++)
{
//設置隨機種子
srand(time(NULL)+i);
//根據隨機種子獲取一個偽隨機數作為搶到的紅包 並通過余運算使其始終小於total_money
player[i].money_get = rand()%total_money;
//限制所有人所能搶到的最大紅包為當前金額池的1/5而不是全部
if (0 != on_off)
{
if (total_money 5)//5塊錢以上再限制
{
player[i].money_get = rand()%(total_money/5);
}
}
//最後一個人拿所有剩下的紅包
if (player_cnt – 1 == i)
{
player[i].money_get = total_money;
}
//運氣差隨機到0元 給你最小面額
else if (0 == player[i].money_get)
{
player[i].money_get = MIN_PER_PLAYER;
}
//剩下的要保證每個人能搶到最小面額
else if (total_money – player[i].money_get (player_cnt-i-1)*MIN_PER_PLAYER)
{
player[i].money_get = total_money – (player_cnt-i-1)*MIN_PER_PLAYER;
}
//把搶到的金額從紅包池中減掉
total_money -= player[i].money_get;
//如果填了name 可以把名字打印出來
printf(“第%d個玩家搶到紅包:%u元\n”, i+1, player[i].money_get);
}
//冒泡排序 找出手氣最佳者
for (i = 0; i player_cnt; i++)
{
for (j = i+1; j player_cnt; j++)
{
if (player[i].money_get player[j].money_get)
{
memcpy(tmp, player[j], sizeof(PLAYER_T));
memcpy(player[j], player[i], sizeof(PLAYER_T));
memcpy(player[i], tmp, sizeof(PLAYER_T));
}
}
}
printf(“手氣最佳者搶到紅包:%u元\n”, player[0].money_get);//如果填了name 可以把名字打印出來
return 0;
}
JSON解析。
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class BMI
{
public static void main(String[] args)
{
String res =
“{\”showapi_res_code\”:0,\”showapi_res_error\”:\”\”,\”showapi_res_body\”:{\”ret_code\”:0,\”basic\”:{\”explains\”:[\”n. 代碼,密碼;編碼;法典\”,\”vt. 編碼;製成法典\”,\”vi. 指定遺傳密碼\”]}}}”;
JSONObject obj = JSONObject.fromString(res);
JSONObject body = obj.getJSONObject(“showapi_res_body”);
JSONObject basic = body.getJSONObject(“basic”);
JSONArray explains = basic.getJSONArray(“explains”);
for(int i = 0; i explains.length(); i++)
{
System.out.println(explains.get(i));
}
}
}
微信紅包PHP怎麼開發
代碼有兩個php文件
1.oauth2.php
?php
$code=$_GET[‘code’];
$state=$_GET[‘state’];
$appid=’XXXX’;
$appsecret=’XXXXXXXX’;//
if (empty($code)) $this-error(‘授權失敗’);
$token_url=”.$appid’secret=’.$appsecret.’code=’.$code.’grant_type=authorization_code’;
$token=json_decode(file_get_contents($token_url));
if (isset($token-errcode)) {
echo ‘h1錯誤1/h1’.$token-errcode;
echo ‘br/h2錯誤信息1:/h2’.$token-errmsg;
exit;
}
session_start();
$_SESSION[‘openid’]= $token-openid;
header(‘location:url/redpack.php’);//要跳轉的文件路徑
?
2.redpack.php
?php
//XXXXX。。是需要開發者自己填寫的內容,注意不要泄密
// 從session中獲取到openid;
$openid=$_SESSION[“openid”];
if(empty($openid))
{
header(‘location:;redirect_uri=;respose_type=codescope=snsapi_basestate=XXXXconnect_redirect=1#wechat_redirect’);
}
}
// 關鍵的函數
public function weixin_red_packet(){
// 請求參數
// 隨機字符串
$data[‘nonce_str’]=$this-get_unique_value();
//商戶號,輸入你的商戶號
$data[‘mch_id’]=”XXXXXXX”;
//商戶訂單號,可以按要求自己組合28位的商戶訂單號
$data[‘mch_billno’]=$data[‘mch_id’].date(“ymd”).”XXXXXX”.rand(1000,9999);
//公眾帳號appid,輸入自己的公眾號appid
$data[‘wxappid’]=”XXXXXXX”;
//商戶名稱
$data[‘send_name’]=”XXXXX”;
//用戶openid,輸入待發紅包的用戶openid
session_start();
$data[‘re_openid’]=$_SESSION[“openid”];
//付款金額
$data[‘total_amount’]=”XXXX”;
//紅包發放總人數
$data[‘total_num’]=”XXXX”;
//紅包祝福語
$data[‘wishing’]=”XXXX”;
//IP地址
$data[‘client_ip’]=$_SERVER[‘LOCAL_ADDR’];
//活動名稱
$data[‘act_name’]=”XXXXX”;
//備註
$data[‘remark’]=”XXXXX”;
// 生成簽名
//對數據數組進行處理
//API密鑰,輸入自己的K 微信商戶號裏面的K
$appsecret=”XXXXXXXXXXXXXX”; //
$data=array_filter($data);
ksort($data);
$str=””;
foreach($data as $k=$v){
$str.=$k.”=”.$v.””;
}
$str.=”key=”.$appsecret;
$data[‘sign’]=strtoupper(MD5($str));
/*
發紅包操作過程:
1.將請求數據轉換成xml
2.發送請求
3.將請求結果轉換為數組
4.將請求信息和請求結果錄入到數據庫中
4.判斷是否通信成功
5.判斷是否轉賬成功
*/
//發紅包接口地址
$url=””;
//將請求數據由數組轉換成xml
$xml=$this-arraytoxml($data);
//進行請求操作
$res=$this-curl($xml,$url);
//將請求結果由xml轉換成數組
$arr=$this-xmltoarray($res);
}
// 生成32位唯一隨機字符串
private function get_unique_value(){
$str=uniqid(mt_rand(),1);
$str=sha1($str);
return md5($str);
}
// 將數組轉換成xml
private function arraytoxml($arr){
$xml=”xml”;
foreach($arr as $k=$v){
$xml.=””.$k.””.$v.”/”.$k.””;
}
$xml.=”/xml”;
return $xml;
}
// 將xml轉換成數組
private function xmltoarray($xml){
//禁止引用外部xml實體
libxml_disable_entity_loader(true);
$xmlstring=simplexml_load_string($xml,”SimpleXMLElement”,LIBXML_NOCDATA);
$arr=json_decode(json_encode($xmlstring),true);
return $arr;
}
//進行curl操作
private function curl($param=””,$url) {
$postUrl = $url;
$curlPost = $param;
//初始化curl
$ch = curl_init();
//抓取指定網頁
curl_setopt($ch, CURLOPT_URL,$postUrl);
//設置header
curl_setopt($ch, CURLOPT_HEADER, 0);
//要求結果為字符串且輸出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//post提交方式
curl_setopt($ch, CURLOPT_POST, 1);
// 增加 HTTP Header(頭)里的字段
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
// 終止從服務端進行驗證
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//證書放到網站根目錄的cert文件夾底下
curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).DIRECTORY_SEPARATOR.
‘cert’.DIRECTORY_SEPARATOR.’apiclient_cert.pem’);
curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).DIRECTORY_SEPARATOR.
‘cert’.DIRECTORY_SEPARATOR.’apiient_key.pem’);
curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR.
‘cert’.DIRECTORY_SEPARATOR.’rootca.pem’);
//運行curl
$data = curl_exec($ch);
//關閉curl
curl_close($ch);
return $data;
}
?
可參考官方文檔進行調整開發,希望能有幫助,望採納
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/236229.html