RegisterMiniForm.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\third\ali;
  8. use app\models\AggregateQrcode;
  9. use app\models\Option;
  10. use app\models\Salesman;
  11. use app\models\StoreAliMini;
  12. use app\models\StoreAliMiniVersion;
  13. use app\models\StoreMiniToken;
  14. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAuthAppAesSetRequest;
  15. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIsvCreateRequest;
  16. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIsvQueryRequest;
  17. use app\utils\QrCode;
  18. use yii\helpers\Json;
  19. class RegisterMiniForm extends BaseForm
  20. {
  21. /**
  22. * 注册小程序form
  23. */
  24. public $param_info;
  25. public $status;
  26. public function rules()
  27. {
  28. return [
  29. [['status'], 'integer']
  30. ];
  31. }
  32. public function inits()
  33. {
  34. try {
  35. $mini_id = $this->mini_id;
  36. $store_id = $this->store_id;
  37. if (!empty($mini_id)) {
  38. $store_mini = StoreAliMini::find()->where(['OR', ['id' => $mini_id], ['store_id' => $store_id]])->orderBy('id desc')->one();
  39. if (empty($store_mini)) {
  40. return [
  41. 'code' => 1,
  42. 'msg' => "查找不到小程序信息"
  43. ];
  44. }
  45. $this->store_mini = $store_mini;
  46. $this->mini_version = StoreAliMiniVersion::find()->where(['mini_id' => $mini_id])->orderBy('id desc')->one();
  47. }
  48. //获取二维码信息
  49. $qrcode_id = $this->qrcode_id;
  50. if (!empty($qrcode_id)) {
  51. $qrcode = AggregateQrcode::findOne($qrcode_id);
  52. if (empty($qrcode)) {
  53. return [
  54. 'code' => 1,
  55. 'msg' => "二维码不存在"
  56. ];
  57. }
  58. if ($qrcode->ali_status == 1) {
  59. return [
  60. 'code' => 1,
  61. 'msg' => "二维码已经被使用"
  62. ];
  63. }
  64. $this->qrcode = $qrcode;
  65. }
  66. } catch (\Exception $e) {
  67. return [
  68. 'code' => 1,
  69. 'msg' => $e->getMessage()
  70. ];
  71. }
  72. }
  73. //快速创建小程序 alipay.open.mini.isv.create(isv服务商代商户创建小程序) 获取order_no 依然需要使用被动模式获取auth_code、auth_token、userid、refresh_token
  74. public function fastRegisterMini()
  75. {
  76. $t = \Yii::$app->db->beginTransaction();
  77. try {
  78. $result = $this->inits();
  79. if ($result['code'] === 1) {
  80. return $result;
  81. }
  82. //获取商城ID
  83. $store_id = $this->store_id;
  84. //获取小程序model
  85. $store_mini = $this->store_mini ?: new StoreAliMini();
  86. //获取请求数据
  87. $param_info = $this->param_info;
  88. //获取最大的id
  89. $mini_id = StoreAliMini::find()->max('id');
  90. //生成订单号
  91. $out_order_no = date("YmdHis") . sprintf("%03d", $store_id) . sprintf("%03d", $mini_id + 1);
  92. //设置请求支付宝数据
  93. $param = json_encode([
  94. 'create_mini_request' => [
  95. //订单号
  96. 'out_order_no' => $out_order_no,
  97. //支付宝账户
  98. 'alipay_account' => $param_info['alipay_account'],
  99. //法人姓名
  100. 'legal_personal_name' => $param_info['legal_personal_name'],
  101. //公司企业名称
  102. 'cert_name' => $param_info['license_name'],
  103. //企业编码
  104. 'cert_no' => $param_info['license_no'],
  105. //小程序名称
  106. 'app_name' => $param_info['app_name'],
  107. //商家手机号
  108. 'contact_phone' => $param_info['service_phone'],
  109. //商家名称
  110. 'contact_name' => $param_info['contact_name'],
  111. 'is_individual' => true
  112. ]
  113. ]);
  114. //保存商城ID
  115. $store_mini->store_id = $store_id;
  116. //保存企业名称
  117. $store_mini->license_name = $param_info['license_name'];
  118. //保存企业编码
  119. $store_mini->license_no = $param_info['license_no'];
  120. //小程序名称
  121. $store_mini->app_name = $param_info['app_name'];
  122. //联系电话
  123. $store_mini->service_phone = $param_info['service_phone'];
  124. //营业执照
  125. $store_mini->license_pic = $param_info['license_pic'];
  126. //支付宝账户
  127. $store_mini->alipay_account = $param_info['alipay_account'];
  128. //法人姓名
  129. $store_mini->legal_personal_name = $param_info['legal_personal_name'];
  130. //商户姓名
  131. $store_mini->contact_name = $param_info['contact_name'];
  132. if (!empty($param_info['license_date'])) {
  133. //判断营业执照是否为长期
  134. $is_long_effective = (int)(strtotime($param_info['license_date']) >= strtotime("2099-12-31"));
  135. //营业执照有效期
  136. $store_mini->license_date = $param_info['license_date'];
  137. //
  138. $store_mini->is_long_effective = $is_long_effective;
  139. }
  140. //请求接口
  141. $request = new AlipayOpenMiniIsvCreateRequest();
  142. $result = $this->miniCommon($request, $param, 1);
  143. if ($result->code == 10000) {
  144. //保存订单号
  145. $store_mini->order_no = $result->order_no;
  146. $store_mini->order_cancel = 0;
  147. if (!$store_mini->save()) {
  148. throw new \Exception(json_encode($store_mini->errors));
  149. }
  150. $t->commit();
  151. return [
  152. 'code' => 0,
  153. 'msg' => "成功",
  154. 'data' => $result,
  155. 'id' => $store_mini->id
  156. ];
  157. } elseif ($result->sub_msg === "非法请求参数,【检查结果不是个体工商户】") {
  158. $result = (new PerfectMiniForm())->updateBussiness($param_info['license_pic'], $param_info['license_no']);
  159. if ($result['code'] === 0) {
  160. $res = self::fastRegisterMini();
  161. if ($res['code'] === 0) {
  162. return $res;
  163. }
  164. }
  165. if ($result['code'] === 2) {
  166. return [
  167. 'code' => 0,
  168. 'msg' => $result['msg'],
  169. 'data' => [
  170. 'status' => 1
  171. ],
  172. ];
  173. }
  174. }
  175. throw new \Exception($result->sub_msg);
  176. } catch (\Exception $e) {
  177. $t->rollBack();
  178. return [
  179. 'code' => 1,
  180. 'msg' => $e->getMessage()
  181. ];
  182. }
  183. }
  184. //查询快速注册状态 alipay.open.mini.isv.query(isv查询代商家创建小程序记录) 根据order_no获取快速注册状态 获取appid
  185. public function getFastRegisterStatus()
  186. {
  187. try {
  188. $result = $this->inits();
  189. if ($result['code'] === 1) {
  190. return $result;
  191. }
  192. //获取小程序model
  193. $store_mini = $this->store_mini;
  194. //判断订单号是否为空
  195. if (empty($store_mini->order_no)) {
  196. throw new \Exception("订单不存在");
  197. }
  198. $param = json_encode([
  199. "order_no" => $store_mini->order_no,
  200. ]);
  201. //请求配置信息
  202. $request = new AlipayOpenMiniIsvQueryRequest();
  203. $result = $this->miniCommon($request, $param, 1);
  204. //如果请求成功
  205. if ($result->code == 10000) {
  206. //判断请求状态
  207. switch ($result->status) {
  208. case "TIMEOUT":
  209. $store_mini->order_cancel = 1;
  210. $msg = "已超时";
  211. break;
  212. case "AGREED":
  213. $msg = "已同意";
  214. break;
  215. case "REJECTED":
  216. $store_mini->order_cancel = 1;
  217. $msg = "已拒绝";
  218. break;
  219. default:
  220. $msg = "审核中";
  221. break;
  222. }
  223. //如果商家已经确认时会返回小程序ID
  224. if (!empty($result->min_app_id)) {
  225. $store_mini->auth_app_id = $result->min_app_id;
  226. }
  227. //保存小程序ID
  228. if (!$store_mini->save()) {
  229. throw new \Exception(json_encode($store_mini->errors));
  230. }
  231. return [
  232. 'code' => 0,
  233. 'msg' => $msg,
  234. 'data' => $result
  235. ];
  236. }else{
  237. //请求失败
  238. throw new \Exception($result->sub_msg);
  239. }
  240. } catch (\Exception $e) {
  241. return [
  242. 'code' => 1,
  243. 'msg' => $e->getMessage()
  244. ];
  245. }
  246. }
  247. //获取aes密钥用于登录以及支付 alipay.open.auth.app.aes.set(授权应用aes密钥设置) 根据appid获取aes_key
  248. public function getAesKey()
  249. {
  250. try {
  251. $result = $this->inits();
  252. if ($result['code'] === 1) {
  253. return $result;
  254. }
  255. //获取小程序model
  256. $store_mini = $this->store_mini;
  257. //商城ID
  258. $store_id = $this->store_id;
  259. //请求数据
  260. $param = json_encode([
  261. "merchant_app_id" => $store_mini->auth_app_id
  262. ]);
  263. //请求配置信息
  264. $request = new AlipayOpenAuthAppAesSetRequest();
  265. $result = $this->miniCommon($request, $param, 1);
  266. //如果请求成功
  267. if ($result->code == 10000) {
  268. //如果小程序信息为空 则更新模板aes_key
  269. if (empty($store_mini)) {
  270. Option::set('ali_template_aes', $result->aes_key, 0, 'saas');
  271. } else {
  272. //如果不为空 则更新小程序aes_key,保存信息
  273. $store_mini->aes_key = $result->aes_key;
  274. if (!$store_mini->save()) {
  275. throw new \Exception(json_encode($store_mini->errors));
  276. }
  277. //保存后将信息放入配置信息里,以便于以后支付使用
  278. $data = [];
  279. $data['aes_key'] = $store_mini->aes_key;
  280. $data['app_id'] = $store_mini->auth_app_id;
  281. $data['alipay_public_key'] = "";
  282. $data['app_public_key'] = "";
  283. $data['app_private_key'] = "";
  284. $data['name'] = $store_mini->license_name;
  285. $data['user_id'] = $store_mini->userid;
  286. $res = Option::set(Option::OPTOPN_KEY, Json::encode($data), $store_id, 'alipay');
  287. //记录到缓存信息
  288. if ($res) {
  289. \Yii::$app->cache->delete('alipay_config_cache_' . $store_id);
  290. \Yii::$app->cache->set('alipay_config_cache_' . $store_id, Json::encode($data));
  291. }
  292. }
  293. return [
  294. 'code' => 0,
  295. 'msg' => "获取成功",
  296. 'data' => $result->aes_key
  297. ];
  298. } else {
  299. //请求到接口失败
  300. throw new \Exception($result->sub_msg);
  301. }
  302. } catch (\Exception $e) {
  303. return [
  304. 'code' => 1,
  305. 'msg' => $e->getMessage()
  306. ];
  307. }
  308. }
  309. /**
  310. * 传递授权回调参数
  311. * @return array
  312. */
  313. public function getParams()
  314. {
  315. $store_id = $this->store_id;
  316. $status = $this->status;
  317. $data = [
  318. 'appid' => $this->base_info['appid'],
  319. 'redirect_uri' => urlencode(\Yii::$app->request->hostInfo . '/index.php/alipay/token'),
  320. 'state' => $store_id
  321. ];
  322. $saas_user_id = get_saas_user_id();
  323. $salesman = Salesman::findOne(['saas_user_id' => $saas_user_id]);
  324. $token_log = StoreMiniToken::findOne(['store_id' => $store_id]) ?: new StoreMiniToken();
  325. $token_log->store_id = $store_id;
  326. $token_log->created_at = time();
  327. $token_log->type = 1;
  328. $token_log->salesman_id = $salesman->id;
  329. $token_log->save();
  330. if ((int)$status === 1) {
  331. $token_log->status = 1;
  332. } else {
  333. $url = "https://openauth.alipay.com/oauth2/appToAppBatchAuth.htm?app_id=" . $data['appid'] . "&application_type=WEBAPP&redirect_uri=" . $data['redirect_uri'] . '&state=' . $data['state'];
  334. $filename = md5(date('Ym') . 'token' . $token_log->id);
  335. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  336. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  337. QrCode::image($url, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  338. $token_log->url = $pic_url;
  339. }
  340. $token_log->save();
  341. return [
  342. 'code' => 0,
  343. 'url' => $pic_url,
  344. 'msg' => "获取成功"
  345. ];
  346. }
  347. }