| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- namespace app\controller\api\store\service;
- use think\App;
- use think\facade\Db;
- use crmeb\basic\BaseController;
- class Huafei extends BaseController
- {
- public $appkey = '1077740514';
- public $secretKey = 'f154a106d56d804fcc640d9da4844b0c';
- // 生产
- public $gatewayUrl = "https://router.wikeyun.cn";
- public $format = "json";
- /** 是否打开入参check**/
- protected $signMethod = "md5";
- protected $apiVersion = "1.0";
- public function __construct(App $app)
- {
- parent::__construct($app);
- }
- //充值话费回调
- public function notify(){
- $param = $this->request->param();
- Db::name('log')->insert(['data'=>'话费充值回调数据'.json_encode($param)]);
- $order_no = $param['order_no'];
- $order_number = $param['order_number'];
- $updateData = [
- 'amount_price' => $param['amount'],
- 'fanli_price' => $param['fanli'],
- 'cost_price' => $param['cost_price'],
- 'arrived_amount'=> $param['arrived_amount'],
- 'status' => $param['status'],
- 'modify_time' => time(),
- ];
- $ret = Db::name("order_huafei")->where("order_number", $order_number)->where("order_sn", $order_no)->update($updateData);
- if($ret){
- echo 'success';exit;
- }else{
- Db::name('log')->insert(['data'=>'话费充值回调数据处理失败']);
- echo 'error';exit;
- }
- }
- //充值话费面额
- public function denomination(){
- $data = [
- ['money'=>50, 'name'=>'50元'],
- ['money'=>100, 'name'=>'100元'],
- ['money'=>200, 'name'=>'200元'],
- ];
- return app('json')->success('获取话费面额成功', $data);
- }
- //充值话费推送
- public function recharge(){
- $uid = $this->request->uid();
- $params = $this->request->params(['phone', 'money']);
- if(!$params['phone']) return app('json')->fail('请提交电话号码');
- if(!$params['money']) return app('json')->fail('请提交充值金额');
- //验证
- $verify = $this->verify($params['phone'], $params['money']);
- if($verify['code'] == '0000'){
- //推送
- $order_sn = time().$uid;
- $push = $this->pushOrder($order_sn, $params['phone'], $params['money']);
- if($push['code'] == '0000'){
- //入库记录
- $insertdata = [
- 'uid'=> $uid,
- 'mobile' => $params['phone'],
- 'commission'=> 0,
- 'commission_share'=> 0,
- 'order_number' => $push['data']['order_number'],
- 'order_sn' => $order_sn,
- 'amount_price' => $push['data']['amount'],
- 'fanli_price' => $push['data']['fanli'],
- 'cost_price' => $push['data']['cost_price'],
- 'pay_money' => $params['money'],
- 'status' => 0,
- 'create_time' => time(),
- 'finish_time' => time(),
- 'modify_time' => time(),
- ];
- Db::name('order_huafei')->insert($insertdata);
- return app('json')->success('话费推送成功', $push);
- }else{
- return app('json')->fail($push['msg']);
- }
- }else{
- return app('json')->fail($verify['msg']);
- }
- }
- //话费充值记录
- public function record(){
- $uid = $this->request->uid();
- [$page, $limit] = $this->getPage();
- $where = [];
- $where[] = ['uid', '=', $uid];
- $query = Db::name("order_huafei");
- $count = $query->where($where)->count();
- $list = $query->page($page, $limit)->where($where)->select()->each(function ($item){
- $item['create_time'] = date("Y-m-d H:i:s", $item['create_time']);
- $item['status'] = (int)$item['status'];
- return $item;
- });
- return app('json')->success('获取话费充值记录成功', ['list'=>$list, 'count'=>$count]);
- }
- //话费充值验证
- public function verify($order_number, $money){
- $request['mobile'] = $order_number;
- $request['amount'] = $money;
- $request['recharge_type'] = 1;
- return $this->execute('/rest/Recharge/verify', $request);
- }
- //话费订单充值推送
- public function pushOrder($order_sn, $phone, $money){
- $request['store_id'] = '9189';
- $request['order_no'] = $order_sn;
- $request['mobile'] = $phone;
- $request['money'] = $money;
- $request['recharge_type'] = 1;
- $request['notify_url'] = 'https://api.gdjbp.com//api/huafei/notify';
- return $this->execute('/rest/Recharge/PushOrder', $request);
- }
- //话费订单查询
- public function query($order_number){
- $request['store_id'] = '9189';
- $request['order_number'] = $order_number;
- return $this->execute('/rest/Recharge/query', $request);
- }
- //话费订单取消
- public function cancel($order_number){
- $request['order_number'] = $order_number;
- return $this->execute('/rest/Recharge/cancel', $request);
- }
- protected function generateSign($params){
- ksort($params);
- $stringToBeSigned = $this->secretKey;
- foreach ($params as $k => $v)
- {
- if (is_array($v)) {
- $stringToBeSigned .= $k;
- }else{
- if ("@" != substr($v, 0, 1)) {
- $stringToBeSigned .= "$k$v";
- }
- }
- }
- unset($k, $v);
- $stringToBeSigned .= $this->secretKey;
- return strtoupper(md5($stringToBeSigned));
- }
- public function curl($url, $postFields = null,$timeOut = 20){
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_FAILONERROR, false);
- curl_setopt($ch, CURLOPT_REFERER, 'https://api.gdjbp.com');
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut);
- //https 请求
- if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- if (is_array($postFields) && 0 < count($postFields)){
- $postBodyString = "";
- $postMultipart = false;
- foreach ($postFields as $k => $v){
- if("@" != substr($v, 0, 1))//判断是不是文件上传
- {
- $postBodyString .= "$k=" . urlencode($v) . "&";
- }
- else//文件上传用multipart/form-data,否则用www-form-urlencoded
- {
- $postMultipart = true;
- }
- $postMultipart = true;
- }
- unset($k, $v);
- curl_setopt($ch, CURLOPT_POST, true);
- if ($postMultipart){
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
- }else{
- curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
- }
- }
- $reponse = curl_exec($ch);
- curl_close($ch);
- return json_decode($reponse, true);
- }
- /**
- * request api接口后缀,如 /recharge/pushOrder
- * apiParams 业务参数 数组
- */
- public function execute($request, $apiParams ,$timeOut =20){
- //组装系统参数
- $sysParams["app_key"] = $this->appkey;
- $sysParams["v"] = $this->apiVersion;
- $sysParams["format"] = $this->format;
- $sysParams["timestamp"] = time();
- $sysParams["client"] = '';
- //签名
- $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
- //系统参数放入GET请求串
- //$requestUrl = $this->gatewayUrl.$sysParams['method']. "?";
- $requestUrl = $this->gatewayUrl.$request. "?";
- foreach ($sysParams as $sysParamKey => $sysParamValue){
- $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
- }
- $requestUrl = substr($requestUrl, 0, -1);
- return $this->curl($requestUrl, $apiParams ,$timeOut);
- }
- }
|