InvoiceExtendController.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. <?php
  2. /**
  3. * 开票功能控制器
  4. * @author CP
  5. * @version 1.0
  6. * @namespace invoice
  7. * @package InvoiceExtentController
  8. */
  9. class InvoiceExtendController extends DooController {
  10. public $staff;
  11. public $NEW;
  12. public $data;
  13. private $INVOICEKEY = "APPROVAL";
  14. private $INVOICECOLLECTPATH = "protected/cache/invoiceCollect/";
  15. private $ACTION = '';
  16. private $appId = 'wx3ee46531947a1384';
  17. private $appSecret = 'awGWzy3J5q_96VXKBLA2BQ9RpiT0LiPXnkMF-xV6h5o'; // awGWzy3J5q_96VXKBLA2BXU6mVCGynPGD-sxUCsdNDE //'PeiQVH8tbQE7pM9pVO4AIPlX9FFNpwu7O6Iy6VFcAVtpjTA3Qn42dbNmktBFkUHo';
  18. public function beforeRun($resource, $action) {
  19. Doo::loadModel ( "invoiceManage" );
  20. $invoiceManage = new invoiceManage ();
  21. Doo::loadClass ( 'XDeode' );
  22. $XDeode = new XDeode ( 5 );
  23. $sid = $XDeode->decode ( $_COOKIE ["staff"] );
  24. header ( 'Content-Type:text/html;charset=utf-8' );
  25. $moldList = $invoiceManage->getInvoiceByStaff ( $sid );
  26. if (empty ( $moldList )) {
  27. $flag = false;
  28. $uGroups = 'ordinary';
  29. if (Doo::acl ()->isAllowed ( $uGroups, $resource, $action )) {
  30. $flag = true;
  31. }
  32. } else {
  33. $flag = false;
  34. array_push ( $moldList, array (
  35. 'mold' => 'ordinary'
  36. ) );
  37. // 删除审批人后-加入权限问题-暂时关闭
  38. $fileInvoice = file_get_contents ( "protected/config/invoice/invoice.ini" );
  39. $fiML = json_decode ( $fileInvoice, true );
  40. if (empty ( $fiML ))
  41. $fiML = array ();
  42. foreach ( $fiML as $key => $value ) {
  43. $sidList = explode ( ',', $value );
  44. foreach ( $sidList as $k => $v ) {
  45. if ($v == $sid) {
  46. array_push ( $moldList, array (
  47. 'mold' => $key
  48. ) );
  49. break;
  50. }
  51. }
  52. }
  53. foreach ( $moldList as $value ) {
  54. if (Doo::acl ()->isAllowed ( $value ['mold'], $resource, $action )) {
  55. $flag = true;
  56. break;
  57. }
  58. }
  59. }
  60. /*
  61. * Doo::acl()->isAllowed($uGroups['groups'], $resource, $action);
  62. */
  63. // 单独判断办事处汇总的访问权限 invoiceAggregateCategoryDetail,invoiceAggregateStaffDetail
  64. if ($action == 'invoiceAggregateCategoryDetail' || $action == 'invoiceAggregateStaffDetail') {
  65. Doo::loadModel ( "invoiceCategoryManage" );
  66. $invoiceCategoryManage = new invoiceCategoryManage ();
  67. $icm = $invoiceCategoryManage->getInvoiceCMByStaff ( $sid );
  68. if (empty ( $icm ))
  69. die ( 'illegal request' );
  70. }
  71. // 单独判断应收款访问权限
  72. if ($action == 'invoiceAggregateCompanyDetail') {
  73. Doo::loadModel ( "invoiceCompanyManage" );
  74. $invoiceCompanyManage = new invoiceCompanyManage ();
  75. $icm = $invoiceCompanyManage->getInvoiceCMByStaff ( $sid );
  76. if (empty ( $icm ))
  77. die ( 'illegal request' );
  78. }
  79. // 主权限获得与判断
  80. Doo::loadModel ( 'staff' );
  81. $staff = new staff ();
  82. $detail = $staff->getStaffBySid ( $_COOKIE ["staff"] );
  83. $accessModular = 'INVOICEEXTEND';
  84. $accessAuthority='INVOICE';
  85. if (empty ( $detail ['cldAccessArray'] ))
  86. die ( 'illegal request' );
  87. else {
  88. if (in_array ( $accessAuthority, $detail ['cldAccessArray'] )) {
  89. if (Doo::acl ()->isAllowed ( $accessModular, $resource, $action )) {
  90. $flag = true;
  91. } else {
  92. $flag = false;
  93. }
  94. } else {
  95. die ( 'illegal request' );
  96. }
  97. }
  98. if (! $flag)
  99. die ( 'illegal request' );
  100. }
  101. function __construct() {
  102. if (isset ( $_COOKIE ["staff"] )) {
  103. if (! empty ( $_COOKIE ["staff"] )) {
  104. Doo::loadModel ( 'staff' );
  105. $staff = new staff ();
  106. $uriPartsOrig = explode ( '/', $_SERVER ['REQUEST_URI'] );
  107. $this->ACTION = $uriPartsOrig [1];
  108. $this->data ['new'] = $this->isInvoiceNew ();
  109. $this->staff = $staff->getUserByIdList ( $_COOKIE ["staff"] );
  110. $this->data ['isStaffCompleteMsg'] = false;
  111. $detail = $staff->checkStaffInfoIsComplete ( $_COOKIE ["staff"] );
  112. if (! empty ( $detail ))
  113. $this->data ['isStaffCompleteMsg'] = true;
  114. return "/";
  115. }
  116. }
  117. Doo::loadCore ( 'uri/DooUriRouter' );
  118. $router = new DooUriRouter ();
  119. $routeRs = $router->execute ( Doo::app ()->route, Doo::conf ()->SUBFOLDER );
  120. if ($routeRs ['1'] != "login") {
  121. header ( 'Content-Type:text/html;charset=utf-8' );
  122. @header ( "Location: /login" );
  123. }
  124. }
  125. function invoiceSettlementChange() {
  126. $iidKey = $this->get_args ( 'iidKey' ) ? $this->get_args ( 'iidKey' ) : "";
  127. $trainId = $this->get_args ( 'trainId' ) ? $this->get_args ( 'trainId' ) : 0;
  128. $settlementType = $this->get_args ( 'settlementType' ) ? $this->get_args ( 'settlementType' ) : "";
  129. if (empty ( $this->staff [0] )) {
  130. die ( 'illegal request-Please login' );
  131. }
  132. if (! empty ( $iidKey ) && ! empty ( $settlementType )) {
  133. Doo::loadModel ( 'invoice' );
  134. $invoice = new invoice ();
  135. Doo::loadClass ( 'XDeode' );
  136. $XDeode = new XDeode ( 5 );
  137. Doo::loadModel ( 'invoiceTraining' );
  138. $invoiceTraining = new invoiceTraining ();
  139. $iid = $XDeode->decode ( $iidKey );
  140. $invoiceDetail=$invoice->getInvoiceByIid($iid);
  141. if(empty($invoiceDetail)){
  142. die ( 'illegal request-invoiceData NULL' );
  143. }
  144. $trainId = $XDeode->decode ( $trainId );
  145. $itDetail=$invoiceTraining->getInvoiceTrainingByItid($trainId);
  146. $st = '';
  147. if ($settlementType == 'SOFTWARE') {
  148. $st = 0;
  149. $trainId = 0;
  150. $operation=' 结算方式 改 软件销售';
  151. } elseif ($settlementType == 'TRAINING') {
  152. $st = 1;
  153. $operation=' 结算方式 改 '.$itDetail['trainName'];
  154. if (empty ( $trainId ) && ! is_numeric ( $trainId )) {
  155. die ( 'illegal request-trainingIdError' );
  156. }
  157. } else {
  158. die ( 'illegal request-settlementError' );
  159. }
  160. $invoice->settlementType=$st;
  161. $invoice->trainId=$trainId;
  162. $invoice->update(array (
  163. 'where' => 'iid = ' . $iid
  164. ));
  165. Doo::loadModel ( 'invoiceStore' );
  166. $invoiceStore = new invoiceStore ();
  167. //重置结算方式的相关数据
  168. $isSuccess=$invoiceStore->delete ( array (
  169. 'where' => 'iid=' . $iid,
  170. 'limit' => 1
  171. ) );
  172. // if($isSuccess){
  173. // $detailIT = $invoiceTraining->getInvoiceTrainingByItid ( $invoiceDetail['trainId'] );
  174. // if (!empty($detailIT)){
  175. // $item = array (
  176. // 'itid' => $invoiceDetail['trainId'],
  177. // 'invoiceTotal' => $detailIT ['invoiceTotal'] - 1,
  178. // );
  179. // $itid = $invoiceTraining->setInvoiceTrainByCondition ( $item );
  180. // }
  181. // }
  182. // 如果是培训班结算 创建培训班发票到培训班备份中
  183. if (! empty ( $st ) && $st == 1) {
  184. $isid = $invoiceStore->setInvoiceToInvoiceStore ( $iid );
  185. $itemIS = array (
  186. 'isid' => $isid,
  187. 'ipSource' => 1,
  188. 'apStatus' => 1,
  189. 'iid' => $iid
  190. );
  191. $invoiceStore->setInvoiceStoreByCondition ( $itemIS );
  192. // 更新培训班总数量
  193. $item = array (
  194. 'itid' => $trainId,
  195. 'submitStatus' => 1
  196. );
  197. $itid = $invoiceTraining->setInvoiceTrainByCondition ( $item );
  198. }
  199. Doo::loadModel ( 'invoiceOperationLog' );
  200. $invoiceOperationLog = new invoiceOperationLog ();
  201. // 操作日志
  202. $invoiceOperationLog->setInvoiceOperationLog ( $item );
  203. $item = array (
  204. 'date' => date ( "Y-m-d H:i:s" ),
  205. 'operation' => $operation,
  206. 'status' => 1,
  207. 'img' => $this->staff [0] ['avatar'],
  208. 'username' => $this->staff [0] ['username'],
  209. 'uid' => $this->staff [0] ['sid'],
  210. 'category' => $this->staff [0] ['category'],
  211. 'iid' => $iid
  212. );
  213. $invoiceOperationLog->setInvoiceOperationLog ( $item );
  214. }
  215. return "/invoiceApprovalAchieve";
  216. }
  217. function invoiceUntreadAdd(){
  218. session_start ();
  219. Doo::loadClass ( 'XDeode' );
  220. $XDeode = new XDeode ( 5 );
  221. $iidKey = isset ( $this->params ['iidKey'] ) ? $this->params ['iidKey'] : "";
  222. $iid = $XDeode->decode ( $iidKey );
  223. if (! is_numeric ( $iid ))
  224. die ( 'illegal request' );
  225. Doo::loadModel ( 'invoice' );
  226. $invoice = new invoice ();
  227. Doo::loadModel ( 'invoiceOperationLog' );
  228. $invoiceOperationLog = new invoiceOperationLog ();
  229. $invoiceDetail = $invoice->getInvoiceByIid ( $iid );
  230. $operation = $invoiceOperationLog->getInvoiceOperationByDropped ( $iid );
  231. //获得继承发票金额合计
  232. $restrictPrice=$invoiceDetail['invoicePrice'];
  233. if (!empty($invoiceDetail['inheritIid'])){
  234. $restrictList=$invoice->getInvoiceInIid($invoiceDetail['inheritIid']);
  235. foreach ($restrictList as $key=>$value){
  236. $restrictPrice-=$value['invoicePrice'];
  237. }
  238. }
  239. if ($restrictPrice<0){
  240. $restrictPrice=0;
  241. }
  242. $invoiceDetail ['mailItemsJson'] = json_decode ( $invoiceDetail ['mailItemsJson'], true );
  243. $token = $this->set_token ();
  244. $_SESSION ['token_' . $this->staff [0] ['sidKey']] = $token;
  245. $this->data ['token'] = $token;
  246. $this->data ['restrictPrice'] = floatval($restrictPrice);
  247. $this->data ['operation'] = $operation;
  248. $this->data ['invoiceDetail'] = $invoiceDetail;
  249. $this->data ['memu'] = "invoice";
  250. $this->data ['staff'] = $this->staff;
  251. $this->data ['receiptMemu'] = 'invoice';
  252. $this->render ( "/invoice/invoiceUntreadAdd", $this->data );
  253. }
  254. function invoiceUntreadAddDo(){
  255. $iidKey = $this->get_args ( 'iidKey' ) ? $this->get_args ( 'iidKey' ) : "";
  256. Doo::loadModel ( 'invoice' );
  257. $invoice = new invoice ();
  258. $iid = $invoice->authcode ( $iidKey );
  259. if (! empty ( $iid ) && ! is_numeric ( $iid ))
  260. die ( 'illegal request' );
  261. $cid = $this->get_args ( 'cid' ) && is_numeric ( $this->get_args ( 'cid' ) ) ? $this->get_args ( 'cid' ) : 0;
  262. $invoiceType = $this->get_args ( 'invoiceType' ) && is_numeric ( $this->get_args ( 'invoiceType' ) ) ? $this->get_args ( 'invoiceType' ) : 0;
  263. $doPost = $this->get_args ( 'doPost' ) && is_numeric ( $this->get_args ( 'doPost' ) ) ? $this->get_args ( 'doPost' ) : 0;
  264. // $invoicePrice = $this->get_args ( 'invoicePrice' ) ? $this->get_args ( 'invoicePrice' ) : "";
  265. $invoiceQuantity = $this->get_args ( 'invoiceQuantity' ) && is_numeric ( $this->get_args ( 'invoiceQuantity' ) ) ? $this->get_args ( 'invoiceQuantity' ) : 0;
  266. $invoiceUnitPrice = $this->get_args ( 'invoiceUnitPrice' ) && is_numeric ( $this->get_args ( 'invoiceUnitPrice' ) ) ? $this->get_args ( 'invoiceUnitPrice' ) : 0;
  267. $invoiceElement = $this->get_args ( 'invoiceElement' ) ? $this->get_args ( 'invoiceElement' ) : "";
  268. $invoiceTitle = $this->get_args ( 'invoiceTitle' ) ? $this->get_args ( 'invoiceTitle' ) : "";
  269. $invoiceCompany = $this->get_args ( 'invoiceCompany' ) ? $this->get_args ( 'invoiceCompany' ) : "";
  270. $TIN = $this->get_args ( 'TIN' ) ? $this->get_args ( 'TIN' ) : "";
  271. $address = $this->get_args ( 'address' ) ? $this->get_args ( 'address' ) : "";
  272. $phone = $this->get_args ( 'phone' ) ? $this->get_args ( 'phone' ) : "";
  273. $bank = $this->get_args ( 'bank' ) ? $this->get_args ( 'bank' ) : "";
  274. $bankAccount = $this->get_args ( 'bankAccount' ) ? $this->get_args ( 'bankAccount' ) : "";
  275. $recipients = $this->get_args ( 'recipients' ) ? $this->get_args ( 'recipients' ) : "";
  276. $recipientsPhone = $this->get_args ( 'recipientsPhone' ) ? $this->get_args ( 'recipientsPhone' ) : "";
  277. $recipientsAddress = $this->get_args ( 'recipientsAddress' ) ? $this->get_args ( 'recipientsAddress' ) : "";
  278. $remark = $this->get_args ( 'remark' ) ? $this->get_args ( 'remark' ) : "";
  279. $TINT = $this->get_args ( 'TINT' ) ? $this->get_args ( 'TINT' ) : "";
  280. $addressT = $this->get_args ( 'addressT' ) ? $this->get_args ( 'addressT' ) : "";
  281. $phoneT = $this->get_args ( 'phoneT' ) ? $this->get_args ( 'phoneT' ) : "";
  282. $bankT = $this->get_args ( 'bankT' ) ? $this->get_args ( 'bankT' ) : "";
  283. $bankAccountT = $this->get_args ( 'bankAccountT' ) ? $this->get_args ( 'bankAccountT' ) : "";
  284. $softLock = $this->get_args ( 'softLock' ) ? $this->get_args ( 'softLock' ) : 0;
  285. $softLockNum = $this->get_args ( 'softLockNum' ) ? $this->get_args ( 'softLockNum' ) : 0;
  286. $contract = $this->get_args ( 'contract' ) ? $this->get_args ( 'contract' ) : 0;
  287. $contractNum = $this->get_args ( 'contractNum' ) ? $this->get_args ( 'contractNum' ) : 0;
  288. $instructions = $this->get_args ( 'instructions' ) ? $this->get_args ( 'instructions' ) : 0;
  289. $instructionsNum = $this->get_args ( 'instructionsNum' ) ? $this->get_args ( 'instructionsNum' ) : 0;
  290. $remittance = $this->get_args ( 'remittance' ) ? $this->get_args ( 'remittance' ) : 0;
  291. $mailItems = $this->get_args ( 'mailItems' ) ? $this->get_args ( 'mailItems' ) : "";
  292. $token = $this->get_args ( 'token' ) ? $this->get_args ( 'token' ) : "";
  293. if (empty ( $token )) {
  294. die ( 'illegal request-token' );
  295. } else {
  296. session_start ();
  297. if ($_SESSION ['token_' . $this->staff [0] ['sidKey']] != $token) {
  298. header ( 'Content-Type:text/html;charset=utf-8' );
  299. die ( '请勿重复申请发票' );
  300. } else {
  301. $_SESSION ['token_' . $this->staff [0] ['sidKey']] = '';
  302. }
  303. }
  304. $temporary = $invoiceQuantity * $invoiceUnitPrice;
  305. if ($invoiceType == 1) {
  306. if ($temporary > 10000) {
  307. header ( 'Content-Type:text/html;charset=utf-8' );
  308. die ( '专用发票总金额暂时不能超过10000.00(一万)元。' );
  309. // die ( 'illegal request-Please priceError' );
  310. }
  311. }
  312. if (empty ( $this->staff [0] ))
  313. die ( 'illegal request-Please login' );
  314. if (! empty ( $cid ) && ! empty ( $invoiceQuantity ) && ! empty ( $invoiceUnitPrice ) && ! empty ( $invoiceElement ) ) {
  315. Doo::loadModel ( 'L_category' );
  316. $lCategory = new L_category ();
  317. Doo::loadModel ( 'invoiceManage' );
  318. $invoiceManage = new invoiceManage ();
  319. Doo::loadModel ( 'invoiceOperationLog' );
  320. $invoiceOperationLog = new invoiceOperationLog ();
  321. Doo::loadClass ( 'XDeode' );
  322. $XDeode = new XDeode ( 5 );
  323. $categoryDetil = $lCategory->getOne ( array (
  324. 'SELECT' => 'title',
  325. 'where' => 'cid=' . $cid,
  326. 'asArray' => true
  327. ) );
  328. $invoicePrice = $invoiceQuantity * $invoiceUnitPrice;
  329. $invoiceManageList = $invoiceManage->getInvoiceByAll ();
  330. $invoiceManageDetail = $invoiceManage->getInvoiceByMold ( "发票审批" );
  331. // print_r(current($invoiceManageDetail ['staffList']));
  332. // die;
  333. $moldManage = array ();
  334. foreach ( $invoiceManageList as $key => $value ) {
  335. if ($value ['mold'] == '发票打印')
  336. $moldManage [$value ['mold']] = $value ['staffList'];
  337. elseif ($value ['mold'] == '发票邮寄')
  338. $moldManage [$value ['mold']] = $value ['staffList'];
  339. elseif ($value ['mold'] == '发票退票')
  340. $moldManage [$value ['mold']] = $value ['staffList'];
  341. }
  342. $pendingApprovalsSid = current ( $invoiceManageDetail ['staffList'] ) [0];
  343. //培训班ID--新增关联-发票关系 发票IID关联
  344. $invoice = new invoice ();
  345. $invoiceDetail=$invoice->getInvoiceByIid($iid);
  346. //退票后,重新开票金额的限制
  347. $restrictPrice=$invoiceDetail['invoicePrice'];
  348. if (!empty($invoiceDetail['inheritIid'])){
  349. $restrictList=$invoice->getInvoiceInIid($invoiceDetail['inheritIid']);
  350. foreach ($restrictList as $key=>$value){
  351. $restrictPrice-=$value['invoicePrice'];
  352. }
  353. }
  354. if ($restrictPrice<0){
  355. header ( 'Content-Type:text/html;charset=utf-8' );
  356. die ( '开票金额超出原退票金额-0' );
  357. }else{
  358. if($restrictPrice<$temporary){
  359. header ( 'Content-Type:text/html;charset=utf-8' );
  360. die ( '开票金额超出原退票金额-1' );
  361. }
  362. }
  363. $trainId=$invoiceDetail['trainId'];
  364. $st = 1;
  365. if (empty ( $trainId ) && ! is_numeric ( $trainId )) {
  366. die ( 'illegal request-trainingIdError' );
  367. }
  368. $invoice = new invoice ();
  369. $item = array (
  370. 'cid' => $cid,
  371. 'categoryName' => $categoryDetil ['title'],
  372. 'invoiceElement' => $invoiceElement,
  373. 'invoiceQuantity' => $invoiceQuantity,
  374. 'invoiceUnitPrice' => $invoiceUnitPrice,
  375. 'invoicePrice' => $invoicePrice,
  376. 'invoiceType' => $invoiceType,
  377. 'settlementType' => $st,
  378. 'doPost' => $doPost,
  379. 'status' => 1,
  380. 'invoiceSerial' => "#F" . date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT),
  381. 'date' => date ( "Y-m-d H:i:s" ),
  382. 'updateTime' => date ( "Y-m-d H:i:s" ),
  383. 'sid' => $this->staff [0] ['sid'],
  384. 'userName' => $this->staff [0] ['username'],
  385. 'invoiceManage' => $invoiceManageDetail ['staff'],
  386. 'pendingApprovals' => $pendingApprovalsSid,
  387. 'moldManage' => json_encode ( $moldManage ),
  388. 'remark' => $remark,
  389. 'trainId' => $trainId,
  390. 'parentUntreadIid'=>$trainId
  391. );
  392. if ($invoiceType == 0)
  393. $item += array (
  394. 'invoiceTitle' => $invoiceTitle,
  395. 'TIN' => $TINT,
  396. 'address' => $addressT,
  397. 'phone' => $phoneT,
  398. 'bank' => $bankT,
  399. 'bankAccount' => $bankAccountT
  400. );
  401. else {
  402. $item += array (
  403. 'invoiceCompany' => $invoiceCompany,
  404. 'TIN' => $TIN,
  405. 'address' => $address,
  406. 'phone' => $phone,
  407. 'bank' => $bank,
  408. 'bankAccount' => $bankAccount
  409. );
  410. }
  411. if ($doPost == 1) {
  412. $mailList = array (
  413. 'invoice' => 1,
  414. 'mailItems' => $mailItems
  415. );
  416. if (! empty ( $softLock ))
  417. $mailList += array (
  418. 'softLock' => $softLockNum
  419. );
  420. else
  421. $mailList += array (
  422. 'softLock' => 0
  423. );
  424. if (! empty ( $contract ))
  425. $mailList += array (
  426. 'contract' => $contractNum
  427. );
  428. else
  429. $mailList += array (
  430. 'contract' => 0
  431. );
  432. if (! empty ( $instructions ))
  433. $mailList += array (
  434. 'instructions' => $instructionsNum
  435. );
  436. else
  437. $mailList += array (
  438. 'instructions' => 0
  439. );
  440. if (! empty ( $remittance ))
  441. $mailList += array (
  442. 'remittance' => $remittance
  443. );
  444. else
  445. $mailList += array (
  446. 'remittance' => ''
  447. );
  448. $mailString = '发票x1张,备注:' . $mailItems;
  449. if (! empty ( $softLock ))
  450. $mailString .= ',软件锁x' . $softLockNum . '个';
  451. if (! empty ( $contract ))
  452. $mailString .= ',合同x' . $contractNum . '份';
  453. if (! empty ( $instructions ))
  454. $mailString .= ',说明书x' . $instructionsNum . '本';
  455. if (! empty ( $remittance ))
  456. $mailString .= ',汇款账号单(张)';
  457. $item += array (
  458. 'recipients' => $recipients,
  459. 'recipientsPhone' => $recipientsPhone,
  460. 'recipientsAddress' => $recipientsAddress,
  461. 'mailItems' => $mailString,
  462. 'mailItemsJson' => json_encode ( $mailList )
  463. );
  464. }
  465. $iid = $invoice->addInvoice ( $item );
  466. $invoice = new invoice ();
  467. if (!empty($invoiceDetail['inheritIid'])){
  468. $inheritIid=$invoiceDetail['inheritIid'].','.$iid;
  469. }else{
  470. $inheritIid=$iid;
  471. }
  472. $item = array (
  473. 'iid' => $invoiceDetail['iid'],
  474. 'inheritIid' => $inheritIid
  475. );
  476. $invoice->setInvoiceByCondition($item);
  477. // 如果是培训班结算 创建培训班发票到培训班备份中
  478. if (! empty ( $st ) && $st == 1) {
  479. Doo::loadModel ( 'invoiceStore' );
  480. $invoiceStore = new invoiceStore ();
  481. Doo::loadModel ( 'invoiceTraining' );
  482. $invoiceTraining = new invoiceTraining ();
  483. $isid = $invoiceStore->setInvoiceToInvoiceStore ( $iid );
  484. $itemIS = array (
  485. 'isid' => $isid,
  486. 'ipSource' => 1,
  487. 'apStatus' => 1,
  488. 'iid' => $iid
  489. );
  490. $invoiceStore->setInvoiceStoreByCondition ( $itemIS );
  491. // 更新培训班总数量
  492. $detailIT = $invoiceTraining->getInvoiceTrainingByItid ( $trainId );
  493. $item = array (
  494. 'itid' => $trainId,
  495. 'invoiceTotal' => $detailIT ['invoiceTotal'] + 1,
  496. 'submitStatus' => 1
  497. );
  498. $itid = $invoiceTraining->setInvoiceTrainByCondition ( $item );
  499. }
  500. // 操作日志
  501. $item = array (
  502. 'date' => date ( "Y-m-d H:i:s" ),
  503. 'operation' => "创建",
  504. 'status' => 1,
  505. 'img' => $this->staff [0] ['avatar'],
  506. 'username' => $this->staff [0] ['username'],
  507. 'uid' => $this->staff [0] ['sid'],
  508. 'category' => $this->staff [0] ['category'],
  509. 'iid' => $iid
  510. );
  511. $invoiceOperationLog->setInvoiceOperationLog ( $item );
  512. $item = array (
  513. 'date' => date ( "Y-m-d H:i:s" ),
  514. 'operation' => "提交审批",
  515. 'status' => 1,
  516. 'img' => $this->staff [0] ['avatar'],
  517. 'username' => $this->staff [0] ['username'],
  518. 'uid' => $this->staff [0] ['sid'],
  519. 'category' => $this->staff [0] ['category'],
  520. 'iid' => $iid
  521. );
  522. $invoiceOperationLog->setInvoiceOperationLog ( $item );
  523. }
  524. return "/invoiceUntreadED";
  525. }
  526. /**
  527. * 旧输入导入
  528. */
  529. function _GetFileEXT($filename) {
  530. $pics = explode ( '.', $filename );
  531. $num = count ( $pics );
  532. return $pics [$num - 1];
  533. }
  534. /**
  535. * 生成token
  536. * @return string
  537. */
  538. function set_token() {
  539. $str = md5 ( uniqid ( md5 ( microtime ( true ) ), true ) ); // 生成一个不会重复的字符串
  540. $str = sha1 ( $str ); // 加密
  541. return $str;
  542. }
  543. /**
  544. * 获取get或者POST值
  545. *
  546. * @param string $name 属性名称
  547. * @return fixed 值
  548. */
  549. function get_args($name) {
  550. if (isset ( $_GET [$name] )) {
  551. if (is_array ( $_GET [$name] ))
  552. return $_GET [$name];
  553. else
  554. return addslashes ( $_GET [$name] );
  555. } elseif (isset ( $_POST [$name] )) {
  556. if (is_array ( $_POST [$name] ))
  557. return $_POST [$name];
  558. else
  559. return addslashes ( $_POST [$name] );
  560. } else
  561. return false;
  562. }
  563. function SafeFilter(&$arr) {
  564. $ra = Array (
  565. '/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/',
  566. '/script/',
  567. '/javascript/',
  568. '/vbscript/',
  569. '/expression/',
  570. '/applet/',
  571. '/meta/',
  572. '/xml/',
  573. '/blink/',
  574. '/link/',
  575. '/style/',
  576. '/embed/',
  577. '/object/',
  578. '/frame/',
  579. '/layer/',
  580. '/title/',
  581. '/bgsound/',
  582. '/base/',
  583. '/onload/',
  584. '/onunload/',
  585. '/onchange/',
  586. '/onsubmit/',
  587. '/onreset/',
  588. '/onselect/',
  589. '/onblur/',
  590. '/onfocus/',
  591. '/onabort/',
  592. '/onkeydown/',
  593. '/onkeypress/',
  594. '/onkeyup/',
  595. '/onclick/',
  596. '/ondblclick/',
  597. '/onmousedown/',
  598. '/onmousemove/',
  599. '/onmouseout/',
  600. '/onmouseover/',
  601. '/onmouseup/',
  602. '/onunload/'
  603. );
  604. if (is_array ( $arr )) {
  605. foreach ( $arr as $key => $value ) {
  606. if (! is_array ( $value )) {
  607. if (! get_magic_quotes_gpc ()) // 不对magic_quotes_gpc转义过的字符使用addslashes(),避免双重转义。
  608. {
  609. $value = addslashes ( $value ); // 给单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)加上反斜线转义
  610. }
  611. $value = preg_replace ( $ra, '', $value ); // 删除非打印字符,粗暴式过滤xss可疑字符串
  612. $arr [$key] = htmlentities ( strip_tags ( $value ) ); // 去除 HTML 和 PHP 标记并转换为 HTML 实体
  613. } else {
  614. SafeFilter ( $arr [$key] );
  615. }
  616. }
  617. }
  618. }
  619. /**
  620. * Escape String
  621. *
  622. * @access public
  623. * @param string
  624. * @param bool whether or not the string will be used in a LIKE condition
  625. * @return string
  626. */
  627. function escape_str($str, $like = FALSE) {
  628. if (is_array ( $str )) {
  629. foreach ( $str as $key => $val ) {
  630. $str [$key] = $this->escape_str ( $val, $like );
  631. }
  632. return $str;
  633. }
  634. var_dump ( Doo::db ()->getDbObject () );
  635. die ();
  636. if (function_exists ( 'mysql_real_escape_string' ) and is_resource ( Doo::db ()->getDbObject () )) { // Doo::db ()->getDbObject()
  637. $str = mysql_real_escape_string ( $str, Doo::db ()->getDbObject () );
  638. } elseif (function_exists ( 'mysql_escape_string' )) {
  639. $str = mysql_escape_string ( $str );
  640. } else {
  641. $str = addslashes ( $str );
  642. }
  643. // escape LIKE condition wildcards
  644. if ($like === TRUE) {
  645. $str = str_replace ( array (
  646. '%',
  647. '_'
  648. ), array (
  649. '\\%',
  650. '\\_'
  651. ), $str );
  652. }
  653. return $str;
  654. }
  655. /**
  656. * 获得分页数据
  657. * @param unknown_type $table
  658. * @param unknown_type $condition
  659. * @param unknown_type $on_page
  660. * @param unknown_type $page_size
  661. */
  662. function get_page($table = "", $condition = "", $on_page = 1, $page_size = 20, $action = "", $get = "", $other = "page") {
  663. $page_c = "";
  664. $page ['previous'] = $this->get_previous ( $on_page );
  665. $page ['on_page'] = $on_page;
  666. $total_count = $this->get_table_count ( $table, $condition );
  667. $total = intval ( $total_count / $page_size );
  668. $page ['total_page'] = ($total_count % $page_size) == 0 ? $total : $total + 1;
  669. $page ['total_data'] = $total_count;
  670. $page ['next'] = $on_page == $page ['total_page'] ? $page ['total_page'] : $on_page + 1;
  671. $i = 1;
  672. $page_max = 1;
  673. $page_width = 3;
  674. if ($on_page >= $page_width) {
  675. $page_max = intval ( $on_page / $page_width ) + 1;
  676. $i = intval ( $on_page / $page_width ) * $page_width - 1;
  677. }
  678. for(; $i <= $page ['total_page']; $i ++) {
  679. if ($i == $on_page)
  680. $page_c .= '<li class="active"><a href="javascript:void(0)">' . $i . '</a></li>';
  681. else
  682. $page_c .= '<li><a href="/' . $action . '/' . $i . $get . '">' . $i . '</a></li>';
  683. if ($i == ($page_width * $page_max))
  684. break;
  685. }
  686. $page ['page'] = $page_c;
  687. $page ['lower'] = (-- $on_page) * $page_size;
  688. return $page;
  689. }
  690. /**
  691. * 获取上一页
  692. * @param unknown_type $on_page
  693. */
  694. function get_previous($on_page = 1) {
  695. return $on_page != 0 ? $on_page - 1 : $on_page;
  696. }
  697. /**
  698. * 获取总页数
  699. * @param unknown_type $table
  700. * @param unknown_type $condition
  701. */
  702. public function get_table_count($table = "", $condition = "") {
  703. // $sql = "select count(*) as count from " . $table . " where 1 " . $condition;
  704. $sql = "select count(*) as count from " . $table . " where " . $condition;
  705. // echo $sql;die;
  706. $query = Doo::db ()->query ( $sql );
  707. $result = $query->fetch ();
  708. return $result ['count'];
  709. }
  710. /**
  711. * 获得分页数据
  712. * @param unknown_type $table
  713. * @param unknown_type $condition
  714. * @param unknown_type $on_page
  715. * @param unknown_type $page_size
  716. */
  717. function getPageBySql($sql = "", $on_page = 1, $page_size = 20, $action = "", $get = "", $other = "page") {
  718. $page_c = "";
  719. $page ['previous'] = $this->get_previous ( $on_page );
  720. $page ['on_page'] = $on_page;
  721. $total_count = $this->getTableCountBySql ( $sql );
  722. $total = intval ( $total_count / $page_size );
  723. $page ['total_page'] = ($total_count % $page_size) == 0 ? $total : $total + 1;
  724. $page ['total_data'] = $total_count;
  725. $page ['next'] = $on_page == $page ['total_page'] ? $page ['total_page'] : $on_page + 1;
  726. $i = 1;
  727. $page_max = 1;
  728. $page_width = 3;
  729. if ($on_page >= $page_width) {
  730. $page_max = intval ( $on_page / $page_width ) + 1;
  731. $i = intval ( $on_page / $page_width ) * $page_width - 1;
  732. }
  733. for(; $i <= $page ['total_page']; $i ++) {
  734. if ($i == $on_page)
  735. $page_c .= '<li class="active"><a href="javascript:void(0)">' . $i . '</a></li>';
  736. else
  737. $page_c .= '<li><a href="/' . $action . '/' . $i . $get . '">' . $i . '</a></li>';
  738. if ($i == ($page_width * $page_max))
  739. break;
  740. }
  741. $page ['page'] = $page_c;
  742. $page ['lower'] = (-- $on_page) * $page_size;
  743. return $page;
  744. }
  745. /**
  746. * 获得总页数 sql
  747. * @param string $type
  748. * @return boolean[]
  749. */
  750. public function getTableCountBySql($sql = "") {
  751. $query = Doo::db ()->query ( $sql );
  752. $result = $query->fetchAll ();
  753. return count ( $result );
  754. }
  755. function isInvoiceNew($type = "GLOBAL") {
  756. Doo::loadModel ( 'invoice' );
  757. $invoice = new invoice ();
  758. Doo::loadModel ( 'invoiceReceivables' );
  759. $invoiceReceivables = new invoiceReceivables ();
  760. Doo::loadModel ( 'staff' );
  761. $staff = new staff ();
  762. $st = $staff->getUserByIdList ( $_COOKIE ["staff"] );
  763. // 我的发票待处理
  764. $pendingInvoice = $invoice->getOne ( array ( // or (status=2 and printStatus=1 and untreadStatus=0 ) or (status=2 and printStatus=1 and untreadStatus=2 )
  765. 'where' => "(status=3 or status=4) and isDelete=0 and sid=" . $st [0] ['sid'],
  766. 'desc' => 'iid',
  767. 'asArray' => TRUE
  768. ) );
  769. // 我的收款待入账
  770. /*
  771. * $myClaim = $invoiceReceivables->getOne ( array ( 'where' => " receivablesStaff like '" . $st [0] ['sid'] . "-%' and receivablesCategory like '" . $st [0] ['cid'] . ":%' and bindStatus=0", 'desc' => 'irid', 'asArray' => TRUE ) );
  772. */
  773. // 发票审批
  774. $approval = $invoice->getOne ( array (
  775. 'where' => 'status=1 and pendingApprovals=' . $st [0] ['sid'],
  776. 'desc' => 'iid',
  777. 'asArray' => true
  778. ) );
  779. // 发票邮寄
  780. $post = $invoice->getOne ( array (
  781. 'where' => "postStatus=0 and printStatus=1 and isDelete=0 and doPost=1 and status=2",
  782. 'asArray' => TRUE
  783. ) );
  784. // 发票打印
  785. $print = $invoice->getOne ( array (
  786. 'where' => "status=2 and printStatus=0 and untreadStatus=0 and moldManage like '%[\"" . $st [0] ['sid'] . "\",%'",
  787. 'desc' => 'iid',
  788. 'asArray' => TRUE
  789. ) );
  790. // 发票退票 printStatus=1 and
  791. $untread = $invoice->getOne ( array (
  792. 'where' => " untreadStatus=1 and status=2 ",
  793. 'asArray' => TRUE
  794. ) );
  795. $new = array (
  796. 'GLOBAL' => FALSE,
  797. 'pendingInvoice' => FALSE,
  798. // 'myClaim' => FALSE,
  799. 'approval' => FALSE,
  800. 'post' => FALSE,
  801. 'print' => FALSE,
  802. 'untread' => FALSE
  803. );
  804. // || ! empty ( $myClaim )
  805. if (! empty ( $pendingInvoice ) || ! empty ( $approval ) || ! empty ( $post ) || ! empty ( $print ) || ! empty ( $untread ))
  806. $new ['GLOBAL'] = TRUE;
  807. if (! empty ( $pendingInvoice ))
  808. $new ['pendingInvoice'] = TRUE;
  809. // if (! empty ( $myClaim ))
  810. // $new ['myClaim'] = TRUE;
  811. if (! empty ( $approval ))
  812. $new ['approval'] = TRUE;
  813. if (! empty ( $post ))
  814. $new ['post'] = TRUE;
  815. if (! empty ( $print ))
  816. $new ['print'] = TRUE;
  817. if (! empty ( $untread ))
  818. $new ['untread'] = TRUE;
  819. return $new;
  820. }
  821. function setWXMsg($rid, $typeMsg = '') {
  822. Doo::loadModel ( 'invoice' );
  823. $invoice = new invoice ();
  824. $invoiceDetail = $invoice->getOne ( array (
  825. 'where' => 'iid=' . $rid,
  826. 'asArray' => true
  827. ) );
  828. if (! empty ( $invoiceDetail )) {
  829. Doo::loadModel ( 'staff' );
  830. $staff = new staff ();
  831. $staffmsg = $staff->getOne ( array (
  832. 'where' => "sid='" . $invoiceDetail ['sid'] . "'",
  833. 'asArray' => TRUE
  834. ) );
  835. if (! empty ( $staffmsg ['wxid'] )) {
  836. require_once (SITE_PATH . '/protected/class/client.php');
  837. $client = new client ( 'http://m.cld.smartcost.com.cn/' );
  838. $user = $staffmsg ['wxid'];
  839. $expCom = explode ( ":", $invoiceDetail ['expressCompany'] );
  840. if (empty ( $expCom ))
  841. $expCom [0] = '';
  842. $msg = "";
  843. if (empty ( $typeMsg ))
  844. $msg = '太好了,您的发票' . $invoiceDetail ['invoiceSerial'] . ' 已邮寄 ' . $expCom [0] . ' ,快递号: <a href="https://m.kuaidi100.com/result.jsp?nu=' . $invoiceDetail ['expressNumber'] . '" >' . $invoiceDetail ['expressNumber'] . '</a>';
  845. elseif ($typeMsg == "PRINT")
  846. $msg = '太好了,您的发票已打印完成。';
  847. $result = $client->SendMsg ( $user, $msg, 'text' );
  848. }
  849. }
  850. }
  851. /**
  852. * 获取微信部门
  853. * @param string $category
  854. * @return number|mixed
  855. */
  856. function getToparty($category = '') {
  857. $token = $this->getAccessToken (); //
  858. $result = json_decode ( $this->httpGet ( "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={$token}&id=5" ), true );
  859. $toparty = 0;
  860. foreach ( $result ['department'] as $key => $value ) {
  861. if ($value ['name'] == $category)
  862. $toparty = $value ['id'];
  863. }
  864. return $toparty;
  865. }
  866. /**
  867. * 获得某部门下的用户
  868. * @param string $toparty
  869. */
  870. function getUserByToparty($toparty = '') {
  871. $token = $this->getAccessToken (); //
  872. $result = json_decode ( $this->httpGet ( "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token={$token}&department_id={$toparty}" ), true );
  873. $userList = array ();
  874. foreach ( $result ['userlist'] as $key => $value ) {
  875. array_push ( $userList, "'" . $value ['userid'] . "'" );
  876. }
  877. return $userList;
  878. }
  879. function msgSendToparty($msg, $title, $url, $toparty, $text = '') {
  880. $token = $this->getAccessToken ();
  881. if ($text == 'text') {
  882. $data = '{
  883. "touser": "' . $toparty . '",
  884. "toparty": "",
  885. "totag": "",
  886. "msgtype": "text",
  887. "agentid": 1000002,
  888. "text": {
  889. "content": "' . $msg . '"
  890. },
  891. "safe":0
  892. }';
  893. }
  894. $result = json_decode ( $this->httpPOST ( "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={$token}", $data ) );
  895. if ($result->errmsg != 'ok') {
  896. return 0;
  897. } else {
  898. return 1;
  899. }
  900. }
  901. /**
  902. * 获得可以发送微信用户列表
  903. * @param string $category
  904. * @return string
  905. */
  906. function getWxidByCategory($category = "") {
  907. if (empty ( $category ))
  908. return "";
  909. Doo::loadModel ( 'staff' );
  910. $staff = new staff ();
  911. $toparty = $this->getToparty ( $category );
  912. $wxList = array ();
  913. $wxString = "";
  914. if (! empty ( $toparty )) {
  915. $userList = $this->getUserByToparty ( $toparty );
  916. $userCon = implode ( ',', $userList );
  917. // echo $userCon;
  918. $sidList = $staff->getStaffByWxid ( $userCon );
  919. foreach ( $sidList as $key => $value ) {
  920. $falg = $this->isSendNotification ( $value ['sid'], 'receivables' );
  921. if ($falg) {
  922. array_push ( $wxList, $value ['wxid'] );
  923. }
  924. }
  925. $wxString = implode ( '|', $wxList );
  926. }
  927. return $wxString;
  928. }
  929. /**
  930. * 读取发送信息规则判断是否需要发送
  931. * @param number $sid
  932. * @param string $sendType
  933. * @return boolean
  934. */
  935. function isSendNotification($sid = 0, $sendType = '') {
  936. Doo::loadModel ( "notificationConf" );
  937. $notificationConf = new notificationConf ();
  938. $detail = $notificationConf->getNotificationConfBySid ( $sid );
  939. $conf = array ();
  940. $sendFlag = true;
  941. if (! empty ( $detail )) {
  942. $conf = json_decode ( $detail ['conf'], true );
  943. if (! empty ( $sendType )) {
  944. if (isset ( $conf [$sendType] )) {
  945. if ($conf [$sendType] == "CLOSE")
  946. $sendFlag = false;
  947. elseif ($conf [$sendType] == "Weekend") {
  948. $day = date ( "w" );
  949. if (1 <= $day && 5 >= $day)
  950. $sendFlag = false;
  951. } elseif ($conf [$sendType] == "OneToFriday") {
  952. $day = date ( "w" );
  953. if (0 == $day || 6 == $day)
  954. $sendFlag = false;
  955. }
  956. }
  957. } else
  958. $sendFlag = false;
  959. }
  960. return $sendFlag;
  961. }
  962. /**
  963. * 根据规则发送信息
  964. * @param unknown $msg
  965. * @param unknown $title
  966. * @param unknown $url
  967. * @param unknown $user
  968. * @param string $text
  969. * @return number
  970. */
  971. private function msgSend($msg, $title, $url, $user, $sid = 0, $sendType = '') {
  972. $token = $this->getAccessToken ();
  973. $falg = $this->isSendNotification ( $sid, $sendType );
  974. // if ($text == 'text') {
  975. // $data = '{
  976. // "touser": "' . $user . '",
  977. // "toparty": "",
  978. // "totag": "",
  979. // "msgtype": "text",
  980. // "agentid": 1000002,
  981. // "text": {
  982. // "content": "' . $msg . '"
  983. // },
  984. // "safe":0
  985. // }';
  986. // } else {
  987. $data = '{
  988. "touser": "' . $user . '",
  989. "toparty": "",
  990. "totag": "",
  991. "msgtype": "news",
  992. "agentid": 1000002,
  993. "news": {
  994. "articles":[
  995. {
  996. "title": "' . $title . '",
  997. "description": "' . $msg . '",
  998. "url": "' . $url . '",
  999. "picurl": ""
  1000. }
  1001. ]
  1002. }
  1003. }';
  1004. // }
  1005. if ($falg) {
  1006. $result = json_decode ( $this->httpPOST ( "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={$token}", $data ) );
  1007. if ($result->errmsg != 'ok') {
  1008. return 0;
  1009. } else {
  1010. return 1;
  1011. }
  1012. } else
  1013. return 0;
  1014. }
  1015. private function getAccessToken() {
  1016. // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
  1017. // $file = './access_token.txt';
  1018. // $data = json_decode ( file_get_contents ( $file ) );
  1019. // if ($data->expire_time < time () or ! $data->expire_time) {
  1020. // 如果是企业号用以下URL获取access_token
  1021. $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
  1022. // $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
  1023. $res = json_decode ( $this->httpGet ( $url ) );
  1024. $access_token = $res->access_token;
  1025. // echo $access_token;
  1026. // if ($access_token) {
  1027. // $data->expire_time = time () + 7000;
  1028. // $data->access_token = $access_token;
  1029. // $fp = fopen ( $file, "w" );
  1030. // fwrite ( $fp, json_encode ( $data ) );
  1031. // fclose ( $fp );
  1032. // }
  1033. // } else {
  1034. // $access_token = $data->access_token;
  1035. // }
  1036. return $access_token;
  1037. }
  1038. private function httpGet($url) {
  1039. $curl = curl_init ();
  1040. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
  1041. curl_setopt ( $curl, CURLOPT_TIMEOUT, 500 );
  1042. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  1043. // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  1044. curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, true );
  1045. curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false );
  1046. curl_setopt ( $curl, CURLOPT_URL, $url );
  1047. $res = curl_exec ( $curl );
  1048. curl_close ( $curl );
  1049. return $res;
  1050. }
  1051. private function httpPost($url, $data) {
  1052. $curl = curl_init ();
  1053. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
  1054. curl_setopt ( $curl, CURLOPT_TIMEOUT, 500 );
  1055. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  1056. // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  1057. curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, true );
  1058. curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false );
  1059. curl_setopt ( $curl, CURLOPT_URL, $url );
  1060. curl_setopt ( $curl, CURLOPT_POST, 1 );
  1061. curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data );
  1062. $res = curl_exec ( $curl );
  1063. curl_close ( $curl );
  1064. return $res;
  1065. }
  1066. }
  1067. ?>