小程序可以视为前端,使用异步请求(wx.request)的方式获取thinkphp(服务端)的接口。
thinkphp方面。只要设埋团置镇液胡好各个接口就行了。
在控制器中使用$this->result返回封装后的api数据到客户端御拦(小程序)。返回的是json格式数据。
class BaiDuAiBaseController extends BaseController{
private $appid
private $appKey
private $secretKey
public function __construct(){
$this->appid= config('api.baidu.appid')
$this->appKey = config('api.baidu.apikey')
$this->secretKey = config('api.baidu.secretkey')
}
//百度ai接口--文字识别--车牌号识别
public function getCarNumber($_imgurl,$_img=''){
$_token = $this->getToken()
$_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token='.$_token
if($_img){
$_data = [
'image'=>$_img//图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式
]
}else{
$_data = [
'url'=>request()->domain().'/'.$_imgurl
]
}
$_res = json_decode(httpGet($_url,$_data),true)
/庆穗/TODO 此处只返回false没有终止,是因为程序执行流程需要,后期可能要改
if(isset($_res['error_msg'])) return false
return $_res['words_result']['number']
}
//获取token
private function getToken(){
if(cache('baidu_token')){
$_access_token = cache('baidu_token')
}else{
$_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='.$this->appKey.'&client_secret='.$this->secretKey
$res = json_decode(httpGet($_url),true)
if(isset($res['error']))TApiException($res['error_description'])//终止程序并抛出异常
$_access_token = $res['access_token']
$_expires_in = $res['粗轮expires_in']
cache('baidu_token',$_access_token,($_expires_in-1000))//我喜欢少存1000秒,没有为什么,问就是癖好
}
return $_access_token
}
}
这是ThinkPhp5.1后端封装的百度AI接口类,getToken()获取凭证,getCarNumber()请求$_url 返回识别结果,这个是车牌号码识岩差信别,车型识别等其他接口大部分都一样,就换个请求地址$_url就行
//接口:
public function getImgCarNum(){
$_number = (new BaiDuAiBaseController())->getCarNumber(false,request()->param('img'))
return self::myShow('申请成功',['carNum'=>$_number])
}
小程序端正常request请求上面的接口就行,下面是微信小程序拍照识别功能
//拍照
goImgSearch(){
uni.chooseImage({
count:1,
sizeType: ['compressed'],//original 原图,compressed 压缩图
sourceType: ['album','camera'],//camera 相机 album相册
success:(r)=>{
console.log(r)
//执行识别车牌号码
this.img = r.tempFilePaths[0]
this.urlTobase64(r.tempFilePaths[0])
}
})
},
//识别车牌号码
urlTobase64(url){
uni.showLoading({
title:'拼命识别车牌中..'
})
//#ifdef MP-WEIXIN
uni.getFileSystemManager().readFile({
filePath: url, //选择图片时返回的路径
encoding: "base64",//这个是很重要的
success: res =>{ //成功的回调
//返回base64格式
let base64 = 'data:image/jpegbase64,' + res.data
//发送请求,识别车牌号码
this.$H.post('/getImgCarNum',{
img:base64 //图片数据
},{
token:true //必须登录
}).then((res)=>{
console.log(res.carNum)
if(!res.carNum){
uni.hideLoading()
return uni.showModal({
title:'识别失败',
content:'没能识别到车牌号码,请拍张清晰的图片再试哦,谢谢',
showCancel:false
})
}
uni.showToast({
title:'识别车牌成功',
icon:'none'
})
this.searchUser = res.carNum
this.userCarNum = res.carNum
uni.hideLoading()
}).catch((e)=>{
uni.hideLoading()
return uni.showModal({
title:'识别失败',
content:'没能识别到车牌号码,请拍张清晰的图片再试哦,谢谢',
showCancel:false
})
})
},
fail:(e)=>{
console.log(e)
}
})
//#endif
},
研究了好几天,坑也遇到了,也百度了很久现在终于做完了,给大家分享出来,
我这个也是参考别人写的。有不明白的朋友可以问我
public function unifiedorder($order_no, $openid, $total_fee, $attach, $order_id, $user_id){
// 当前时间
$time = time()
// 生成随机字符串
$nonceStr = md5($time . $openid)
// API参数
$params = [
'appid' =>$this->appid, //微信分配的小程序id
'attach' =>$attach, //附加数据,在查询API和支付通知中原样返回,可作为自定义参数使用。
'body' =>'会员卡', //募捐描述
'mch_id' =>$this->mchid, //微信支付分配的商户号
'nonce_str' =>$nonceStr, //随机字符串,32位以内
'notify_url' =>$this->notify_url, // base_url() . 'notice.php?s=/task/notify/order/wxapp_id/'.$wxapp_id, // 异步通知地址
'openid' =>$openid, //用户标识;trade_type=JSAPI,此参数必传,用户在桥枯商户appid下的唯一标识。
'out_trade_no' =>$order_no, 槐消码 //商户账单号
'spbill_create_ip' =>\request()->ip(), //终端IP;支持IPV4和IPV6两种格式的IP地址。调用微信支付API的机器IP
'total_fee' =>(int)$total_fee * 100, // 价格:单位分 // 价格:单位分
'trade_type' =>'JSAPI', //交易类型
]
// 生成签名
$params['sign'] = $this->makeSign($params) //这个地方最坑,需要的是配置 1、appid和商户号必须是绑定的状态
// 请求API
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'
$result = $this->post($url, $this->toXml($params))
$prepay = $this->fromXml($result)
//添加preapay_id
$data = [
'user_id' =>$user_id,
'order_id'铅哪 =>$order_id,
'attach' =>json_encode($attach),
'prepay_id' =>$prepay['prepay_id'],
]
(new AppleWxPrepay())->addInfo($data)
// 请求失败
if ($prepay['return_code'] === 'FAIL') {
return [API_CODE_NAME =>2000004, API_MSG_NAME =>$prepay['return_msg']]
}
if ($prepay['result_code'] === 'FAIL') {
return [API_CODE_NAME =>2000004, API_MSG_NAME =>$prepay['err_code_des']]
}
// 生成 nonce_str 供前端使用
$paySign = $this->makePaySign($params['nonce_str'], $prepay['prepay_id'], $time)
return [
'prepay_id' =>$prepay['prepay_id'],
'nonceStr' =>$nonceStr,
'timeStamp' =>(string)$time,
'paySign' =>$paySign
]
}
/**
* 生成签名
* @param $values
* @return string 本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
*/
private function makeSign($values)
{
//签名步骤一:按字典序排序参数
ksort($values)
$string = $this->toUrlParams($values)
//签名步骤二:在string后加入KEY
$string = $string . '&key=' . $this->apikey
//签名步骤三:MD5加密
$string = md5($string)
//签名步骤四:所有字符转为大写
$result = strtoupper($string)
return $result
}
/**
* 格式化参数格式化成url参数
* @param $values
* @return string
*/
private function toUrlParams($values)
{
$buff = ''
foreach ($values as $k =>$v) {
if ($k != 'sign' &&$v != '' &&!is_array($v)) {
$buff .= $k . '=' . $v . '&'
}
}
return trim($buff, '&')
}
/**
* 模拟POST请求
* @param $url
* @param array $data
* @param bool $useCert
* @param array $sslCert
* @return mixed
*/
public function post($url, $data = [], $useCert = false, $sslCert = [])
{
$header = [
'Content-type: application/jsoncharset=UTF8'
]
$curl = curl_init()
curl_setopt($curl, CURLOPT_URL, $url)
curl_setopt($curl, CURLOPT_HTTPHEADER, $header)
curl_setopt($curl, CURLOPT_HEADER, false)
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1)
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false)
curl_setopt($curl, CURLOPT_POST, TRUE)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data)
if ($useCert == true) {
// 设置证书:cert 与 key 分别属于两个.pem文件
curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM')
curl_setopt($curl, CURLOPT_SSLCERT, $sslCert['certPem'])
curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM')
curl_setopt($curl, CURLOPT_SSLKEY, $sslCert['keyPem'])
}
$result = curl_exec($curl)
curl_close($curl)
return $result
}
/**
* 输出xml字符
* @param $values
* @return bool|string
*/
private function toXml($values)
{
if (!is_array($values) || count($values) <= 0) {
return false
}
$xml = "<xml>"
foreach ($values as $key =>$val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">"
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">"
}
}
$xml .= "</xml>"
return $xml
}
/**
* 将xml转为array
* @param $xml
* @return mixed
*/
private function fromXml($xml)
{
// 禁止引用外部xml实体
libxml_disable_entity_loader(true)
return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true)
}
/**
* 生成paySign
* @param $nonceStr
* @param $prepay_id
* @param $timeStamp
* @return string
*/
private function makePaySign($nonceStr, $prepay_id, $timeStamp)
{
$data = [
'appId' =>$this->appid,
'nonceStr' =>$nonceStr,
'package' =>'prepay_id=' . $prepay_id,
'signType' =>'MD5',
'timeStamp' =>$timeStamp,
]
// 签名步骤一:按字典序排序参数
ksort($data)
$string = $this->toUrlParams($data)
// 签名步骤二:在string后加入KEY
$string = $string . '&key=' . $this->apikey
// 签名步骤三:MD5加密
$string = md5($string)
// 签名步骤四:所有字符转为大写
$result = strtoupper($string)
return $result
}
/*********************************微信回调**********************/
public function getNotify()
{
if (!$xml = file_get_contents('php://input')) {
$this->returnCode(50000001, 'Not found DATA')
}
// 将服务器返回的XML数据转化为数组
$data = $this->fromXml($xml)
$payLog = new ApplePayLog()
// 记录日志
$payLog->addInfo(['content'=>json_encode($xml)])
$payLog->addInfo(['content'=>json_encode($data)])
// 实例化账单模型
$OrderModel = new AppleOrder()
// 账单信息
$orderInfo = $OrderModel->getInfo(['id'=>$data['attach']],'*')
if (empty($orderInfo)) {
$this->returnCode(50000001, '账单不存在')
}
if($orderInfo['pay_status'] != 1 || !empty($orderInfo['pay_time'])){
$this->returnCode(50000001,'订单已支付,请勿再次支付')
}
// 保存微信服务器返回的签名sign
$dataSign = $data['sign']
$return_code = $data['return_code']
$result_code = $data['result_code']
$data['body'] = '会员卡'
$data['spbill_create_ip'] = \request()->ip()
$data['notify_url'] = $this->notify_url
// sign 与 s 参数 不参与签名算法
unset($data['sign'])
unset($data['transaction_id'])
unset($data['coupon_id'])
unset($data['coupon_type'])
unset($data['coupon_count'])
unset($data['coupon_fee'])
unset($data['time_end'])
unset($data['return_code'])
unset($data['result_code'])
unset($data['is_subscribe'])
unset($data['fee_type'])
unset($data['bank_type'])
unset($data['bank_type'])
// 生成签名
$sign = $this->makeSign($data)
// 判断签名是否正确 判断支付状态
if (($sign === $dataSign) &&($return_code == 'SUCCESS') &&($result_code == 'SUCCESS')) {
$OrderModel->startTrans()
try {
// 账单支付成功业务处理
$appleOrderInfo = $OrderModel->where(['id'=>$orderInfo['id']])->lock(true)->find()
$result = $appleOrderInfo->addInfo(['pay_status'=>2,'pay_time'=>time()],['id'=>$orderInfo['id']])
if(!$result){
$OrderModel->rollback()
$this->returnCode(5000003, '修改订单失败,失败原因:'.$OrderModel->getError())
}
$appleUserModel = new AppleUser()
$appleUserInfo = $appleUserModel->where(['openid'=>$orderInfo['openid']])->lock(true)->find()
$appleUser = $appleUserInfo->where(['openid'=>$orderInfo['openid']])->setInc('moxibustion',$orderInfo['moxibustion'])
if(!$appleUser){
$OrderModel->rollback()
$this->returnCode(5000003, '添加会员针灸次数失败,失败原因:'.$appleUserModel->getError())
}
}catch (\Exception $exception){
$OrderModel->rollback()
$this->returnCode(5000003, ' *** 作失败,失败原因:'.$exception->getMessage())
}
$OrderModel->commit()
// 返回状态
die(json(['code'=>0,'支付成功']))
}
// 返回状态
$this->returnCode(2000003, '签名失败')
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)