AutoexeController.class.php 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  1. <?php
  2. namespace Mobile\Controller;
  3. class AutoexeController extends \Think\Controller
  4. {
  5. protected function _initialize()
  6. {
  7. $allow_action = array("hycarryout", "ups_and_downs", "getnewprice", "setwl", "setwl_ty", "autokjsy", "releasedjprofit", "autoxjtade", "authsharesjsy", "releaseissue", "hycarryout_ty", "AutoCreateOrder", "AutoCloseOrder");
  8. if (!in_array(ACTION_NAME, $allow_action)) {
  9. $this->error("非法操作!");
  10. }
  11. }
  12. //永续合约自动结算
  13. public function AutoCloseOrder()
  14. {
  15. $list = M("leverorder")->where(array('status' => 2))->select();
  16. if (!empty($list)) {
  17. $sysinfo = M("leversetting")->where(array('id' => 1))->find();
  18. $lever_fee = $sysinfo['lever_fee'];//费率
  19. $new_close = [];
  20. foreach ($list as $key => $vo) {
  21. $id = $vo['id'];
  22. $uid = $vo['uid'];
  23. $uinfo = M("user")->where(array('id' => $uid))->field("id,username")->find();
  24. // $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  25. // $bond_money_info = M("levermoney")->where(array('uid' => $uid))->find();
  26. $lowercoin = $vo['coinname'];
  27. $user = M('levermoney')->where(['uid' => $uid])->find();
  28. if (!isset($new_close[$vo['coinname']])) {
  29. if ($lowercoin == 'ukbusdt') {
  30. $o_symbol = $vo['symbol'];
  31. $symbol_arr = explode('/', $o_symbol);
  32. $symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
  33. $mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
  34. $close = $mlist['new_price'];
  35. } else {
  36. $oy_list = do_request(oy_url($vo['coinname'], 'api/v5/market/ticker?instId='), []);
  37. $oy_list = $oy_list->data[0];
  38. // $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
  39. // $result = $this->getprice($url);
  40. // $pdata = $result['data'][0];
  41. $close = $oy_list->last;//现价
  42. $new_close[$vo['coinname']] = $close;
  43. }
  44. } else {
  45. $close = $new_close[$vo['coinname']];
  46. }
  47. if ($close) {
  48. $price = $vo['price']; //开仓价格
  49. $direction = $vo['direction']; //交易方向
  50. $zsprice = $vo['zsprice']; //止损价格
  51. $zyprice = $vo['zyprice']; //止盈价格
  52. if ($zyprice > 0) {
  53. if ($direction == 1) {
  54. // 买多
  55. if ($close >= $zyprice) {
  56. //收益计算
  57. //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
  58. //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
  59. $profit = sprintf("%.2f", ($close - $price) * $vo['num']);
  60. $tprofit = abs($profit);
  61. $data['pc_price'] = $close;
  62. $data['status'] = 3;
  63. $data['yk_status'] = 1;
  64. $data['ylmoney'] = $tprofit;
  65. $data['endtime'] = date("Y-m-d H:i:s", time());
  66. $result = M("leverorder")->where(array('id' => $id))->save($data);
  67. $ubill['uid'] = $uid;
  68. $ubill['username'] = $uinfo['username'];
  69. $ubill['num'] = $tprofit;
  70. $ubill['coinname'] = "lever";
  71. $ubill['afternum'] = $user['money'] + $tprofit;
  72. $ubill['type'] = 20;
  73. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  74. $ubill['st'] = 1;
  75. $ubill['remark'] = L('合约交易止盈平仓盈利收益');
  76. $ubillre = M("bill")->add($ubill);
  77. $bond_ubill = [
  78. 'uid' => $uid,
  79. 'username' => $uinfo['username'],
  80. 'num' => $vo['bond'],
  81. 'coinname' => 'usdt',
  82. 'afternum' => $user['money'] + $vo['bond'],
  83. 'type' => 25,
  84. 'addtime' => date("Y-m-d H:i:s", time()),
  85. 'st' => 1,
  86. 'remark' => '合約交易止盈平倉返還佔用合約'
  87. ];// 扣除合约保证金
  88. M("levermoney")->where(array('uid' => $uid))->setInc('money', $vo['bond']);
  89. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  90. M("bill")->add($bond_ubill);
  91. if ($result && $ubillre) {
  92. M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
  93. echo "=订单ID:" . $id . ",止盈平仓=";
  94. }
  95. }
  96. } elseif ($direction == 2) {
  97. //买空
  98. if ($close <= $zyprice) {
  99. $profit = sprintf("%.2f", ($price - $close) * $vo['num']);
  100. $tprofit = abs($profit);
  101. $data['pc_price'] = $close;
  102. $data['status'] = 3;
  103. $data['yk_status'] = 1;
  104. $data['ylmoney'] = $tprofit;
  105. $data['endtime'] = date("Y-m-d H:i:s", time());
  106. $result = M("leverorder")->where(array('id' => $id))->save($data);
  107. $ubill['uid'] = $uid;
  108. $ubill['username'] = $uinfo['username'];
  109. $ubill['num'] = $tprofit;
  110. $ubill['coinname'] = "lever";
  111. $ubill['afternum'] = $user['money'] + $tprofit;
  112. $ubill['type'] = 20;
  113. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  114. $ubill['st'] = 1;
  115. $ubill['remark'] = L('合约交易止盈平仓盈利收益');
  116. $ubillre = M("bill")->add($ubill);
  117. $bond_ubill = [
  118. 'uid' => $uid,
  119. 'username' => $uinfo['username'],
  120. 'num' => $vo['bond'],
  121. 'coinname' => 'usdt',
  122. 'afternum' => $user['money'] + $vo['bond'],
  123. 'type' => 25,
  124. 'addtime' => date("Y-m-d H:i:s", time()),
  125. 'st' => 1,
  126. 'remark' => '合約交易止盈平倉返還佔用合約'
  127. ];// 扣除合约保证金
  128. M("levermoney")->where(array('uid' => $uid))->setInc('money', $vo['bond']);
  129. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  130. M("bill")->add($bond_ubill);
  131. if ($result && $ubillre) {
  132. M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
  133. echo "=订单ID:" . $id . ",止盈平仓=";
  134. }
  135. }
  136. }
  137. }
  138. if ($zsprice > 0) {
  139. if ($direction == 1) {
  140. // 买多
  141. if ($close <= $zsprice) {
  142. //收益计算
  143. //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
  144. //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
  145. $profit = sprintf("%.2f", ($close - $price) * $vo['num']);
  146. $tprofit = abs($profit);
  147. if ($tprofit < $user['money']) {
  148. //亏损小于保证金
  149. $data['pc_price'] = $close;
  150. $data['status'] = 3;
  151. $data['yk_status'] = 2;
  152. $data['ylmoney'] = $tprofit;
  153. $data['endtime'] = date("Y-m-d H:i:s", time());
  154. $bond = ($user['money'] + $vo['bond']) - $tprofit;
  155. $ubill['uid'] = $uid;
  156. $ubill['username'] = $uinfo['username'];
  157. $ubill['num'] = $vo['bond'];
  158. $ubill['afternum'] = $user['money'] + $vo['bond'];
  159. $ubill['coinname'] = "lever";
  160. $ubill['type'] = 20;
  161. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  162. $ubill['st'] = 1;
  163. $ubill['remark'] = '合約交易止損平倉返回保證金';
  164. $ubillre = M("bill")->add($ubill);
  165. $ubill['num'] = $tprofit;
  166. $ubill['afternum'] = $bond;
  167. $ubill['coinname'] = "lever";
  168. $ubill['st'] = 2;
  169. $ubill['remark'] = '合約交易止損平倉虧損';
  170. $ubillre = M("bill")->add($ubill);
  171. M("leverorder")->where(array('id' => $id))->save($data);
  172. M("levermoney")->where(['uid' => $uid])->setInc('money', $vo['bond']);
  173. M("levermoney")->where(['uid' => $uid])->setDec('money', $tprofit);
  174. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  175. echo "=订单ID:" . $id . ",止损平仓=";
  176. } else {
  177. if ($user['money'] + $vo['bond'] >= $tprofit) {
  178. $user_bond = $vo['bond'] - ($tprofit - $user['money']);
  179. $data['pc_price'] = $close;
  180. $data['status'] = 3;
  181. $data['yk_status'] = 2;
  182. $data['ylmoney'] = $tprofit;
  183. $data['endtime'] = date("Y-m-d H:i:s", time());
  184. $bond = $tprofit - $vo['bond'];
  185. $ubill['uid'] = $uid;
  186. $ubill['username'] = $uinfo['username'];
  187. $ubill['num'] = $user_bond;
  188. $ubill['afternum'] = $user_bond;
  189. $ubill['coinname'] = "lever";
  190. $ubill['type'] = 20;
  191. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  192. $ubill['st'] = 1;
  193. $ubill['remark'] = '合約交易止損平倉退回剩餘保證金';
  194. $ubillre = M("bill")->add($ubill);
  195. $ubill['num'] = $user['money'];
  196. $ubill['afternum'] = 0;
  197. $ubill['type'] = 20;
  198. $ubill['st'] = 2;
  199. $ubill['remark'] = '合約交易手動平倉虧損';
  200. M("leverorder")->where(array('id' => $id))->save($data);
  201. M("levermoney")->where(['uid' => $uid])->setInc('money', $user_bond);
  202. M("levermoney")->where(array('uid' => $uid))->setDec('money', $user['money']);
  203. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  204. echo "=订单ID:" . $id . ",止损平仓=";
  205. } else {
  206. $data['pc_price'] = $close;
  207. $data['status'] = 3;
  208. $data['yk_status'] = 2;
  209. $data['ylmoney'] = $tprofit;
  210. $data['endtime'] = date("Y-m-d H:i:s", time());
  211. $bond = $tprofit - $vo['bond'];
  212. $ubill['uid'] = $uid;
  213. $ubill['username'] = $uinfo['username'];
  214. $ubill['num'] = $user['money'];
  215. $ubill['afternum'] = 0;
  216. $ubill['coinname'] = "lever";
  217. $ubill['type'] = 20;
  218. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  219. $ubill['st'] = 1;
  220. $ubill['remark'] = '合約交易ID' . $vo['id'] . '止損平倉扣除完保證金,並扣除合約帳戶';
  221. $ubillre = M("bill")->add($ubill);
  222. M("leverorder")->where(array('id' => $id))->save($data);
  223. M("levermoney")->where(['uid' => $uid])->setDec('money', $user['money']);
  224. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  225. echo "=订单ID:" . $id . ",止损平仓=";
  226. }
  227. }
  228. }
  229. } elseif ($direction == 2) {
  230. //买空
  231. if ($close >= $zsprice) {
  232. $profit = sprintf("%.2f", ($price - $close) * $vo['num']);
  233. $tprofit = abs($profit);
  234. if ($tprofit < $user['money']) {
  235. //亏损小于保证金
  236. $data['pc_price'] = $close;
  237. $data['status'] = 3;
  238. $data['yk_status'] = 2;
  239. $data['ylmoney'] = $tprofit;
  240. $data['endtime'] = date("Y-m-d H:i:s", time());
  241. $bond = ($user['money'] + $vo['bond']) - $tprofit;
  242. $ubill['uid'] = $uid;
  243. $ubill['username'] = $uinfo['username'];
  244. $ubill['num'] = $vo['bond'];
  245. $ubill['afternum'] = $user['money'] + $vo['bond'];
  246. $ubill['coinname'] = "lever";
  247. $ubill['type'] = 20;
  248. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  249. $ubill['st'] = 1;
  250. $ubill['remark'] = '合約交易止損平倉返回保證金';
  251. $ubillre = M("bill")->add($ubill);
  252. $ubill['num'] = $tprofit;
  253. $ubill['afternum'] = $bond;
  254. $ubill['coinname'] = "lever";
  255. $ubill['st'] = 2;
  256. $ubill['remark'] = '合約交易止損平倉虧損';
  257. $ubillre = M("bill")->add($ubill);
  258. M("leverorder")->where(array('id' => $id))->save($data);
  259. M("levermoney")->where(['uid' => $uid])->setInc('money', $vo['bond']);
  260. M("levermoney")->where(['uid' => $uid])->setDec('money', $tprofit);
  261. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  262. echo "=订单ID:" . $id . ",止损平仓=";
  263. } else {
  264. if ($user['money'] + $vo['bond'] >= $tprofit) {
  265. $user_bond = $vo['bond'] - ($tprofit - $user['money']);
  266. $data['pc_price'] = $close;
  267. $data['status'] = 3;
  268. $data['yk_status'] = 2;
  269. $data['ylmoney'] = $tprofit;
  270. $data['endtime'] = date("Y-m-d H:i:s", time());
  271. $bond = $tprofit - $vo['bond'];
  272. $ubill['uid'] = $uid;
  273. $ubill['username'] = $uinfo['username'];
  274. $ubill['num'] = $user_bond;
  275. $ubill['afternum'] = $user_bond;
  276. $ubill['coinname'] = "lever";
  277. $ubill['type'] = 20;
  278. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  279. $ubill['st'] = 1;
  280. $ubill['remark'] = '合約交易止損平倉退回剩餘保證金';
  281. $ubillre = M("bill")->add($ubill);
  282. $ubill['num'] = $user['money'];
  283. $ubill['afternum'] = 0;
  284. $ubill['type'] = 20;
  285. $ubill['st'] = 2;
  286. $ubill['remark'] = '合約交易手動平倉虧損';
  287. M("leverorder")->where(array('id' => $id))->save($data);
  288. M("levermoney")->where(['uid' => $uid])->setInc('money', $user_bond);
  289. M("levermoney")->where(array('uid' => $uid))->setDec('money', $user['money']);
  290. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  291. echo "=订单ID:" . $id . ",止损平仓=";
  292. } else {
  293. $data['pc_price'] = $close;
  294. $data['status'] = 3;
  295. $data['yk_status'] = 2;
  296. $data['ylmoney'] = $tprofit;
  297. $data['endtime'] = date("Y-m-d H:i:s", time());
  298. $bond = $tprofit - $vo['bond'];
  299. $ubill['uid'] = $uid;
  300. $ubill['username'] = $uinfo['username'];
  301. $ubill['num'] = $user['money'];
  302. $ubill['afternum'] = 0;
  303. $ubill['coinname'] = "lever";
  304. $ubill['type'] = 20;
  305. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  306. $ubill['st'] = 1;
  307. $ubill['remark'] = '合約交易ID' . $vo['id'] . '止損平倉扣除完保證金,並扣除合約帳戶';
  308. $ubillre = M("bill")->add($ubill);
  309. M("leverorder")->where(array('id' => $id))->save($data);
  310. M("levermoney")->where(['uid' => $uid])->setDec('money', $user['money']);
  311. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  312. echo "=订单ID:" . $id . ",止损平仓=";
  313. }
  314. }
  315. }
  316. }
  317. }
  318. if ($zsprice == 0) {
  319. if ($direction == 1) {
  320. $profit_fd = sprintf("%.2f", ($close - $price) * $vo['num']);
  321. if ($profit_fd < 0) {
  322. if (abs($profit_fd) > ($user['money'] + $user['moneyd'])) {
  323. $data['pc_price'] = $close;
  324. $data['status'] = 4;
  325. $data['yk_status'] = 2;
  326. $data['ylmoney'] = $profit_fd;
  327. $data['endtime'] = date("Y-m-d H:i:s", time());
  328. $ubill['uid'] = $uid;
  329. $ubill['username'] = $uinfo['username'];
  330. $ubill['num'] = $user['money'];
  331. $ubill['afternum'] = 0;
  332. $ubill['coinname'] = "lever";
  333. $ubill['type'] = 20;
  334. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  335. $ubill['st'] = 1;
  336. $ubill['remark'] = '合約交易ID' . $vo['id'] . '爆倉';
  337. $ubillre = M("bill")->add($ubill);
  338. M("leverorder")->where(array('id' => $id))->save($data);
  339. M("levermoney")->where(['uid' => $uid])->save(['money' => 0, 'moneyd' => 0]);
  340. $pc_list = M('leverorder')->where(['uid' => $uid])->where(['status' => 2])->select();
  341. foreach ($pc_list as $item) {
  342. $direction = $item['direction'];
  343. if ($direction == 1) { //做多
  344. $profit = sprintf("%.2f", ($close - $item['price']) * $item['num']);
  345. } elseif ($direction == 2) { //做空
  346. $profit = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
  347. }
  348. if ($profit >= 0) {
  349. $yk_status = 1;
  350. } else {
  351. $yk_status = 2;
  352. }
  353. $tprofit = abs($profit);
  354. $datas['pc_price'] = $close;
  355. $datas['status'] = 3;
  356. $datas['yk_status'] = $yk_status;
  357. $datas['ylmoney'] = $tprofit;
  358. $datas['endtime'] = date("Y-m-d H:i:s", time());
  359. $result = M("leverorder")->where(array('id' => $item['id'], 'status' => 2))->save($datas);
  360. $user = M('levermoney')->where(['uid' => $uid])->find();
  361. if ($result) {
  362. if ($profit >= 0) {
  363. M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
  364. $ubill['uid'] = $uid;
  365. $ubill['username'] = $uinfo['username'];
  366. $ubill['num'] = $tprofit;
  367. $ubill['coinname'] = "lever";
  368. $ubill['afternum'] = $user['money'] + $tprofit;
  369. $ubill['type'] = 20;
  370. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  371. $ubill['st'] = 1;
  372. $ubill['remark'] = L('合约交易自動平仓盈利收益');
  373. $ubillre = M("bill")->add($ubill);
  374. }
  375. }
  376. }
  377. echo "=订单ID:" . $id . ",爆仓=";
  378. }
  379. } elseif ($direction == 2) {
  380. $profit_fd = sprintf("%.2f", ($price - $close) * $vo['num']);
  381. if ($profit_fd < 0) {
  382. if (abs($profit_fd) > ($vo['bond'] + $user['moneyd'])) {
  383. $data['pc_price'] = $close;
  384. $data['status'] = 4;
  385. $data['yk_status'] = 2;
  386. $data['ylmoney'] = $profit_fd;
  387. $data['endtime'] = date("Y-m-d H:i:s", time());
  388. $ubill['uid'] = $uid;
  389. $ubill['username'] = $uinfo['username'];
  390. $ubill['num'] = $user['money'];
  391. $ubill['afternum'] = 0;
  392. $ubill['coinname'] = "lever";
  393. $ubill['type'] = 20;
  394. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  395. $ubill['st'] = 1;
  396. $ubill['remark'] = '合約交易ID' . $vo['id'] . '爆倉';
  397. $ubillre = M("bill")->add($ubill);
  398. M("leverorder")->where(array('id' => $id))->save($data);
  399. M("levermoney")->where(['uid' => $uid])->save(['money' => 0, 'moneyd' => 0]);
  400. $pc_list = M('leverorder')->where(['uid' => $uid])->where(['status' => 2])->select();
  401. foreach ($pc_list as $item) {
  402. $direction = $item['direction'];
  403. if ($direction == 1) { //做多
  404. $profit = sprintf("%.2f", ($close - $item['price']) * $item['num']);
  405. } elseif ($direction == 2) { //做空
  406. $profit = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
  407. }
  408. if ($profit >= 0) {
  409. $yk_status = 1;
  410. } else {
  411. $yk_status = 2;
  412. }
  413. $tprofit = abs($profit);
  414. $datas['pc_price'] = $close;
  415. $datas['status'] = 3;
  416. $datas['yk_status'] = $yk_status;
  417. $datas['ylmoney'] = $tprofit;
  418. $datas['endtime'] = date("Y-m-d H:i:s", time());
  419. $result = M("leverorder")->where(array('id' => $item['id'], 'status' => 2))->save($datas);
  420. $user = M('levermoney')->where(['uid' => $uid])->find();
  421. if ($result) {
  422. if ($profit >= 0) {
  423. M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
  424. $ubill['uid'] = $uid;
  425. $ubill['username'] = $uinfo['username'];
  426. $ubill['num'] = $tprofit;
  427. $ubill['coinname'] = "lever";
  428. $ubill['afternum'] = $user['money'] + $tprofit;
  429. $ubill['type'] = 20;
  430. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  431. $ubill['st'] = 1;
  432. $ubill['remark'] = L('合约交易自動平仓盈利收益');
  433. $ubillre = M("bill")->add($ubill);
  434. }
  435. }
  436. }
  437. echo "=订单ID:" . $id . ",爆仓=";
  438. }
  439. }
  440. }
  441. }
  442. }
  443. // echo "正常请求".$close;
  444. } else {
  445. echo "未获取到参数" . $id;
  446. }
  447. }
  448. }
  449. }
  450. //永续合约委托定单自动建仓
  451. public function AutoCreateOrder()
  452. {
  453. $list = M("leverorder")->where(array('status' => 1))->select();
  454. if (!empty($list)) {
  455. $sysinfo = M("leversetting")->where(array('id' => 1))->find();
  456. $lever_fee = $sysinfo['lever_fee'];//费率
  457. foreach ($list as $key => $vo) {
  458. $id = $vo['id'];
  459. $uid = $vo['uid'];
  460. $uinfo = M("user")->where(array('id' => $uid))->field("id,username")->find();
  461. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  462. $usermoney = M('levermoney')->where(['uid' => $uid])->find();
  463. $lowercoin = $vo['coinname'];
  464. if ($lowercoin == 'ukbusdt') {
  465. $o_symbol = $vo['symbol'];
  466. $symbol_arr = explode('/', $o_symbol);
  467. $symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
  468. $mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
  469. $close = $mlist['new_price'];
  470. } else {
  471. $oy_list = do_request(oy_url($vo['coinname'], 'api/v5/market/ticker?instId='), []);
  472. $oy_list = $oy_list->data[0];
  473. $close = $oy_list->last;//现价
  474. }
  475. if ($close) {
  476. //手续费计算(开仓价格 * 开仓数量 * 手续费率) ,开仓时扣除手续费
  477. $wt_price = $vo['wt_price']; //委托价格
  478. $direction = $vo['direction']; //交易方向
  479. if ($direction == 1) { //做多(上涨) 当前价小于等于委托价格就开仓 要跌了才开仓
  480. if ($close <= $wt_price) {
  481. $data['fee'] = $fee = $vo['fee'];
  482. if ($usermoney['money'] >= $fee) {
  483. $data['price'] = $close;
  484. $data['status'] = 2;
  485. $data['addtime'] = date("Y-m-d H:i:s", time());
  486. $re_a = M("leverorder")->where(array('id' => $id))->save($data);
  487. $re_b = M("levermoney")->where(array('uid' => $uid))->setDec('money', $fee);
  488. $ubill['uid'] = $uid;
  489. $ubill['username'] = $uinfo['username'];
  490. $ubill['num'] = $fee;
  491. $ubill['coinname'] = "lever";
  492. $ubill['afternum'] = $usermoney['money'] - $fee;
  493. $ubill['type'] = 25;
  494. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  495. $ubill['st'] = 2;
  496. $ubill['remark'] = L('合约交易开仓手续费');
  497. $re_c = M("bill")->add($ubill);
  498. if ($re_a && $re_b && $re_c) {
  499. echo "=订单ID:" . $id . ",开仓成功=";
  500. }
  501. } else {
  502. echo "=订单ID:" . $id . ",手续费不足=";
  503. }
  504. } else {
  505. echo "=订单ID:" . $id . ",做多,价格不合适=";
  506. }
  507. } else if ($direction == 2) { //做空(下跌) 当前价大于等于委托价格就开仓 要涨了才开仓
  508. if ($close >= $wt_price) {
  509. $data['fee'] = $fee = $vo['fee'];
  510. if ($usermoney['money'] >= $fee) {
  511. $data['price'] = $close;
  512. $data['status'] = 2;
  513. $data['addtime'] = date("Y-m-d H:i:s", time());
  514. $re_a = M("leverorder")->where(array('id' => $id))->save($data);
  515. $re_b = M("levermoney")->where(array('uid' => $uid))->setDec('money', $fee);
  516. $ubill['uid'] = $uid;
  517. $ubill['username'] = $uinfo['username'];
  518. $ubill['num'] = $fee;
  519. $ubill['coinname'] = "usdt";
  520. $ubill['afternum'] = $usermoney['money'] - $fee;
  521. $ubill['type'] = 25;
  522. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  523. $ubill['st'] = 2;
  524. $ubill['remark'] = L('合约交易开仓手续费');
  525. $re_c = M("bill")->add($ubill);
  526. if ($re_a && $re_b && $re_c) {
  527. echo "=订单ID:" . $id . ",开仓成功=";
  528. }
  529. } else {
  530. echo "=订单ID:" . $id . ",手续费不足=";
  531. }
  532. } else {
  533. echo "=订单ID:" . $id . ",做空,价格不合适=";
  534. }
  535. }
  536. }else{
  537. echo "未获取到参数";
  538. }
  539. }
  540. }
  541. }
  542. //自动释放冻结的认购币,设置计划任务,每天执行一次
  543. public function releaseissue()
  544. {
  545. $nowday = date("Y-m-d", time());
  546. $map['status'] = 1;
  547. $map['endday'] = array('elt', $nowday);
  548. $list = M("issue_log")->where($map)->select();
  549. if (!empty($list)) {
  550. foreach ($list as $key => $vo) {
  551. $id = $vo['id'];
  552. $uid = $vo['uid'];
  553. $num = $vo['num'];
  554. $cname = trim($vo['coinname']);
  555. $cnamed = trim($vo['coinname']) . "d";
  556. //修改记录状态
  557. $result = M("issue_log")->where(array('id' => $id))->save(array('status' => 2));
  558. if ($result) {
  559. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  560. //扣除冻结的资产
  561. M("user_coin")->where(array('userid' => $uid))->setDec($cnamed, $num);
  562. //增加可用资产的数量
  563. M("user_coin")->where(array('userid' => $uid))->setInc($cname, $num);
  564. //写入日志
  565. $data['uid'] = $uid;
  566. $data['username'] = $vo['account'];
  567. $data['num'] = $num;
  568. $data['coinname'] = $cname;
  569. $data['afternum'] = $minfo[$cname] + $num;
  570. $data['type'] = 18;
  571. $data['addtime'] = date("Y-m-d H:i:s", time());
  572. $data['st'] = 1;
  573. $data['remark'] = L("认购资产释放");
  574. M("bill")->add($data);
  575. echo "==认购记录ID:" . $id . "释放成功";
  576. } else {
  577. echo "==认购记录ID:" . $id . "释放失败";
  578. }
  579. }
  580. } else {
  581. echo "==没有可释放认购记录==";
  582. }
  583. }
  584. //委托订单自动交易
  585. //设置成5-10秒执行一次的计划任务
  586. public function autoxjtade()
  587. {
  588. $list = M("bborder")->where(array('ordertype' => 1, 'status' => 1))->select();
  589. if (!empty($list)) {
  590. foreach ($list as $k => $v) {
  591. $type = $v['type'];
  592. $uid = $v['uid'];
  593. $id = $v['id'];
  594. $symbol = strtolower($v['coin']) . 'usdt';
  595. $lowercoin = strtolower($v['coin']);
  596. //限价单价
  597. $xjprice = $v['xjprice'];
  598. $sxfbl = $v['sxfbl'];
  599. if ($lowercoin == "ukb") {
  600. $priceinfo = M("market")->where(array('name' => "ukb_usdt"))->field("new_price")->find();
  601. $newprice = $priceinfo['new_price'];
  602. }else if ($lowercoin == "gcb") {
  603. $priceinfo = M("market")->where(array('name' => "gcb_usdt"))->field("new_price")->find();
  604. $newprice = $priceinfo['new_price'];
  605. } else {
  606. //获取当前行情价
  607. $oy_list = do_request(oy_url(strtolower($v['coin']), 'api/v5/market/ticker?instId='), []);
  608. $oy_list = $oy_list->data[0];
  609. $newprice = $oy_list->last;//现价
  610. // $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  611. // $newprice = $this->getnewprice($coinapi);
  612. }
  613. if ($newprice){
  614. //买入,当行情价小于等于限价时则交易
  615. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  616. if ($type == 1) {
  617. $usdtnum = $v['usdtnum'];
  618. if ($newprice <= $xjprice) {
  619. //计算能够买到的量
  620. $buy_coinnum = sprintf("%.8f", ($usdtnum / $newprice));
  621. //手续费
  622. $fee = $buy_coinnum * $sxfbl / 100;
  623. //实际到账号的金额
  624. $tcoinnum = $buy_coinnum - $fee;
  625. //更新订单
  626. $data['coinnum'] = $tcoinnum;
  627. $data['price'] = $newprice;
  628. $data['tradetime'] = date("Y-m-d H:i:s", time());
  629. $data['fee'] = $fee;
  630. $data['status'] = 2;
  631. $savere = M("bborder")->where(array('id' => $id))->save($data);
  632. //增加购买数量并写入日志
  633. $incre = M("user_coin")->where(array('userid' => $uid))->setInc($lowercoin, $tcoinnum);
  634. $cincbill['uid'] = $uid;
  635. $cincbill['username'] = $v['account'];
  636. $cincbill['num'] = $tcoinnum;
  637. $cincbill['coinname'] = $lowercoin;
  638. $cincbill['afternum'] = $minfo[$lowercoin] + $tcoinnum;
  639. $cincbill['type'] = 10;
  640. $cincbill['addtime'] = date("Y-m-d H:i:s", time());
  641. $cincbill['st'] = 1;
  642. $cincbill['remark'] = L('币币交易限价购买委托成交');
  643. $cincre = M("bill")->add($cincbill);
  644. //扣除冻结的USDT并写入日志
  645. $decre = M("user_coin")->where(array('userid' => $uid))->setDec("usdtd", $usdtnum);
  646. $uincbill['uid'] = $uid;
  647. $uincbill['username'] = $v['account'];
  648. $uincbill['num'] = $usdtnum;
  649. $uincbill['coinname'] = "usdt";
  650. $uincbill['afternum'] = $minfo['usdt'];
  651. $uincbill['type'] = 9;
  652. $uincbill['addtime'] = date("Y-m-d H:i:s", time());
  653. $uincbill['st'] = 2;
  654. $uincbill['remark'] = L('币币交易限价购买委托成交');
  655. $uincre = M("bill")->add($uincbill);
  656. if ($savere && $cincre && $uincre) {
  657. $notice['uid'] = $uid;
  658. $notice['account'] = $v['account'];
  659. $notice['title'] = L('币币交易限价委托交易');
  660. $notice['content'] = L('币币交易限价购买委托订单购买成功');
  661. $notice['addtime'] = date("Y-m-d H:i:s", time());
  662. $notice['status'] = 1;
  663. M("notice")->add($notice);
  664. echo "==委托订单ID:" . $id . ",购买成功==";
  665. }
  666. } else {
  667. echo "==委托订单ID:" . $id . ",没有达到限价购买价格==";
  668. }
  669. //卖出,当行情价大于等于限价时则交易
  670. } elseif ($type == 2) {
  671. $coinnum = $v['coinnum'];
  672. if ($newprice >= $xjprice) {
  673. //求出卖出所得的USDT量
  674. $allusdt = sprintf("%.8f", ($coinnum * $newprice));
  675. //求出手续费
  676. $fee = $allusdt * $sxfbl / 100;
  677. //求出实际到账USDT量
  678. $tusdtnum = $allusdt - $fee;
  679. //更新订单
  680. $data['usdtnum'] = $tusdtnum;
  681. $data['price'] = $newprice;
  682. $data['tradetime'] = date("Y-m-d H:i:s", time());
  683. $data['fee'] = $fee;
  684. $data['status'] = 2;
  685. $savere = M("bborder")->where(array('id' => $id))->save($data);
  686. //增加卖出所得的USDT量并写入日志
  687. $incre = M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $tusdtnum);
  688. $uincbill['uid'] = $uid;
  689. $uincbill['username'] = $v['account'];
  690. $uincbill['num'] = $tusdtnum;
  691. $uincbill['coinname'] = 'usdt';
  692. $uincbill['afternum'] = $minfo['usdt'] + $tusdtnum;
  693. $uincbill['type'] = 9;
  694. $uincbill['addtime'] = date("Y-m-d H:i:s", time());
  695. $uincbill['st'] = 1;
  696. $uincbill['remark'] = L('币币交易限价出售委托成交');
  697. $uincre = M("bill")->add($uincbill);
  698. //扣除冻结的卖出币量并写入日志
  699. $decre = M("user_coin")->where(array('userid' => $uid))->setDec($lowercoin . "d", $coinnum);
  700. $cincbill['uid'] = $uid;
  701. $cincbill['username'] = $v['account'];
  702. $cincbill['num'] = $coinnum;
  703. $cincbill['coinname'] = $lowercoin;
  704. $cincbill['afternum'] = $minfo[$lowercoin] - $coinnum;
  705. $cincbill['type'] = 10;
  706. $cincbill['addtime'] = date("Y-m-d H:i:s", time());
  707. $cincbill['st'] = 2;
  708. $cincbill['remark'] = L('币币交易限价出售委托成交');
  709. $cincre = M("bill")->add($cincbill);
  710. if ($savere && $cincre && $uincre) {
  711. $notice['uid'] = $uid;
  712. $notice['account'] = $v['account'];
  713. $notice['title'] = L('币币交易限价委托交易');
  714. $notice['content'] = L('币币交易限价购买委托订单出售成功');
  715. $notice['addtime'] = date("Y-m-d H:i:s", time());
  716. $notice['status'] = 1;
  717. M("notice")->add($notice);
  718. echo "==委托订单ID:" . $id . ",出售成功==";
  719. }
  720. } else {
  721. echo "==委托订单ID:" . $id . ",没有达到限价出售价格==";
  722. }
  723. }
  724. }else{
  725. echo "未获取到参数";
  726. }
  727. }
  728. } else {
  729. echo "没有限价委托可交易!";
  730. }
  731. }
  732. //释放冻结的矿机收益币
  733. //设置一天执行一次的计划任务
  734. public function releasedjprofit()
  735. {
  736. $nowday = date("Y-m-d", time());
  737. $where['thawday'] = array('elt', $nowday);
  738. $where['status'] = array('eq', 1);
  739. $list = M("djprofit")->where($where)->select();
  740. if (!empty($list)) {
  741. foreach ($list as $key => $vo) {
  742. $id = $vo['id'];
  743. $uid = $vo['uid'];
  744. $username = $vo['username'];
  745. $num = $vo['num'];
  746. $coinname = trim($vo['coin']);
  747. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  748. //修改冻结状态
  749. M("djprofit")->where(array('id' => $id))->save(array('status' => 2));
  750. //添加财务日志
  751. $billdata['uid'] = $uid;
  752. $billdata['username'] = $username;
  753. $billdata['num'] = $num;
  754. $billdata['coinname'] = $coinname;
  755. $billdata['afternum'] = $minfo[$coinname] + $num;
  756. $billdata['type'] = 8;
  757. $billdata['addtime'] = date("Y-m-d H:i:s", time());
  758. $billdata['st'] = 1;
  759. $billdata['remark'] = L('释放冻结收益');
  760. M("bill")->add($billdata);
  761. //增加会员资产,减少冻结额度
  762. $coinname_d = $coinname . "d";
  763. M("user_coin")->where(array('userid' => $uid))->setDec($coinname_d, $num);
  764. M("user_coin")->where(array('userid' => $uid))->setInc($coinname, $num);
  765. $notice['uid'] = $uid;
  766. $notice['account'] = $username;
  767. $notice['title'] = L('释放冻结收益');
  768. $notice['content'] = L('您冻结的矿机收益释放成功,可以交易');
  769. $notice['addtime'] = date("Y-m-d H:i:s", time());
  770. $notice['status'] = 1;
  771. M("notice")->add($notice);
  772. echo "==ID:" . $id . "释放" . $num . $coinname . "成功==";
  773. echo "<br />";
  774. }
  775. } else {
  776. echo "====没有可释放的冻结记录====";
  777. }
  778. }
  779. //共享矿机自动结算收益,设置一天执行一次的计划任务
  780. public function authsharesjsy()
  781. {
  782. $kjlist = M("kjorder")->where(array('status' => 1, 'type' => 2))->select();
  783. if (!empty($kjlist)) {
  784. foreach ($kjlist as $key => $vo) {
  785. $id = $vo['id'];
  786. $uid = $vo['uid'];
  787. $username = $vo['username'];
  788. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  789. $kid = $vo['kid'];
  790. $nowdate = date("Y-m-d", time());
  791. $profitinfo = M("kjprofit")->where(array('uid' => $uid, 'kid' => $id, 'day' => $nowdate))->find();
  792. if (empty($profitinfo)) {
  793. $sharbltxt = $vo['sharbltxt'];
  794. if ($sharbltxt <= 0) {
  795. echo "===共享矿机ID" . $id . "共享码有误===";
  796. } else {
  797. $sharekj = M("kjorder")->where(array('sharbltxt' => $sharbltxt))->count();
  798. if ($sharekj >= 2) {
  799. //查找矿机收益的类型以及查找收益是否需要冻结及冻结天数
  800. $outtype = $vo['outtype'];
  801. if ($outtype == 1) {//按产值需要查找产出币种的最新行情
  802. $coinname = strtolower(trim($vo['outcoin']));
  803. $outnum = $vo['outusdt'];
  804. $symbol = $coinname . 'usdt';
  805. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  806. $newprice = $this->getnewprice($coinapi);
  807. $tcoinnum = sprintf("%.6f", ($outnum / $newprice)); //实际产生的币量,保留6位小数
  808. } elseif ($outtype == 2) {
  809. $coinname = strtolower(trim($vo['outcoin']));
  810. $tcoinnum = $vo['outnum'];
  811. }
  812. $djout = $vo['djout'];//1冻结2不冻结
  813. $djday = $vo['djnum'];//冻结天数
  814. //写入矿机收益日志
  815. $kjprofit_d['uid'] = $uid;
  816. $kjprofit_d['username'] = $username;
  817. $kjprofit_d['kid'] = $id;
  818. $kjprofit_d['ktitle'] = $vo['kjtitle'];
  819. $kjprofit_d['num'] = $tcoinnum;
  820. $kjprofit_d['coin'] = $coinname;
  821. $kjprofit_d['addtime'] = date("Y-m-d H:i:s", time());
  822. $kjprofit_d['day'] = date("Y-m-d", time());
  823. M("kjprofit")->add($kjprofit_d);
  824. if ($djout == 2) {
  825. $coin_d = $coinname . "d";
  826. M("user_coin")->where(array('userid' => $uid))->setInc($coin_d, $tcoinnum);
  827. $djprofit_d['uid'] = $uid;
  828. $djprofit_d['username'] = $username;
  829. $djprofit_d['num'] = $tcoinnum;
  830. $djprofit_d['coin'] = $coinname;
  831. $djprofit_d['status'] = 1;
  832. $djprofit_d['addtime'] = date("Y-m-d H:i:s", time());
  833. $djprofit_d['addday'] = date("Y-m-d", time());
  834. $djprofit_d['thawtime'] = date("Y-m-d H:i:s", (time() + 86400 * $djday));
  835. $djprofit_d['thawday'] = date("Y-m-d", (time() + 86400 * $djday));
  836. $djprofit_d['remark'] = L('冻结矿机释放收益');
  837. M("djprofit")->add($djprofit_d);
  838. //写资金日志
  839. $billdata['uid'] = $uid;
  840. $billdata['username'] = $username;
  841. $billdata['num'] = $tcoinnum;
  842. $billdata['coinname'] = $coinname;
  843. $billdata['afternum'] = $minfo[$coin_d] + $tcoinnum;
  844. $billdata['type'] = 7;
  845. $billdata['addtime'] = date("Y-m-d H:i:s", time());
  846. $billdata['st'] = 1;
  847. $billdata['remark'] = L('矿机收益释放冻结');
  848. M("bill")->add($billdata);
  849. $notice['uid'] = $uid;
  850. $notice['account'] = $username;
  851. $notice['title'] = L('矿机收益');
  852. $notice['content'] = L('今日矿机收益已成功到账,请注册查收');
  853. $notice['addtime'] = date("Y-m-d H:i:s", time());
  854. $notice['status'] = 1;
  855. M("notice")->add($notice);
  856. } elseif ($djout == 1) {
  857. M("user_coin")->where(array('userid' => $uid))->setInc($coinname, $tcoinnum);
  858. //写资金日志
  859. $billdata['uid'] = $uid;
  860. $billdata['username'] = $username;
  861. $billdata['num'] = $tcoinnum;
  862. $billdata['coinname'] = $coinname;
  863. $billdata['afternum'] = $minfo[$coinname] + $tcoinnum;
  864. $billdata['type'] = 8;
  865. $billdata['addtime'] = date("Y-m-d H:i:s", time());
  866. $billdata['st'] = 1;
  867. $billdata['remark'] = L('矿机收益释放');
  868. M("bill")->add($billdata);
  869. }
  870. //修改矿机收益次数
  871. M("kjorder")->where(array('id' => $id))->setDec("synum", 1);
  872. $reinfo = M("kjorder")->where(array('id' => $id))->find();
  873. if ($reinfo['synum'] < 1) {
  874. M("kjorder")->where(array('id' => $id))->save(array('status' => 3));
  875. }
  876. echo "==共享矿机ID:" . $kid . "收益成功==";
  877. echo "<br />";
  878. } else {
  879. echo "===共享矿机ID" . $id . "另一部分没有购买,不能收益===";
  880. }
  881. }
  882. } else {
  883. echo "==矿机ID:" . $kid . "不能重复收益==";
  884. echo "<br />";
  885. }
  886. }
  887. }
  888. }
  889. //独资矿机自动收益,每天执行一次
  890. //设置一天执行一次的计划任务
  891. public function autokjsy()
  892. {
  893. $kjlist = M("kjorder")->where(array('status' => 1, 'type' => 1))->select();
  894. if (!empty($kjlist)) {
  895. foreach ($kjlist as $key => $vo) {
  896. $id = $vo['id'];
  897. $uid = $vo['uid'];
  898. $username = $vo['username'];
  899. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  900. $kid = $vo['kid'];
  901. $nowdate = date("Y-m-d", time());
  902. $profitinfo = M("kjprofit")->where(array('uid' => $uid, 'kid' => $id, 'day' => $nowdate))->find();
  903. if (empty($profitinfo)) {
  904. //查找矿机收益的类型以及查找收益是否需要冻结及冻结天数
  905. $outtype = $vo['outtype'];
  906. if ($outtype == 1) {//按产值需要查找产出币种的最新行情
  907. $coinname = strtolower(trim($vo['outcoin']));
  908. $outnum = $vo['outusdt'];
  909. $symbol = $coinname . 'usdt';
  910. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  911. $newprice = $this->getnewprice($coinapi);
  912. $tcoinnum = sprintf("%.6f", ($outnum / $newprice)); //实际产生的币量,保留6位小数
  913. } elseif ($outtype == 2) {
  914. $coinname = strtolower(trim($vo['outcoin']));
  915. $tcoinnum = $vo['outnum'];
  916. }
  917. $djout = $vo['djout'];//1冻结2不冻结
  918. $djday = $vo['djnum'];//冻结天数
  919. //写入矿机收益日志
  920. $kjprofit_d['uid'] = $uid;
  921. $kjprofit_d['username'] = $username;
  922. $kjprofit_d['kid'] = $id;
  923. $kjprofit_d['ktitle'] = $vo['kjtitle'];
  924. $kjprofit_d['num'] = $tcoinnum;
  925. $kjprofit_d['coin'] = $coinname;
  926. $kjprofit_d['addtime'] = date("Y-m-d H:i:s", time());
  927. $kjprofit_d['day'] = date("Y-m-d", time());
  928. M("kjprofit")->add($kjprofit_d);
  929. if ($djout == 2) {
  930. $coin_d = $coinname . "d";
  931. M("user_coin")->where(array('userid' => $uid))->setInc($coin_d, $tcoinnum);
  932. $djprofit_d['uid'] = $uid;
  933. $djprofit_d['username'] = $username;
  934. $djprofit_d['num'] = $tcoinnum;
  935. $djprofit_d['coin'] = $coinname;
  936. $djprofit_d['status'] = 1;
  937. $djprofit_d['addtime'] = date("Y-m-d H:i:s", time());
  938. $djprofit_d['addday'] = date("Y-m-d", time());
  939. $djprofit_d['thawtime'] = date("Y-m-d H:i:s", (time() + 86400 * $djday));
  940. $djprofit_d['thawday'] = date("Y-m-d", (time() + 86400 * $djday));
  941. $djprofit_d['remark'] = L('冻结矿机释放收益');
  942. M("djprofit")->add($djprofit_d);
  943. //写资金日志
  944. $billdata['uid'] = $uid;
  945. $billdata['username'] = $username;
  946. $billdata['num'] = $tcoinnum;
  947. $billdata['coinname'] = $coinname;
  948. $billdata['afternum'] = $minfo[$coin_d] + $tcoinnum;
  949. $billdata['type'] = 7;
  950. $billdata['addtime'] = date("Y-m-d H:i:s", time());
  951. $billdata['st'] = 1;
  952. $billdata['remark'] = L('矿机收益释放冻结');
  953. M("bill")->add($billdata);
  954. $notice['uid'] = $uid;
  955. $notice['account'] = $username;
  956. $notice['title'] = L('矿机收益');
  957. $notice['content'] = L('今日矿机收益已成功到账,请注册查收');
  958. $notice['addtime'] = date("Y-m-d H:i:s", time());
  959. $notice['status'] = 1;
  960. M("notice")->add($notice);
  961. } elseif ($djout == 1) {
  962. M("user_coin")->where(array('userid' => $uid))->setInc($coinname, $tcoinnum);
  963. //写资金日志
  964. $billdata['uid'] = $uid;
  965. $billdata['username'] = $username;
  966. $billdata['num'] = $tcoinnum;
  967. $billdata['coinname'] = $coinname;
  968. $billdata['afternum'] = $minfo[$coinname] + $tcoinnum;
  969. $billdata['type'] = 8;
  970. $billdata['addtime'] = date("Y-m-d H:i:s", time());
  971. $billdata['st'] = 1;
  972. $billdata['remark'] = L('矿机收益释放');
  973. M("bill")->add($billdata);
  974. }
  975. //修改矿机收益次数
  976. M("kjorder")->where(array('id' => $id))->setDec("synum", 1);
  977. $reinfo = M("kjorder")->where(array('id' => $id))->find();
  978. if ($reinfo['synum'] < 1) {
  979. M("kjorder")->where(array('id' => $id))->save(array('status' => 3));
  980. }
  981. echo "==矿机ID:" . $kid . "收益成功==";
  982. echo "<br />";
  983. } else {
  984. echo "==矿机ID:" . $kid . "不能重复收益==";
  985. echo "<br />";
  986. }
  987. }
  988. } else {
  989. echo "++||没有正常运行的矿机||++";
  990. }
  991. }
  992. //休验订单自动按风控比例设置订单的盈亏比例
  993. //设置成5-10秒执行一次的计划任务
  994. public function setwl_ty()
  995. {
  996. $map['status'] = 1;
  997. $map['kongyk'] = 0;
  998. $orderobj = M("tyhyorder");
  999. $count = $orderobj->where($map)->count();
  1000. $setting = M("hysetting")->where(array('id' => 1))->field("hy_fkgl")->find();
  1001. if ($setting['hy_fkgl'] > 0) {
  1002. $ylcount = intval($count * $setting['hy_fkgl'] / 100);
  1003. $kscount = $count - $ylcount;
  1004. if ($ylcount > 0) {
  1005. $yllist = $orderobj->where($map)->order("num asc")->limit($ylcount)->select();
  1006. if (!empty($yllist)) {
  1007. foreach ($yllist as $k => $v) {
  1008. $yid = $v['id'];
  1009. $orderobj->where(array('id' => $yid))->save(array('kongyk' => 1));
  1010. echo "订单ID:" . $yid . "设为盈利==";
  1011. }
  1012. }
  1013. }
  1014. if ($kscount > 0) {
  1015. $kslist = $orderobj->where($map)->order("num asc")->limit($kscount)->select();
  1016. if (!empty($kslist)) {
  1017. foreach ($kslist as $k => $v) {
  1018. $kid = $v['id'];
  1019. $orderobj->where(array('id' => $kid))->save(array('kongyk' => 2));
  1020. echo "订单ID:" . $kid . "设为亏损==";
  1021. }
  1022. }
  1023. }
  1024. }
  1025. echo "操作成功";
  1026. }
  1027. //自动按风控比例设置订单的盈亏比例
  1028. //设置成5-10秒执行一次的计划任务
  1029. public function setwl()
  1030. {
  1031. $map['status'] = 1;
  1032. $map['kongyk'] = 0;
  1033. $orderobj = M("hyorder");
  1034. $count = $orderobj->where($map)->count();
  1035. $setting = M("hysetting")->where(array('id' => 1))->field("hy_fkgl")->find();
  1036. if ($setting['hy_fkgl'] > 0) {
  1037. $ylcount = intval($count * $setting['hy_fkgl'] / 100);
  1038. $kscount = $count - $ylcount;
  1039. if ($ylcount > 0) {
  1040. $yllist = $orderobj->where($map)->order("num asc")->limit($ylcount)->select();
  1041. if (!empty($yllist)) {
  1042. foreach ($yllist as $k => $v) {
  1043. $yid = $v['id'];
  1044. $orderobj->where(array('id' => $yid))->save(array('kongyk' => 1));
  1045. echo "订单ID:" . $yid . "设为盈利==";
  1046. }
  1047. }
  1048. }
  1049. if ($kscount > 0) {
  1050. $kslist = $orderobj->where($map)->order("num asc")->limit($kscount)->select();
  1051. if (!empty($kslist)) {
  1052. foreach ($kslist as $k => $v) {
  1053. $kid = $v['id'];
  1054. $orderobj->where(array('id' => $kid))->save(array('kongyk' => 2));
  1055. echo "订单ID:" . $kid . "设为亏损==";
  1056. }
  1057. }
  1058. }
  1059. }
  1060. echo "操作成功";
  1061. }
  1062. //自动结算合约订单
  1063. public function hycarryout_ty()
  1064. {
  1065. $nowtime = time();
  1066. $map['status'] = 1;
  1067. $map['intselltime'] = array('elt', $nowtime);
  1068. $orderobj = M("tyhyorder");
  1069. $list = $orderobj->where($map)->select();
  1070. if (!empty($list)) {
  1071. foreach ($list as $key => $vo) {
  1072. $coinname = $vo['coinname'];
  1073. $coinarr = explode("/", $coinname);
  1074. $symbol = strtolower($coinarr[0]) . strtolower($coinarr[1]);
  1075. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  1076. $newprice = $this->getnewprice($coinapi);
  1077. $randnum = "0." . rand(1000, 9999);
  1078. $buyprice = $vo['buyprice'];
  1079. $otype = $vo['hyzd']; //合约方向
  1080. $dkong = $vo['kongyk']; //单控设置
  1081. $uid = $vo['uid'];//会员ID
  1082. $id = $vo['id'];//记录ID
  1083. $num = $vo['num'];
  1084. $hybl = $vo['hybl']; //收益比例
  1085. $ylnum = $num * ($hybl / 100); //盈利金额
  1086. $money = $num + $ylnum;//本金+盈利金额
  1087. //$dkong分三种情况 1、0表示随行情,1表示盈利 2 表示亏损
  1088. //盈利时增加$money,,亏损时亏本金
  1089. if ($dkong == 0) {
  1090. //买涨
  1091. if ($otype == 1) { //买跌
  1092. if ($newprice > $buyprice) { //盈利
  1093. //增加资产
  1094. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1095. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1096. //修改订单状态
  1097. $sd['status'] = 2;
  1098. $sd['is_win'] = 1;
  1099. $sd['sellprice'] = $newprice;
  1100. $sd['ploss'] = $ylnum;
  1101. $orderobj->where(array('id' => $id))->save($sd);
  1102. //写财务日志
  1103. //$this->addlog($uid,$vo['username'],$money);
  1104. } else {//亏损
  1105. //修改订单状态
  1106. $sd['status'] = 2;
  1107. $sd['is_win'] = 2;
  1108. $sd['sellprice'] = $newprice;
  1109. $sd['ploss'] = $num;
  1110. $orderobj->where(array('id' => $id))->save($sd);
  1111. }
  1112. } elseif ($otype == 2) { //买跌
  1113. if ($newprice < $buyprice) { //盈利
  1114. //增加资产
  1115. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1116. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1117. //修改订单状态
  1118. $sd['status'] = 2;
  1119. $sd['is_win'] = 1;
  1120. $sd['sellprice'] = $newprice;
  1121. $sd['ploss'] = $ylnum;
  1122. $orderobj->where(array('id' => $id))->save($sd);
  1123. //写财务日志
  1124. //$this->addlog($uid,$vo['username'],$money);
  1125. } else {//亏损
  1126. //修改订单状态
  1127. $sd['status'] = 2;
  1128. $sd['is_win'] = 2;
  1129. $sd['sellprice'] = $newprice;
  1130. $sd['ploss'] = $num;
  1131. $orderobj->where(array('id' => $id))->save($sd);
  1132. }
  1133. }
  1134. } elseif ($dkong == 1) {//单控盈利
  1135. if ($otype == 1) {//买涨
  1136. if ($newprice > $buyprice) {
  1137. $sellprice = $newprice;
  1138. } elseif ($newprice == $buyprice) {
  1139. $sellprice = $newprice + $randnum;
  1140. } elseif ($newprice < $buyprice) {
  1141. $sellprice = $buyprice + $randnum;
  1142. }
  1143. //增加资产
  1144. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1145. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1146. //修改订单状态
  1147. $sd['status'] = 2;
  1148. $sd['is_win'] = 1;
  1149. $sd['sellprice'] = $sellprice;
  1150. $sd['ploss'] = $ylnum;
  1151. $orderobj->where(array('id' => $id))->save($sd);
  1152. //写财务日志
  1153. //$this->addlog($uid,$vo['username'],$money);
  1154. } elseif ($otype == 2) {//买跌
  1155. if ($newprice > $buyprice) {
  1156. $sellprice = $buyprice - $randnum;
  1157. } elseif ($newprice == $buyprice) {
  1158. $sellprice = $buyprice - $randnum;
  1159. } elseif ($newprice < $buyprice) {
  1160. $sellprice = $newprice;
  1161. }
  1162. //增加资产
  1163. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1164. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1165. //修改订单状态
  1166. $sd['status'] = 2;
  1167. $sd['is_win'] = 1;
  1168. $sd['sellprice'] = $sellprice;
  1169. $sd['ploss'] = $ylnum;
  1170. $orderobj->where(array('id' => $id))->save($sd);
  1171. //写财务日志
  1172. //$this->addlog($uid,$vo['username'],$money);
  1173. }
  1174. } elseif ($dkong == 2) {
  1175. if ($otype == 1) {//买涨
  1176. //买涨,指定亏损,结算价格要低于买入价格
  1177. if ($newprice > $buyprice) {
  1178. $sellprice = $buyprice - $randnum;
  1179. } elseif ($newprice == $buyprice) {
  1180. $sellprice = $buyprice - $randnum;
  1181. } elseif ($newprice < $buyprice) {
  1182. $sellprice = $newprice;
  1183. }
  1184. //修改订单状态
  1185. $sd['status'] = 2;
  1186. $sd['is_win'] = 2;
  1187. $sd['sellprice'] = $sellprice;
  1188. $sd['ploss'] = $num;
  1189. $orderobj->where(array('id' => $id))->save($sd);
  1190. } elseif ($otype == 2) {//买跌
  1191. if ($newprice > $buyprice) {
  1192. $sellprice = $newprice;
  1193. } elseif ($newprice == $buyprice) {
  1194. $sellprice = $buyprice + $randnum;
  1195. } elseif ($newprice < $buyprice) {
  1196. $sellprice = $buyprice + $randnum;
  1197. }
  1198. //修改订单状态
  1199. $sd['status'] = 2;
  1200. $sd['is_win'] = 2;
  1201. $sd['sellprice'] = $sellprice;
  1202. $sd['ploss'] = $num;
  1203. $orderobj->where(array('id' => $id))->save($sd);
  1204. }
  1205. }
  1206. echo "==订单ID:" . $id . "出售成功==";
  1207. }
  1208. } else {
  1209. echo "没有订单可以结算!";
  1210. }
  1211. }
  1212. //自动结算体验合约订单
  1213. public function hycarryout_ty_old()
  1214. {
  1215. $nowtime = time();
  1216. $map['status'] = 1;
  1217. $map['intselltime'] = array('elt', $nowtime);
  1218. $orderobj = M("tyhyorder");
  1219. $list = $orderobj->where($map)->select();
  1220. //获取合约参数
  1221. $setting = M("hysetting")->where(array('id' => 1))->field("hy_ksid,hy_ylid,hy_fkgl")->find();
  1222. //指定盈利ID组
  1223. $winarr = explode(',', $setting['hy_ylid']);
  1224. //指定亏损ID组
  1225. $lossarr = explode(',', $setting['hy_ksid']);
  1226. //风控比例组
  1227. $fkarr = explode(',', $setting['hy_fkgl']);
  1228. if (!empty($list)) {
  1229. foreach ($list as $key => $vo) {
  1230. $coinname = $vo['coinname'];
  1231. $coinarr = explode("/", $coinname);
  1232. $symbol = strtolower($coinarr[0]) . strtolower($coinarr[1]);
  1233. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  1234. $newprice = $this->getnewprice($coinapi);
  1235. $randnum = "0." . rand(1000, 9999);
  1236. $buyprice = $vo['buyprice'];
  1237. $otype = $vo['hyzd']; //合约方向
  1238. $dkong = $vo['kongyk']; //单控设置
  1239. $uid = $vo['uid'];//会员ID
  1240. $id = $vo['id'];//记录ID
  1241. $num = $vo['num'];
  1242. $hybl = $vo['hybl'];
  1243. $ylnum = $num * ($hybl / 100);
  1244. $money = $num + $ylnum;//盈利金额
  1245. //买涨
  1246. if ($otype == 1) {
  1247. if (in_array($uid, $winarr)) {//如果有指定盈利ID,则按盈利结算
  1248. if ($newprice > $buyprice) {
  1249. $sellprice = $newprice;
  1250. } elseif ($newprice == $buyprice) {
  1251. $sellprice = $newprice + $randnum;
  1252. } elseif ($newprice < $buyprice) {
  1253. $sellprice = $buyprice + $randnum;
  1254. }
  1255. //增加资产
  1256. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1257. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1258. //修改订单状态
  1259. $sd['status'] = 2;
  1260. $sd['is_win'] = 1;
  1261. $sd['sellprice'] = $sellprice;
  1262. $sd['ploss'] = $ylnum;
  1263. $orderobj->where(array('id' => $id))->save($sd);
  1264. //写财务日志
  1265. //$this->addlog($uid,$vo['username'],$money);
  1266. } elseif (in_array($uid, $lossarr)) {//如果有指定亏损ID,则按亏损结算
  1267. //买涨,指定亏损,结算价格要低于买入价格
  1268. if ($newprice > $buyprice) {
  1269. $sellprice = $buyprice - $randnum;
  1270. } elseif ($newprice == $buyprice) {
  1271. $sellprice = $buyprice - $randnum;
  1272. } elseif ($newprice < $buyprice) {
  1273. $sellprice = $newprice;
  1274. }
  1275. //修改订单状态
  1276. $sd['status'] = 2;
  1277. $sd['is_win'] = 2;
  1278. $sd['sellprice'] = $sellprice;
  1279. $sd['ploss'] = $num;
  1280. $orderobj->where(array('id' => $id))->save($sd);
  1281. } else {//如果未指定盈利和亏损,则按单控的计算
  1282. if ($dkong == 1) {//盈利
  1283. if ($newprice > $buyprice) {
  1284. $sellprice = $newprice;
  1285. } elseif ($newprice == $buyprice) {
  1286. $sellprice = $newprice + $randnum;
  1287. } elseif ($newprice < $buyprice) {
  1288. $sellprice = $buyprice + $randnum;
  1289. }
  1290. // echo '买入价格:'.$buyprice;
  1291. // echo "<br />";
  1292. // echo '结算价格:'.$sellprice;die;
  1293. //增加资产
  1294. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1295. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1296. //修改订单状态
  1297. $sd['status'] = 2;
  1298. $sd['is_win'] = 1;
  1299. $sd['sellprice'] = $sellprice;
  1300. $sd['ploss'] = $ylnum;
  1301. $orderobj->where(array('id' => $id))->save($sd);
  1302. //写财务日志
  1303. //$this->addlog($uid,$vo['username'],$money);
  1304. } elseif ($dkong == 2) {//亏损
  1305. if ($newprice > $buyprice) {
  1306. $sellprice = $buyprice - $randnum;
  1307. } elseif ($newprice == $buyprice) {
  1308. $sellprice = $buyprice - $randnum;
  1309. } elseif ($newprice < $buyprice) {
  1310. $sellprice = $newprice;
  1311. }
  1312. // echo '买入价格:'.$buyprice;
  1313. // echo "<br />";
  1314. // echo '结算价格:'.$sellprice;die;
  1315. //修改订单状态
  1316. $sd['status'] = 2;
  1317. $sd['is_win'] = 2;
  1318. $sd['sellprice'] = $sellprice;
  1319. $sd['ploss'] = $num;
  1320. $orderobj->where(array('id' => $id))->save($sd);
  1321. }
  1322. }
  1323. //买跌
  1324. } elseif ($otype == 2) {
  1325. if (in_array($uid, $winarr)) {//如果有指定盈利ID,则按盈利结算
  1326. if ($newprice > $buyprice) {
  1327. $sellprice = $buyprice - $randnum;
  1328. } elseif ($newprice == $buyprice) {
  1329. $sellprice = $buyprice - $randnum;
  1330. } elseif ($newprice < $buyprice) {
  1331. $sellprice = $newprice;
  1332. }
  1333. //增加资产
  1334. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1335. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1336. //修改订单状态
  1337. $sd['status'] = 2;
  1338. $sd['is_win'] = 1;
  1339. $sd['sellprice'] = $sellprice;
  1340. $sd['ploss'] = $ylnum;
  1341. $orderobj->where(array('id' => $id))->save($sd);
  1342. //写财务日志
  1343. //$this->addlog($uid,$vo['username'],$money);
  1344. } elseif (in_array($uid, $lossarr)) {//如果有指定亏损ID,则按亏损结算
  1345. if ($newprice > $buyprice) {
  1346. $sellprice = $newprice;
  1347. } elseif ($newprice == $buyprice) {
  1348. $sellprice = $buyprice + $randnum;
  1349. } elseif ($newprice < $buyprice) {
  1350. $sellprice = $buyprice + $randnum;
  1351. }
  1352. //修改订单状态
  1353. $sd['status'] = 2;
  1354. $sd['is_win'] = 2;
  1355. $sd['sellprice'] = $sellprice;
  1356. $sd['ploss'] = $num;
  1357. $orderobj->where(array('id' => $id))->save($sd);
  1358. } else {//如果未指定盈利和亏损,则按单控的计算
  1359. if ($dkong == 1) {//盈利
  1360. if ($newprice > $buyprice) {
  1361. $sellprice = $buyprice - $randnum;
  1362. } elseif ($newprice == $buyprice) {
  1363. $sellprice = $buyprice - $randnum;
  1364. } elseif ($newprice < $buyprice) {
  1365. $sellprice = $newprice;
  1366. }
  1367. //增加资产
  1368. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1369. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1370. //修改订单状态
  1371. $sd['status'] = 2;
  1372. $sd['is_win'] = 1;
  1373. $sd['sellprice'] = $sellprice;
  1374. $sd['ploss'] = $ylnum;
  1375. $orderobj->where(array('id' => $id))->save($sd);
  1376. //写财务日志
  1377. //$this->addlog($uid,$vo['username'],$money);
  1378. } elseif ($dkong == 2) {//亏损
  1379. if ($newprice > $buyprice) {
  1380. $sellprice = $newprice;
  1381. } elseif ($newprice == $buyprice) {
  1382. $sellprice = $buyprice + $randnum;
  1383. } elseif ($newprice < $buyprice) {
  1384. $sellprice = $buyprice + $randnum;
  1385. }
  1386. //修改订单状态
  1387. $sd['status'] = 2;
  1388. $sd['is_win'] = 2;
  1389. $sd['sellprice'] = $sellprice;
  1390. $sd['ploss'] = $num;
  1391. $orderobj->where(array('id' => $id))->save($sd);
  1392. }
  1393. }
  1394. }
  1395. echo "==订单ID:" . $id . "出售成功==";
  1396. }
  1397. } else {
  1398. echo "没有订单可以结算!";
  1399. }
  1400. }
  1401. //自动结算合约订单
  1402. public function hycarryout()
  1403. {
  1404. $nowtime = time();
  1405. $map['status'] = 1;
  1406. $map['intselltime'] = array('elt', $nowtime);
  1407. $orderobj = M("hyorder");
  1408. $list = $orderobj->where($map)->select();
  1409. if (!empty($list)) {
  1410. foreach ($list as $key => $vo) {
  1411. $coinname = $vo['coinname'];
  1412. $coinarr = explode("/", $coinname);
  1413. $symbol = strtolower($coinarr[0]) . strtolower($coinarr[1]);
  1414. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  1415. $newprice = $this->getnewprice($coinapi);
  1416. $randnum = "0." . rand(1000, 9999);
  1417. $buyprice = $vo['buyprice'];
  1418. $otype = $vo['hyzd']; //合约方向
  1419. $dkong = $vo['kongyk']; //单控设置
  1420. $uid = $vo['uid'];//会员ID
  1421. $id = $vo['id'];//记录ID
  1422. $num = $vo['num'];
  1423. $hybl = $vo['hybl']; //收益比例
  1424. $ylnum = $num * ($hybl / 100); //盈利金额
  1425. $money = $num + $ylnum;//本金+盈利金额
  1426. //$dkong分三种情况 1、0表示随行情,1表示盈利 2 表示亏损
  1427. //盈利时增加$money,,亏损时亏本金
  1428. if ($dkong == 0) {
  1429. //买涨
  1430. if ($otype == 1) { //买跌
  1431. if ($newprice > $buyprice) { //盈利
  1432. //增加资产
  1433. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1434. //修改订单状态
  1435. $sd['status'] = 2;
  1436. $sd['is_win'] = 1;
  1437. $sd['sellprice'] = $newprice;
  1438. $sd['ploss'] = $ylnum;
  1439. $orderobj->where(array('id' => $id))->save($sd);
  1440. //写财务日志
  1441. $this->addlog($uid, $vo['username'], $money);
  1442. } else {//亏损
  1443. //修改订单状态
  1444. $sd['status'] = 2;
  1445. $sd['is_win'] = 2;
  1446. $sd['sellprice'] = $newprice;
  1447. $sd['ploss'] = $num;
  1448. $orderobj->where(array('id' => $id))->save($sd);
  1449. }
  1450. } elseif ($otype == 2) { //买跌
  1451. if ($newprice < $buyprice) { //盈利
  1452. //增加资产
  1453. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1454. //修改订单状态
  1455. $sd['status'] = 2;
  1456. $sd['is_win'] = 1;
  1457. $sd['sellprice'] = $newprice;
  1458. $sd['ploss'] = $ylnum;
  1459. $orderobj->where(array('id' => $id))->save($sd);
  1460. //写财务日志
  1461. $this->addlog($uid, $vo['username'], $money);
  1462. } else {//亏损
  1463. //修改订单状态
  1464. $sd['status'] = 2;
  1465. $sd['is_win'] = 2;
  1466. $sd['sellprice'] = $newprice;
  1467. $sd['ploss'] = $num;
  1468. $orderobj->where(array('id' => $id))->save($sd);
  1469. }
  1470. }
  1471. } elseif ($dkong == 1) {//单控盈利
  1472. if ($otype == 1) {//买涨
  1473. if ($newprice > $buyprice) {
  1474. $sellprice = $newprice;
  1475. } elseif ($newprice == $buyprice) {
  1476. $sellprice = $newprice + $randnum;
  1477. } elseif ($newprice < $buyprice) {
  1478. $sellprice = $buyprice + $randnum;
  1479. }
  1480. //增加资产
  1481. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1482. //修改订单状态
  1483. $sd['status'] = 2;
  1484. $sd['is_win'] = 1;
  1485. $sd['sellprice'] = $sellprice;
  1486. $sd['ploss'] = $ylnum;
  1487. $orderobj->where(array('id' => $id))->save($sd);
  1488. //写财务日志
  1489. $this->addlog($uid, $vo['username'], $money);
  1490. } elseif ($otype == 2) {//买跌
  1491. if ($newprice > $buyprice) {
  1492. $sellprice = $buyprice - $randnum;
  1493. } elseif ($newprice == $buyprice) {
  1494. $sellprice = $buyprice - $randnum;
  1495. } elseif ($newprice < $buyprice) {
  1496. $sellprice = $newprice;
  1497. }
  1498. //增加资产
  1499. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1500. //修改订单状态
  1501. $sd['status'] = 2;
  1502. $sd['is_win'] = 1;
  1503. $sd['sellprice'] = $sellprice;
  1504. $sd['ploss'] = $ylnum;
  1505. $orderobj->where(array('id' => $id))->save($sd);
  1506. //写财务日志
  1507. $this->addlog($uid, $vo['username'], $money);
  1508. }
  1509. } elseif ($dkong == 2) {
  1510. if ($otype == 1) {//买涨
  1511. //买涨,指定亏损,结算价格要低于买入价格
  1512. if ($newprice > $buyprice) {
  1513. $sellprice = $buyprice - $randnum;
  1514. } elseif ($newprice == $buyprice) {
  1515. $sellprice = $buyprice - $randnum;
  1516. } elseif ($newprice < $buyprice) {
  1517. $sellprice = $newprice;
  1518. }
  1519. //修改订单状态
  1520. $sd['status'] = 2;
  1521. $sd['is_win'] = 2;
  1522. $sd['sellprice'] = $sellprice;
  1523. $sd['ploss'] = $num;
  1524. $orderobj->where(array('id' => $id))->save($sd);
  1525. } elseif ($otype == 2) {//买跌
  1526. if ($newprice > $buyprice) {
  1527. $sellprice = $newprice;
  1528. } elseif ($newprice == $buyprice) {
  1529. $sellprice = $buyprice + $randnum;
  1530. } elseif ($newprice < $buyprice) {
  1531. $sellprice = $buyprice + $randnum;
  1532. }
  1533. //修改订单状态
  1534. $sd['status'] = 2;
  1535. $sd['is_win'] = 2;
  1536. $sd['sellprice'] = $sellprice;
  1537. $sd['ploss'] = $num;
  1538. $orderobj->where(array('id' => $id))->save($sd);
  1539. }
  1540. }
  1541. echo "==订单ID:" . $id . "出售成功==";
  1542. }
  1543. } else {
  1544. echo "没有订单可以结算!";
  1545. }
  1546. }
  1547. //自动结算合约订单
  1548. public function hycarryout____old()
  1549. {
  1550. $nowtime = time();
  1551. $map['status'] = 1;
  1552. $map['intselltime'] = array('elt', $nowtime);
  1553. $orderobj = M("hyorder");
  1554. $list = $orderobj->where($map)->select();
  1555. //获取合约参数
  1556. $setting = M("hysetting")->where(array('id' => 1))->field("hy_ksid,hy_ylid,hy_fkgl")->find();
  1557. //指定盈利ID组
  1558. $winarr = explode(',', $setting['hy_ylid']);
  1559. //指定亏损ID组
  1560. $lossarr = explode(',', $setting['hy_ksid']);
  1561. //风控比例组
  1562. $fkarr = explode(',', $setting['hy_fkgl']);
  1563. if (!empty($list)) {
  1564. foreach ($list as $key => $vo) {
  1565. $coinname = $vo['coinname'];
  1566. $coinarr = explode("/", $coinname);
  1567. $symbol = strtolower($coinarr[0]) . strtolower($coinarr[1]);
  1568. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  1569. $newprice = $this->getnewprice($coinapi);
  1570. $randnum = "0." . rand(1000, 9999);
  1571. $buyprice = $vo['buyprice'];
  1572. $otype = $vo['hyzd']; //合约方向
  1573. $dkong = $vo['kongyk']; //单控设置
  1574. $uid = $vo['uid'];//会员ID
  1575. $id = $vo['id'];//记录ID
  1576. $num = $vo['num'];
  1577. $hybl = $vo['hybl'];
  1578. $ylnum = $num * ($hybl / 100);
  1579. $money = $num + $ylnum;//盈利金额
  1580. //买涨
  1581. if ($otype == 1) {
  1582. if (in_array($uid, $winarr)) {//如果有指定盈利ID,则按盈利结算
  1583. if ($newprice > $buyprice) {
  1584. $sellprice = $newprice;
  1585. } elseif ($newprice == $buyprice) {
  1586. $sellprice = $newprice + $randnum;
  1587. } elseif ($newprice < $buyprice) {
  1588. $sellprice = $buyprice + $randnum;
  1589. }
  1590. //增加资产
  1591. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1592. //修改订单状态
  1593. $sd['status'] = 2;
  1594. $sd['is_win'] = 1;
  1595. $sd['sellprice'] = $sellprice;
  1596. $sd['ploss'] = $ylnum;
  1597. $orderobj->where(array('id' => $id))->save($sd);
  1598. //写财务日志
  1599. $this->addlog($uid, $vo['username'], $money);
  1600. } elseif (in_array($uid, $lossarr)) {//如果有指定亏损ID,则按亏损结算
  1601. //买涨,指定亏损,结算价格要低于买入价格
  1602. if ($newprice > $buyprice) {
  1603. $sellprice = $buyprice - $randnum;
  1604. } elseif ($newprice == $buyprice) {
  1605. $sellprice = $buyprice - $randnum;
  1606. } elseif ($newprice < $buyprice) {
  1607. $sellprice = $newprice;
  1608. }
  1609. //修改订单状态
  1610. $sd['status'] = 2;
  1611. $sd['is_win'] = 2;
  1612. $sd['sellprice'] = $sellprice;
  1613. $sd['ploss'] = $num;
  1614. $orderobj->where(array('id' => $id))->save($sd);
  1615. } else {//如果未指定盈利和亏损,则按单控的计算
  1616. if ($dkong == 1) {//盈利
  1617. if ($newprice > $buyprice) {
  1618. $sellprice = $newprice;
  1619. } elseif ($newprice == $buyprice) {
  1620. $sellprice = $newprice + $randnum;
  1621. } elseif ($newprice < $buyprice) {
  1622. $sellprice = $buyprice + $randnum;
  1623. }
  1624. // echo '买入价格:'.$buyprice;
  1625. // echo "<br />";
  1626. // echo '结算价格:'.$sellprice;die;
  1627. //增加资产
  1628. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1629. //修改订单状态
  1630. $sd['status'] = 2;
  1631. $sd['is_win'] = 1;
  1632. $sd['sellprice'] = $sellprice;
  1633. $sd['ploss'] = $ylnum;
  1634. $orderobj->where(array('id' => $id))->save($sd);
  1635. //写财务日志
  1636. $this->addlog($uid, $vo['username'], $money);
  1637. } elseif ($dkong == 2) {//亏损
  1638. if ($newprice > $buyprice) {
  1639. $sellprice = $buyprice - $randnum;
  1640. } elseif ($newprice == $buyprice) {
  1641. $sellprice = $buyprice - $randnum;
  1642. } elseif ($newprice < $buyprice) {
  1643. $sellprice = $newprice;
  1644. }
  1645. // echo '买入价格:'.$buyprice;
  1646. // echo "<br />";
  1647. // echo '结算价格:'.$sellprice;die;
  1648. //修改订单状态
  1649. $sd['status'] = 2;
  1650. $sd['is_win'] = 2;
  1651. $sd['sellprice'] = $sellprice;
  1652. $sd['ploss'] = $num;
  1653. $orderobj->where(array('id' => $id))->save($sd);
  1654. }
  1655. }
  1656. //买跌
  1657. } elseif ($otype == 2) {
  1658. if (in_array($uid, $winarr)) {//如果有指定盈利ID,则按盈利结算
  1659. if ($newprice > $buyprice) {
  1660. $sellprice = $buyprice - $randnum;
  1661. } elseif ($newprice == $buyprice) {
  1662. $sellprice = $buyprice - $randnum;
  1663. } elseif ($newprice < $buyprice) {
  1664. $sellprice = $newprice;
  1665. }
  1666. //增加资产
  1667. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1668. //修改订单状态
  1669. $sd['status'] = 2;
  1670. $sd['is_win'] = 1;
  1671. $sd['sellprice'] = $sellprice;
  1672. $sd['ploss'] = $ylnum;
  1673. $orderobj->where(array('id' => $id))->save($sd);
  1674. //写财务日志
  1675. $this->addlog($uid, $vo['username'], $money);
  1676. } elseif (in_array($uid, $lossarr)) {//如果有指定亏损ID,则按亏损结算
  1677. if ($newprice > $buyprice) {
  1678. $sellprice = $newprice;
  1679. } elseif ($newprice == $buyprice) {
  1680. $sellprice = $buyprice + $randnum;
  1681. } elseif ($newprice < $buyprice) {
  1682. $sellprice = $buyprice + $randnum;
  1683. }
  1684. //修改订单状态
  1685. $sd['status'] = 2;
  1686. $sd['is_win'] = 2;
  1687. $sd['sellprice'] = $sellprice;
  1688. $sd['ploss'] = $num;
  1689. $orderobj->where(array('id' => $id))->save($sd);
  1690. } else {//如果未指定盈利和亏损,则按单控的计算
  1691. if ($dkong == 1) {//盈利
  1692. if ($newprice > $buyprice) {
  1693. $sellprice = $buyprice - $randnum;
  1694. } elseif ($newprice == $buyprice) {
  1695. $sellprice = $buyprice - $randnum;
  1696. } elseif ($newprice < $buyprice) {
  1697. $sellprice = $newprice;
  1698. }
  1699. //增加资产
  1700. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1701. //修改订单状态
  1702. $sd['status'] = 2;
  1703. $sd['is_win'] = 1;
  1704. $sd['sellprice'] = $sellprice;
  1705. $sd['ploss'] = $ylnum;
  1706. $orderobj->where(array('id' => $id))->save($sd);
  1707. //写财务日志
  1708. $this->addlog($uid, $vo['username'], $money);
  1709. } elseif ($dkong == 2) {//亏损
  1710. if ($newprice > $buyprice) {
  1711. $sellprice = $newprice;
  1712. } elseif ($newprice == $buyprice) {
  1713. $sellprice = $buyprice + $randnum;
  1714. } elseif ($newprice < $buyprice) {
  1715. $sellprice = $buyprice + $randnum;
  1716. }
  1717. //修改订单状态
  1718. $sd['status'] = 2;
  1719. $sd['is_win'] = 2;
  1720. $sd['sellprice'] = $sellprice;
  1721. $sd['ploss'] = $num;
  1722. $orderobj->where(array('id' => $id))->save($sd);
  1723. }
  1724. }
  1725. }
  1726. echo "==订单ID:" . $id . "出售成功==";
  1727. }
  1728. } else {
  1729. echo "没有订单可以结算!";
  1730. }
  1731. }
  1732. //写财务日志
  1733. public function addlog($uid, $username, $money)
  1734. {
  1735. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  1736. $data['uid'] = $uid;
  1737. $data['username'] = $username;
  1738. $data['num'] = $money;
  1739. $data['coinname'] = "usdt";
  1740. $data['afternum'] = $minfo['usdt'] + $money;
  1741. $data['type'] = 4;
  1742. $data['addtime'] = date("Y-m-d H:i:s", time());
  1743. $data['st'] = 1;
  1744. $data['remark'] = L('合约出售');
  1745. M("bill")->add($data);
  1746. $notice['uid'] = $uid;
  1747. $notice['account'] = $username;
  1748. $notice['title'] = L('快速合约交易');
  1749. $notice['content'] = L('快速合约已平仓,请及时加仓');
  1750. $notice['addtime'] = date("Y-m-d H:i:s", time());
  1751. $notice['status'] = 1;
  1752. M("notice")->add($notice);
  1753. }
  1754. //获取行情数据
  1755. public function getnewprice($api)
  1756. {
  1757. $ch = curl_init();
  1758. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  1759. curl_setopt($ch, CURLOPT_URL, $api);
  1760. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1761. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  1762. $result = json_decode(curl_exec($ch), true);
  1763. $price_arr = $result['data'][0];
  1764. $close = $price_arr['close'];//现价
  1765. return $close;
  1766. }
  1767. public function ups_and_downs()
  1768. {
  1769. $coin = M('coin')->where(['type' => 2, 'status' => 1])->select();
  1770. $delete = M('trade_log')->where('addtime < '.(time()-600))->delete();
  1771. foreach ($coin as $item) {
  1772. $name = $item['name'] . '_' . 'usdt';
  1773. $market = M('market')->where(['name' => $name])->find();
  1774. $trade_list = M('trade_json')->where(['type' => 1])->order('id DESC')->find();// k线15分钟
  1775. $trade_list1 = M('trade_json')->where(['type' => 15])->order('id DESC')->find();// k线15分钟
  1776. $trade_list2 = M('trade_json')->where(['type' => 30])->order('id DESC')->find();// k线30分钟
  1777. $trade_list3 = M('trade_json')->where(['type' => 60])->order('id DESC')->find();// k线1小时
  1778. $trade_list4 = M('trade_json')->where(['type' => 240])->order('id DESC')->find();// k线4小时
  1779. $trade_list5 = M('trade_json')->where(['type' => 1440])->order('id DESC')->find();// k线1天
  1780. if ($market['zhang'] and $market['die']) {
  1781. //同时设置涨跌幅
  1782. $zd = rand(1, 2); //1涨幅2下跌
  1783. if ($zd == 1) {
  1784. $zf = $this->randomFloat(0, $market['zhang']); // 涨幅价格
  1785. $price = $market['new_price'] + ($market['new_price'] * ($zf / 100));//最新涨幅价格
  1786. if ($market['max_price'] < $price) {
  1787. $market['max_price'] = $price;
  1788. M('market')->where(['id' => $market['id']])->save(['max_price' => $price]);
  1789. }
  1790. } else {
  1791. $xd = $this->randomFloat(0, $market['die']); // 下跌价格
  1792. $price = $market['new_price'] - ($market['new_price'] * ($xd / 100));//最新下跌价格
  1793. if ($market['min_price'] > $price) {
  1794. $market['min_price'] = $price;
  1795. M('market')->where(['id' => $market['id']])->save(['min_price' => $price]);
  1796. }
  1797. }
  1798. M('market')->where(['id' => $market['id']])->save(['new_price' => $price]);
  1799. if ($market['hou_time'] < strtotime('today')) {
  1800. M('market')->where(['id' => $market['id']])->save(['hou_price' => $price, 'hou_time' => strtotime('today'), 'max_day' => $price, 'min_day' => $price]);
  1801. }
  1802. }else{
  1803. if ($market['zhang'] > 0 and $market['die'] == 0){
  1804. $zf = $this->randomFloat(0, $market['zhang']); // 涨幅价格
  1805. $price = $market['new_price'] + ($market['new_price'] * ($zf / 100));//最新涨幅价格
  1806. if ($market['max_price'] < $price) {
  1807. $market['max_price'] = $price;
  1808. M('market')->where(['id' => $market['id']])->save(['max_price' => $price]);
  1809. }
  1810. M('market')->where(['id' => $market['id']])->save(['new_price' => $price]);
  1811. if ($market['hou_time'] < strtotime('today')) {
  1812. M('market')->where(['id' => $market['id']])->save(['hou_price' => $price, 'hou_time' => strtotime('today'), 'max_day' => $price, 'min_day' => $price]);
  1813. }
  1814. }
  1815. if ($market['zhang'] == 0 and $market['die'] > 0){
  1816. $xd = $this->randomFloat(0, $market['die']); // 下跌价格
  1817. $price = $market['new_price'] - ($market['new_price'] * ($xd / 100));//最新下跌价格
  1818. if ($market['min_price'] > $price) {
  1819. $market['min_price'] = $price;
  1820. M('market')->where(['id' => $market['id']])->save(['min_price' => $price]);
  1821. }
  1822. M('market')->where(['id' => $market['id']])->save(['new_price' => $price]);
  1823. if ($market['hou_time'] < strtotime('today')) {
  1824. M('market')->where(['id' => $market['id']])->save(['hou_price' => $price, 'hou_time' => strtotime('today'), 'max_day' => $price, 'min_day' => $price]);
  1825. }
  1826. }
  1827. }
  1828. if ($price){
  1829. $price = round($price, 6);
  1830. $sd = $this->randomFloat($market['sdlow_num'], $market['sdhigh_num']);
  1831. for ($i = 0; $i <= $sd; $i ++){
  1832. M('trade_log')->add([
  1833. 'market' => $name,
  1834. 'price' => $price,
  1835. 'num' => $this->randomFloat(0, 1),
  1836. 'mum' => $this->randomFloat(0, 1),
  1837. 'addtime' => time() - rand(1, 10),
  1838. 'type' => rand(1, 2)
  1839. ]);
  1840. }
  1841. if ($price > $market['max_day']){
  1842. M('market')->where(['id' => $market['id']])->save(['max_day' => $price]);
  1843. }
  1844. if ($price < $market['min_day']){
  1845. M('market')->where(['id' => $market['id']])->save(['min_day' => $price]);
  1846. }
  1847. if ($price > $market['max_price']){
  1848. M('market')->where(['id' => $market['id']])->save(['max_price' => $price]);
  1849. $market['max_price'] = $price;
  1850. }
  1851. if ($price < $market['min_price']){
  1852. M('market')->where(['id' => $market['id']])->save(['min_price' => $price]);
  1853. $market['min_price'] = $price;
  1854. }
  1855. if ($trade_list) {
  1856. $data = json_decode($trade_list['data']);
  1857. $time = $trade_list['addtime'] + ($trade_list['type'] * 60);
  1858. if ($time <= time()) {
  1859. M('market')->where(['id' => $market['id']])->save(['max_price' => $price]);
  1860. M('market')->where(['id' => $market['id']])->save(['min_price' => $price]);
  1861. $json = [
  1862. 'market' => $name,
  1863. 'data' => json_encode([
  1864. time() * 1000,
  1865. $data[4],
  1866. $market['max_price'],
  1867. $market['min_price'],
  1868. $price,
  1869. $this->randomFloat(0, $market['sdhigh_num'])
  1870. ]),
  1871. 'type' => 1,
  1872. 'addtime' => time()
  1873. ];
  1874. M('trade_json')->add($json);
  1875. }
  1876. } else {
  1877. $json = [
  1878. 'market' => $name,
  1879. 'data' => json_encode([
  1880. time() * 1000,
  1881. $market['new_price'],
  1882. $market['max_price'],
  1883. $market['min_price'],
  1884. $price,
  1885. $this->randomFloat(0, $market['sdhigh_num'])
  1886. ]),
  1887. 'type' => 1,
  1888. 'addtime' => time()
  1889. ];
  1890. M('trade_json')->add($json);
  1891. }
  1892. if ($trade_list1) {
  1893. $data = json_decode($trade_list1['data']);
  1894. $time = $trade_list1['addtime'] + ($trade_list1['type'] * 60);
  1895. if ($time <= time()) {
  1896. $json = [
  1897. 'market' => $name,
  1898. 'data' => json_encode([
  1899. time() * 1000,
  1900. $data[4],
  1901. $market['max_price'],
  1902. $market['min_price'],
  1903. $price,
  1904. $this->randomFloat(0, $market['sdhigh_num'])
  1905. ]),
  1906. 'type' => 15,
  1907. 'addtime' => time()
  1908. ];
  1909. M('trade_json')->add($json);
  1910. }
  1911. } else {
  1912. $json = [
  1913. 'market' => $name,
  1914. 'data' => json_encode([
  1915. time() * 1000,
  1916. $market['new_price'],
  1917. $market['max_price'],
  1918. $market['min_price'],
  1919. $price,
  1920. $this->randomFloat(0, $market['sdhigh_num'])
  1921. ]),
  1922. 'type' => 15,
  1923. 'addtime' => time()
  1924. ];
  1925. M('trade_json')->add($json);
  1926. }
  1927. if ($trade_list2) {
  1928. $data = json_decode($trade_list2['data']);
  1929. $time = $trade_list2['addtime'] + ($trade_list2['type'] * 60);
  1930. if ($time <= time()) {
  1931. $json = [
  1932. 'market' => $name,
  1933. 'data' => json_encode([
  1934. time() * 1000,
  1935. $data[4],
  1936. $market['max_price'],
  1937. $market['min_price'],
  1938. $price,
  1939. $this->randomFloat(0, $market['sdhigh_num'])
  1940. ]),
  1941. 'type' => 30,
  1942. 'addtime' => time()
  1943. ];
  1944. M('trade_json')->add($json);
  1945. }
  1946. } else {
  1947. $json = [
  1948. 'market' => $name,
  1949. 'data' => json_encode([
  1950. time() * 1000,
  1951. $market['new_price'],
  1952. $market['max_price'],
  1953. $market['min_price'],
  1954. $price,
  1955. $this->randomFloat(0, $market['sdhigh_num'])
  1956. ]),
  1957. 'type' => 30,
  1958. 'addtime' => time()
  1959. ];
  1960. M('trade_json')->add($json);
  1961. }
  1962. if ($trade_list3) {
  1963. $data = json_decode($trade_list3['data']);
  1964. $time = $trade_list3['addtime'] + ($trade_list3['type'] * 60);
  1965. if ($time <= time()) {
  1966. $json = [
  1967. 'market' => $name,
  1968. 'data' => json_encode([
  1969. time() * 1000,
  1970. $data[4],
  1971. $market['max_price'],
  1972. $market['min_price'],
  1973. $price,
  1974. $this->randomFloat(0, $market['sdhigh_num'])
  1975. ]),
  1976. 'type' => 60,
  1977. 'addtime' => time()
  1978. ];
  1979. M('trade_json')->add($json);
  1980. }
  1981. } else {
  1982. $json = [
  1983. 'market' => $name,
  1984. 'data' => json_encode([
  1985. time() * 1000,
  1986. $market['new_price'],
  1987. $market['max_price'],
  1988. $market['min_price'],
  1989. $price,
  1990. $this->randomFloat(0, $market['sdhigh_num'])
  1991. ]),
  1992. 'type' => 60,
  1993. 'addtime' => time()
  1994. ];
  1995. M('trade_json')->add($json);
  1996. }
  1997. if ($trade_list4) {
  1998. $data = json_decode($trade_list4['data']);
  1999. $time = $trade_list4['addtime'] + ($trade_list4['type'] * 60);
  2000. if ($time <= time()) {
  2001. $json = [
  2002. 'market' => $name,
  2003. 'data' => json_encode([
  2004. time() * 1000,
  2005. $data[4],
  2006. $market['max_price'],
  2007. $market['min_price'],
  2008. $price,
  2009. $this->randomFloat(0, $market['sdhigh_num'])
  2010. ]),
  2011. 'type' => 240,
  2012. 'addtime' => time()
  2013. ];
  2014. M('trade_json')->add($json);
  2015. }
  2016. } else {
  2017. $json = [
  2018. 'market' => $name,
  2019. 'data' => json_encode([
  2020. time() * 1000,
  2021. $market['new_price'],
  2022. $market['max_price'],
  2023. $market['min_price'],
  2024. $price,
  2025. $this->randomFloat(0, $market['sdhigh_num'])
  2026. ]),
  2027. 'type' => 240,
  2028. 'addtime' => time()
  2029. ];
  2030. M('trade_json')->add($json);
  2031. }
  2032. if ($trade_list5) {
  2033. $data = json_decode($trade_list5['data']);
  2034. $time = $trade_list5['addtime'] + ($trade_list5['type'] * 60);
  2035. if ($time <= time()) {
  2036. $json = [
  2037. 'market' => $name,
  2038. 'data' => json_encode([
  2039. time() * 1000,
  2040. $data[4],
  2041. $market['max_price'],
  2042. $market['min_price'],
  2043. $price,
  2044. $this->randomFloat(0, $market['sdhigh_num'])
  2045. ]),
  2046. 'type' => 1440,
  2047. 'addtime' => time()
  2048. ];
  2049. M('trade_json')->add($json);
  2050. }
  2051. } else {
  2052. $json = [
  2053. 'market' => $name,
  2054. 'data' => json_encode([
  2055. time() * 1000,
  2056. $market['new_price'],
  2057. $market['max_price'],
  2058. $market['min_price'],
  2059. $price,
  2060. $this->randomFloat(0, $market['sdhigh_num'])
  2061. ]),
  2062. 'type' => 1440,
  2063. 'addtime' => time()
  2064. ];
  2065. M('trade_json')->add($json);
  2066. }
  2067. }
  2068. }
  2069. echo '自动增长';
  2070. }
  2071. function randomFloat($min = 0, $max = 1)
  2072. {
  2073. $num = $min + mt_rand() / mt_getrandmax() * ($max - $min);
  2074. return sprintf("%.4f", $num); //控制小数后几位
  2075. }
  2076. }
  2077. ?>