commonjob.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. //阿里云短信接口
  3. /**
  4. * 生成签名并发起请求
  5. *
  6. * @param $accessKeyId string AccessKeyId (https://ak-console.aliyun.com/)
  7. * @param $accessKeySecret string AccessKeySecret
  8. * @param $domain string API接口所在域名
  9. * @param $params array API具体参数
  10. * @param $security boolean 使用https
  11. * @return bool|\stdClass 返回API接口调用结果,当发生错误时返回false
  12. */
  13. function aliencode($str)
  14. {
  15. $res = urlencode($str);
  16. $res = preg_replace("/\+/", "%20", $res);
  17. $res = preg_replace("/\*/", "%2A", $res);
  18. $res = preg_replace("/%7E/", "~", $res);
  19. return $res;
  20. }
  21. function alirequest($accessKeyId, $accessKeySecret, $domain, $params, $security=false) {
  22. $apiParams = array_merge(array (
  23. "SignatureMethod" => "HMAC-SHA1",
  24. "SignatureNonce" => uniqid(mt_rand(0,0xffff), true),
  25. "SignatureVersion" => "1.0",
  26. "AccessKeyId" => $accessKeyId,
  27. "Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
  28. "Format" => "JSON",
  29. ), $params);
  30. ksort($apiParams);
  31. $sortedQueryStringTmp = "";
  32. foreach ($apiParams as $key => $value) {
  33. $sortedQueryStringTmp .= "&" . aliencode($key) . "=" . aliencode($value);
  34. }
  35. $stringToSign = "GET&%2F&" . aliencode(substr($sortedQueryStringTmp, 1));
  36. $sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true));
  37. $signature = aliencode($sign);
  38. $url = ($security ? 'https' : 'http')."://{$domain}/?Signature={$signature}{$sortedQueryStringTmp}";
  39. try {
  40. $content = fetchContent($url);
  41. return json_decode($content);
  42. } catch( \Exception $e) {
  43. return false;
  44. }
  45. }
  46. function fetchContent($url) {
  47. $ch = curl_init();
  48. curl_setopt($ch, CURLOPT_URL, $url);
  49. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  50. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  51. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  52. "x-sdk-client" => "php/2.0.0"
  53. ));
  54. if(substr($url, 0,5) == 'https') {
  55. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  57. }
  58. $rtn = curl_exec($ch);
  59. if($rtn === false) {
  60. trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
  61. }
  62. curl_close($ch);
  63. return $rtn;
  64. }
  65. //发送短信 商城 定时脚本
  66. function createSmsParmsAndSend($user_mobile,$code) {
  67. $params = array ();
  68. // $code = '123456';
  69. // *** 需用户填写部分 ***
  70. // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
  71. $accessKeyId = "LTAIrsLAXDWeZU2h";
  72. $accessKeySecret = "7sYaChE8HEglWvO4yeUnRrHarNkIzL";
  73. // fixme 必填: 短信接收号码
  74. $params["PhoneNumbers"] = $user_mobile;
  75. // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
  76. $params["SignName"] = "小白马金服";
  77. // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
  78. $params["TemplateCode"] = "SMS_135802079";
  79. // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
  80. $params['TemplateParam'] = Array (
  81. "code" => "$code"
  82. );
  83. // fixme 可选: 设置发送短信流水号
  84. // $params['OutId'] = "12345";
  85. // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
  86. // $params['SmsUpExtendCode'] = "1234567";
  87. // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
  88. if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
  89. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  90. }
  91. // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
  92. // $helper = new SignatureHelper();
  93. // 此处可能会抛出异常,注意catch
  94. $content = alirequest(
  95. $accessKeyId,
  96. $accessKeySecret,
  97. "dysmsapi.aliyuncs.com",
  98. array_merge($params, array(
  99. "RegionId" => "cn-hangzhou",
  100. "Action" => "SendSms",
  101. "Version" => "2017-05-25",
  102. ))
  103. // fixme 选填: 启用https
  104. // ,true
  105. );
  106. return $content;
  107. }
  108. //发送短信 商城 半小时脚本
  109. function createSmsAndSend($user_mobile,$code) {
  110. $params = array ();
  111. // $code = '123456';
  112. // *** 需用户填写部分 ***
  113. // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
  114. $accessKeyId = "LTAIrsLAXDWeZU2h";
  115. $accessKeySecret = "7sYaChE8HEglWvO4yeUnRrHarNkIzL";
  116. // fixme 必填: 短信接收号码
  117. $params["PhoneNumbers"] = $user_mobile;
  118. // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
  119. $params["SignName"] = "小白马金服";
  120. // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
  121. $params["TemplateCode"] = "SMS_135797143";
  122. // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
  123. $params['TemplateParam'] = Array (
  124. "code" => "$code"
  125. );
  126. // fixme 可选: 设置发送短信流水号
  127. // $params['OutId'] = "12345";
  128. // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
  129. // $params['SmsUpExtendCode'] = "1234567";
  130. // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
  131. if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
  132. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  133. }
  134. // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
  135. // $helper = new SignatureHelper();
  136. // 此处可能会抛出异常,注意catch
  137. $content = alirequest(
  138. $accessKeyId,
  139. $accessKeySecret,
  140. "dysmsapi.aliyuncs.com",
  141. array_merge($params, array(
  142. "RegionId" => "cn-hangzhou",
  143. "Action" => "SendSms",
  144. "Version" => "2017-05-25",
  145. ))
  146. // fixme 选填: 启用https
  147. // ,true
  148. );
  149. return $content;
  150. }
  151. //发送短信 理财 半小时脚本
  152. function createSmsAndSendtwo($user_mobile,$code) {
  153. $params = array ();
  154. // $code = '123456';
  155. // *** 需用户填写部分 ***
  156. // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
  157. $accessKeyId = "LTAIrsLAXDWeZU2h";
  158. $accessKeySecret = "7sYaChE8HEglWvO4yeUnRrHarNkIzL";
  159. // fixme 必填: 短信接收号码
  160. $params["PhoneNumbers"] = $user_mobile;
  161. // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
  162. $params["SignName"] = "小白马金服";
  163. // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
  164. $params["TemplateCode"] = "SMS_135792962";
  165. // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
  166. $params['TemplateParam'] = Array (
  167. "code" => "$code"
  168. );
  169. // fixme 可选: 设置发送短信流水号
  170. // $params['OutId'] = "12345";
  171. // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
  172. // $params['SmsUpExtendCode'] = "1234567";
  173. // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
  174. if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
  175. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  176. }
  177. // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
  178. // $helper = new SignatureHelper();
  179. // 此处可能会抛出异常,注意catch
  180. $content = alirequest(
  181. $accessKeyId,
  182. $accessKeySecret,
  183. "dysmsapi.aliyuncs.com",
  184. array_merge($params, array(
  185. "RegionId" => "cn-hangzhou",
  186. "Action" => "SendSms",
  187. "Version" => "2017-05-25",
  188. ))
  189. // fixme 选填: 启用https
  190. // ,true
  191. );
  192. return $content;
  193. }
  194. //发送短信 理财 定时脚本
  195. function createSmsParmsAndSendtwo($user_mobile,$code) {
  196. $params = array ();
  197. // $code = '123456';
  198. // *** 需用户填写部分 ***
  199. // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
  200. $accessKeyId = "LTAIrsLAXDWeZU2h";
  201. $accessKeySecret = "7sYaChE8HEglWvO4yeUnRrHarNkIzL";
  202. // fixme 必填: 短信接收号码
  203. $params["PhoneNumbers"] = $user_mobile;
  204. // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
  205. $params["SignName"] = "小白马金服";
  206. // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
  207. $params["TemplateCode"] = "SMS_135807896";
  208. // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
  209. $params['TemplateParam'] = Array (
  210. "code" => "$code"
  211. );
  212. // fixme 可选: 设置发送短信流水号
  213. // $params['OutId'] = "12345";
  214. // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
  215. // $params['SmsUpExtendCode'] = "1234567";
  216. // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
  217. if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
  218. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  219. }
  220. // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
  221. // $helper = new SignatureHelper();
  222. // 此处可能会抛出异常,注意catch
  223. $content = alirequest(
  224. $accessKeyId,
  225. $accessKeySecret,
  226. "dysmsapi.aliyuncs.com",
  227. array_merge($params, array(
  228. "RegionId" => "cn-hangzhou",
  229. "Action" => "SendSms",
  230. "Version" => "2017-05-25",
  231. ))
  232. // fixme 选填: 启用https
  233. // ,true
  234. );
  235. return $content;
  236. }
  237. //发送短信 理财 定时脚本 大于100万
  238. function createSmsParmsAndSendbig($user_mobile,$code) {
  239. $params = array ();
  240. // $code = '123456';
  241. // *** 需用户填写部分 ***
  242. // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
  243. $accessKeyId = "LTAIrsLAXDWeZU2h";
  244. $accessKeySecret = "7sYaChE8HEglWvO4yeUnRrHarNkIzL";
  245. // fixme 必填: 短信接收号码
  246. $params["PhoneNumbers"] = $user_mobile;
  247. // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
  248. $params["SignName"] = "小白马金服";
  249. // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
  250. $params["TemplateCode"] = "SMS_136160447";
  251. // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
  252. $params['TemplateParam'] = Array (
  253. "code" => "$code"
  254. );
  255. // fixme 可选: 设置发送短信流水号
  256. // $params['OutId'] = "12345";
  257. // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
  258. // $params['SmsUpExtendCode'] = "1234567";
  259. // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
  260. if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
  261. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  262. }
  263. // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
  264. // $helper = new SignatureHelper();
  265. // 此处可能会抛出异常,注意catch
  266. $content = alirequest(
  267. $accessKeyId,
  268. $accessKeySecret,
  269. "dysmsapi.aliyuncs.com",
  270. array_merge($params, array(
  271. "RegionId" => "cn-hangzhou",
  272. "Action" => "SendSms",
  273. "Version" => "2017-05-25",
  274. ))
  275. // fixme 选填: 启用https
  276. // ,true
  277. );
  278. return $content;
  279. }
  280. function foo($n ,$max = 100){
  281. $array = $zero = $normal = [];
  282. for($i=1;$i<=$n;$i++){
  283. $array[] = mt_rand(0,100);
  284. }
  285. $k = $max / array_sum($array); //求出放大系数k
  286. foreach($array as $key => $val){
  287. $value = floor($val * $k); //直接保留整数,以保证下一步的和肯定<100
  288. if($value<1){
  289. $zero[] = $value;
  290. }else{
  291. $normal[] = $value;
  292. }
  293. }
  294. $sum = array_sum($normal);
  295. $diff = $max - $sum; //这个值肯定<100
  296. if(!empty($zero)){ //如果有为0的值
  297. $count = count($zero);
  298. foreach($zero as $z){
  299. $normal[] = $diff / $count;
  300. }
  301. }else{ //随机分配给一个人
  302. $key = array_rand($normal);
  303. $normal[$key] = $normal[$key]+$diff;
  304. }
  305. return $normal;
  306. }
  307. function createNewProduct($data){
  308. $day = date('Y-m-d');
  309. $dayhis = date('Y-m-d H:i:s');
  310. //封标
  311. $update18 = mysql_query("UPDATE xbm_product_bid SET status=2,end_time='$dayhis' WHERE bid_id=$data[bid_id]");
  312. //发标
  313. //查询当前标的详情
  314. $product_bid18 = mysql_query("SELECT * FROM xbm_product_zd WHERE product_sn='$data[product_sn]'");
  315. //查询当前标的今天发布数量
  316. $todayCount18 = mysql_query("SELECT COUNT(1) AS count FROM xbm_product_bid WHERE product_sn='$data[product_sn]' AND create_time LIKE '$day%'");
  317. $productb18 = mysql_fetch_assoc($product_bid18);
  318. $todayC18 = mysql_fetch_assoc($todayCount18);
  319. if (($productb18['count'] - $todayC18['count']) >= 1) {
  320. //发短信提醒
  321. // $result = createSmsParmsAndSendbig(13810263268, "0018".($productb18['count'] - $todayC18['count']));
  322. // M('product_bid')->where("product_sn='{$sn}' AND create_time LIKE '{$day}%'")->count();
  323. $resRES = mysql_query("SELECT count(1) AS count FROM xbm_product_bid WHERE product_sn='$data[product_sn]' AND create_time LIKE '$day%'");
  324. $res =mysql_fetch_assoc($resRES);
  325. $resCount = $res['count']+1;
  326. $day18 = $data['product_sn'] . date('Ymd');
  327. if ($resCount) {
  328. $bid_sn18 = $day18 . ($resCount < 10 ? 0 . $resCount : $resCount);
  329. }
  330. //生成bid_name
  331. $start = strlen($data['product_sn']);
  332. $name = mb_substr($data['bid_name'], 0, $start);
  333. $name18 = mb_substr($data['bid_name'], $start);
  334. $name18 += 1;
  335. $strnum = strlen($name18);
  336. if ($strnum < 2) {
  337. $name18 = $name . str_pad($name18, 2, 0, STR_PAD_LEFT);
  338. }
  339. //生成借款人信息
  340. if($productb18['total_amount']/10000>=1){
  341. if($productb18['total_amount']/10000>1){
  342. $n18=rand(4,intval($productb18['total_amount']/10000));
  343. }else{
  344. $n18=$productb18['total_amount']/10000;
  345. }
  346. $money18=foo($n18,$productb18['total_amount']/1000);
  347. //查询对应的借款人信息
  348. $user_data18 = mysql_query("SELECT xbm_users_realname.user_id FROM xbm_users_realname LEFT JOIN xbm_users_phone ON xbm_users_realname.user_id=xbm_users_phone.user_id WHERE xbm_users_phone.user_id <> '' ORDER BY rand() LIMIT 0,$n18");
  349. //获取标的期限
  350. $period18 = mysql_query("SELECT period FROM xbm_product WHERE product_sn='$productb18[product_sn]' LIMIT 1");
  351. $period18 = mysql_fetch_assoc($period18);
  352. //循环插入借款人表
  353. $i=0;
  354. while($user_data182 = mysql_fetch_assoc($user_data18)){
  355. $borrowMoney18 = $money18[$i]*1000;
  356. // $user_id18 = $user_data18['user_id'];
  357. //插入借款人表
  358. $borrowInsert = mysql_query("INSERT INTO xbm_product_borrower (`borrow_time`,`user_id`,`money`,`bid_sn`,`borrow_term`,`create_time`) VALUES ('$day',$user_data182[user_id],$borrowMoney18,'$bid_sn18','$period18[period]','$dayhis')");
  359. $i++;
  360. }
  361. }
  362. //发布新标的
  363. $addProduct18 = mysql_query("INSERT INTO `xbm_product_bid` ( `bid_sn`, `bid_name`, `product_sn`, `current_amount`, `total_amount`, `status`, `y_status`, `send_status`, `password`, `borrow_type`, `commend`, `create_time`, `annualized_rate`, `bid_url`, `bid_content_url`)
  364. VALUES
  365. ('$bid_sn18','$name$name18','$data[product_sn]',0,$productb18[total_amount],1,0,0,NULL,0,0,'$dayhis',$productb18[annualized_rate],'','$productb18[bid_content_url]')");
  366. echo true;
  367. }elseif (($productb18['count'] - $todayCount18['count']) < 2){
  368. //发短信
  369. $result = createSmsParmsAndSendbig(13810263268,"181818");
  370. }
  371. }