admin/addon/aliapp/model/CreateApplet.php

163 lines
5.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace addon\aliapp\model;
use addon\alipay\data\sdk\AopClient;
use addon\alipay\data\sdk\request\AlipayTradeCloseRequest;
use app\model\BaseModel;
use Alipay\EasySDK\Kernel\Factory;
use Alipay\EasySDK\Kernel\Config as AliCon;
use app\model\order\OrderCreateTool;
use think\Exception;
use think\facade\Cache;
use think\facade\Db;
class CreateApplet extends BaseModel{
protected $site_id;
/**
* Common: 创建小程序 —— 开始创建
* Author: wu-hui
* Time: 2022/12/29 9:15
* @param $info
* @param $siteId
* @return array
* @throws Exception
*/
public function createAppletStart($info,$siteId){
$this->site_id = $siteId;
// 获取参数信息
$params = $this->createAppletParams($info);
$result = (new MinCode())->requestApi("alipay.open.mini.isv.create", ['create_mini_request' => $params])['alipay_open_mini_isv_create_response'];
if($result['code'] == 10000) {
$info['out_order_no'] = $params['out_order_no'];
$info['order_no'] = $result['order_no'];
return $this->success($info);
}else{
throw new Exception($result['sub_msg']);
}
}
/**
* Common: 创建小程序 —— 参数获取
* Author: wu-hui
* Time: 2022/12/29 9:10
* @param $info
* @return array
*/
private function createAppletParams($info){
// 获取图片base64
if($info['license_pic']) $info['license_pic'] = UrlimgBase64(img($info['license_pic']));
// 处理配置信息
$info['out_order_no'] = $this->createAppletOutOrderNo($this->site_id,$info['cert_no']);
return [
'out_order_no' => $info['out_order_no'], // 开发者外部订单号
'alipay_account' => $info['alipay_account'], // 商家登录支付宝的邮箱帐号或手机号
'legal_personal_name' => $info['legal_personal_name'],// 商家法人名称
'cert_name' => $info['cert_name'],// 营业执照企业名称,如果是“无主体名称个体工商户”则填“个体户+法人姓名”,例如“个体户张三”
'cert_no' => $info['cert_no'],// 营业执照编码
'app_name' => $info['app_name'], // 小程序名称
'contact_phone' => $info['contact_phone'], // 商家联系人手机电话
'contact_name' => $info['contact_name'], // 商家联系人名称
'is_individual' => TRUE, // 是否支持个体工商户的账号类型
'license_pic' => $info['license_pic'], // 营业执照图片的Base64编码字符串图片大小不能超过2M。
];
}
/**
* Common: 生成订单号
* Author: wu-hui
* Time: 2022/12/28 16:54
* @param $site_id
* @param $id
* @return string
*/
public function createAppletOutOrderNo($site_id, $cert_no){
$time_str = date('YmdHi');
$max_no = Cache::get($site_id . '_' . $cert_no . '_' . $time_str);
if (!isset($max_no) || empty($max_no)) $max_no = 1;
else $max_no = $max_no + 1;
$order_no = $time_str . $cert_no . sprintf('%03d', $max_no);
Cache::set($site_id . '_' . $cert_no . '_' . $time_str, $max_no);
return $order_no;
}
/**
* Common: 获取最新的小程序配置信息
* Author: wu-hui
* Time: 2022/12/28 17:41
* @param $siteId
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getNewParams($siteId){
$info = Db::name('applet_list')
->where('site_id',$siteId)
->where('type',1)
->order('id','DESC')
->find();
if(!$info){
$content = [
'alipay_account' => '',// 商家登录支付宝的邮箱帐号或手机号
'legal_personal_name' => '',// 商家法人名称
'cert_name' => '',// 营业执照企业名称,如果是“无主体名称个体工商户”则填“个体户+法人姓名”,例如“个体户张三”
'cert_no' => '',// 营业执照编码
'app_name' => '',// 小程序名称
'contact_phone' => '',// 商家联系人手机电话
'contact_name' => '',// 商家联系人名称
'license_pic' => '',// 营业执照图片的Base64编码字符串图片大小不能超过2M。
];
}else{
$content = json_decode($info['content'],TRUE);
}
return [$info['id'] ?? 0,$content];
}
/**
* Common: 设置小程序信息
* Author: wu-hui
* Time: 2022/12/28 18:15
* @param $info
* @param $siteId
* @return array
*/
public function setNewParams($info,$siteId){
// 信息处理
$id = $info['id'];
unset($info['id']);
Db::name('applet_list')->startTrans();
try{
// 发起请求 创建小程序
$info = $this->createAppletStart($info,$siteId);
$data = [
'content' => json_encode($info,JSON_UNESCAPED_UNICODE)
];
// 记录信息
if($id > 0){
Db::name('applet_list')->where('id',$id)->update($data);
}else{
$data['site_id'] = $siteId;
$data['name'] = $info['app_name'];
$data['type'] = 1;
$data['status'] = 1;
$data['create_time'] = time();
Db::name('applet_list')->insert($data);
}
Db::name('applet_list')->commit();
return $this->success();
}catch(\Exception $e){
Db::name('applet_list')->rollback();
return $this->error('',$e->getMessage());
}
}
}