Huafei.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace app\controller\api\store\service;
  3. use think\App;
  4. use think\facade\Db;
  5. use crmeb\basic\BaseController;
  6. class Huafei extends BaseController
  7. {
  8. public $appkey = '1077740514';
  9. public $secretKey = 'f154a106d56d804fcc640d9da4844b0c';
  10. // 生产
  11. public $gatewayUrl = "https://router.wikeyun.cn";
  12. public $format = "json";
  13. /** 是否打开入参check**/
  14. protected $signMethod = "md5";
  15. protected $apiVersion = "1.0";
  16. public function __construct(App $app)
  17. {
  18. parent::__construct($app);
  19. }
  20. //充值话费回调
  21. public function notify(){
  22. $param = $this->request->param();
  23. Db::name('log')->insert(['data'=>'话费充值回调数据'.json_encode($param)]);
  24. $order_no = $param['order_no'];
  25. $order_number = $param['order_number'];
  26. $updateData = [
  27. 'amount_price' => $param['amount'],
  28. 'fanli_price' => $param['fanli'],
  29. 'cost_price' => $param['cost_price'],
  30. 'arrived_amount'=> $param['arrived_amount'],
  31. 'status' => $param['status'],
  32. 'modify_time' => time(),
  33. ];
  34. $ret = Db::name("order_huafei")->where("order_number", $order_number)->where("order_sn", $order_no)->update($updateData);
  35. if($ret){
  36. echo 'success';exit;
  37. }else{
  38. Db::name('log')->insert(['data'=>'话费充值回调数据处理失败']);
  39. echo 'error';exit;
  40. }
  41. }
  42. //充值话费面额
  43. public function denomination(){
  44. $data = [
  45. ['money'=>50, 'name'=>'50元'],
  46. ['money'=>100, 'name'=>'100元'],
  47. ['money'=>200, 'name'=>'200元'],
  48. ];
  49. foreach($data as $k=>$v){
  50. $commissionMoney = $v['money'];
  51. $systemvalueObj = $goods_obj = Db::name('system_config_value')->where('config_key','sanfangfenpei')->find();
  52. $sanfangfenpei=json_decode($systemvalueObj['value'],true);
  53. $commissionMoney80=$commissionMoney*0.8;
  54. $pv=$commissionMoney80*$sanfangfenpei['pv']; //PV
  55. $yanglaojin=$commissionMoney80*$sanfangfenpei['yanglaojin']; //新的养老金
  56. $gongxian=$pv; //新的养老金
  57. $jifen=$pv; //新的养老金
  58. $data[$k]['pv']=round($pv,2);
  59. $data[$k]['yanglaojin']=round($yanglaojin,2);
  60. $data[$k]['gongxian']=round($gongxian,2);
  61. $data[$k]['jifen']=round($jifen,2);
  62. }
  63. return app('json')->success('获取话费面额成功', $data);
  64. }
  65. //充值话费推送
  66. public function recharge(){
  67. $uid = $this->request->uid();
  68. $params = $this->request->params(['phone', 'money']);
  69. if(!$params['phone']) return app('json')->fail('请提交电话号码');
  70. if(!$params['money']) return app('json')->fail('请提交充值金额');
  71. //验证
  72. $verify = $this->verify($params['phone'], $params['money']);
  73. if($verify['code'] == '0000'){
  74. //推送
  75. $order_sn = time().$uid;
  76. $push = $this->pushOrder($order_sn, $params['phone'], $params['money']);
  77. if($push['code'] == '0000'){
  78. //入库记录
  79. $insertdata = [
  80. 'uid'=> $uid,
  81. 'mobile' => $params['phone'],
  82. 'commission'=> 0,
  83. 'commission_share'=> 0,
  84. 'order_number' => $push['data']['order_number'],
  85. 'order_sn' => $order_sn,
  86. 'amount_price' => $push['data']['amount'],
  87. 'fanli_price' => $push['data']['fanli'],
  88. 'cost_price' => $push['data']['cost_price'],
  89. 'pay_money' => $params['money'],
  90. 'status' => 0,
  91. 'create_time' => time(),
  92. 'finish_time' => time(),
  93. 'modify_time' => time(),
  94. ];
  95. Db::name('order_huafei')->insert($insertdata);
  96. return app('json')->success('话费推送成功', $push);
  97. }else{
  98. return app('json')->fail($push['msg']);
  99. }
  100. }else{
  101. return app('json')->fail($verify['msg']);
  102. }
  103. }
  104. //话费充值记录
  105. public function record(){
  106. $uid = $this->request->uid();
  107. [$page, $limit] = $this->getPage();
  108. $where = [];
  109. $where[] = ['uid', '=', $uid];
  110. $query = Db::name("order_huafei");
  111. $count = $query->where($where)->count();
  112. $list = $query->page($page, $limit)->where($where)->select()->each(function ($item){
  113. $item['create_time'] = date("Y-m-d H:i:s", $item['create_time']);
  114. $item['status'] = (int)$item['status'];
  115. return $item;
  116. });
  117. return app('json')->success('获取话费充值记录成功', ['list'=>$list, 'count'=>$count]);
  118. }
  119. //话费充值验证
  120. public function verify($order_number, $money){
  121. $request['mobile'] = $order_number;
  122. $request['amount'] = $money;
  123. $request['recharge_type'] = 1;
  124. return $this->execute('/rest/Recharge/verify', $request);
  125. }
  126. //话费订单充值推送
  127. public function pushOrder($order_sn, $phone, $money){
  128. $request['store_id'] = '9189';
  129. $request['order_no'] = $order_sn;
  130. $request['mobile'] = $phone;
  131. $request['money'] = $money;
  132. $request['recharge_type'] = 1;
  133. $request['notify_url'] = 'https://api.gdjbp.com//api/huafei/notify';
  134. return $this->execute('/rest/Recharge/PushOrder', $request);
  135. }
  136. //话费订单查询
  137. public function query($order_number){
  138. $request['store_id'] = '9189';
  139. $request['order_number'] = $order_number;
  140. return $this->execute('/rest/Recharge/query', $request);
  141. }
  142. //话费订单取消
  143. public function cancel($order_number){
  144. $request['order_number'] = $order_number;
  145. return $this->execute('/rest/Recharge/cancel', $request);
  146. }
  147. protected function generateSign($params){
  148. ksort($params);
  149. $stringToBeSigned = $this->secretKey;
  150. foreach ($params as $k => $v)
  151. {
  152. if (is_array($v)) {
  153. $stringToBeSigned .= $k;
  154. }else{
  155. if ("@" != substr($v, 0, 1)) {
  156. $stringToBeSigned .= "$k$v";
  157. }
  158. }
  159. }
  160. unset($k, $v);
  161. $stringToBeSigned .= $this->secretKey;
  162. return strtoupper(md5($stringToBeSigned));
  163. }
  164. public function curl($url, $postFields = null,$timeOut = 20){
  165. $ch = curl_init();
  166. curl_setopt($ch, CURLOPT_URL, $url);
  167. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  168. curl_setopt($ch, CURLOPT_REFERER, 'https://api.gdjbp.com');
  169. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  170. curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut);
  171. //https 请求
  172. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  173. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  174. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  175. }
  176. if (is_array($postFields) && 0 < count($postFields)){
  177. $postBodyString = "";
  178. $postMultipart = false;
  179. foreach ($postFields as $k => $v){
  180. if("@" != substr($v, 0, 1))//判断是不是文件上传
  181. {
  182. $postBodyString .= "$k=" . urlencode($v) . "&";
  183. }
  184. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  185. {
  186. $postMultipart = true;
  187. }
  188. $postMultipart = true;
  189. }
  190. unset($k, $v);
  191. curl_setopt($ch, CURLOPT_POST, true);
  192. if ($postMultipart){
  193. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  194. }else{
  195. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  196. }
  197. }
  198. $reponse = curl_exec($ch);
  199. curl_close($ch);
  200. return json_decode($reponse, true);
  201. }
  202. /**
  203. * request api接口后缀,如 /recharge/pushOrder
  204. * apiParams 业务参数 数组
  205. */
  206. public function execute($request, $apiParams ,$timeOut =20){
  207. //组装系统参数
  208. $sysParams["app_key"] = $this->appkey;
  209. $sysParams["v"] = $this->apiVersion;
  210. $sysParams["format"] = $this->format;
  211. $sysParams["timestamp"] = time();
  212. $sysParams["client"] = '';
  213. //签名
  214. $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
  215. //系统参数放入GET请求串
  216. //$requestUrl = $this->gatewayUrl.$sysParams['method']. "?";
  217. $requestUrl = $this->gatewayUrl.$request. "?";
  218. foreach ($sysParams as $sysParamKey => $sysParamValue){
  219. $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
  220. }
  221. $requestUrl = substr($requestUrl, 0, -1);
  222. return $this->curl($requestUrl, $apiParams ,$timeOut);
  223. }
  224. }