common.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. use AlibabaCloud\SDK\Cloudauth\V20190307\Cloudauth;
  3. use AlibabaCloud\SDK\Cloudauth\V20190307\Models\DescribeFaceVerifyRequest;
  4. use AlibabaCloud\SDK\Cloudauth\V20190307\Models\InitFaceVerifyRequest;
  5. use AlibabaCloud\Tea\Exception\TeaError;
  6. use AlibabaCloud\Tea\Utils\Utils;
  7. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  8. use app\data\model\DataUserRepair;
  9. use app\data\model\ShopPurchase;
  10. use Darabonba\OpenApi\Models\Config;
  11. use think\admin\model\SystemConfig;
  12. use app\common\library\QRcode;
  13. use PhpOffice\PhpWord\TemplateProcessor;
  14. use JPush\Client as jpush;
  15. function systemConfig($name,$value=null){
  16. $nameArr=array_filter(explode('.',$name));
  17. if(is_null($value)){
  18. if(count($nameArr)==1) {
  19. $source = SystemConfig::where('type', $nameArr[0])->select();
  20. $data=[];
  21. foreach ($source as $model){
  22. $value=$model['value'];
  23. if($decode=json_decode($value,true)){
  24. $value=$decode;
  25. }
  26. $data[$model['name']]=$value;
  27. }
  28. return $data?:null;
  29. }else{
  30. $model = SystemConfig::where('type', $nameArr[0])->where('name',$nameArr[1])->find();
  31. $value=$model['value'];
  32. if($decode=json_decode($value,true)){
  33. $value=$decode;
  34. }
  35. return $value;
  36. }
  37. }else{
  38. SystemConfig::where('type', $nameArr[0])->delete();
  39. if(count($nameArr)==1) {
  40. if(is_array($value)){
  41. foreach ($value as $key=>$val){
  42. SystemConfig::insert([
  43. 'type'=>$nameArr[0],
  44. 'name'=>$key,
  45. 'value'=>is_array($val)?json_encode($val,256):$val,
  46. ]);
  47. }
  48. }
  49. }else{
  50. SystemConfig::insert([
  51. 'type'=>$nameArr[0],
  52. 'name'=>$nameArr[1],
  53. 'value'=>$value,
  54. ]);
  55. }
  56. }
  57. }
  58. /**
  59. * 生成??位随机数
  60. */
  61. function get32Str($length='32'){
  62. // $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  63. $str = 'abcdefghijklmnopqrstuvwxyz0123456789';
  64. $len = strlen($str)-1;
  65. $randstr = '';
  66. for ($i=0;$i<$length;$i++) {
  67. $num=mt_rand(0,$len);
  68. $randstr .= $str[$num];
  69. }
  70. return $randstr;
  71. }
  72. /**
  73. * 使用AK&SK初始化账号Client
  74. * @param string $accessKeyId
  75. * @param string $accessKeySecret
  76. * @return Cloudauth Client
  77. */
  78. function createClient($accessKeyId, $accessKeySecret){
  79. $config = new Config([
  80. // 您的 AccessKey ID
  81. "accessKeyId" => $accessKeyId,
  82. // 您的 AccessKey Secret
  83. "accessKeySecret" => $accessKeySecret,
  84. ]);
  85. // 访问的域名
  86. $config->endpoint = "cloudauth.aliyuncs.com";
  87. return new Cloudauth($config);
  88. }
  89. /**
  90. * @param string[] $args
  91. * @return void
  92. */
  93. function main($certName,$certNo,$metaInfo,$ReturnUrl){
  94. $client = createClient("LTAI5tMVT8vs7B1CApqhEgBo", "ESDiEM6zED1vyCMoGc9j5gZ9hWIXkP");
  95. $initFaceVerifyRequest = new InitFaceVerifyRequest([
  96. "sceneId" => '1000006090',
  97. "outerOrderNo" => get32Str(),
  98. // 要接入的认证方案。
  99. "productCode" => "ID_PRO",
  100. "certType" => "IDENTITY_CARD",
  101. "certName" => $certName,
  102. "certNo" => $certNo,
  103. // MetaInfo环境参数,需要通过客户端SDK获取。
  104. "metaInfo" => $metaInfo,
  105. "returnUrl" => $ReturnUrl,
  106. ]);
  107. $runtime = new RuntimeOptions([]);
  108. try {
  109. // 复制代码运行请自行打印 API 的返回值
  110. $res = $client->initFaceVerifyWithOptions($initFaceVerifyRequest, $runtime);
  111. return $res;
  112. } catch (Exception $error) {
  113. if (!($error instanceof TeaError)) {
  114. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  115. }
  116. // 如有需要,请打印 error
  117. Utils::assertAsString($error->message);
  118. return '';
  119. }
  120. }
  121. /**
  122. * @param string[] $args
  123. * @return void
  124. */
  125. function check($CertifyId){
  126. $client = createClient("LTAI5tMVT8vs7B1CApqhEgBo", "ESDiEM6zED1vyCMoGc9j5gZ9hWIXkP");
  127. $describeFaceVerifyRequest = new DescribeFaceVerifyRequest([
  128. 'sceneId' => '1000006090',
  129. 'certifyId' => $CertifyId,
  130. ]);
  131. $runtime = new RuntimeOptions([]);
  132. try {
  133. // 复制代码运行请自行打印 API 的返回值
  134. $rs = $client->describeFaceVerifyWithOptions($describeFaceVerifyRequest, $runtime);
  135. return $rs;
  136. }
  137. catch (Exception $error) {
  138. if (!($error instanceof TeaError)) {
  139. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  140. }
  141. // 如有需要,请打印 error
  142. Utils::assertAsString($error->message);
  143. return false;
  144. }
  145. }
  146. /**
  147. * 生成二维码
  148. */
  149. function setqrcode($value,$name){
  150. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/';
  151. if(!file_exists($dir)){
  152. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  153. mkdir($dir, 0700,true);
  154. }
  155. $filename = $dir.'/'.$name.'.png';
  156. QRcode::png($value,$filename,QR_ECLEVEL_L,7);
  157. $url = 'http://'.$_SERVER['SERVER_NAME']."/".$name.'.png';
  158. return $url;
  159. }
  160. //生成邀请二维码
  161. function setappcode($address){
  162. $name = time();
  163. $url = setqrcode($address,$name);
  164. return $url;
  165. }
  166. //维修单,四大订单生成下载文件
  167. function setword($type,$id){
  168. $url = '';
  169. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/';
  170. $dir2 = $dir.'down/';
  171. if(!file_exists($dir2)){
  172. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  173. mkdir($dir2, 0700,true);
  174. }
  175. switch ($type){
  176. case 1: //维修订单
  177. $templateProcessor = new TemplateProcessor($dir.'/wxdd.docx');
  178. $info = DataUserRepair::mk()->with(['user','user2'])->findOrEmpty($id);
  179. $array = [
  180. 'order_no'=>$info['order_no'],
  181. 'date'=>$info['start_time'],
  182. 'fbz'=>$info['user']['nickname'],
  183. 'jdf'=>$info['user2']['nickname'],
  184. 'fbzphone'=>$info['user']['phone'],
  185. 'jdfphone'=>$info['user2']['phone'],
  186. 'address'=>$info['province'].$info['city'].$info['area'].$info['address'],
  187. 'time'=>$info['start_time'].'-'.$info['end_time'],
  188. 'desc'=>$info['describe'],
  189. 'price'=>$info['success_price']
  190. ];
  191. $templateProcessor->setValues($array);
  192. $name = 'wxdd-'.$id.'-'.time().'.docx';
  193. $url = 'http://'.$_SERVER['SERVER_NAME']."/down/".$name;
  194. $templateProcessor->saveAs($dir2.$name);
  195. break;
  196. case 2: //采购订单
  197. $templateProcessor = new TemplateProcessor($dir.'/cgdd.docx');
  198. $info = $model=ShopPurchase::getItem(1)
  199. ->lock(true)
  200. ->where('purchase_id',$id)
  201. ->with(['main','ppoffer','main.merchant','ppoffer.user'])
  202. ->findOrEmpty();
  203. // dump($info->toArray());die;
  204. $allprice = bcmul($info['price'],$info['number'],2);
  205. $array = [
  206. 'order_no'=>$info['main']['order_no'],
  207. 'date'=>date('Y-m-d',strtotime($info['main']['post_time'])),
  208. 'shopname'=>$info['main']['merchant']['name'],
  209. 'mfname'=>$info['main']['merchant']['contact_name'],
  210. 'mfphone'=>$info['main']['merchant']['contact_phone'],
  211. 'mmfname'=>$info['ppoffer']['user']['nickname'],
  212. 'mmfphone'=>$info['ppoffer']['user']['phone'],
  213. 'id'=>1,
  214. 'name'=>$info['name'],
  215. 'gg'=>$info['spec'],
  216. 'num'=>$info['number'],
  217. 'price'=>$info['price'],
  218. 'allprice'=>$allprice,
  219. 'dxprice'=>num_to_rmb($allprice),
  220. 'toprice'=>$allprice,
  221. 'sprice'=>$info['ppoffer']['amount']
  222. ];
  223. $templateProcessor->setValues($array);
  224. $name = 'cgdd-'.$id.'-'.time().'.docx';
  225. $url = 'http://'.$_SERVER['SERVER_NAME']."/down/".$name;
  226. $templateProcessor->saveAs($dir2.$name);
  227. break;
  228. case 3: case 4: //生产订单//外协订单
  229. if ($type==3){
  230. $filename = 'scdd';
  231. }elseif ($type==4){
  232. $filename = 'wxxdd';
  233. }
  234. $templateProcessor = new TemplateProcessor($dir.'/'.$filename.'.docx');
  235. $info =ShopPurchase::getItem($type-1)
  236. ->with(['merchant','items','ppoffer','ppoffer.user'])
  237. ->lock(true)
  238. ->findOrEmpty($id);
  239. $array = [
  240. 'order_no'=>$info['order_no'],
  241. 'date'=>date('Y-m-d',strtotime($info['post_time'])),
  242. 'shopname'=>$info['merchant']['name'],
  243. 'mfname'=>$info['merchant']['contact_name'],
  244. 'mfphone'=>$info['merchant']['contact_phone'],
  245. 'mmfname'=>$info['ppoffer']['user']['nickname'],
  246. 'mmfphone'=>$info['ppoffer']['user']['phone'],
  247. 'price'=>$info['ppoffer']['amount']
  248. ];
  249. $templateProcessor->setValues($array);
  250. $count = count($info['items']);
  251. $templateProcessor->cloneRow('id',$count);
  252. foreach ($info['items'] as $k=>$v){
  253. $templateProcessor->setValue('id#'.($k+1), ($k+1));
  254. $templateProcessor->setValue('name#'.($k+1),$v['name']);
  255. $templateProcessor->setValue('gg#'.($k+1), $v['spec']);
  256. $templateProcessor->setValue('num#'.($k+1), $v['number']);
  257. }
  258. $name = $filename.'-'.$id.'-'.time().'.docx';
  259. $url = 'http://'.$_SERVER['SERVER_NAME']."/down/".$name;
  260. $templateProcessor->saveAs($dir2.$name);
  261. break;
  262. case 5: //海运订单
  263. $templateProcessor = new TemplateProcessor($dir.'/hydd.docx');
  264. $info =ShopPurchase::getItem($type-1)
  265. ->with(['merchant','items','ppoffer','ppoffer.user'])
  266. ->lock(true)
  267. ->findOrEmpty($id);
  268. $array = [
  269. 'order_no'=>$info['order_no'],
  270. 'date'=>date('Y-m-d',strtotime($info['create_time'])),
  271. 'fbr'=>$info['merchant']['contact_name'],
  272. 'jdr'=>$info['ppoffer']['user']['nickname'],
  273. 'bz'=>$info['form'],
  274. 'zhmt'=>$info['wharf_to'],
  275. 'dgmt'=>$info['wharf_for'],
  276. 'price'=>$info['ppoffer']['amount'],
  277. 'fbrphone'=>$info['merchant']['contact_phone'],
  278. 'jdrphone'=>$info['ppoffer']['user']['phone']
  279. ];
  280. $templateProcessor->setValues($array);
  281. $count = count($info['items']);
  282. $templateProcessor->cloneRow('id',$count);
  283. foreach ($info['items'] as $k=>$v){
  284. $templateProcessor->setValue('id#'.($k+1), ($k+1));
  285. $templateProcessor->setValue('name#'.($k+1),$v['name']);
  286. $templateProcessor->setValue('num#'.($k+1), $v['weight']);
  287. }
  288. $name = 'hydd-'.$id.'-'.time().'.docx';
  289. $url = 'http://'.$_SERVER['SERVER_NAME']."/down/".$name;
  290. $templateProcessor->saveAs($dir2.$name);
  291. break;
  292. }
  293. return $url;
  294. }
  295. /**
  296. *数字金额转换成中文大写金额的函数
  297. *String Int $num 要转换的小写数字或小写字符串
  298. *return 大写字母
  299. *小数位为两位
  300. **/
  301. function num_to_rmb($num){
  302. $c1 = "零壹贰叁肆伍陆柒捌玖";
  303. $c2 = "分角元拾佰仟万拾佰仟亿";
  304. //精确到分后面就不要了,所以只留两个小数位
  305. $num = round($num, 2);
  306. //将数字转化为整数
  307. $num = $num * 100;
  308. if (strlen($num) > 10) {
  309. return "金额太大,请检查";
  310. }
  311. $i = 0;
  312. $c = "";
  313. while (1) {
  314. if ($i == 0) {
  315. //获取最后一位数字
  316. $n = substr($num, strlen($num)-1, 1);
  317. } else {
  318. $n = $num % 10;
  319. }
  320. //每次将最后一位数字转化为中文
  321. $p1 = substr($c1, 3 * $n, 3);
  322. $p2 = substr($c2, 3 * $i, 3);
  323. if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
  324. $c = $p1 . $p2 . $c;
  325. } else {
  326. $c = $p1 . $c;
  327. }
  328. $i = $i + 1;
  329. //去掉数字最后一位了
  330. $num = $num / 10;
  331. $num = (int)$num;
  332. //结束循环
  333. if ($num == 0) {
  334. break;
  335. }
  336. }
  337. $j = 0;
  338. $slen = strlen($c);
  339. while ($j < $slen) {
  340. //utf8一个汉字相当3个字符
  341. $m = substr($c, $j, 6);
  342. //处理数字中很多0的情况,每次循环去掉一个汉字“零”
  343. if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
  344. $left = substr($c, 0, $j);
  345. $right = substr($c, $j + 3);
  346. $c = $left . $right;
  347. $j = $j-3;
  348. $slen = $slen-3;
  349. }
  350. $j = $j + 3;
  351. }
  352. //这个是为了去掉类似23.0中最后一个“零”字
  353. if (substr($c, strlen($c)-3, 3) == '零') {
  354. $c = substr($c, 0, strlen($c)-3);
  355. }
  356. //将处理的汉字加上“整”
  357. if (empty($c)) {
  358. return "零元整";
  359. }else{
  360. return $c . "整";
  361. }
  362. }
  363. /**
  364. * @消息推送(最新版本)
  365. */
  366. function push5($uid,$content,$alias){
  367. $app_key = '9dd98f61eb4ad58dca4c8214';
  368. $master_secret = 'f7cac744173f8ace703719cf';
  369. $client = new JPush($app_key, $master_secret);
  370. try {
  371. $response = $client->push()
  372. ->setPlatform(array('ios', 'android'))
  373. ->addAlias($alias)
  374. // ->addRegistrationId($registration_id)
  375. ->setNotificationAlert($content)
  376. ->iosNotification($content,array(
  377. 'sound'=>'default',
  378. 'badge' => 2,
  379. 'content-available' => true,
  380. 'category' => 'jiguang',
  381. 'extras' => ['user_id'=>$uid,'alias'=>$alias]
  382. )
  383. )
  384. ->androidNotification($content,array(
  385. 'title' => $content,
  386. 'build_id' => 2,
  387. 'extras' => array(
  388. "user_id"=>$uid,"alias"=>$alias),
  389. )
  390. )
  391. ->options([
  392. 'sendno' => 100,
  393. 'time_to_live' => 86400,
  394. 'apns_production' => false,
  395. 'big_push_duration' => 0
  396. ])
  397. ->send();
  398. return 1;
  399. }catch (APIRequestException $e) {
  400. return 2;
  401. } catch (APIConnectionException $e) {
  402. return 3;
  403. }
  404. }